generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 24
Sigv4 AuthScheme + Static/Environment Credentials Providers #406
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
Merged
alextwoods
merged 10 commits into
smithy-lang:develop
from
alextwoods:codegen_sigv4_auth
Mar 11, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c5c137b
Add basic implementation of Sigv4 AuthScheme + wire up codegen
alextwoods a2dac51
Add Static and Environment credentials providers
alextwoods 29bf5af
Add env creds tests
alextwoods 04c8e4a
Merge branch 'develop' into codegen_sigv4_auth
alextwoods bf7ac20
Merge branch 'develop' into codegen_sigv4_auth
alextwoods 09d513c
Swap to AsyncSigV4Signer and create type for aws credentials identity…
alextwoods 351c753
Rename resolver files
alextwoods 45cce24
Fix linter
alextwoods 5c043c2
Merge branch 'develop' into codegen_sigv4_auth
alextwoods 96ae7f2
cleanups from PR
alextwoods File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
packages/smithy-aws-core/src/smithy_aws_core/auth/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from .sigv4 import SigV4AuthScheme | ||
|
|
||
| __all__ = ("SigV4AuthScheme",) |
53 changes: 53 additions & 0 deletions
53
packages/smithy-aws-core/src/smithy_aws_core/auth/sigv4.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| from dataclasses import dataclass | ||
| from typing import Protocol | ||
|
|
||
| from smithy_aws_core.identity import AWSCredentialsIdentity | ||
| from smithy_core.aio.interfaces.identity import IdentityResolver | ||
| from smithy_core.exceptions import SmithyIdentityException | ||
| from smithy_core.interfaces.identity import IdentityProperties | ||
| from smithy_http.aio.interfaces.auth import HTTPAuthScheme, HTTPSigner | ||
| from aws_sdk_signers import SigV4SigningProperties, AsyncSigV4Signer | ||
|
|
||
|
|
||
| class SigV4Config(Protocol): | ||
| aws_credentials_identity_resolver: ( | ||
| IdentityResolver[AWSCredentialsIdentity, IdentityProperties] | None | ||
| ) | ||
|
|
||
|
|
||
| @dataclass(init=False) | ||
| class SigV4AuthScheme( | ||
| HTTPAuthScheme[ | ||
| AWSCredentialsIdentity, SigV4Config, IdentityProperties, SigV4SigningProperties | ||
| ] | ||
| ): | ||
| """SigV4 AuthScheme.""" | ||
|
|
||
| scheme_id: str = "aws.auth#sigv4" | ||
| signer: HTTPSigner[AWSCredentialsIdentity, SigV4SigningProperties] | ||
|
|
||
| def __init__( | ||
| self, | ||
| *, | ||
| signer: HTTPSigner[AWSCredentialsIdentity, SigV4SigningProperties] | ||
| | None = None, | ||
| ) -> None: | ||
| """Constructor. | ||
|
|
||
| :param identity_resolver: The identity resolver to extract the api key identity. | ||
| :param signer: The signer used to sign the request. | ||
| """ | ||
| # TODO: There are type mismatches in the signature of the "sign" method. | ||
alextwoods marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.signer = signer or AsyncSigV4Signer() # type: ignore | ||
|
|
||
| def identity_resolver( | ||
| self, *, config: SigV4Config | ||
| ) -> IdentityResolver[AWSCredentialsIdentity, IdentityProperties]: | ||
| if not config.aws_credentials_identity_resolver: | ||
| raise SmithyIdentityException( | ||
| "Attempted to use SigV4 auth, but aws_credentials_identity_resolver was not " | ||
| "set on the config." | ||
| ) | ||
| return config.aws_credentials_identity_resolver | ||
6 changes: 6 additions & 0 deletions
6
packages/smithy-aws-core/src/smithy_aws_core/credentials_resolvers/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| from .environment import EnvironmentCredentialsResolver | ||
| from .static import StaticCredentialsResolver | ||
|
|
||
| __all__ = ("EnvironmentCredentialsResolver", "StaticCredentialsResolver") |
42 changes: 42 additions & 0 deletions
42
packages/smithy-aws-core/src/smithy_aws_core/credentials_resolvers/environment.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| import os | ||
|
|
||
| from smithy_aws_core.identity import AWSCredentialsIdentity | ||
| from smithy_core.aio.interfaces.identity import IdentityResolver | ||
| from smithy_core.exceptions import SmithyIdentityException | ||
| from smithy_core.interfaces.identity import IdentityProperties | ||
|
|
||
|
|
||
| class EnvironmentCredentialsResolver( | ||
| IdentityResolver[AWSCredentialsIdentity, IdentityProperties] | ||
| ): | ||
| """Resolves AWS Credentials from system environment variables.""" | ||
|
|
||
| def __init__(self): | ||
| self._credentials = None | ||
|
|
||
| async def get_identity( | ||
| self, *, identity_properties: IdentityProperties | ||
| ) -> AWSCredentialsIdentity: | ||
| if self._credentials is not None: | ||
| return self._credentials | ||
|
|
||
| access_key_id = os.getenv("AWS_ACCESS_KEY_ID") | ||
| secret_access_key = os.getenv("AWS_SECRET_ACCESS_KEY") | ||
| session_token = os.getenv("AWS_SESSION_TOKEN") | ||
| account_id = os.getenv("AWS_ACCOUNT_ID") | ||
|
|
||
| if access_key_id is None or secret_access_key is None: | ||
| raise SmithyIdentityException( | ||
| "AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are required" | ||
| ) | ||
|
|
||
| self._credentials = AWSCredentialsIdentity( | ||
| access_key_id=access_key_id, | ||
| secret_access_key=secret_access_key, | ||
| session_token=session_token, | ||
| account_id=account_id, | ||
| ) | ||
|
|
||
| return self._credentials |
19 changes: 19 additions & 0 deletions
19
packages/smithy-aws-core/src/smithy_aws_core/credentials_resolvers/static.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| from smithy_aws_core.identity import AWSCredentialsIdentity | ||
| from smithy_core.aio.interfaces.identity import IdentityResolver | ||
| from smithy_core.interfaces.identity import IdentityProperties | ||
|
|
||
|
|
||
| class StaticCredentialsResolver( | ||
| IdentityResolver[AWSCredentialsIdentity, IdentityProperties] | ||
JordonPhillips marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ): | ||
| """Resolve Static AWS Credentials.""" | ||
|
|
||
| def __init__(self, *, credentials: AWSCredentialsIdentity) -> None: | ||
| self._credentials = credentials | ||
|
|
||
| async def get_identity( | ||
| self, *, identity_properties: IdentityProperties | ||
| ) -> AWSCredentialsIdentity: | ||
| return self._credentials | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tragic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the alternative would be to add signingService as an auth param. We would then need a way to wire that up during the request. It could be an undocumented config property or have it somewhere else.