Skip to content

Commit

Permalink
add AWS_PROFILE env var to credentials chain
Browse files Browse the repository at this point in the history
this behavior is more in line with the behavior expected by users coming
from boto3 or aiobotocore.

[Issue HENNGE#139]
  • Loading branch information
BTripp1986 committed Dec 26, 2022
1 parent b7530b7 commit bcc2234
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/aiodynamo/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from dataclasses import dataclass, field
from enum import Enum, auto
from pathlib import Path
from typing import Callable, Optional, Sequence, TypeVar
from typing import Callable, Optional, Sequence, TypeVar, cast

from yarl import URL

Expand Down Expand Up @@ -158,8 +158,10 @@ def __init__(
self,
*,
path: Optional[Path] = None,
profile_name: str = "default",
profile_name: Optional[str] = None,
) -> None:
if profile_name is None:
profile_name = cast(str, os.environ.get("AWS_PROFILE", "default"))
if path is None:
path = Path.home().joinpath(".aws", "credentials")
self.key = None
Expand Down
9 changes: 8 additions & 1 deletion tests/unit/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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(
Expand Down

0 comments on commit bcc2234

Please sign in to comment.