|
5 | 5 |
|
6 | 6 | from aiohttp import ClientSession |
7 | 7 |
|
8 | | -from blueapi.config import OpaConfig |
| 8 | +from blueapi.config import OIDCConfig, OpaConfig, ServiceAccount |
| 9 | +from blueapi.service.authentication import TiledAuth |
9 | 10 |
|
10 | 11 | LOGGER = logging.getLogger(__name__) |
11 | 12 |
|
@@ -45,3 +46,29 @@ def for_config( |
45 | 46 | return aclosing(cls(instrument, config)) |
46 | 47 | LOGGER.info("No OPA config provided - not creating OpaClient") |
47 | 48 | return nullcontext() |
| 49 | + |
| 50 | + async def require_tiled_service_account(self, token: str): |
| 51 | + if not await self._call_opa( |
| 52 | + self._config.tiled_service_account_check, |
| 53 | + {"token": token, "beamline": self._instrument}, |
| 54 | + ): |
| 55 | + raise ValueError( |
| 56 | + f"Tiled service account is not valid for '{self._instrument}'" |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +async def validate_tiled_config( |
| 61 | + tiled: ServiceAccount | str | None, oidc: OIDCConfig | None, opa: OpaClient | None |
| 62 | +): |
| 63 | + if not isinstance(tiled, ServiceAccount): |
| 64 | + # can't validate an API key |
| 65 | + return |
| 66 | + |
| 67 | + if not opa or not oidc: |
| 68 | + LOGGER.info("Missing OPA or OIDC configuration required to validate tiled auth") |
| 69 | + return |
| 70 | + |
| 71 | + LOGGER.info("Validating tiled configuration") |
| 72 | + tiled.token_url = oidc.token_endpoint |
| 73 | + auth = TiledAuth(tiled) |
| 74 | + await opa.require_tiled_service_account(auth.get_access_token()) |
0 commit comments