-
Notifications
You must be signed in to change notification settings - Fork 0
Add ability to authenticate with blueapi #40
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
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
75952c1
Add udc auth to helm chart
jacob-williamson edc221b
Create UDC session manager
jacob-williamson 983869f
Allow no oidc config and improve logging
jacob-williamson ac224ac
Fix tests
jacob-williamson 9233717
Instantiate blueapi client with rest client
jacob-williamson c736ef0
Merge branch 'main' into auth
jacob-williamson 2fe630a
Upgrade blueapi
jacob-williamson 3ee8f45
Avoid duplicate logs
jacob-williamson b91d40c
Update docstrings + comments
jacob-williamson e4a9eaa
Update import
jacob-williamson 80d3d9d
Comment
jacob-williamson 0da0340
Update client_id name in UDC session manager
jacob-williamson d281ce6
Lint yaml files
jacob-williamson ed53378
Rework token retriever
jacob-williamson 99e83e0
Work with current blueapi
jacob-williamson e6570a1
Add comment
jacob-williamson e8b38b4
Merge branch 'main' into auth
jacob-williamson fabd2fe
Fix tests wip
jacob-williamson 6cb368b
Merge branch 'main' into auth
jacob-williamson a49c685
Get udc client ID from env variable
jacob-williamson 97533d7
Add client ID to env var in helm chart
jacob-williamson 41876f1
Add tests for token retriever
jacob-williamson a410416
Add tests for get_blueapi_clients
jacob-williamson 838a786
Merge branch 'main' into auth
jacob-williamson a77c13e
Merge branch 'main' into auth
jacob-williamson ddc736c
Merge branch 'main' into auth
DominicOram 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
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,39 @@ | ||
| from unittest.mock import MagicMock | ||
|
|
||
| from blueapi.client import BlueapiClient | ||
| from blueapi.client.event_bus import EventBusClient | ||
| from blueapi.client.rest import BlueapiRestClient | ||
| from blueapi.config import ApplicationConfig | ||
| from bluesky_stomp.messaging import Broker, StompClient | ||
|
|
||
| from daq_queuing_service.blueapi_interaction.token_retriever import UDCTokenRetriever | ||
|
|
||
|
|
||
| def get_blueapi_clients( | ||
| blueapi_config: ApplicationConfig, | ||
| ) -> tuple[BlueapiRestClient, BlueapiClient]: | ||
| if not blueapi_config.oidc: | ||
| blueapi_config.oidc = MagicMock() | ||
|
|
||
| blueapi_rest_client = BlueapiRestClient( | ||
| config=blueapi_config.api, | ||
| # Waiting on https://github.com/DiamondLightSource/blueapi/pull/1553 | ||
| session_manager=UDCTokenRetriever(), # type: ignore | ||
| ) | ||
|
|
||
| if blueapi_config.stomp.enabled: | ||
| assert blueapi_config.stomp.url.host is not None, "Stomp URL missing host" | ||
| assert blueapi_config.stomp.url.port is not None, "Stomp URL missing port" | ||
| stomp_client = StompClient.for_broker( | ||
| broker=Broker( | ||
| host=blueapi_config.stomp.url.host, | ||
| port=blueapi_config.stomp.url.port, | ||
| auth=blueapi_config.stomp.auth, | ||
| ) | ||
| ) | ||
| events = EventBusClient(stomp_client) | ||
| blueapi_client = BlueapiClient(blueapi_rest_client, events) | ||
| else: | ||
| blueapi_client = BlueapiClient(blueapi_rest_client) | ||
|
|
||
| return blueapi_rest_client, blueapi_client | ||
47 changes: 47 additions & 0 deletions
47
src/daq_queuing_service/blueapi_interaction/token_retriever.py
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,47 @@ | ||
| import os | ||
|
|
||
| import requests | ||
|
|
||
| from daq_queuing_service.log import LOGGER | ||
|
|
||
|
|
||
| class UDCTokenRetriever: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should: I think it would be good to have some unit tests on this |
||
| """Implements `get_valid_access_token` to get a token using a sealed secret.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| secret_variable_name: str = "UDC_SECRET", | ||
| client_id_variable_name: str = "UDC_CLIENT_ID", | ||
| ): | ||
| self._secret_variable_name = secret_variable_name | ||
| self._client_id_variable_name = client_id_variable_name | ||
|
|
||
| def get_valid_access_token(self) -> str: | ||
| token_url = ( | ||
| "https://identity.diamond.ac.uk/realms/dls/protocol/openid-connect/token" | ||
| ) | ||
|
|
||
| client_id = os.environ.get(self._client_id_variable_name) | ||
| client_secret = os.environ.get(self._secret_variable_name) | ||
|
|
||
| if not client_secret: | ||
| LOGGER.debug("No UDC secret found") | ||
| return "" | ||
| if not client_id: | ||
| LOGGER.debug("No UDC client ID found") | ||
| return "" | ||
|
|
||
| LOGGER.debug("Found UDC secret") | ||
|
|
||
| response = requests.post( | ||
| token_url, | ||
| data={ | ||
| "client_id": client_id, | ||
| "client_secret": client_secret, | ||
| "grant_type": "client_credentials", | ||
| }, | ||
| ) | ||
| response.raise_for_status() | ||
| token = response.json().get("access_token") | ||
| LOGGER.debug("Returning token") | ||
| return token | ||
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,21 @@ | ||
| import logging | ||
|
|
||
| import colorlog | ||
|
|
||
| HANDLER = colorlog.StreamHandler() | ||
| HANDLER.setFormatter( | ||
| colorlog.ColoredFormatter( | ||
| "%(log_color)s%(asctime)s [%(name)s] %(levelname)s: %(message)s", | ||
| log_colors={ | ||
| "DEBUG": "cyan", | ||
| "INFO": "green", | ||
| "WARNING": "yellow", | ||
| "ERROR": "red", | ||
| "CRITICAL": "bold_red", | ||
| }, | ||
| ) | ||
| ) | ||
| LOGGER = logging.getLogger("Queue") | ||
| LOGGER.addHandler(HANDLER) | ||
| LOGGER.setLevel(logging.DEBUG) | ||
| LOGGER.propagate = False |
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,46 @@ | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| from blueapi.config import ApplicationConfig, RestConfig, StompConfig | ||
| from pydantic import HttpUrl | ||
|
|
||
| from daq_queuing_service.blueapi_interaction.clients import get_blueapi_clients | ||
|
|
||
|
|
||
| @patch("daq_queuing_service.blueapi_interaction.clients.UDCTokenRetriever") | ||
| @patch("daq_queuing_service.blueapi_interaction.clients.BlueapiClient") | ||
| @patch("daq_queuing_service.blueapi_interaction.clients.BlueapiRestClient") | ||
| def test_get_blueapi_clients_constructs_clients_with_expected_args_and_returns_clients( | ||
| mock_rest_client: MagicMock, | ||
| mock_blueapi_client: MagicMock, | ||
| mock_token_retriever: MagicMock, | ||
| ): | ||
| rest_config = RestConfig(url=HttpUrl("http://test_url.com")) | ||
| rest_client, blueapi_client = get_blueapi_clients( | ||
| ApplicationConfig(api=rest_config) | ||
| ) | ||
|
|
||
| mock_rest_client.assert_called_once_with( | ||
| config=rest_config, session_manager=mock_token_retriever.return_value | ||
| ) | ||
| mock_blueapi_client.assert_called_once_with(rest_client) | ||
|
|
||
| assert rest_client is mock_rest_client.return_value | ||
| assert blueapi_client is mock_blueapi_client.return_value | ||
|
|
||
|
|
||
| @patch("daq_queuing_service.blueapi_interaction.clients.EventBusClient") | ||
| @patch("daq_queuing_service.blueapi_interaction.clients.BlueapiClient") | ||
| @patch("daq_queuing_service.blueapi_interaction.clients.BlueapiRestClient") | ||
| def test_get_blueapi_clients_constructs_blueapi_client_with_stomp_if_enabled_in_config( | ||
| mock_rest_client: MagicMock, | ||
| mock_blueapi_client: MagicMock, | ||
| mock_event_bus_client: MagicMock, | ||
| ): | ||
| rest_config = RestConfig(url=HttpUrl("http://test_url.com")) | ||
| rest_client, _ = get_blueapi_clients( | ||
| ApplicationConfig(api=rest_config, stomp=StompConfig(enabled=True)) | ||
| ) | ||
|
|
||
| mock_blueapi_client.assert_called_once_with( | ||
| rest_client, mock_event_bus_client.return_value | ||
| ) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should: I think it would be good to have some unit tests on this