Coverage for execution/secrets_io.py: 100%
51 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 12:55 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 12:55 +0000
1"""The real environment, files and OS keyring behind `execution/secrets.py` (V6.6.5, #348).
3`secret_env("POSTGRES_DSN", default)` is the drop-in for `os.environ.get("AETHER_POSTGRES_DSN", default)`: with only the
4environment variable set it returns the same string, and it also finds the value in a `*_FILE` file, a Docker secret or
5the keyring. Imports nothing heavy; `keyring` loads only when it is installed and a lookup gets that far.
6"""
8from __future__ import annotations
10import os
11from pathlib import Path
13from execution.secrets import KEYRING_SERVICE, SECRET_NAMES, ResolvedSecret, SecretValue, env_prefix, resolve_secret, secret_status
15__all__ = ["read_secret_file", "keyring_get", "keyring_set", "keyring_delete", "load_secret", "secret_env", "all_secret_status"]
18def read_secret_file(path: str) -> str | None:
19 """A file's text, or None when it is missing or unreadable."""
20 try:
21 target = Path(path)
22 return target.read_text(encoding="utf-8") if target.is_file() else None
23 except OSError:
24 return None
27def _keyring():
28 try:
29 import keyring # type: ignore[import-not-found]
30 except Exception:
31 return None
32 return keyring
35def keyring_get(service: str, user: str) -> str | None:
36 module = _keyring()
37 if module is None:
38 return None
39 try:
40 return module.get_password(service, user)
41 except Exception:
42 return None
45def keyring_set(name: str, value: str, account_id: str | None = None) -> None:
46 """Store a secret in the OS keyring (`aq secrets set`). Raises RuntimeError without the `keyring` package."""
47 module = _keyring()
48 if module is None:
49 raise RuntimeError("the keyring package is not installed: pip install keyring (or aether-quant[live])")
50 env_prefix(account_id) # validates the account id
51 user = name if not account_id or account_id == "default" else f"{account_id}/{name}"
52 module.set_password(KEYRING_SERVICE, user, value)
55def keyring_delete(name: str, account_id: str | None = None) -> bool:
56 module = _keyring()
57 if module is None:
58 return False
59 user = name if not account_id or account_id == "default" else f"{account_id}/{name}"
60 try:
61 module.delete_password(KEYRING_SERVICE, user)
62 return True
63 except Exception:
64 return False
67def load_secret(name: str, account_id: str | None = None) -> ResolvedSecret:
68 """Resolve one secret from the real sources."""
69 return resolve_secret(name, account_id=account_id, env=os.environ, read_file=read_secret_file, keyring_get=keyring_get)
72def secret_env(name: str, default: str = "", account_id: str | None = None) -> str:
73 """The secret's text, or `default`: the replacement for `os.environ.get("AETHER_<NAME>", default)`."""
74 resolved = load_secret(name, account_id)
75 if isinstance(resolved.value, SecretValue):
76 return resolved.value.reveal()
77 # a variable set to "" reads as "" (not the default), exactly as os.environ.get did
78 return os.environ.get(f"{env_prefix(account_id)}{name}", default)
81def all_secret_status(account_id: str | None = None, names: tuple[str, ...] = SECRET_NAMES) -> list[dict[str, str | bool]]:
82 """`secret_status` of every known secret for one account."""
83 return secret_status(load_secret(name, account_id) for name in names)