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 21, 2022
1 parent b7530b7 commit e220234
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/aiodynamo/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
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 e220234

Please sign in to comment.