Skip to content

Commit 156b5fd

Browse files
Fallback to K8s SA token for PLATFORM_SERVICE_JWT
Use the Kubernetes pod service account token as a fallback for PLATFORM_SERVICE_JWT when no explicit env var is provided. Adds K8S_SA_TOKEN_PATH (with a default of /var/run/secrets/kubernetes.io/serviceaccount/token) and a helper _read_k8s_sa_token() that safely reads the token file (handling missing/permission errors). PLATFORM_SERVICE_JWT is now populated from the env var or the token file, allowing in-cluster authentication without injecting a JWT.
1 parent ea03629 commit 156b5fd

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

src/config/settings.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,21 @@
7171
# OPENRAG_BOOTSTRAP_OS_SECURITY_ON_STARTUP is on, lifespan decodes this
7272
# token to derive the admin username used to bootstrap the OpenSearch
7373
# security context (roles + all_access mapping).
74-
PLATFORM_SERVICE_JWT = os.getenv("PLATFORM_SERVICE_JWT")
74+
# Falls back to the pod's Kubernetes service account token when running
75+
# inside a cluster and no explicit JWT is injected.
76+
_DEFAULT_K8S_SA_TOKEN_PATH = "/var/run/secrets/kubernetes.io/serviceaccount/token"
77+
K8S_SA_TOKEN_PATH = os.getenv("K8S_SA_TOKEN_PATH", _DEFAULT_K8S_SA_TOKEN_PATH)
78+
79+
80+
def _read_k8s_sa_token() -> str | None:
81+
try:
82+
with open(K8S_SA_TOKEN_PATH) as f:
83+
return f.read().strip() or None
84+
except (FileNotFoundError, PermissionError):
85+
return None
86+
87+
88+
PLATFORM_SERVICE_JWT = os.getenv("PLATFORM_SERVICE_JWT") or _read_k8s_sa_token()
7589
IBM_JWT_PUBLIC_KEY_URL = os.getenv("IBM_JWT_PUBLIC_KEY_URL", "")
7690
IBM_SESSION_COOKIE_NAME = os.getenv("IBM_SESSION_COOKIE_NAME", "ibm-openrag-session")
7791
IBM_CREDENTIALS_HEADER = os.getenv("IBM_CREDENTIALS_HEADER", "X-IBM-LH-Credentials")

0 commit comments

Comments
 (0)