From e2202341ada912da58688fe47bb5d5a64d813bf5 Mon Sep 17 00:00:00 2001 From: Benjamin Tripp Date: Sun, 18 Dec 2022 10:45:26 -0500 Subject: [PATCH] add AWS_PROFILE env var to credentials chain this behavior is more in line with the behavior expected by users coming from boto3 or aiobotocore. [Issue #139] --- src/aiodynamo/credentials.py | 5 +++-- tests/unit/test_credentials.py | 9 ++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/aiodynamo/credentials.py b/src/aiodynamo/credentials.py index bc02064..df0b035 100644 --- a/src/aiodynamo/credentials.py +++ b/src/aiodynamo/credentials.py @@ -158,8 +158,9 @@ def __init__( self, *, path: Optional[Path] = None, - profile_name: str = "default", + profile_name: Optional[str] = None, ) -> None: + profile_name = profile_name or os.environ.get("AWS_PROFILE", "default") if path is None: path = Path.home().joinpath(".aws", "credentials") self.key = None @@ -174,7 +175,7 @@ def __init__( return try: - profile = parser[profile_name] + profile = parser[str(profile_name)] except KeyError: logger.exception( "Profile %r not found in credentials file %r", profile_name, path diff --git a/tests/unit/test_credentials.py b/tests/unit/test_credentials.py index 9c0ae57..f0d6c56 100644 --- a/tests/unit/test_credentials.py +++ b/tests/unit/test_credentials.py @@ -180,7 +180,9 @@ async def test_disabled(monkeypatch: MonkeyPatch) -> None: assert not creds.is_disabled() -async def test_file_credentials(fs: FakeFilesystem, http: HttpImplementation) -> None: +async def test_file_credentials( + fs: FakeFilesystem, http: HttpImplementation, monkeypatch: MonkeyPatch +) -> None: assert FileCredentials().is_disabled() fs.create_file( Path.home().joinpath(".aws", "credentials"), @@ -201,6 +203,11 @@ async def test_file_credentials(fs: FakeFilesystem, http: HttpImplementation) -> credentials = FileCredentials(profile_name="my-profile") assert not credentials.is_disabled() assert await credentials.get_key(http) == Key(id="baz", secret="hoge") + monkeypatch.setenv("AWS_PROFILE", "my-profile") + credentials = FileCredentials() + assert not credentials.is_disabled() + assert await credentials.get_key(http) == Key(id="baz", secret="hoge") + monkeypatch.delenv("AWS_PROFILE", raising=False) custom_path = Path("/custom/credentials/file") assert FileCredentials(path=custom_path).is_disabled() fs.create_file(