Skip to content

Commit dca9f79

Browse files
committed
change: move service-role path parsing for AmazonSageMaker-ExecutionRole for get_execution_role() into except block of IAM get_role() call and add warning message
1 parent 3dd4373 commit dca9f79

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

src/sagemaker/session.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -3498,14 +3498,6 @@ def get_caller_identity_arn(self):
34983498
endpoint_url=sts_regional_endpoint(self.boto_region_name),
34993499
).get_caller_identity()["Arn"]
35003500

3501-
if "AmazonSageMaker-ExecutionRole" in assumed_role:
3502-
role = re.sub(
3503-
r"^(.+)sts::(\d+):assumed-role/(.+?)/.*$",
3504-
r"\1iam::\2:role/service-role/\3",
3505-
assumed_role,
3506-
)
3507-
return role
3508-
35093501
role = re.sub(r"^(.+)sts::(\d+):assumed-role/(.+?)/.*$", r"\1iam::\2:role/\3", assumed_role)
35103502

35113503
# Call IAM to get the role's path
@@ -3518,6 +3510,22 @@ def get_caller_identity_arn(self):
35183510
role_name,
35193511
)
35203512

3513+
# This conditional has been present since the inception of SageMaker
3514+
# Guessing this conditional's purpose was to handle lack of IAM permissions
3515+
# https://github.com/aws/sagemaker-python-sdk/issues/2089#issuecomment-791802713
3516+
if "AmazonSageMaker-ExecutionRole" in assumed_role:
3517+
LOGGER.warning('Assuming role was created in SageMaker AWS console, '
3518+
'as the name contains `AmazonSageMaker-ExecutionRole`. '
3519+
'Defaulting to Role ARN with service-role in path. '
3520+
'If this Role ARN is incorrect, please add '
3521+
'IAM read permissions to your role or supply the '
3522+
'Role Arn directly.')
3523+
role = re.sub(
3524+
r"^(.+)sts::(\d+):assumed-role/(.+?)/.*$",
3525+
r"\1iam::\2:role/service-role/\3",
3526+
assumed_role,
3527+
)
3528+
35213529
return role
35223530

35233531
def logs_for_job( # noqa: C901 - suppress complexity warning for this method

tests/unit/test_session.py

+18
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,24 @@ def test_get_caller_identity_arn_from_an_execution_role(sts_regional_endpoint, b
405405
}
406406
sess.boto_session.client("iam").get_role.return_value = {"Role": {"Arn": arn}}
407407

408+
actual = sess.get_caller_identity_arn()
409+
assert (
410+
actual
411+
== "arn:aws:iam::369233609183:role/AmazonSageMaker-ExecutionRole-20171129T072388"
412+
)
413+
414+
415+
@patch("os.path.exists", side_effect=mock_exists(NOTEBOOK_METADATA_FILE, False))
416+
@patch("sagemaker.session.sts_regional_endpoint", return_value=STS_ENDPOINT)
417+
def test_get_caller_identity_arn_from_a_sagemaker_execution_role_with_iam_client_error(sts_regional_endpoint, boto_session):
418+
sess = Session(boto_session)
419+
arn = "arn:aws:sts::369233609183:assumed-role/AmazonSageMaker-ExecutionRole-20171129T072388/SageMaker"
420+
sess.boto_session.client("sts", endpoint_url=STS_ENDPOINT).get_caller_identity.return_value = {
421+
"Arn": arn
422+
}
423+
424+
sess.boto_session.client("iam").get_role.side_effect = ClientError({}, {})
425+
408426
actual = sess.get_caller_identity_arn()
409427
assert (
410428
actual

0 commit comments

Comments
 (0)