Repository navigation
fix(bedrock_mantle): add SigV4 fallback to chat completions auth #30714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
8d77f33
41012f2
1b2d022
1d0e2db
949815a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| """ | ||
| Shared auth and region resolution for the Amazon Bedrock Mantle backends. | ||
|
|
||
| Mantle authenticates with a Bearer token when one is available | ||
| (litellm_params.api_key, BEDROCK_MANTLE_API_KEY, or the standard | ||
| AWS_BEARER_TOKEN_BEDROCK); otherwise it falls back to AWS SigV4 (service | ||
| "bedrock") over the standard credential chain (IAM role / access key / profile / | ||
| web identity). The Chat Completions and Responses backends share this behaviour | ||
| through BedrockMantleAuthMixin so the two paths can never drift apart. | ||
| """ | ||
|
|
||
| import re | ||
| from typing import Tuple | ||
|
|
||
| from botocore.exceptions import ( | ||
| CredentialRetrievalError, | ||
| NoCredentialsError, | ||
| PartialCredentialsError, | ||
| ProfileNotFound, | ||
| ) | ||
|
|
||
| from litellm.llms.bedrock.base_aws_llm import BaseAWSLLM | ||
| from litellm.secret_managers.main import get_secret_str | ||
|
|
||
| BEDROCK_MANTLE_DEFAULT_REGION = "us-east-1" | ||
|
|
||
| # Standard Mantle host: https://bedrock-mantle.<region>.api.aws (group 1 = region). | ||
| MANTLE_HOST_RE = re.compile( | ||
| r"^https?://bedrock-mantle\.([^/.]+)\.api\.aws", re.IGNORECASE | ||
| ) | ||
|
|
||
|
|
||
| class BedrockMantleAuthMixin: | ||
| _aws_signer: BaseAWSLLM | ||
|
|
||
| @staticmethod | ||
| def _resolve_bearer_token(api_key: str | None) -> str | None: | ||
| return ( | ||
| api_key | ||
| or get_secret_str("BEDROCK_MANTLE_API_KEY") | ||
| or get_secret_str("AWS_BEARER_TOKEN_BEDROCK") | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def _resolve_region(params: dict) -> str: | ||
| region = params.get("aws_region_name") | ||
| if region: | ||
| BaseAWSLLM._validate_aws_region_name(region) | ||
| return region | ||
| base = params.get("api_base") or get_secret_str("BEDROCK_MANTLE_API_BASE") | ||
| if base: | ||
| match = MANTLE_HOST_RE.match(base.rstrip("/")) | ||
| if match: | ||
| return match.group(1) | ||
| return ( | ||
| get_secret_str("BEDROCK_MANTLE_REGION") | ||
| or get_secret_str("AWS_REGION_NAME") | ||
| or get_secret_str("AWS_REGION") | ||
| or BEDROCK_MANTLE_DEFAULT_REGION | ||
| ) | ||
|
|
||
| def sign_request( | ||
| self, | ||
| headers: dict, | ||
| optional_params: dict, | ||
| request_data: dict, | ||
| api_base: str, | ||
| api_key: str | None = None, | ||
| model: str | None = None, | ||
| stream: bool | None = None, | ||
| fake_stream: bool | None = None, | ||
| ) -> Tuple[dict, bytes | None]: | ||
| bearer = self._resolve_bearer_token(api_key) | ||
| if not bearer: | ||
| # SigV4 path. Pin the credential-scope region to the region of the actual | ||
| # signing URL (api_base is already region-resolved) so the SigV4 scope and | ||
| # the URL host can never disagree. Also drop any caller Authorization so | ||
| # _sign_request's restore-original-Authorization step cannot override the | ||
| # SigV4 header. | ||
| optional_params = { | ||
| **optional_params, | ||
| "aws_region_name": self._resolve_region( | ||
| {**optional_params, "api_base": api_base} | ||
| ), | ||
| } | ||
| headers = {k: v for k, v in headers.items() if k.lower() != "authorization"} | ||
| try: | ||
| return self._aws_signer._sign_request( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Medium: AWS SigV4 headers sent to client-controlled hosts When no bearer token is set, this signs the request for whatever
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The premise here is incorrect: If an admin does turn on On the SDK path there is no untrusted caller; the developer sets Finally, this SigV4-to- Generated by Claude Code |
||
| service_name="bedrock", | ||
| headers=headers, | ||
| optional_params=optional_params, | ||
| request_data=request_data, | ||
| api_base=api_base, | ||
| api_key=bearer, | ||
| model=model, | ||
| stream=stream, | ||
| fake_stream=fake_stream, | ||
| ) | ||
| except ( | ||
| NoCredentialsError, | ||
| PartialCredentialsError, | ||
| ProfileNotFound, | ||
| CredentialRetrievalError, | ||
| ) as e: | ||
| raise ValueError( | ||
| "Bedrock Mantle auth failed: no Bearer token and no usable AWS " | ||
| "credentials. Set BEDROCK_MANTLE_API_KEY (or AWS_BEARER_TOKEN_BEDROCK) " | ||
| "or pass api_key for Bearer auth, or provide AWS credentials " | ||
| "(IAM role / access key / profile / web identity) for SigV4." | ||
| ) from e | ||
Uh oh!
There was an error while loading. Please reload this page.