Workload identity configuration from env.
Two auth modes:
- federation:
DATAROBOT_WORKLOAD_TOKEN_FILE holds a projected Kubernetes
ServiceAccount token, exchanged at the auth-server token bridge (RFC 8693).
Preferred when both are present, since it needs no secret.
- private_key_jwt:
DATAROBOT_CLIENT_PRIVATE_KEY (PKCS#8 PEM) and
DATAROBOT_CLIENT_KEY_ID sign a client assertion for client_credentials
with DATAROBOT_CLIENT_KEY_ALG (default RS256; PyJWT + cryptography).
The client id is DATAROBOT_CLIENT_ID (WORKLOAD_ID as a fallback). The
token endpoint comes from overrides or discovery off DATAROBOT_PUBLIC_API_ENDPOINT,
which is also the default requested audience.
The token is always refreshed at 80% of its lifetime. DATAROBOT_WORKLOAD_TOKEN_REFRESH_SECONDS
(default 3600, minimum 300) refreshes it sooner when that comes first; 0 or none turns
this off, leaving only the lifetime-based refresh.
WorkloadTokenError
Bases: Exception
A workload token request failed. Messages never carry the key or a token.
Source code in datarobot_genai/core/auth/workload_identity/config.py
| class WorkloadTokenError(Exception):
"""A workload token request failed. Messages never carry the key or a token."""
|
WorkloadIdentityConfig
dataclass
Source code in datarobot_genai/core/auth/workload_identity/config.py
| @dataclass(frozen=True)
class WorkloadIdentityConfig:
mode: AuthMode
client_id: str
api_endpoint: str
token_file: str = ""
private_key: str = field(default="", repr=False)
key_id: str = ""
key_alg: str = DEFAULT_KEY_ALG
discovery_url: str = ""
token_endpoint: str = ""
assertion_audience: str = ""
# Seconds between refreshes, on top of the lifetime-based one; None for lifetime only.
refresh_interval: float | None = DEFAULT_REFRESH_INTERVAL_SECONDS
def __post_init__(self) -> None:
if (
self.refresh_interval is not None
and self.refresh_interval < MIN_REFRESH_INTERVAL_SECONDS
):
raise ValueError(
f"refresh_interval must be None or at least {MIN_REFRESH_INTERVAL_SECONDS:.0f}s"
)
@classmethod
def from_env(cls, environ: Mapping[str, str] | None = None) -> WorkloadIdentityConfig | None:
"""Read the workload identity env; ``None`` when it is absent or incomplete."""
env = os.environ if environ is None else environ
client_id = (env.get(CLIENT_ID_ENV) or env.get(WORKLOAD_ID_ENV) or "").strip()
api_endpoint = env.get(PUBLIC_API_ENDPOINT_ENV, "").strip()
token_file = env.get(WORKLOAD_TOKEN_FILE_ENV, "").strip()
private_key = env.get(PRIVATE_KEY_ENV, "").strip()
key_id = env.get(KEY_ID_ENV, "").strip()
if not (token_file or private_key) or not (client_id and api_endpoint):
return None
if token_file:
mode = AuthMode.FEDERATION
if private_key:
logger.warning("Workload identity: both auth modes configured; using federation")
private_key = key_id = ""
else:
mode = AuthMode.PRIVATE_KEY_JWT
if not key_id:
logger.warning("Workload identity disabled: %s is not set", KEY_ID_ENV)
return None
return cls(
mode=mode,
client_id=client_id,
api_endpoint=api_endpoint,
token_file=token_file,
private_key=private_key,
key_id=key_id,
key_alg=env.get(KEY_ALG_ENV, "").strip() or DEFAULT_KEY_ALG,
discovery_url=env.get(DISCOVERY_URL_ENV, "").strip(),
token_endpoint=env.get(TOKEN_ENDPOINT_ENV, "").strip(),
assertion_audience=env.get(ASSERTION_AUDIENCE_ENV, "").strip(),
refresh_interval=_refresh_interval_from_env(env),
)
def discovery_urls(self) -> list[str]:
"""Metadata URLs to try in order: the override, else RFC 8414 then OIDC."""
if self.discovery_url:
return [self.discovery_url]
# Strip a literal /api/v2; urljoin("../..") breaks path-prefixed installs.
base = self.api_endpoint.rstrip("/")
if base.endswith(_API_V2_SUFFIX):
base = base[: -len(_API_V2_SUFFIX)]
parts = urlsplit(base)
origin = f"{parts.scheme}://{parts.netloc}"
return [
f"{origin}/.well-known/oauth-authorization-server{parts.path}",
f"{base}/.well-known/openid-configuration",
]
|
from_env
classmethod
from_env(environ: Mapping[str, str] | None = None) -> WorkloadIdentityConfig | None
Read the workload identity env; None when it is absent or incomplete.
Source code in datarobot_genai/core/auth/workload_identity/config.py
| @classmethod
def from_env(cls, environ: Mapping[str, str] | None = None) -> WorkloadIdentityConfig | None:
"""Read the workload identity env; ``None`` when it is absent or incomplete."""
env = os.environ if environ is None else environ
client_id = (env.get(CLIENT_ID_ENV) or env.get(WORKLOAD_ID_ENV) or "").strip()
api_endpoint = env.get(PUBLIC_API_ENDPOINT_ENV, "").strip()
token_file = env.get(WORKLOAD_TOKEN_FILE_ENV, "").strip()
private_key = env.get(PRIVATE_KEY_ENV, "").strip()
key_id = env.get(KEY_ID_ENV, "").strip()
if not (token_file or private_key) or not (client_id and api_endpoint):
return None
if token_file:
mode = AuthMode.FEDERATION
if private_key:
logger.warning("Workload identity: both auth modes configured; using federation")
private_key = key_id = ""
else:
mode = AuthMode.PRIVATE_KEY_JWT
if not key_id:
logger.warning("Workload identity disabled: %s is not set", KEY_ID_ENV)
return None
return cls(
mode=mode,
client_id=client_id,
api_endpoint=api_endpoint,
token_file=token_file,
private_key=private_key,
key_id=key_id,
key_alg=env.get(KEY_ALG_ENV, "").strip() or DEFAULT_KEY_ALG,
discovery_url=env.get(DISCOVERY_URL_ENV, "").strip(),
token_endpoint=env.get(TOKEN_ENDPOINT_ENV, "").strip(),
assertion_audience=env.get(ASSERTION_AUDIENCE_ENV, "").strip(),
refresh_interval=_refresh_interval_from_env(env),
)
|
discovery_urls
discovery_urls() -> list[str]
Metadata URLs to try in order: the override, else RFC 8414 then OIDC.
Source code in datarobot_genai/core/auth/workload_identity/config.py
| def discovery_urls(self) -> list[str]:
"""Metadata URLs to try in order: the override, else RFC 8414 then OIDC."""
if self.discovery_url:
return [self.discovery_url]
# Strip a literal /api/v2; urljoin("../..") breaks path-prefixed installs.
base = self.api_endpoint.rstrip("/")
if base.endswith(_API_V2_SUFFIX):
base = base[: -len(_API_V2_SUFFIX)]
parts = urlsplit(base)
origin = f"{parts.scheme}://{parts.netloc}"
return [
f"{origin}/.well-known/oauth-authorization-server{parts.path}",
f"{base}/.well-known/openid-configuration",
]
|
load_signing_key
load_signing_key(private_key: str, alg: str) -> RSAPrivateKey | EllipticCurvePrivateKey
Load the PKCS#8 PEM private key and check it fits alg.
Source code in datarobot_genai/core/auth/workload_identity/config.py
| def load_signing_key(private_key: str, alg: str) -> RSAPrivateKey | EllipticCurvePrivateKey:
"""Load the PKCS#8 PEM private key and check it fits ``alg``."""
if alg not in _RSA_ALGS and alg not in _EC_ALG_CURVES:
raise WorkloadTokenError(f"{KEY_ALG_ENV}={alg!r} is not a supported signing alg")
try:
key = load_pem_private_key(private_key.encode(), password=None)
except Exception:
# Drop the cause: some parser errors echo key material.
raise WorkloadTokenError(f"cannot load {PRIVATE_KEY_ENV} as a PKCS#8 PEM") from None
if alg in _RSA_ALGS:
if not isinstance(key, RSAPrivateKey):
raise WorkloadTokenError(f"{PRIVATE_KEY_ENV} is not an RSA key, as {alg} needs")
elif not isinstance(key, EllipticCurvePrivateKey) or key.curve.name != _EC_ALG_CURVES[alg]:
raise WorkloadTokenError(
f"{PRIVATE_KEY_ENV} is not an EC {_EC_ALG_CURVES[alg]} key, as {alg} needs"
)
return key
|