Skip to content

fix: call DescribeDomain as fallback in get_execution_role #2435

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

Merged
merged 4 commits into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion src/sagemaker/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -3515,7 +3515,11 @@ def get_caller_identity_arn(self):
user_profile_desc = self.sagemaker_client.describe_user_profile(
DomainId=domain_id, UserProfileName=user_profile_name
)
return user_profile_desc["UserSettings"]["ExecutionRole"]
if user_profile_desc.get("UserSettings") is not None:
return user_profile_desc["UserSettings"]["ExecutionRole"]

domain_desc = self.sagemaker_client.describe_domain(DomainId=domain_id)
return domain_desc["DefaultUserSettings"]["ExecutionRole"]
except ClientError:
LOGGER.debug(
"Couldn't call 'describe_notebook_instance' to get the Role "
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,33 @@ def test_get_caller_identity_arn_from_describe_user_profile(boto_session):
)


@patch(
"six.moves.builtins.open",
mock_open(
read_data='{"ResourceName": "SageMakerInstance", '
'"DomainId": "d-kbnw5yk6tg8j", '
'"UserProfileName": "default-1617915559064"}'
),
)
@patch("os.path.exists", side_effect=mock_exists(NOTEBOOK_METADATA_FILE, True))
def test_get_caller_identity_arn_from_describe_domain(boto_session):
sess = Session(boto_session)
expected_role = "arn:aws:iam::369233609183:role/service-role/SageMakerRole-20171129T072388"
sess.sagemaker_client.describe_user_profile.return_value = {}
sess.sagemaker_client.describe_domain.return_value = {
"DefaultUserSettings": {"ExecutionRole": expected_role}
}

actual = sess.get_caller_identity_arn()

assert actual == expected_role
sess.sagemaker_client.describe_user_profile.assert_called_once_with(
DomainId="d-kbnw5yk6tg8j",
UserProfileName="default-1617915559064",
)
sess.sagemaker_client.describe_domain.assert_called_once_with(DomainId="d-kbnw5yk6tg8j")


@patch("six.moves.builtins.open", mock_open(read_data='{"ResourceName": "SageMakerInstance"}'))
@patch("os.path.exists", side_effect=mock_exists(NOTEBOOK_METADATA_FILE, True))
@patch("sagemaker.session.sts_regional_endpoint", return_value=STS_ENDPOINT)
Expand Down