| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import re |
| 4 |
|
| 5 |
import httpx2 |
| 6 |
import pytest |
| 7 |
|
| 8 |
from anthropic.lib.aws._auth import get_auth_headers |
| 9 |
from anthropic.lib.aws._credentials import ( |
| 10 |
resolve_region, |
| 11 |
resolve_api_key, |
| 12 |
resolve_base_url, |
| 13 |
resolve_auth_mode, |
| 14 |
resolve_workspace_id, |
| 15 |
validate_credentials, |
| 16 |
) |
| 17 |
|
| 18 |
# --- validate_credentials --- |
| 19 |
|
| 20 |
|
| 21 |
class TestValidateCredentials: |
| 22 |
def test_both_provided_passes(self) -> None: |
| 23 |
validate_credentials(aws_access_key="AKID", aws_secret_key="secret") |
| 24 |
|
| 25 |
def test_neither_provided_passes(self) -> None: |
| 26 |
validate_credentials(aws_access_key=None, aws_secret_key=None) |
| 27 |
|
| 28 |
def test_access_key_only_raises(self) -> None: |
| 29 |
with pytest.raises(ValueError, match="aws_access_key.*without.*aws_secret_key"): |
| 30 |
validate_credentials(aws_access_key="AKID", aws_secret_key=None) |
| 31 |
|
| 32 |
def test_secret_key_only_raises(self) -> None: |
| 33 |
with pytest.raises(ValueError, match="aws_secret_key.*without.*aws_access_key"): |
| 34 |
validate_credentials(aws_access_key=None, aws_secret_key="secret") |
| 35 |
|
| 36 |
|
| 37 |
# --- resolve_auth_mode --- |
| 38 |
|
| 39 |
|
| 40 |
class TestResolveAuthMode: |
| 41 |
def test_api_key_arg_returns_false(self) -> None: |
| 42 |
assert ( |
| 43 |
resolve_auth_mode( |
| 44 |
api_key="key", |
| 45 |
aws_access_key=None, |
| 46 |
aws_secret_key=None, |
| 47 |
aws_profile=None, |
| 48 |
) |
| 49 |
is False |
| 50 |
) |
| 51 |
|
| 52 |
def test_explicit_creds_returns_true(self) -> None: |
| 53 |
assert ( |
| 54 |
resolve_auth_mode( |
| 55 |
api_key=None, |
| 56 |
aws_access_key="AKID", |
| 57 |
aws_secret_key="secret", |
| 58 |
aws_profile=None, |
| 59 |
) |
| 60 |
is True |
| 61 |
) |
| 62 |
|
| 63 |
def test_access_key_alone_returns_true(self) -> None: |
| 64 |
assert ( |
| 65 |
resolve_auth_mode( |
| 66 |
api_key=None, |
| 67 |
aws_access_key="AKID", |
| 68 |
aws_secret_key=None, |
| 69 |
aws_profile=None, |
| 70 |
) |
| 71 |
is True |
| 72 |
) |
| 73 |
|
| 74 |
def test_profile_returns_true(self) -> None: |
| 75 |
assert ( |
| 76 |
resolve_auth_mode( |
| 77 |
api_key=None, |
| 78 |
aws_access_key=None, |
| 79 |
aws_secret_key=None, |
| 80 |
aws_profile="my-profile", |
| 81 |
) |
| 82 |
is True |
| 83 |
) |
| 84 |
|
| 85 |
def test_env_api_key_returns_false(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 86 |
monkeypatch.setenv("ANTHROPIC_AWS_API_KEY", "env-key") |
| 87 |
assert ( |
| 88 |
resolve_auth_mode( |
| 89 |
api_key=None, |
| 90 |
aws_access_key=None, |
| 91 |
aws_secret_key=None, |
| 92 |
aws_profile=None, |
| 93 |
) |
| 94 |
is False |
| 95 |
) |
| 96 |
|
| 97 |
def test_no_args_no_env_defaults_to_sigv4(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 98 |
monkeypatch.delenv("ANTHROPIC_AWS_API_KEY", raising=False) |
| 99 |
assert ( |
| 100 |
resolve_auth_mode( |
| 101 |
api_key=None, |
| 102 |
aws_access_key=None, |
| 103 |
aws_secret_key=None, |
| 104 |
aws_profile=None, |
| 105 |
) |
| 106 |
is True |
| 107 |
) |
| 108 |
|
| 109 |
def test_explicit_creds_suppress_env_api_key(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 110 |
monkeypatch.setenv("ANTHROPIC_AWS_API_KEY", "env-key") |
| 111 |
assert ( |
| 112 |
resolve_auth_mode( |
| 113 |
api_key=None, |
| 114 |
aws_access_key="AKID", |
| 115 |
aws_secret_key="secret", |
| 116 |
aws_profile=None, |
| 117 |
) |
| 118 |
is True |
| 119 |
) |
| 120 |
|
| 121 |
def test_profile_suppresses_env_api_key(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 122 |
monkeypatch.setenv("ANTHROPIC_AWS_API_KEY", "env-key") |
| 123 |
assert ( |
| 124 |
resolve_auth_mode( |
| 125 |
api_key=None, |
| 126 |
aws_access_key=None, |
| 127 |
aws_secret_key=None, |
| 128 |
aws_profile="my-profile", |
| 129 |
) |
| 130 |
is True |
| 131 |
) |
| 132 |
|
| 133 |
def test_api_key_arg_beats_explicit_creds(self) -> None: |
| 134 |
"""api_key constructor arg takes highest precedence.""" |
| 135 |
assert ( |
| 136 |
resolve_auth_mode( |
| 137 |
api_key="key", |
| 138 |
aws_access_key="AKID", |
| 139 |
aws_secret_key="secret", |
| 140 |
aws_profile=None, |
| 141 |
) |
| 142 |
is False |
| 143 |
) |
| 144 |
|
| 145 |
def test_custom_env_var(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 146 |
monkeypatch.setenv("AWS_BEARER_TOKEN_BEDROCK", "mantle-key") |
| 147 |
monkeypatch.delenv("ANTHROPIC_AWS_API_KEY", raising=False) |
| 148 |
assert ( |
| 149 |
resolve_auth_mode( |
| 150 |
api_key=None, |
| 151 |
aws_access_key=None, |
| 152 |
aws_secret_key=None, |
| 153 |
aws_profile=None, |
| 154 |
api_key_env_vars=("AWS_BEARER_TOKEN_BEDROCK",), |
| 155 |
) |
| 156 |
is False |
| 157 |
) |
| 158 |
|
| 159 |
def test_custom_env_var_not_set_defaults_to_sigv4(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 160 |
monkeypatch.delenv("AWS_BEARER_TOKEN_BEDROCK", raising=False) |
| 161 |
monkeypatch.delenv("ANTHROPIC_AWS_API_KEY", raising=False) |
| 162 |
assert ( |
| 163 |
resolve_auth_mode( |
| 164 |
api_key=None, |
| 165 |
aws_access_key=None, |
| 166 |
aws_secret_key=None, |
| 167 |
aws_profile=None, |
| 168 |
api_key_env_vars=("AWS_BEARER_TOKEN_BEDROCK",), |
| 169 |
) |
| 170 |
is True |
| 171 |
) |
| 172 |
|
| 173 |
def test_env_var_fallback_chain_first_wins(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 174 |
"""First env var in the chain takes priority.""" |
| 175 |
monkeypatch.setenv("AWS_BEARER_TOKEN_BEDROCK", "mantle-key") |
| 176 |
monkeypatch.setenv("ANTHROPIC_AWS_API_KEY", "aws-key") |
| 177 |
assert ( |
| 178 |
resolve_auth_mode( |
| 179 |
api_key=None, |
| 180 |
aws_access_key=None, |
| 181 |
aws_secret_key=None, |
| 182 |
aws_profile=None, |
| 183 |
api_key_env_vars=("AWS_BEARER_TOKEN_BEDROCK", "ANTHROPIC_AWS_API_KEY"), |
| 184 |
) |
| 185 |
is False |
| 186 |
) |
| 187 |
|
| 188 |
def test_env_var_fallback_chain_falls_through(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 189 |
"""Falls back to second env var when first is not set.""" |
| 190 |
monkeypatch.delenv("AWS_BEARER_TOKEN_BEDROCK", raising=False) |
| 191 |
monkeypatch.setenv("ANTHROPIC_AWS_API_KEY", "aws-key") |
| 192 |
assert ( |
| 193 |
resolve_auth_mode( |
| 194 |
api_key=None, |
| 195 |
aws_access_key=None, |
| 196 |
aws_secret_key=None, |
| 197 |
aws_profile=None, |
| 198 |
api_key_env_vars=("AWS_BEARER_TOKEN_BEDROCK", "ANTHROPIC_AWS_API_KEY"), |
| 199 |
) |
| 200 |
is False |
| 201 |
) |
| 202 |
|
| 203 |
def test_env_var_fallback_chain_none_set(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 204 |
"""Defaults to SigV4 when no env vars in the chain are set.""" |
| 205 |
monkeypatch.delenv("AWS_BEARER_TOKEN_BEDROCK", raising=False) |
| 206 |
monkeypatch.delenv("ANTHROPIC_AWS_API_KEY", raising=False) |
| 207 |
assert ( |
| 208 |
resolve_auth_mode( |
| 209 |
api_key=None, |
| 210 |
aws_access_key=None, |
| 211 |
aws_secret_key=None, |
| 212 |
aws_profile=None, |
| 213 |
api_key_env_vars=("AWS_BEARER_TOKEN_BEDROCK", "ANTHROPIC_AWS_API_KEY"), |
| 214 |
) |
| 215 |
is True |
| 216 |
) |
| 217 |
|
| 218 |
|
| 219 |
# --- resolve_api_key --- |
| 220 |
|
| 221 |
|
| 222 |
class TestResolveApiKey: |
| 223 |
def test_returns_arg_when_provided(self) -> None: |
| 224 |
assert resolve_api_key(api_key="arg-key", use_sigv4=False) == "arg-key" |
| 225 |
|
| 226 |
def test_returns_none_for_sigv4(self) -> None: |
| 227 |
assert resolve_api_key(api_key=None, use_sigv4=True) is None |
| 228 |
|
| 229 |
def test_returns_env_when_not_sigv4(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 230 |
monkeypatch.setenv("ANTHROPIC_AWS_API_KEY", "env-key") |
| 231 |
assert resolve_api_key(api_key=None, use_sigv4=False) == "env-key" |
| 232 |
|
| 233 |
def test_returns_none_when_not_sigv4_and_no_env(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 234 |
monkeypatch.delenv("ANTHROPIC_AWS_API_KEY", raising=False) |
| 235 |
assert resolve_api_key(api_key=None, use_sigv4=False) is None |
| 236 |
|
| 237 |
def test_arg_takes_precedence_over_env(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 238 |
monkeypatch.setenv("ANTHROPIC_AWS_API_KEY", "env-key") |
| 239 |
assert resolve_api_key(api_key="arg-key", use_sigv4=False) == "arg-key" |
| 240 |
|
| 241 |
def test_sigv4_ignores_env(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 242 |
monkeypatch.setenv("ANTHROPIC_AWS_API_KEY", "env-key") |
| 243 |
assert resolve_api_key(api_key=None, use_sigv4=True) is None |
| 244 |
|
| 245 |
def test_custom_env_var(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 246 |
monkeypatch.setenv("AWS_BEARER_TOKEN_BEDROCK", "mantle-key") |
| 247 |
assert ( |
| 248 |
resolve_api_key( |
| 249 |
api_key=None, |
| 250 |
use_sigv4=False, |
| 251 |
api_key_env_vars=("AWS_BEARER_TOKEN_BEDROCK",), |
| 252 |
) |
| 253 |
== "mantle-key" |
| 254 |
) |
| 255 |
|
| 256 |
def test_env_var_fallback_chain_first_wins(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 257 |
monkeypatch.setenv("AWS_BEARER_TOKEN_BEDROCK", "mantle-key") |
| 258 |
monkeypatch.setenv("ANTHROPIC_AWS_API_KEY", "aws-key") |
| 259 |
assert ( |
| 260 |
resolve_api_key( |
| 261 |
api_key=None, |
| 262 |
use_sigv4=False, |
| 263 |
api_key_env_vars=("AWS_BEARER_TOKEN_BEDROCK", "ANTHROPIC_AWS_API_KEY"), |
| 264 |
) |
| 265 |
== "mantle-key" |
| 266 |
) |
| 267 |
|
| 268 |
def test_env_var_fallback_chain_falls_through(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 269 |
monkeypatch.delenv("AWS_BEARER_TOKEN_BEDROCK", raising=False) |
| 270 |
monkeypatch.setenv("ANTHROPIC_AWS_API_KEY", "aws-key") |
| 271 |
assert ( |
| 272 |
resolve_api_key( |
| 273 |
api_key=None, |
| 274 |
use_sigv4=False, |
| 275 |
api_key_env_vars=("AWS_BEARER_TOKEN_BEDROCK", "ANTHROPIC_AWS_API_KEY"), |
| 276 |
) |
| 277 |
== "aws-key" |
| 278 |
) |
| 279 |
|
| 280 |
|
| 281 |
# --- resolve_region --- |
| 282 |
|
| 283 |
|
| 284 |
class TestResolveRegion: |
| 285 |
def test_returns_arg_when_provided(self) -> None: |
| 286 |
assert resolve_region("us-west-2") == "us-west-2" |
| 287 |
|
| 288 |
def test_returns_aws_region_env(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 289 |
monkeypatch.setenv("AWS_REGION", "eu-west-1") |
| 290 |
assert resolve_region(None) == "eu-west-1" |
| 291 |
|
| 292 |
def test_returns_aws_default_region_env(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 293 |
monkeypatch.delenv("AWS_REGION", raising=False) |
| 294 |
monkeypatch.setenv("AWS_DEFAULT_REGION", "ap-southeast-1") |
| 295 |
assert resolve_region(None) == "ap-southeast-1" |
| 296 |
|
| 297 |
def test_aws_region_takes_precedence_over_default(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 298 |
monkeypatch.setenv("AWS_REGION", "us-east-1") |
| 299 |
monkeypatch.setenv("AWS_DEFAULT_REGION", "us-west-2") |
| 300 |
assert resolve_region(None) == "us-east-1" |
| 301 |
|
| 302 |
def test_arg_takes_precedence_over_env(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 303 |
monkeypatch.setenv("AWS_REGION", "us-east-1") |
| 304 |
assert resolve_region("eu-central-1") == "eu-central-1" |
| 305 |
|
| 306 |
def test_returns_none_when_no_source(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 307 |
monkeypatch.delenv("AWS_REGION", raising=False) |
| 308 |
monkeypatch.delenv("AWS_DEFAULT_REGION", raising=False) |
| 309 |
assert resolve_region(None) is None |
| 310 |
|
| 311 |
|
| 312 |
# --- resolve_workspace_id --- |
| 313 |
|
| 314 |
|
| 315 |
class TestResolveWorkspaceId: |
| 316 |
def test_returns_arg_when_provided(self) -> None: |
| 317 |
assert resolve_workspace_id("ws-123") == "ws-123" |
| 318 |
|
| 319 |
def test_returns_env_when_no_arg(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 320 |
monkeypatch.setenv("ANTHROPIC_AWS_WORKSPACE_ID", "env-ws") |
| 321 |
assert resolve_workspace_id(None) == "env-ws" |
| 322 |
|
| 323 |
def test_returns_none_when_no_source(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 324 |
monkeypatch.delenv("ANTHROPIC_AWS_WORKSPACE_ID", raising=False) |
| 325 |
assert resolve_workspace_id(None) is None |
| 326 |
|
| 327 |
def test_arg_takes_precedence_over_env(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 328 |
monkeypatch.setenv("ANTHROPIC_AWS_WORKSPACE_ID", "env-ws") |
| 329 |
assert resolve_workspace_id("arg-ws") == "arg-ws" |
| 330 |
|
| 331 |
def test_custom_env_var(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 332 |
monkeypatch.setenv("ANTHROPIC_BEDROCK_MANTLE_WORKSPACE_ID", "mantle-ws") |
| 333 |
assert ( |
| 334 |
resolve_workspace_id( |
| 335 |
None, |
| 336 |
workspace_id_env_vars=("ANTHROPIC_BEDROCK_MANTLE_WORKSPACE_ID",), |
| 337 |
) |
| 338 |
== "mantle-ws" |
| 339 |
) |
| 340 |
|
| 341 |
def test_env_var_fallback_chain_first_wins(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 342 |
monkeypatch.setenv("ANTHROPIC_BEDROCK_MANTLE_WORKSPACE_ID", "mantle-ws") |
| 343 |
monkeypatch.setenv("ANTHROPIC_AWS_WORKSPACE_ID", "aws-ws") |
| 344 |
assert ( |
| 345 |
resolve_workspace_id( |
| 346 |
None, |
| 347 |
workspace_id_env_vars=("ANTHROPIC_BEDROCK_MANTLE_WORKSPACE_ID", "ANTHROPIC_AWS_WORKSPACE_ID"), |
| 348 |
) |
| 349 |
== "mantle-ws" |
| 350 |
) |
| 351 |
|
| 352 |
def test_env_var_fallback_chain_falls_through(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 353 |
monkeypatch.delenv("ANTHROPIC_BEDROCK_MANTLE_WORKSPACE_ID", raising=False) |
| 354 |
monkeypatch.setenv("ANTHROPIC_AWS_WORKSPACE_ID", "aws-ws") |
| 355 |
assert ( |
| 356 |
resolve_workspace_id( |
| 357 |
None, |
| 358 |
workspace_id_env_vars=("ANTHROPIC_BEDROCK_MANTLE_WORKSPACE_ID", "ANTHROPIC_AWS_WORKSPACE_ID"), |
| 359 |
) |
| 360 |
== "aws-ws" |
| 361 |
) |
| 362 |
|
| 363 |
def test_env_var_fallback_chain_none_set(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 364 |
monkeypatch.delenv("ANTHROPIC_BEDROCK_MANTLE_WORKSPACE_ID", raising=False) |
| 365 |
monkeypatch.delenv("ANTHROPIC_AWS_WORKSPACE_ID", raising=False) |
| 366 |
assert ( |
| 367 |
resolve_workspace_id( |
| 368 |
None, |
| 369 |
workspace_id_env_vars=("ANTHROPIC_BEDROCK_MANTLE_WORKSPACE_ID", "ANTHROPIC_AWS_WORKSPACE_ID"), |
| 370 |
) |
| 371 |
is None |
| 372 |
) |
| 373 |
|
| 374 |
|
| 375 |
# --- get_auth_headers --- |
| 376 |
|
| 377 |
|
| 378 |
class TestResolveBaseUrl: |
| 379 |
def test_returns_arg_when_provided(self) -> None: |
| 380 |
assert resolve_base_url("https://custom.example.com", region="us-east-1") == "https://custom.example.com" |
| 381 |
|
| 382 |
def test_returns_env_when_no_arg(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 383 |
monkeypatch.setenv("ANTHROPIC_AWS_BASE_URL", "https://env-gateway.example.com") |
| 384 |
assert resolve_base_url(None, region="us-east-1") == "https://env-gateway.example.com" |
| 385 |
|
| 386 |
def test_derives_from_region(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 387 |
monkeypatch.delenv("ANTHROPIC_AWS_BASE_URL", raising=False) |
| 388 |
assert resolve_base_url(None, region="us-west-2") == "https://aws-external-anthropic.us-west-2.api.aws" |
| 389 |
|
| 390 |
def test_returns_none_when_no_source(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 391 |
monkeypatch.delenv("ANTHROPIC_AWS_BASE_URL", raising=False) |
| 392 |
assert resolve_base_url(None, region=None) is None |
| 393 |
|
| 394 |
def test_arg_takes_precedence_over_env(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 395 |
monkeypatch.setenv("ANTHROPIC_AWS_BASE_URL", "https://env-gateway.example.com") |
| 396 |
assert resolve_base_url("https://arg.example.com", region="us-east-1") == "https://arg.example.com" |
| 397 |
|
| 398 |
def test_env_takes_precedence_over_region(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 399 |
monkeypatch.setenv("ANTHROPIC_AWS_BASE_URL", "https://env-gateway.example.com") |
| 400 |
assert resolve_base_url(None, region="us-east-1") == "https://env-gateway.example.com" |
| 401 |
|
| 402 |
def test_custom_url_template(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 403 |
monkeypatch.delenv("ANTHROPIC_AWS_BASE_URL", raising=False) |
| 404 |
assert ( |
| 405 |
resolve_base_url( |
| 406 |
None, |
| 407 |
region="us-east-1", |
| 408 |
url_template="https://bedrock-mantle.{region}.api.aws/anthropic", |
| 409 |
) |
| 410 |
== "https://bedrock-mantle.us-east-1.api.aws/anthropic" |
| 411 |
) |
| 412 |
|
| 413 |
def test_custom_env_var(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 414 |
monkeypatch.setenv("ANTHROPIC_BEDROCK_MANTLE_BASE_URL", "https://mantle.example.com") |
| 415 |
assert ( |
| 416 |
resolve_base_url( |
| 417 |
None, |
| 418 |
region="us-east-1", |
| 419 |
base_url_env_vars=("ANTHROPIC_BEDROCK_MANTLE_BASE_URL",), |
| 420 |
) |
| 421 |
== "https://mantle.example.com" |
| 422 |
) |
| 423 |
|
| 424 |
def test_env_var_fallback_chain(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 425 |
monkeypatch.delenv("ANTHROPIC_BEDROCK_MANTLE_BASE_URL", raising=False) |
| 426 |
monkeypatch.setenv("ANTHROPIC_AWS_BASE_URL", "https://aws-fallback.example.com") |
| 427 |
assert ( |
| 428 |
resolve_base_url( |
| 429 |
None, |
| 430 |
region="us-east-1", |
| 431 |
base_url_env_vars=("ANTHROPIC_BEDROCK_MANTLE_BASE_URL", "ANTHROPIC_AWS_BASE_URL"), |
| 432 |
) |
| 433 |
== "https://aws-fallback.example.com" |
| 434 |
) |
| 435 |
|
| 436 |
|
| 437 |
class TestGetAuthHeaders: |
| 438 |
def test_uses_service_name_parameter(self) -> None: |
| 439 |
"""service_name is passed through to SigV4Auth, not hardcoded.""" |
| 440 |
headers = get_auth_headers( |
| 441 |
method="POST", |
| 442 |
url="https://gateway.us-east-1.api.aws/v1/messages", |
| 443 |
headers=httpx2.Headers({"content-type": "application/json"}), |
| 444 |
aws_access_key="AKIAIOSFODNN7EXAMPLE", |
| 445 |
aws_secret_key="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", |
| 446 |
aws_session_token=None, |
| 447 |
region="us-east-1", |
| 448 |
profile=None, |
| 449 |
data='{"hello": "world"}', |
| 450 |
service_name="aws-external-anthropic", |
| 451 |
) |
| 452 |
assert "Authorization" in headers |
| 453 |
assert "aws-external-anthropic" in headers["Authorization"] |
| 454 |
|
| 455 |
def test_different_service_name(self) -> None: |
| 456 |
"""Mantle uses a different service name for SigV4 signing.""" |
| 457 |
headers = get_auth_headers( |
| 458 |
method="POST", |
| 459 |
url="https://bedrock-mantle.us-east-1.api.aws/anthropic/v1/messages", |
| 460 |
headers=httpx2.Headers({"content-type": "application/json"}), |
| 461 |
aws_access_key="AKIAIOSFODNN7EXAMPLE", |
| 462 |
aws_secret_key="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", |
| 463 |
aws_session_token=None, |
| 464 |
region="us-east-1", |
| 465 |
profile=None, |
| 466 |
data='{"hello": "world"}', |
| 467 |
service_name="bedrock-mantle", |
| 468 |
) |
| 469 |
assert "Authorization" in headers |
| 470 |
assert "bedrock-mantle" in headers["Authorization"] |
| 471 |
assert "aws-external-anthropic" not in headers["Authorization"] |
| 472 |
|
| 473 |
def test_returns_authorization_and_date_headers(self) -> None: |
| 474 |
headers = get_auth_headers( |
| 475 |
method="POST", |
| 476 |
url="https://gateway.us-east-1.api.aws/v1/messages", |
| 477 |
headers=httpx2.Headers({"content-type": "application/json"}), |
| 478 |
aws_access_key="AKIAIOSFODNN7EXAMPLE", |
| 479 |
aws_secret_key="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", |
| 480 |
aws_session_token=None, |
| 481 |
region="us-east-1", |
| 482 |
profile=None, |
| 483 |
data='{"hello": "world"}', |
| 484 |
service_name="aws-external-anthropic", |
| 485 |
) |
| 486 |
assert "Authorization" in headers |
| 487 |
assert headers["Authorization"].startswith("AWS4-HMAC-SHA256") |
| 488 |
assert "X-Amz-Date" in headers |
| 489 |
|
| 490 |
def test_includes_security_token_header(self) -> None: |
| 491 |
headers = get_auth_headers( |
| 492 |
method="POST", |
| 493 |
url="https://gateway.us-east-1.api.aws/v1/messages", |
| 494 |
headers=httpx2.Headers({"content-type": "application/json"}), |
| 495 |
aws_access_key="AKIAIOSFODNN7EXAMPLE", |
| 496 |
aws_secret_key="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", |
| 497 |
aws_session_token="FwoGZXIvYXdzEBYaDH7example", |
| 498 |
region="us-east-1", |
| 499 |
profile=None, |
| 500 |
data='{"hello": "world"}', |
| 501 |
service_name="aws-external-anthropic", |
| 502 |
) |
| 503 |
assert headers.get("X-Amz-Security-Token") == "FwoGZXIvYXdzEBYaDH7example" |
| 504 |
|
| 505 |
def test_strips_connection_header(self) -> None: |
| 506 |
"""Connection header must not be signed (may be stripped by proxies).""" |
| 507 |
headers = get_auth_headers( |
| 508 |
method="POST", |
| 509 |
url="https://gateway.us-east-1.api.aws/v1/messages", |
| 510 |
headers=httpx2.Headers({"content-type": "application/json", "connection": "keep-alive"}), |
| 511 |
aws_access_key="AKIAIOSFODNN7EXAMPLE", |
| 512 |
aws_secret_key="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", |
| 513 |
aws_session_token=None, |
| 514 |
region="us-east-1", |
| 515 |
profile=None, |
| 516 |
data='{"hello": "world"}', |
| 517 |
service_name="aws-external-anthropic", |
| 518 |
) |
| 519 |
# The signed headers in Authorization should not include "connection" |
| 520 |
auth = headers["Authorization"] |
| 521 |
signed_headers_match = re.search(r"SignedHeaders=([^,]+)", auth) |
| 522 |
assert signed_headers_match is not None |
| 523 |
signed_headers = signed_headers_match.group(1).split(";") |
| 524 |
assert "connection" not in signed_headers |
| 525 |
|
| 526 |
def test_handles_null_body(self) -> None: |
| 527 |
headers = get_auth_headers( |
| 528 |
method="GET", |
| 529 |
url="https://gateway.us-east-1.api.aws/v1/models", |
| 530 |
headers=httpx2.Headers({}), |
| 531 |
aws_access_key="AKIAIOSFODNN7EXAMPLE", |
| 532 |
aws_secret_key="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", |
| 533 |
aws_session_token=None, |
| 534 |
region="us-east-1", |
| 535 |
profile=None, |
| 536 |
data=None, |
| 537 |
service_name="aws-external-anthropic", |
| 538 |
) |
| 539 |
assert "Authorization" in headers |
| 540 |
|
| 541 |
def test_includes_query_params_in_signing(self) -> None: |
| 542 |
"""URL query params must be part of the signed request.""" |
| 543 |
headers_with_query = get_auth_headers( |
| 544 |
method="GET", |
| 545 |
url="https://gateway.us-east-1.api.aws/v1/models?limit=10", |
| 546 |
headers=httpx2.Headers({}), |
| 547 |
aws_access_key="AKIAIOSFODNN7EXAMPLE", |
| 548 |
aws_secret_key="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", |
| 549 |
aws_session_token=None, |
| 550 |
region="us-east-1", |
| 551 |
profile=None, |
| 552 |
data=None, |
| 553 |
service_name="aws-external-anthropic", |
| 554 |
) |
| 555 |
headers_without_query = get_auth_headers( |
| 556 |
method="GET", |
| 557 |
url="https://gateway.us-east-1.api.aws/v1/models", |
| 558 |
headers=httpx2.Headers({}), |
| 559 |
aws_access_key="AKIAIOSFODNN7EXAMPLE", |
| 560 |
aws_secret_key="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", |
| 561 |
aws_session_token=None, |
| 562 |
region="us-east-1", |
| 563 |
profile=None, |
| 564 |
data=None, |
| 565 |
service_name="aws-external-anthropic", |
| 566 |
) |
| 567 |
# Different URLs should produce different signatures |
| 568 |
assert headers_with_query["Authorization"] != headers_without_query["Authorization"] |
| 569 |
|
| 570 |
def test_uppercases_method(self) -> None: |
| 571 |
"""Method should be uppercased for signing.""" |
| 572 |
headers = get_auth_headers( |
| 573 |
method="post", |
| 574 |
url="https://gateway.us-east-1.api.aws/v1/messages", |
| 575 |
headers=httpx2.Headers({}), |
| 576 |
aws_access_key="AKIAIOSFODNN7EXAMPLE", |
| 577 |
aws_secret_key="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", |
| 578 |
aws_session_token=None, |
| 579 |
region="us-east-1", |
| 580 |
profile=None, |
| 581 |
data="{}", |
| 582 |
service_name="aws-external-anthropic", |
| 583 |
) |
| 584 |
assert "Authorization" in headers |
| 585 |
|
| 586 |
def test_raises_on_missing_credentials(self, monkeypatch: pytest.MonkeyPatch) -> None: |
| 587 |
class NoCredentialsSession: |
| 588 |
region_name = "us-east-1" |
| 589 |
|
| 590 |
def get_credentials(self) -> None: |
| 591 |
return None |
| 592 |
|
| 593 |
def fake_get_session(**_kwargs: object) -> NoCredentialsSession: |
| 594 |
return NoCredentialsSession() |
| 595 |
|
| 596 |
monkeypatch.setattr("anthropic.lib.aws._auth._get_session", fake_get_session) |
| 597 |
|
| 598 |
with pytest.raises(RuntimeError, match="Could not resolve AWS credentials"): |
| 599 |
get_auth_headers( |
| 600 |
method="POST", |
| 601 |
url="https://gateway.us-east-1.api.aws/v1/messages", |
| 602 |
headers=httpx2.Headers({}), |
| 603 |
aws_access_key=None, |
| 604 |
aws_secret_key=None, |
| 605 |
aws_session_token=None, |
| 606 |
region="us-east-1", |
| 607 |
profile=None, |
| 608 |
data="{}", |
| 609 |
service_name="aws-external-anthropic", |
| 610 |
) |