identity
azure_bootstrap.identity
¶
Workload Identity / DefaultAzureCredential wrapper.
Single source of truth for "which Azure credential should this process
use." Replaces ad-hoc DefaultAzureCredential() instantiation across an
app and codifies the WorkloadIdentity-first preference (no client secrets
in pod env in production).
Classes:
| Name | Description |
|---|---|
TokenCache |
In-process LRU/TTL cache for |
Functions:
| Name | Description |
|---|---|
credential_kind |
Inspect inputs + env to decide which kind |
build_credential |
Build the preferred Azure credential for the current environment. |
credential_health |
Acquire a token, measure latency, return a health-check dict. |
build_tenant_credential |
Build a |
build_tenant_credential_cached |
Return an access token for tenant_id / scope, using the cache. |
TokenCache
¶
In-process LRU/TTL cache for (tenant_id, scope) -> token pairs.
Tokens are considered stale when they are within
_TOKEN_EARLY_REFRESH_SECS (5 minutes) of their expires_at
timestamp. The maximum number of entries is controlled by the
TOKEN_CACHE_MAX_SIZE environment variable (default 500).
All methods are thread-safe. This class is a namespace — it wraps module-level state so the cache can be reset between tests.
Methods:
| Name | Description |
|---|---|
get_cached_token |
Return a cached token for (tenant_id, scope) or |
cache_token |
Store token in the cache under (tenant_id, scope). |
invalidate |
Invalidate cache entries. |
get_cached_token
staticmethod
¶
Return a cached token for (tenant_id, scope) or None.
Returns None when no entry exists or the token is within
5 minutes of expiry (triggering an early refresh).
Source code in azure_bootstrap/identity/__init__.py
cache_token
staticmethod
¶
Store token in the cache under (tenant_id, scope).
If the cache is at capacity the oldest entry (by insertion order, since Python 3.7+ dicts are ordered) is evicted first.
Source code in azure_bootstrap/identity/__init__.py
invalidate
staticmethod
¶
invalidate(tenant_id: str | None = None) -> None
Invalidate cache entries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tenant_id
|
str | None
|
When supplied, only entries for this tenant are
removed. When |
None
|
Source code in azure_bootstrap/identity/__init__.py
credential_kind
¶
credential_kind(*, tenant_id: str | None = None, client_id: str | None = None, client_secret: str | None = None) -> CredentialKind
Inspect inputs + env to decide which kind build_credential would return.
Does not actually construct a credential — useful for /api/health probes.
Source code in azure_bootstrap/identity/__init__.py
build_credential
¶
build_credential(*, tenant_id: str | None = None, client_id: str | None = None, client_secret: str | None = None, prefer: CredentialKind | None = None, token_file_path: str = _DEFAULT_TOKEN_FILE) -> Any
Build the preferred Azure credential for the current environment.
Resolution order (when prefer is None):
1. ClientSecretCredential when client_secret (or env) is set.
2. WorkloadIdentityCredential when tenant_id and client_id
are set but secret is empty.
3. DefaultAzureCredential as last-resort fallback.
Source code in azure_bootstrap/identity/__init__.py
credential_health
¶
credential_health(scopes: tuple[str, ...] = ('https://management.azure.com/.default',)) -> dict[str, Any]
Acquire a token, measure latency, return a health-check dict.
Source code in azure_bootstrap/identity/__init__.py
build_tenant_credential
¶
build_tenant_credential(tenant_id: str, *, app_client_id: str | None = None, token_file_path: str = _DEFAULT_TOKEN_FILE) -> Any
Build a WorkloadIdentityCredential scoped to tenant_id.
Zero-secret — uses the federated token file on disk. Designed for multi-tenant Entra applications that need to acquire per-customer-tenant Graph (or ARM) tokens from a single registered app.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tenant_id
|
str
|
The target customer / resource tenant. |
required |
app_client_id
|
str | None
|
The multi-tenant app's client ID. Falls back to
|
None
|
token_file_path
|
str
|
Path to the federated token file written by the Azure Workload Identity webhook. Defaults to the standard AKS path. |
_DEFAULT_TOKEN_FILE
|
Returns:
| Type | Description |
|---|---|
Any
|
A |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no client ID can be resolved. |
ImportError
|
If |
Source code in azure_bootstrap/identity/__init__.py
build_tenant_credential_cached
¶
build_tenant_credential_cached(tenant_id: str, scope: str, *, app_client_id: str | None = None) -> str
Return an access token for tenant_id / scope, using the cache.
On a cache hit the cached token string is returned immediately.
On a miss a fresh WorkloadIdentityCredential is built via
:func:build_tenant_credential, get_token(scope) is called, and
the result is stored in the cache before being returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tenant_id
|
str
|
The target customer / resource tenant. |
required |
scope
|
str
|
The OAuth2 scope to request (e.g.
|
required |
app_client_id
|
str | None
|
Passed through to :func: |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The raw token string ( |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no client ID can be resolved. |
ImportError
|
If |