Skip to content

Commit 95e790e

Browse files
author
Dan
authored
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 (#2191)
1 parent 3dd4373 commit 95e790e

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

src/sagemaker/session.py

+18-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,24 @@ 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(
3518+
"Assuming role was created in SageMaker AWS console, "
3519+
"as the name contains `AmazonSageMaker-ExecutionRole`. "
3520+
"Defaulting to Role ARN with service-role in path. "
3521+
"If this Role ARN is incorrect, please add "
3522+
"IAM read permissions to your role or supply the "
3523+
"Role Arn directly."
3524+
)
3525+
role = re.sub(
3526+
r"^(.+)sts::(\d+):assumed-role/(.+?)/.*$",
3527+
r"\1iam::\2:role/service-role/\3",
3528+
assumed_role,
3529+
)
3530+
35213531
return role
35223532

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

tests/unit/test_session.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -398,12 +398,30 @@ def test_get_caller_identity_arn_from_a_role(sts_regional_endpoint, boto_session
398398
@patch("os.path.exists", side_effect=mock_exists(NOTEBOOK_METADATA_FILE, False))
399399
@patch("sagemaker.session.sts_regional_endpoint", return_value=STS_ENDPOINT)
400400
def test_get_caller_identity_arn_from_an_execution_role(sts_regional_endpoint, boto_session):
401+
sess = Session(boto_session)
402+
sts_arn = "arn:aws:sts::369233609183:assumed-role/AmazonSageMaker-ExecutionRole-20171129T072388/SageMaker"
403+
sess.boto_session.client("sts", endpoint_url=STS_ENDPOINT).get_caller_identity.return_value = {
404+
"Arn": sts_arn
405+
}
406+
iam_arn = "arn:aws:iam::369233609183:role/AmazonSageMaker-ExecutionRole-20171129T072388"
407+
sess.boto_session.client("iam").get_role.return_value = {"Role": {"Arn": iam_arn}}
408+
409+
actual = sess.get_caller_identity_arn()
410+
assert actual == iam_arn
411+
412+
413+
@patch("os.path.exists", side_effect=mock_exists(NOTEBOOK_METADATA_FILE, False))
414+
@patch("sagemaker.session.sts_regional_endpoint", return_value=STS_ENDPOINT)
415+
def test_get_caller_identity_arn_from_a_sagemaker_execution_role_with_iam_client_error(
416+
sts_regional_endpoint, boto_session
417+
):
401418
sess = Session(boto_session)
402419
arn = "arn:aws:sts::369233609183:assumed-role/AmazonSageMaker-ExecutionRole-20171129T072388/SageMaker"
403420
sess.boto_session.client("sts", endpoint_url=STS_ENDPOINT).get_caller_identity.return_value = {
404421
"Arn": arn
405422
}
406-
sess.boto_session.client("iam").get_role.return_value = {"Role": {"Arn": arn}}
423+
424+
sess.boto_session.client("iam").get_role.side_effect = ClientError({}, {})
407425

408426
actual = sess.get_caller_identity_arn()
409427
assert (

0 commit comments

Comments
 (0)