|
14 | 14 | from galileo_core.helpers.api_client import ApiClient |
15 | 15 | from galileo_core.schemas.base_config import GalileoConfig |
16 | 16 | from splunk_ao.config import O11yApiClient, SplunkAOConfig |
17 | | -from splunk_ao.shared.exceptions import AmbiguousConfigurationError |
| 17 | +from splunk_ao.shared.exceptions import AmbiguousConfigurationError, MissingConfigurationError |
18 | 18 |
|
19 | 19 | _CONFIG_ENV_VARS = ( |
20 | 20 | "SPLUNK_AO_REALM", |
@@ -133,10 +133,29 @@ def fake_stream_request( |
133 | 133 |
|
134 | 134 | @pytest.mark.parametrize("token_var", ["SPLUNK_AO_SF_TOKEN", "SPLUNK_AO_SF_API_TOKEN"]) |
135 | 135 | def test_o11y_auth_guard_accepts_environment_tokens(token_var: str) -> None: |
136 | | - with config_env(**{token_var: "tok"}): |
| 136 | + with config_env(SPLUNK_AO_REALM="us1", **{token_var: "tok"}): |
137 | 137 | assert SplunkAOConfig._check_auth_config({}) is None |
138 | 138 |
|
139 | 139 |
|
| 140 | +def test_o11y_auth_guard_requires_realm() -> None: |
| 141 | + with config_env(SPLUNK_AO_SF_API_TOKEN="tok"): |
| 142 | + assert "SPLUNK_AO_REALM" in (SplunkAOConfig._check_auth_config({}) or "") |
| 143 | + |
| 144 | + |
| 145 | +def test_o11y_get_with_api_token_but_no_realm_fails_clearly(monkeypatch: pytest.MonkeyPatch) -> None: |
| 146 | + monkeypatch.setattr(SplunkAOConfig, "_instance", None) |
| 147 | + with config_env(SPLUNK_AO_SF_API_TOKEN="tok"): |
| 148 | + with pytest.raises(MissingConfigurationError, match="SPLUNK_AO_REALM"): |
| 149 | + SplunkAOConfig.get() |
| 150 | + |
| 151 | + |
| 152 | +def test_o11y_auth_guard_requires_at_least_one_token() -> None: |
| 153 | + with config_env(SPLUNK_AO_REALM="us1"): |
| 154 | + error = SplunkAOConfig._check_auth_config({}) or "" |
| 155 | + assert "SPLUNK_AO_SF_TOKEN" in error |
| 156 | + assert "SPLUNK_AO_SF_API_TOKEN" in error |
| 157 | + |
| 158 | + |
140 | 159 | def test_o11y_auth_guard_does_not_accept_token_kwargs() -> None: |
141 | 160 | with config_env(): |
142 | 161 | assert SplunkAOConfig._check_auth_config({"sf_token": "tok"}) is not None |
@@ -204,6 +223,27 @@ async def async_fail(*args: object, **kwargs: object) -> None: |
204 | 223 | assert client.ssl_context is False |
205 | 224 |
|
206 | 225 |
|
| 226 | +def test_o11y_get_supports_crud_only_api_token(monkeypatch: pytest.MonkeyPatch) -> None: |
| 227 | + def fail(*args: object, **kwargs: object) -> None: |
| 228 | + raise AssertionError("o11y configuration must not use standalone validation") |
| 229 | + |
| 230 | + async def async_fail(*args: object, **kwargs: object) -> None: |
| 231 | + fail() |
| 232 | + |
| 233 | + monkeypatch.setattr(SplunkAOConfig, "_instance", None) |
| 234 | + monkeypatch.setattr(GalileoConfig, "get_jwt_token", staticmethod(fail)) |
| 235 | + monkeypatch.setattr(ApiClient, "make_request", staticmethod(async_fail)) |
| 236 | + monkeypatch.setattr(ApiClient, "request", fail) |
| 237 | + |
| 238 | + with config_env(SPLUNK_AO_REALM="us1", SPLUNK_AO_SF_API_TOKEN="api-token"): |
| 239 | + cfg = SplunkAOConfig.get(ssl_context=False) |
| 240 | + client = cfg.api_client |
| 241 | + |
| 242 | + assert cfg.jwt_token is None |
| 243 | + assert isinstance(client, O11yApiClient) |
| 244 | + assert client.auth_header == {"X-SF-Token": "api-token"} |
| 245 | + |
| 246 | + |
207 | 247 | def test_ambiguous_environment_fails_before_config_construction(monkeypatch: pytest.MonkeyPatch) -> None: |
208 | 248 | monkeypatch.setattr(SplunkAOConfig, "_instance", None) |
209 | 249 | with config_env(SPLUNK_AO_REALM="us1", SPLUNK_AO_SF_TOKEN="tok", SPLUNK_AO_API_KEY="key"): |
|
0 commit comments