Skip to content
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
8 changes: 4 additions & 4 deletions src/sagemaker/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -6285,16 +6285,16 @@ def get_caller_identity_arn(self):
user_profile_name = metadata.get("UserProfileName")
execution_role_arn = metadata.get("ExecutionRoleArn")
try:
# find execution role from the metadata file if present
if execution_role_arn is not None:
return execution_role_arn

if domain_id is None:
instance_desc = self.sagemaker_client.describe_notebook_instance(
NotebookInstanceName=instance_name
)
return instance_desc["RoleArn"]

# find execution role from the metadata file if present
if execution_role_arn is not None:
return execution_role_arn

user_profile_desc = self.sagemaker_client.describe_user_profile(
DomainId=domain_id, UserProfileName=user_profile_name
)
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,25 @@ def test_get_caller_identity_arn_from_metadata_file_for_space(boto_session):
assert actual == expected_role


@patch(
"six.moves.builtins.open",
mock_open(
read_data='{"ResourceName": "SageMakerInstance", '
'"ExecutionRoleArn": "arn:aws:iam::369233609183:role/service-role/SageMakerRole-20171129T072388"}'
),
)
@patch("os.path.exists", side_effect=mock_exists(NOTEBOOK_METADATA_FILE, True))
def test_get_caller_identity_arn_from_metadata_file_with_no_domain_id(boto_session):
sess = Session(boto_session)
expected_role = "arn:aws:iam::369233609183:role/service-role/SageMakerRole-20171129T072388"

actual = sess.get_caller_identity_arn()

assert actual == expected_role
# Should not call describe_notebook_instance since ExecutionRoleArn is available
sess.sagemaker_client.describe_notebook_instance.assert_not_called()


@patch(
"six.moves.builtins.open",
mock_open(read_data='{"ResourceName": "SageMakerInstance"}'),
Expand Down