Short-lived credentials via OIDC
Implement `OidcBroker` — exchange a CI job's OIDC token for short-lived
credentials, without long-lived secrets in the repository.
```python
broker = OidcBroker(
exchange,
allowed_repos=["org/api"],
allowed_refs=["refs/heads/main", "refs/tags/v*"],
max_ttl=900,
)
credentials = broker.get_credentials(
repo="org/api",
ref="refs/heads/main",
oidc_token="...",
requested_ttl=900,
)
```
`exchange(oidc_token, *, audience, duration_seconds)` returns a dict:
```python
{
"access_key": "...",
"secret_key": "...",
"session_token": "...",
"expires_in": 900,
}
```
Rules:
- `repo` must match one of `allowed_repos` exactly, otherwise
`CredentialError`;
- `ref` must match at least one glob pattern from `allowed_refs`
(`fnmatchcase`), otherwise `CredentialError`;
- an empty `oidc_token` raises `CredentialError`;
- `requested_ttl` is an integer greater than zero, otherwise `ValueError`;
- the effective TTL is `min(requested_ttl, max_ttl)`; a request above the limit
is trimmed, not rejected;
- `exchange` receives `audience=AUDIENCE` (a module constant) and
`duration_seconds` set to the effective TTL;
- the result must contain non-empty `access_key`, `secret_key`, `session_token`
and an integer `expires_in` in the range `1..max_ttl`; otherwise
`CredentialError`;
- return a new dict with exactly four fields, dropping any extra provider
fields;
- an `exchange` error of another type is wrapped into `CredentialError`, while a
`CredentialError` is re-raised as is;
- credentials are not cached: every call is a fresh exchange.
In the constructor, empty `allowed_repos` / `allowed_refs`, `max_ttl <= 0` or a
non-callable `exchange` raise `ValueError`.
The AI mentor is available after you log in: it sees your attempt history and does not spend resources on anonymous requests.
Log in and askHint 1
Why is an OIDC exchange better than a secret in a CI variable, and which checks
must happen before the exchange itself?