-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a conditional request plugin interface so access requests c…
…an be automatically approved or denied (#34)
- Loading branch information
1 parent
1fde1aa
commit 4275a7f
Showing
17 changed files
with
335 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,15 @@ | ||
import pluggy | ||
|
||
from api.plugins.conditional_access import ConditionalAccessResponse, get_conditional_access_hook | ||
from api.plugins.notifications import get_notification_hook | ||
|
||
condtional_access_hook_impl = pluggy.HookimplMarker("access_conditional_access") | ||
notification_hook_impl = pluggy.HookimplMarker("access_notifications") | ||
|
||
__all__ = ["get_notification_hook", "notification_hook_impl"] | ||
__all__ = [ | ||
"ConditionalAccessResponse", | ||
"conditional_access_hook_impl", | ||
"get_conditional_access_hook", | ||
"get_notification_hook", | ||
"notification_hook_impl", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import logging | ||
import sys | ||
from dataclasses import dataclass | ||
from datetime import datetime | ||
from typing import Any, Generator, List, Optional | ||
|
||
import pluggy | ||
|
||
from api.models import AccessRequest, OktaGroup, OktaUser | ||
|
||
conditional_access_plugin_name = "access_conditional_access" | ||
hookspec = pluggy.HookspecMarker(conditional_access_plugin_name) | ||
hookimpl = pluggy.HookimplMarker(conditional_access_plugin_name) | ||
|
||
_cached_conditional_access_hook = None | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
|
||
@dataclass | ||
class ConditionalAccessResponse: | ||
approved: bool | ||
reason: str = '' | ||
ending_at: Optional[datetime] = None | ||
|
||
class ConditionalAccessPluginSpec: | ||
@hookspec | ||
def access_request_created(self, | ||
access_request: AccessRequest, | ||
group: OktaGroup, | ||
requester: OktaUser) -> Optional[ConditionalAccessResponse]: | ||
"""Automatically approve, deny, or continue the access request.""" | ||
|
||
|
||
@hookimpl(wrapper=True) | ||
def access_request_created( | ||
access_request: AccessRequest, | ||
group: OktaGroup, | ||
requester: OktaUser | ||
) -> Generator[Any, None, Optional[ConditionalAccessResponse]] | List[Optional[ConditionalAccessResponse]]: | ||
try: | ||
# Trigger exception if it exists | ||
return (yield) | ||
except Exception: | ||
# Log and do not raise since request failures should not | ||
# break the flow. The access request can still be manually | ||
# approved or denied | ||
logger.error("Failed to execute request created callback") | ||
|
||
return [] | ||
|
||
|
||
def get_conditional_access_hook() -> pluggy.HookRelay: | ||
global _cached_conditional_access_hook | ||
|
||
if _cached_conditional_access_hook is not None: | ||
return _cached_conditional_access_hook | ||
|
||
pm = pluggy.PluginManager(conditional_access_plugin_name) | ||
pm.add_hookspecs(ConditionalAccessPluginSpec) | ||
|
||
# Register the hook wrappers | ||
pm.register(sys.modules[__name__]) | ||
|
||
count = pm.load_setuptools_entrypoints(conditional_access_plugin_name) | ||
print(f"Count of loaded conditional access plugins: {count}") | ||
_cached_conditional_access_hook = pm.hook | ||
|
||
return _cached_conditional_access_hook |
Oops, something went wrong.