-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathnotifications.py
189 lines (158 loc) · 5.89 KB
/
notifications.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import datetime
import logging
import sys
from typing import Generator
import pluggy
from api.models import AccessRequest, OktaGroup, OktaUser, RoleGroup, RoleRequest
notification_plugin_name = "access_notifications"
hookspec = pluggy.HookspecMarker(notification_plugin_name)
hookimpl = pluggy.HookimplMarker(notification_plugin_name)
_cached_notification_hook = None
logger = logging.getLogger(__name__)
class NotificationPluginSpec:
@hookspec
def access_request_created(
self, access_request: AccessRequest, group: OktaGroup, requester: OktaUser, approvers: list[OktaUser]
) -> None:
"""Notify the approvers of the access request."""
@hookspec
def access_request_completed(
self,
access_request: AccessRequest,
group: OktaGroup,
requester: OktaUser,
approvers: list[OktaUser],
notify_requester: bool,
) -> None:
"""Notify the requester that their access request has been processed."""
@hookspec
def access_expiring_user(
self, groups: list[OktaGroup], user: OktaUser, expiration_datetime: datetime.datetime
) -> None:
"""Notify individuals that their access to a group is expiring soon"""
@hookspec
def access_expiring_owner(
self,
owner: OktaUser,
groups: list[OktaGroup],
roles: list[OktaGroup],
users: list[RoleGroup],
expiration_datetime: datetime.datetime,
) -> None:
"""Notify group owners that individuals or roles access to a group is expiring soon"""
@hookspec
def access_role_request_created(
self,
role_request: RoleRequest,
role: RoleGroup,
group: OktaGroup,
requester: OktaUser,
approvers: list[OktaUser],
) -> None:
"""Notify the approvers of the role request."""
@hookspec
def access_role_request_completed(
self,
role_request: RoleRequest,
role: RoleGroup,
group: OktaGroup,
requester: OktaUser,
approvers: list[OktaUser],
notify_requester: bool,
) -> None:
"""Notify the requester that their role request has been processed."""
@hookimpl(wrapper=True)
def access_request_created(
access_request: AccessRequest, group: OktaGroup, requester: OktaUser, approvers: list[OktaUser]
) -> Generator[None, None, None]:
try:
return (yield)
except Exception:
# Log and do not raise since notification failures should not
# break the flow. Users can still manually ping approvers
# to process their request from the UI
logger.exception("Failed to execute access request created notification callback")
@hookimpl(wrapper=True)
def access_request_completed(
access_request: AccessRequest,
group: OktaGroup,
requester: OktaUser,
approvers: list[OktaUser],
notify_requester: bool,
) -> Generator[None, None, None]:
try:
return (yield)
except Exception:
# Log and do not raise since notification failures should not
# break the flow. Users can still manually ping approvers
# to process their request from the UI
logger.exception("Failed to execute access request completed notification callback")
@hookimpl(wrapper=True)
def access_expiring_user(
groups: list[OktaGroup], user: OktaUser, expiration_datetime: datetime.datetime
) -> Generator[None, None, None]:
try:
return (yield)
except Exception:
# Log and do not raise since notification failures should not
# break the flow. Users can still manually ping approvers
# to process their request from the UI
logger.exception("Failed to execute access expiring for user notification callback")
@hookimpl(wrapper=True)
def access_expiring_owner(
owner: OktaUser,
groups: list[OktaGroup],
roles: list[RoleGroup],
users: list[OktaUser],
expiration_datetime: datetime.datetime,
) -> Generator[None, None, None]:
try:
return (yield)
except Exception:
# Log and do not raise since notification failures should not
# break the flow. Users can still manually ping approvers
# to process their request from the UI
logger.exception("Failed to execute access expiring for owner notification callback")
@hookimpl(wrapper=True)
def access_role_request_created(
role_request: RoleRequest,
role: RoleGroup,
group: OktaGroup,
requester: OktaUser,
approvers: list[OktaUser],
) -> Generator[None, None, None]:
try:
return (yield)
except Exception:
# Log and do not raise since notification failures should not
# break the flow. Users can still manually ping approvers
# to process their request from the UI
logger.exception("Failed to execute role request created notification callback")
@hookimpl(wrapper=True)
def access_role_request_completed(
role_request: RoleRequest,
role: RoleGroup,
group: OktaGroup,
requester: OktaUser,
approvers: list[OktaUser],
notify_requester: bool,
) -> Generator[None, None, None]:
try:
return (yield)
except Exception:
# Log and do not raise since notification failures should not
# break the flow. Users can still manually ping approvers
# to process their request from the UI
logger.exception("Failed to execute role request completed notification callback")
def get_notification_hook() -> pluggy.HookRelay:
global _cached_notification_hook
if _cached_notification_hook is not None:
return _cached_notification_hook
pm = pluggy.PluginManager(notification_plugin_name)
pm.add_hookspecs(NotificationPluginSpec)
# Register the hook wrappers
pm.register(sys.modules[__name__])
count = pm.load_setuptools_entrypoints(notification_plugin_name)
print(f"Count of loaded notification plugins: {count}")
_cached_notification_hook = pm.hook
return _cached_notification_hook