-
Notifications
You must be signed in to change notification settings - Fork 14
feat: Validate tiled ServiceAccount config at startup #1548
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
base: opa-client
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,13 @@ | ||
| from unittest.mock import AsyncMock, MagicMock, patch | ||
| from contextlib import AbstractContextManager, nullcontext | ||
| from unittest.mock import AsyncMock, MagicMock, Mock, patch | ||
|
|
||
| import pytest | ||
| from pydantic import HttpUrl | ||
|
|
||
| from blueapi.config import OpaConfig | ||
| from blueapi.config import OIDCConfig, OpaConfig, ServiceAccount | ||
| from blueapi.service.authorization import ( | ||
| OpaClient, | ||
| validate_tiled_config, | ||
| ) | ||
|
|
||
| # Reusable client patch decorator | ||
|
|
@@ -20,9 +22,50 @@ | |
| def opa_config() -> OpaConfig: | ||
| return OpaConfig( | ||
| root=HttpUrl("http://auth.example.com"), | ||
| tiled_service_account_check="/auth/tiled", | ||
| ) | ||
|
|
||
|
|
||
| @patch_client_session | ||
| @pytest.mark.parametrize( | ||
| "result,context", | ||
| [ | ||
| (False, pytest.raises(ValueError, match="Tiled service account is not valid ")), | ||
| (True, nullcontext()), | ||
| ], | ||
| ) | ||
| async def test_tiled_service_account( | ||
| session: MagicMock, | ||
| opa_config: OpaConfig, | ||
| result: bool, | ||
| context: AbstractContextManager, | ||
| ): | ||
| session.return_value.post = AsyncMock( | ||
|
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. I'm writing this comment only once,But it applies to all the tests. I personally think that pytest-aiohttp provide a better way of do this type of test. Creating so many mocks looks unnecessary/ clutters the code.
Contributor
Author
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. I'm not sure how pytest-aiohttp would be used here? It appears to be targetted at testing webservers not mocking out responses. |
||
| return_value=MagicMock(json=AsyncMock(return_value={"result": result})) | ||
| ) | ||
|
|
||
| client = OpaClient(instrument="p99", config=opa_config) | ||
|
|
||
| session.assert_called_once_with(base_url="http://auth.example.com/") | ||
| with context: | ||
| await client.require_tiled_service_account(token="foo_bar") | ||
| session().post.assert_called_once_with( | ||
| "/auth/tiled", | ||
| json={"input": {"token": "foo_bar", "beamline": "p99", "audience": "account"}}, | ||
| ) | ||
|
|
||
|
|
||
| @patch_client_session | ||
| async def test_exception_raised_when_opa_fails( | ||
| session: MagicMock, opa_config: OpaConfig | ||
| ): | ||
| session.return_value.post = AsyncMock(side_effect=RuntimeError("Connection failed")) | ||
| async with OpaClient.for_config("p45", opa_config) as client: | ||
| assert client is not None | ||
| with pytest.raises(RuntimeError, match="Connection failed"): | ||
| await client.require_tiled_service_account(token="foo_bar") | ||
|
|
||
|
|
||
| @patch_client_session | ||
| async def test_session_closed(session: MagicMock, opa_config: OpaConfig): | ||
| async with OpaClient.for_config("p45", opa_config): | ||
|
|
@@ -60,3 +103,49 @@ async def test_opa_adds_input_fields(session: MagicMock, opa_config: OpaConfig): | |
| "foo/bar", | ||
| json={"input": {"beamline": "p45", "audience": "account", "foo": "bar"}}, | ||
| ) | ||
|
|
||
|
|
||
| async def test_validate_tiled_config(): | ||
| opa = MagicMock(spec=OpaClient) | ||
| tiled = ServiceAccount() | ||
| oidc = Mock(spec=OIDCConfig) | ||
| oidc.token_endpoint = "token-endpoint" | ||
| with patch("blueapi.service.authorization.TiledAuth") as auth: | ||
| auth.return_value.get_access_token.return_value = "tiled-token" | ||
| await validate_tiled_config(tiled, oidc, opa) | ||
|
|
||
| auth.assert_called_once_with(tiled) | ||
| opa.require_tiled_service_account.assert_called_once_with("tiled-token") | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "tiled_auth,oidc,opa_client", | ||
| [ | ||
| (None, None, MagicMock(spec=OpaClient)), | ||
| ( | ||
| None, | ||
| OIDCConfig(well_known_url="http://example.com", client_id="test-client"), | ||
| MagicMock(spec=OpaClient), | ||
| ), | ||
| ("api_key", None, MagicMock(spec=OpaClient)), | ||
| ( | ||
| "api_key", | ||
| OIDCConfig(well_known_url="http://example.com", client_id="test-client"), | ||
| MagicMock(spec=OpaClient), | ||
| ), | ||
| (ServiceAccount(), None, MagicMock(spec=OpaClient)), | ||
| ( | ||
| ServiceAccount(), | ||
| OIDCConfig(well_known_url="http://example.com", client_id="test-client"), | ||
| None, | ||
| ), | ||
| ], | ||
| ) | ||
| async def test_validate_tiled_config_with_missing_config( | ||
| tiled_auth: ServiceAccount | str | None, | ||
| oidc: OIDCConfig | None, | ||
| opa_client: MagicMock | None, | ||
| ): | ||
| assert await validate_tiled_config(tiled_auth, oidc, opa_client) is None | ||
| if opa_client is not None: | ||
| opa_client.require_tiled_service_account.assert_not_called() | ||
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.
nit: This changes only when the policy changes so we can have a default value for it here
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.
It depends if we want to include diamond specific configuration as defaults? As far as I can see so far, there is nothing included so far. Would the services copier template be a better place to put the default values?