Retrieve a secret from predefined sources in order of priority.
Sources tried in order
- HashiCorp Vault (https://10.42.1.2:8200, policy justicier-runtime)
- Docker secrets (/run/secrets/)
- Local file (/secrets/)
- Environment variable
Source code in source/src/secret.py
| def read_secret(secret_name):
"""Retrieve a secret from predefined sources in order of priority.
Sources tried in order:
1. HashiCorp Vault (https://10.42.1.2:8200, policy justicier-runtime)
2. Docker secrets (/run/secrets/<name>)
3. Local file (<project_root>/secrets/<name>)
4. Environment variable
"""
sources = [
lambda: read_vault_secret(secret_name),
lambda: read_file_content(f"/run/secrets/{secret_name}"),
lambda: read_file_content(
os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
"secrets",
secret_name
).__str__()
),
lambda: read_env_var(secret_name),
]
for source in sources:
try:
return source()
except Exception as e:
print(e)
continue
print(f"Could not read {secret_name} from any source")
sys.exit(1)
|