Skip to content

Commit 9f399ce

Browse files
icywang86ruiWang Napieralskiahsan-z-khan
authored
fix: call DescribeDomain as fallback in get_execution_role (#2435)
* fix: call DescribeDomain as fallback in get_execution_role * reformat Co-authored-by: Wang Napieralski <[email protected]> Co-authored-by: Ahsan Khan <[email protected]>
1 parent f03985f commit 9f399ce

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/sagemaker/session.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3515,7 +3515,11 @@ def get_caller_identity_arn(self):
35153515
user_profile_desc = self.sagemaker_client.describe_user_profile(
35163516
DomainId=domain_id, UserProfileName=user_profile_name
35173517
)
3518-
return user_profile_desc["UserSettings"]["ExecutionRole"]
3518+
if user_profile_desc.get("UserSettings") is not None:
3519+
return user_profile_desc["UserSettings"]["ExecutionRole"]
3520+
3521+
domain_desc = self.sagemaker_client.describe_domain(DomainId=domain_id)
3522+
return domain_desc["DefaultUserSettings"]["ExecutionRole"]
35193523
except ClientError:
35203524
LOGGER.debug(
35213525
"Couldn't call 'describe_notebook_instance' to get the Role "

tests/unit/test_session.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,33 @@ def test_get_caller_identity_arn_from_describe_user_profile(boto_session):
340340
)
341341

342342

343+
@patch(
344+
"six.moves.builtins.open",
345+
mock_open(
346+
read_data='{"ResourceName": "SageMakerInstance", '
347+
'"DomainId": "d-kbnw5yk6tg8j", '
348+
'"UserProfileName": "default-1617915559064"}'
349+
),
350+
)
351+
@patch("os.path.exists", side_effect=mock_exists(NOTEBOOK_METADATA_FILE, True))
352+
def test_get_caller_identity_arn_from_describe_domain(boto_session):
353+
sess = Session(boto_session)
354+
expected_role = "arn:aws:iam::369233609183:role/service-role/SageMakerRole-20171129T072388"
355+
sess.sagemaker_client.describe_user_profile.return_value = {}
356+
sess.sagemaker_client.describe_domain.return_value = {
357+
"DefaultUserSettings": {"ExecutionRole": expected_role}
358+
}
359+
360+
actual = sess.get_caller_identity_arn()
361+
362+
assert actual == expected_role
363+
sess.sagemaker_client.describe_user_profile.assert_called_once_with(
364+
DomainId="d-kbnw5yk6tg8j",
365+
UserProfileName="default-1617915559064",
366+
)
367+
sess.sagemaker_client.describe_domain.assert_called_once_with(DomainId="d-kbnw5yk6tg8j")
368+
369+
343370
@patch("six.moves.builtins.open", mock_open(read_data='{"ResourceName": "SageMakerInstance"}'))
344371
@patch("os.path.exists", side_effect=mock_exists(NOTEBOOK_METADATA_FILE, True))
345372
@patch("sagemaker.session.sts_regional_endpoint", return_value=STS_ENDPOINT)

0 commit comments

Comments
 (0)