-
Notifications
You must be signed in to change notification settings - Fork 1
Outer auth methods #185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Outer auth methods #185
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
af064f2
Pg Outer
dyakovri 5efeb7e
Coder
dyakovri 12eebb0
Airflow
dyakovri 7a4cbf4
Format
dyakovri 7017a64
Mailu
dyakovri ab6bc4a
Update names
dyakovri 9232dbe
Fix session creation
dyakovri bd109de
Fix in auth methods
dyakovri 75301e6
No group add in cli
dyakovri b254129
Base scope fix
dyakovri c73635b
Create link scopes
dyakovri c6c896e
Fix path
dyakovri a1a33de
Fix commit
dyakovri 5463058
Add defaults
dyakovri c17cf6d
Fixes
dyakovri b2d025a
Better log
dyakovri 54dfe84
envs in cd
dyakovri d428b14
Fix style
dyakovri dcc2d15
Is deleted
dyakovri 5d07439
No type hint
dyakovri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,55 @@ | ||
| import logging | ||
|
|
||
| import aiohttp | ||
| from pydantic import AnyUrl | ||
|
|
||
| from auth_backend.auth_method.outer import ConnectionIssue, OuterAuthMeta | ||
| from auth_backend.settings import Settings | ||
|
|
||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class AirflowOuterAuthSettings(Settings): | ||
| AIRFLOW_AUTH_BASE_URL: AnyUrl | None = None | ||
| AIRFLOW_AUTH_ADMIN_USERNAME: str | None = None | ||
| AIRFLOW_AUTH_ADMIN_PASSWORD: str | None = None | ||
|
|
||
|
|
||
| class AirflowOuterAuth(OuterAuthMeta): | ||
| prefix = '/airflow' | ||
| settings = AirflowOuterAuthSettings() | ||
|
|
||
| @classmethod | ||
| async def _is_outer_user_exists(cls, username: str) -> bool: | ||
| """Проверяет наличие пользователя в Airflow""" | ||
| logger.debug("_is_outer_user_exists class=%s started", cls.get_name()) | ||
| async with aiohttp.ClientSession() as session: | ||
| async with session.get( | ||
| str(cls.settings.AIRFLOW_AUTH_BASE_URL).removesuffix('/') + '/auth/fab/v1/users/' + username, | ||
| auth=aiohttp.BasicAuth( | ||
| cls.settings.AIRFLOW_AUTH_ADMIN_USERNAME, cls.settings.AIRFLOW_AUTH_ADMIN_PASSWORD | ||
| ), | ||
| ) as response: | ||
| if not response.ok: | ||
| raise ConnectionIssue(response.text) | ||
| res: dict[str] = await response.json() | ||
| return res.get('username') == username | ||
|
|
||
| @classmethod | ||
| async def _update_outer_user_password(cls, username: str, password: str): | ||
| """Устанавливает пользователю новый пароль в Airflow""" | ||
| logger.debug("_update_outer_user_password class=%s started", cls.get_name()) | ||
| res = False | ||
|
dyakovri marked this conversation as resolved.
|
||
| async with aiohttp.ClientSession() as session: | ||
| async with session.patch( | ||
| str(cls.settings.AIRFLOW_AUTH_BASE_URL).removesuffix('/') + '/auth/fab/v1/users' + username, | ||
| auth=(cls.settings.AIRFLOW_AUTH_ADMIN_USERNAME, cls.settings.AIRFLOW_AUTH_ADMIN_PASSWORD), | ||
| json={'password': password}, | ||
| ) as response: | ||
| res = response.ok | ||
| logger.debug("_update_outer_user_password class=%s response %s", cls.get_name(), str(response.status)) | ||
| if res: | ||
| logger.info("User %s updated in Airflow", username) | ||
| else: | ||
| logger.error("User %s can't be updated in Airflow. Error: %s", username, res) | ||
This file contains hidden or 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,52 @@ | ||
| import logging | ||
|
|
||
| import aiohttp | ||
| from pydantic import AnyUrl | ||
|
|
||
| from auth_backend.auth_method.outer import ConnectionIssue, OuterAuthMeta | ||
| from auth_backend.settings import Settings | ||
|
|
||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class CoderOuterAuthSettings(Settings): | ||
| CODER_AUTH_BASE_URL: AnyUrl | None = None | ||
| CODER_AUTH_ADMIN_TOKEN: str | None = None | ||
|
|
||
|
|
||
| class CoderOuterAuth(OuterAuthMeta): | ||
| prefix = '/coder' | ||
| settings = CoderOuterAuthSettings() | ||
|
|
||
| @classmethod | ||
| async def _is_outer_user_exists(cls, username: str) -> bool: | ||
| """Проверяет наличие пользователя в Coder""" | ||
| logger.debug("_is_outer_user_exists class=%s started", cls.get_name()) | ||
| async with aiohttp.ClientSession() as session: | ||
| async with session.get( | ||
| str(cls.settings.CODER_AUTH_BASE_URL).removesuffix('/') + '/api/v2/users/' + username, | ||
| headers={'Coder-Session-Token': cls.settings.CODER_AUTH_ADMIN_TOKEN, 'Accept': 'application/json'}, | ||
| ) as response: | ||
| if not response.ok: | ||
| raise ConnectionIssue(response.text) | ||
| res: dict[str] = await response.json() | ||
| return res.get('username') == username | ||
|
|
||
| @classmethod | ||
| async def _update_outer_user_password(cls, username: str, password: str): | ||
| """Устанавливает пользователю новый пароль в Coder""" | ||
| logger.debug("_update_outer_user_password class=%s started", cls.get_name()) | ||
| res = False | ||
| async with aiohttp.ClientSession() as session: | ||
| async with session.put( | ||
| str(cls.settings.CODER_AUTH_BASE_URL).removesuffix('/') + '/api/v2/users/' + username + '/password', | ||
| headers={'Coder-Session-Token': cls.settings.CODER_AUTH_ADMIN_TOKEN, 'Accept': 'application/json'}, | ||
| json={'password': password}, | ||
| ) as response: | ||
| res: dict[str] = response.ok | ||
| logger.debug("_update_outer_user_password class=%s response %s", cls.get_name(), str(response.status)) | ||
| if res: | ||
| logger.info("User %s updated in Coder", username) | ||
| else: | ||
| logger.error("User %s can't be updated in Coder. Error: %s", username, res) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.