Skip to content
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

AWS Credentials Refresh when Not Assuming Roles #335

Merged
merged 3 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions nodestream/pipeline/extractors/credential_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,11 @@ def get_boto_session_with_refreshable_credentials(self):
session._credentials = refreshable_credentials
return session

def assume_role_if_supplied_and_get_session(self):
def make_client(self, client_name: str):
if self.assume_role_arn:
return self.get_boto_session_with_refreshable_credentials()
return Session()
botocore_session = self.get_boto_session_with_refreshable_credentials()
return boto3.Session(
botocore_session=botocore_session, **self.session_args
).client(client_name)

def make_client(self, client_name: str):
session = self.assume_role_if_supplied_and_get_session()
return boto3.Session(botocore_session=session, **self.session_args).client(
client_name
)
return boto3.client(client_name, **self.session_args)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this base client will refresh credentials using the user's arn by default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. Turns out It will in some cases... but not all: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html

I've updated it.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "nodestream"
version = "0.12.5a8"
version = "0.12.5a9"
description = "A Fast, Declarative ETL for Graph Databases."
license = "GPL-3.0-only"
authors = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime

import pytest
from hamcrest import assert_that, equal_to, has_key, instance_of, not_
from hamcrest import assert_that, equal_to, has_key, not_


@pytest.fixture
Expand Down Expand Up @@ -72,26 +72,21 @@ def test_get_boto_session_with_refreshable_credentials(mocker, client_with_role)
def test_assume_role_if_supplied_and_get_session(mocker, client_with_role):
# create a AwsClientFactory with a role arn and mock get_boto_session_with_refreshable_credentials.
# assert that a the result of get_boto_session_with_refreshable_credentials is what is returned.
client_with_role.get_boto_session_with_refreshable_credentials = mocker.MagicMock(
return_value="test_session"
client_with_role.get_boto_session_with_refreshable_credentials = mocker.MagicMock()
client = client_with_role.make_client("sqs")
client._session = (
client_with_role.get_boto_session_with_refreshable_credentials.return_value
)
session = client_with_role.assume_role_if_supplied_and_get_session()
assert_that(session, equal_to("test_session"))


def test_assume_role_if_supplied_and_get_session_no_role_arn(
mocker, client_without_role
):
from botocore.session import Session

# create a AwsClientFactory without a role arn and mock get_boto_session_with_refreshable_credentials.
# assert that a the result of get_boto_session_with_refreshable_credentials is what is returned.
client_without_role.get_boto_session_with_refreshable_credentials = (
mocker.MagicMock()
mock_boto3_client = mocker.patch(
"nodestream.pipeline.extractors.credential_utils.boto3.client"
)
session = client_without_role.assume_role_if_supplied_and_get_session()
client_without_role.get_boto_session_with_refreshable_credentials.assert_not_called()
assert_that(session, instance_of(Session))
client = client_without_role.make_client("sqs")
assert_that(client, equal_to(mock_boto3_client.return_value))


def test_make_client(mocker, client_without_role):
Expand Down