Skip to content

Commit a2dac51

Browse files
committed
Add Static and Environment credentials providers
1 parent c5c137b commit a2dac51

File tree

8 files changed

+82
-10
lines changed

8 files changed

+82
-10
lines changed

codegen/aws/core/src/main/java/software/amazon/smithy/python/aws/codegen/AwsAuthIntegration.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ public List<RuntimeClientPlugin> getClientPlugins(GenerationContext context) {
4545
.name("aws_credentials_identity_resolver")
4646
.documentation("Resolves AWS Credentials. Required for operations that use Sigv4 Auth.")
4747
.type(Symbol.builder()
48-
.name("IdentityResolver[AWSCredentialIdentity, IdentityProperties]")
48+
.name("IdentityResolver[AWSCredentialsIdentity, IdentityProperties]")
4949
.addReference(Symbol.builder()
5050
.addDependency(SmithyPythonDependency.SMITHY_CORE)
5151
.name("IdentityResolver")
5252
.namespace("smithy_core.aio.interfaces.identity", ".")
5353
.build())
5454
.addReference(Symbol.builder()
5555
.addDependency(AwsPythonDependency.SMITHY_AWS_CORE)
56-
.name("AWSCredentialIdentity")
56+
.name("AWSCredentialsIdentity")
5757
.namespace("smithy_aws_core.identity", ".")
5858
.build())
5959
.addReference(Symbol.builder()
@@ -154,7 +154,7 @@ public Symbol getAuthOptionGenerator(GenerationContext context) {
154154
public Symbol getAuthSchemeSymbol(GenerationContext context) {
155155
return Symbol.builder()
156156
.name("SigV4AuthScheme")
157-
.namespace("smithy_aws_core.auth.sigv4", ".")
157+
.namespace("smithy_aws_core.auth", ".")
158158
.addDependency(AwsPythonDependency.SMITHY_AWS_CORE)
159159
.build();
160160
}

codegen/core/src/main/java/software/amazon/smithy/python/codegen/ClientGenerator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ async def _handle_attempt(
416416
for option in auth_options:
417417
if option.scheme_id in config.http_auth_schemes:
418418
auth_option = option
419+
break
419420
420421
signer: HTTPSigner[Any, Any] | None = None
421422
identity: Identity | None = None
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0
3+
4+
from .sigv4 import SigV4AuthScheme
5+
6+
__all__ = ("SigV4AuthScheme",)

packages/smithy-aws-core/src/smithy_aws_core/auth/sigv4.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from dataclasses import dataclass
44
from typing import Protocol
55

6-
from smithy_aws_core.identity import AWSCredentialIdentity
6+
from smithy_aws_core.identity import AWSCredentialsIdentity
77
from smithy_core.aio.interfaces.identity import IdentityResolver
88
from smithy_core.exceptions import SmithyIdentityException
99
from smithy_core.interfaces.identity import IdentityProperties
@@ -13,25 +13,26 @@
1313

1414
class SigV4Config(Protocol):
1515
aws_credentials_identity_resolver: (
16-
IdentityResolver[AWSCredentialIdentity, IdentityProperties] | None
16+
IdentityResolver[AWSCredentialsIdentity, IdentityProperties] | None
1717
)
1818

1919

2020
@dataclass(init=False)
2121
class SigV4AuthScheme(
2222
HTTPAuthScheme[
23-
AWSCredentialIdentity, SigV4Config, IdentityProperties, SigV4SigningProperties
23+
AWSCredentialsIdentity, SigV4Config, IdentityProperties, SigV4SigningProperties
2424
]
2525
):
2626
"""SigV4 AuthScheme."""
2727

2828
scheme_id: str
29-
signer: HTTPSigner[AWSCredentialIdentity, SigV4SigningProperties]
29+
signer: HTTPSigner[AWSCredentialsIdentity, SigV4SigningProperties]
3030

3131
def __init__(
3232
self,
3333
*,
34-
signer: HTTPSigner[AWSCredentialIdentity, SigV4SigningProperties] | None = None,
34+
signer: HTTPSigner[AWSCredentialsIdentity, SigV4SigningProperties]
35+
| None = None,
3536
) -> None:
3637
"""Constructor.
3738
@@ -44,7 +45,7 @@ def __init__(
4445

4546
def identity_resolver(
4647
self, *, config: SigV4Config
47-
) -> IdentityResolver[AWSCredentialIdentity, IdentityProperties]:
48+
) -> IdentityResolver[AWSCredentialsIdentity, IdentityProperties]:
4849
if not config.aws_credentials_identity_resolver:
4950
raise SmithyIdentityException(
5051
"Attempted to use SigV4 auth, but aws_credentials_identity_resolver was not "
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
from .environment_credentials_resolver import EnvironmentCredentialsResolver
4+
from .static_credentials_resolver import StaticCredentialsResolver
5+
6+
__all__ = ("EnvironmentCredentialsResolver", "StaticCredentialsResolver")
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
import os
4+
5+
from smithy_aws_core.identity import AWSCredentialsIdentity
6+
from smithy_core.aio.interfaces.identity import IdentityResolver
7+
from smithy_core.exceptions import SmithyIdentityException
8+
from smithy_core.interfaces.identity import IdentityProperties
9+
10+
11+
class EnvironmentCredentialsResolver(
12+
IdentityResolver[AWSCredentialsIdentity, IdentityProperties]
13+
):
14+
"""Resolves AWS Credentials from system environment variables."""
15+
16+
async def get_identity(
17+
self, *, identity_properties: IdentityProperties
18+
) -> AWSCredentialsIdentity:
19+
access_key_id = os.getenv("AWS_ACCESS_KEY_ID")
20+
secret_access_key = os.getenv("AWS_SECRET_ACCESS_KEY")
21+
session_token = os.getenv("AWS_SESSION_TOKEN")
22+
account_id = os.getenv("AWS_ACCOUNT_ID")
23+
24+
if access_key_id is None or secret_access_key is None:
25+
raise SmithyIdentityException(
26+
"AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are required"
27+
)
28+
29+
return AWSCredentialsIdentity(
30+
access_key_id=access_key_id,
31+
secret_access_key=secret_access_key,
32+
session_token=session_token,
33+
account_id=account_id,
34+
)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
from smithy_aws_core.identity import AWSCredentialsIdentity
4+
from smithy_core.aio.interfaces.identity import IdentityResolver
5+
from smithy_core.interfaces.identity import IdentityProperties
6+
7+
8+
class StaticCredentialsResolver(
9+
IdentityResolver[AWSCredentialsIdentity, IdentityProperties]
10+
):
11+
"""Resolve Static AWS Credentials."""
12+
13+
def __init__(self, *, credentials: AWSCredentialsIdentity) -> None:
14+
self._credentials = credentials
15+
16+
async def get_identity(
17+
self, *, identity_properties: IdentityProperties
18+
) -> AWSCredentialsIdentity:
19+
return self._credentials

packages/smithy-aws-core/src/smithy_aws_core/identity.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from smithy_core.identity import Identity
1616

1717

18-
class AWSCredentialIdentity(Identity):
18+
class AWSCredentialsIdentity(Identity):
1919
"""Container for AWS authentication credentials."""
2020

2121
def __init__(
@@ -25,6 +25,7 @@ def __init__(
2525
secret_access_key: str,
2626
session_token: str | None = None,
2727
expiration: datetime | None = None,
28+
account_id: str | None = None,
2829
) -> None:
2930
"""Initialize the AWSCredentialIdentity.
3031
@@ -35,11 +36,13 @@ def __init__(
3536
the supplied credentials.
3637
:param expiration: The expiration time of the identity. If time zone is
3738
provided, it is updated to UTC. The value must always be in UTC.
39+
:param account_id: The AWS account's ID.
3840
"""
3941
super().__init__(expiration=expiration)
4042
self._access_key_id: str = access_key_id
4143
self._secret_access_key: str = secret_access_key
4244
self._session_token: str | None = session_token
45+
self._account_id: str | None = account_id
4346

4447
@property
4548
def access_key_id(self) -> str:
@@ -52,3 +55,7 @@ def secret_access_key(self) -> str:
5255
@property
5356
def session_token(self) -> str | None:
5457
return self._session_token
58+
59+
@property
60+
def account_id(self) -> str | None:
61+
return self._account_id

0 commit comments

Comments
 (0)