blob: a02334e5637bec2030bc5d5e15372229a58e23d8 [file] [log] [blame]
Gabriel Cubaf7349962023-04-26 12:01:25 -05001from kubernetes import client, config
2from kubernetes.client.rest import ApiException
3
4
5def get_secret_data(name) -> dict:
6 # assume that we are executing in a kubernetes pod
7 try:
8 config.load_incluster_config()
9 except config.ConfigException:
10 # we are not running in kubernetes
11 return {}
12 # Read the namespace from the service account
13 current_namespace = open("/var/run/secrets/kubernetes.io/serviceaccount/namespace").read()
14
15 v1 = client.CoreV1Api()
16 try:
17 secret = v1.read_namespaced_secret(name, current_namespace)
18 except ApiException as e:
Gabriel Cuba66df14d2023-06-29 03:20:20 -050019 if e.reason in ("Not Found", "Forbidden"):
20 # Backwards compatibility: we run in k8s but certs don't exist, or we are running in a different namespace
Gabriel Cubaf7349962023-04-26 12:01:25 -050021 return {}
22 else:
23 raise
24 return secret.data