|
16 | 16 | logger = logging_config.logging.getLogger("bootstrap.oci") |
17 | 17 |
|
18 | 18 |
|
| 19 | +def _apply_env_overrides_to_default_profile(config: list[dict]) -> None: |
| 20 | + """Apply environment variable overrides to the DEFAULT OCI profile.""" |
| 21 | + def override(profile: dict, key: str, env_key: str, env: dict, overrides: dict, default=None): |
| 22 | + val = env.get(env_key) |
| 23 | + if val is not None and val != profile.get(key): |
| 24 | + overrides[key] = (profile.get(key), val) |
| 25 | + return val |
| 26 | + return profile.get(key, default) |
| 27 | + |
| 28 | + env = os.environ |
| 29 | + |
| 30 | + for profile in config: |
| 31 | + if profile["auth_profile"] == oci.config.DEFAULT_PROFILE: |
| 32 | + overrides = {} |
| 33 | + |
| 34 | + profile.update( |
| 35 | + { |
| 36 | + "tenancy": override(profile, "tenancy", "OCI_CLI_TENANCY", env, overrides), |
| 37 | + "region": override(profile, "region", "OCI_CLI_REGION", env, overrides), |
| 38 | + "user": override(profile, "user", "OCI_CLI_USER", env, overrides), |
| 39 | + "fingerprint": override(profile, "fingerprint", "OCI_CLI_FINGERPRINT", env, overrides), |
| 40 | + "key_file": override(profile, "key_file", "OCI_CLI_KEY_FILE", env, overrides), |
| 41 | + "security_token_file": override( |
| 42 | + profile, "security_token_file", "OCI_CLI_SECURITY_TOKEN_FILE", env, overrides |
| 43 | + ), |
| 44 | + "authentication": env.get("OCI_CLI_AUTH") |
| 45 | + or ("security_token" if profile.get("security_token_file") else "api_key"), |
| 46 | + "genai_compartment_id": override( |
| 47 | + profile, "genai_compartment_id", "OCI_GENAI_COMPARTMENT_ID", env, overrides, None |
| 48 | + ), |
| 49 | + "genai_region": override(profile, "genai_region", "OCI_GENAI_REGION", env, overrides, None), |
| 50 | + "log_requests": profile.get("log_requests", False), |
| 51 | + "additional_user_agent": profile.get("additional_user_agent", ""), |
| 52 | + "pass_phrase": profile.get("pass_phrase"), |
| 53 | + } |
| 54 | + ) |
| 55 | + |
| 56 | + if overrides: |
| 57 | + logger.info("Environment variable overrides for OCI DEFAULT profile:") |
| 58 | + for key, (old, new) in overrides.items(): |
| 59 | + logger.info(" %s: '%s' -> '%s'", key, old, new) |
| 60 | + |
| 61 | + |
19 | 62 | def main() -> list[OracleCloudSettings]: |
20 | 63 | """Read in OCI Configuration options into an object""" |
21 | 64 | logger.debug("*** Bootstrapping OCI - Start") |
@@ -62,45 +105,7 @@ def main() -> list[OracleCloudSettings]: |
62 | 105 | config.append({"auth_profile": oci.config.DEFAULT_PROFILE}) |
63 | 106 |
|
64 | 107 | # Override DEFAULT profile with environment variables |
65 | | - def override(profile: dict, key: str, env_key: str, env: dict, overrides: dict, default=None): |
66 | | - val = env.get(env_key) |
67 | | - if val is not None and val != profile.get(key): |
68 | | - overrides[key] = (profile.get(key), val) |
69 | | - return val |
70 | | - return profile.get(key, default) |
71 | | - |
72 | | - env = os.environ |
73 | | - |
74 | | - for profile in config: |
75 | | - if profile["auth_profile"] == oci.config.DEFAULT_PROFILE: |
76 | | - overrides = {} |
77 | | - |
78 | | - profile.update( |
79 | | - { |
80 | | - "tenancy": override(profile, "tenancy", "OCI_CLI_TENANCY", env, overrides), |
81 | | - "region": override(profile, "region", "OCI_CLI_REGION", env, overrides), |
82 | | - "user": override(profile, "user", "OCI_CLI_USER", env, overrides), |
83 | | - "fingerprint": override(profile, "fingerprint", "OCI_CLI_FINGERPRINT", env, overrides), |
84 | | - "key_file": override(profile, "key_file", "OCI_CLI_KEY_FILE", env, overrides), |
85 | | - "security_token_file": override( |
86 | | - profile, "security_token_file", "OCI_CLI_SECURITY_TOKEN_FILE", env, overrides |
87 | | - ), |
88 | | - "authentication": env.get("OCI_CLI_AUTH") |
89 | | - or ("security_token" if profile.get("security_token_file") else "api_key"), |
90 | | - "genai_compartment_id": override( |
91 | | - profile, "genai_compartment_id", "OCI_GENAI_COMPARTMENT_ID", env, overrides, None |
92 | | - ), |
93 | | - "genai_region": override(profile, "genai_region", "OCI_GENAI_REGION", env, overrides, None), |
94 | | - "log_requests": profile.get("log_requests", False), |
95 | | - "additional_user_agent": profile.get("additional_user_agent", ""), |
96 | | - "pass_phrase": profile.get("pass_phrase"), |
97 | | - } |
98 | | - ) |
99 | | - |
100 | | - if overrides: |
101 | | - logger.info("Environment variable overrides for OCI DEFAULT profile:") |
102 | | - for key, (old, new) in overrides.items(): |
103 | | - logger.info(" %s: '%s' -> '%s'", key, old, new) |
| 108 | + _apply_env_overrides_to_default_profile(config) |
104 | 109 |
|
105 | 110 | # Build final OracleCloudSettings objects |
106 | 111 | oci_objects = [] |
|
0 commit comments