-
Notifications
You must be signed in to change notification settings - Fork 43
OSAC-850: Replace static OC tokens with auto-refreshing Keycloak JWTs… #69
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: main
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,23 +1,46 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import base64 | ||
| import json | ||
| import re | ||
| import subprocess | ||
| import time | ||
| from collections.abc import Callable | ||
| from typing import Any | ||
|
|
||
| from tests.core.runner import run | ||
|
|
||
| PUBLIC_API: str = "osac.public.v1" | ||
| PRIVATE_API: str = "osac.private.v1" | ||
|
|
||
| _TOKEN_REFRESH_MARGIN_SECONDS: int = 30 | ||
|
|
||
|
|
||
| def _jwt_exp(token: str) -> float: | ||
| """Extract the ``exp`` claim from a JWT without verifying the signature.""" | ||
| payload = token.split(".")[1] | ||
| # Pad base64url to a multiple of 4 | ||
| padded = payload + "=" * (-len(payload) % 4) | ||
| claims: dict[str, Any] = json.loads(base64.urlsafe_b64decode(padded)) | ||
| return float(claims["exp"]) | ||
|
|
||
|
|
||
| class GRPCClient: | ||
| def __init__(self, *, address: str, token: str) -> None: | ||
| def __init__(self, *, address: str, token_factory: Callable[[], str]) -> None: | ||
| self.address: str = address | ||
| self.token: str = token | ||
| self._token_factory: Callable[[], str] = token_factory | ||
| self._cached_token: str | None = None | ||
| self._token_exp: float = 0.0 | ||
|
|
||
| @property | ||
| def _token(self) -> str: | ||
| if self._cached_token is None or time.time() >= self._token_exp - _TOKEN_REFRESH_MARGIN_SECONDS: | ||
| self._cached_token = self._token_factory() | ||
| self._token_exp = _jwt_exp(self._cached_token) | ||
| return self._cached_token | ||
|
Comment on lines
+35
to
+40
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. 🧹 Nitpick | 🔵 Trivial | ⚖️ Poor tradeoff Thread-safety concern in token refresh logic - Risk: Low The Impact: Not critical for test code, but could cause unnecessary Keycloak requests if tests run with threading. Since this is test infrastructure with session-scoped fixtures, the practical risk is minimal. 🤖 Prompt for AI Agents |
||
|
|
||
| def call(self, *, service: str, data: dict[str, Any] | None = None) -> dict[str, Any]: | ||
| args: list[str] = ["grpcurl", "-insecure", "-H", f"Authorization: Bearer {self.token}"] | ||
| args: list[str] = ["grpcurl", "-insecure", "-H", f"Authorization: Bearer {self._token}"] | ||
| if data is not None: | ||
| args.extend(["-d", json.dumps(data)]) | ||
| args.extend([self.address, service]) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.