Coverage for execution/live_credentials_io.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-22 12:55 +0000

1"""IO layer for loading live-broker credentials (Phase V2-22). 

2 

3Two equally-supported patterns, tried in order: 

41. `ib_config.py` (repo-root, gitignored, planned-but-absent per 

5 .gitignore - a human creates it locally, never committed). 

62. AETHER_IB_ACCOUNT / AETHER_IB_USER_NAME / AETHER_IB_PASSWORD / 

7 AETHER_IB_TRADING_MODE environment variables (populated from `.env.live`, 

8 see .env.live.example). 

9 

10This is pre-flight validation only, feeding execution.live_credentials's 

11credentials_present() - it does not wire Lean itself. The tracked `lean.json` 

12ships with ib-account/ib-user-name/ib-password/ib-trading-mode as empty 

13placeholders; real values are rendered into a gitignored `lean.live.json` by 

14scripts/render_lean_credentials.py (`aq render-lean-config`) from these same 

15AETHER_* vars, and Lean is pointed at that file via `--lean-config` (see 

16execution/lean_config_render.py and development/infrastructure.md's "Live" 

17runbook). Never raises - missing everything just yields all-empty-string values. 

18""" 

19 

20from __future__ import annotations 

21 

22from execution.secrets_io import secret_env 

23 

24 

25def load_live_credentials(account_id: str | None = None) -> dict: 

26 """The IB credential fields for one account (`None` or "default" is today's single account). The default account still 

27 tries a gitignored `ib_config.py` first; every value then comes through `execution.secrets_io.secret_env` (a `*_FILE` 

28 file, a Docker secret, `AETHER_[<ACCOUNT>_]IB_*`, or the keyring), V6.6.5 #348/#349.""" 

29 default = not account_id or account_id == "default" 

30 if default: 

31 try: 

32 import ib_config # type: ignore[import-not-found] 

33 

34 return { 

35 "ib_account": str(getattr(ib_config, "IB_ACCOUNT", "")), 

36 "ib_user_name": str(getattr(ib_config, "IB_USER_NAME", "")), 

37 "ib_password": str(getattr(ib_config, "IB_PASSWORD", "")), 

38 "ib_trading_mode": str(getattr(ib_config, "IB_TRADING_MODE", "paper")), 

39 } 

40 except ImportError: 

41 pass 

42 

43 return { 

44 "ib_account": secret_env("IB_ACCOUNT", "", account_id), 

45 "ib_user_name": secret_env("IB_USER_NAME", "", account_id), 

46 "ib_password": secret_env("IB_PASSWORD", "", account_id), 

47 "ib_trading_mode": secret_env("IB_TRADING_MODE", "paper", account_id), 

48 } 

49 

50 

51def load_coinbase_credentials(account_id: str | None = None) -> dict: 

52 """The Coinbase Advanced Trade key pair for one account, resolved like the IB fields. Empty strings when absent.""" 

53 return { 

54 "coinbase_api_key": secret_env("COINBASE_API_KEY", "", account_id), 

55 "coinbase_api_secret": secret_env("COINBASE_API_SECRET", "", account_id), 

56 } 

57 

58 

59def load_postgres_dsn() -> str: 

60 """The runtime Postgres DSN, read from AETHER_POSTGRES_DSN (the same var 

61 docker-compose.yml injects; or its `*_FILE`, Docker secret or keyring entry). Empty string if unset. Fed to 

62 execution.paper_readiness's live gate so a live run against the published 

63 dev-default password fails closed - see 

64 execution.live_credentials.postgres_dsn_is_live_safe().""" 

65 return secret_env("POSTGRES_DSN", "")