Skip to content

Commit 369ec72

Browse files
authored
fix(iam): scope repo-level ECR actions to prevent false deny in preflight validation (#6024)
Split ecr_policy statements in training, serving, and hyperpod role types so that only ecr:GetAuthorizationToken (account-level) remains under Resource: "*". The repository-level actions (BatchGetImage, GetDownloadUrlForLayer, BatchCheckLayerAvailability) are now scoped to arn:aws:ecr:*:*:repository/*, which excludes them from _get_smoke_test_actions. This prevents SimulatePrincipalPolicy from returning implicitDeny for roles that correctly scope ECR permissions to specific repo ARNs (least privilege), fixing the regression that blocked deploys/training/pipelines for those customers.
1 parent 45985d4 commit 369ec72

2 files changed

Lines changed: 164 additions & 9 deletions

File tree

‎sagemaker-core/src/sagemaker/core/helper/iam_policies.py‎

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,26 @@
4949
"Version": "2012-10-17",
5050
"Statement": [
5151
{
52+
# GetAuthorizationToken is an account-level action that
53+
# does not target a specific repository — Resource: "*" is
54+
# correct and safe to simulate without ResourceArns.
55+
"Effect": "Allow",
56+
"Action": ["ecr:GetAuthorizationToken"],
57+
"Resource": "*",
58+
},
59+
{
60+
# These are repository-level actions. Scoping to
61+
# repository/* prevents false implicitDeny results from
62+
# SimulatePrincipalPolicy when the customer's real policy
63+
# correctly scopes them to specific repo ARNs.
5264
"Effect": "Allow",
5365
"Action": [
5466
"ecr:GetDownloadUrlForLayer",
5567
"ecr:BatchGetImage",
5668
"ecr:BatchCheckLayerAvailability",
57-
"ecr:GetAuthorizationToken",
5869
],
59-
"Resource": "*",
60-
}
70+
"Resource": "arn:aws:ecr:*:*:repository/*",
71+
},
6172
],
6273
},
6374
"cloudwatch_logs_policy": {
@@ -341,16 +352,20 @@
341352
"ecr_policy": {
342353
"Version": "2012-10-17",
343354
"Statement": [
355+
{
356+
"Effect": "Allow",
357+
"Action": ["ecr:GetAuthorizationToken"],
358+
"Resource": "*",
359+
},
344360
{
345361
"Effect": "Allow",
346362
"Action": [
347363
"ecr:GetDownloadUrlForLayer",
348364
"ecr:BatchGetImage",
349365
"ecr:BatchCheckLayerAvailability",
350-
"ecr:GetAuthorizationToken",
351366
],
352-
"Resource": "*",
353-
}
367+
"Resource": "arn:aws:ecr:*:*:repository/*",
368+
},
354369
],
355370
},
356371
"cloudwatch_logs_policy": {
@@ -515,16 +530,20 @@
515530
"ecr_policy": {
516531
"Version": "2012-10-17",
517532
"Statement": [
533+
{
534+
"Effect": "Allow",
535+
"Action": ["ecr:GetAuthorizationToken"],
536+
"Resource": "*",
537+
},
518538
{
519539
"Effect": "Allow",
520540
"Action": [
521541
"ecr:GetDownloadUrlForLayer",
522542
"ecr:BatchGetImage",
523543
"ecr:BatchCheckLayerAvailability",
524-
"ecr:GetAuthorizationToken",
525544
],
526-
"Resource": "*",
527-
}
545+
"Resource": "arn:aws:ecr:*:*:repository/*",
546+
},
528547
],
529548
},
530549
"cloudwatch_logs_policy": {

‎sagemaker-core/tests/unit/helper/test_iam_role_resolver.py‎

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
HYPERPOD_CLI_CONNECT_ACTIONS,
1515
_load_policy_config,
1616
_get_required_actions,
17+
_get_smoke_test_actions,
1718
_replace_placeholders,
1819
_get_boto_session,
1920
_simulate_denied_actions,
@@ -262,6 +263,10 @@ def paginate(**kwargs):
262263
assert not any(a.startswith("sagemaker-mlflow:") for a in simulated)
263264
assert "sagemaker:DescribeHubContent" not in simulated
264265
assert "s3:GetObject" not in simulated # S3 is scoped to S3_PLACEHOLDER
266+
# Repository-level ECR actions are scoped and excluded from the gate.
267+
assert "ecr:BatchGetImage" not in simulated
268+
assert "ecr:GetDownloadUrlForLayer" not in simulated
269+
assert "ecr:BatchCheckLayerAvailability" not in simulated
265270
# ...but *-resource smoke-test actions are included.
266271
assert "cloudwatch:PutMetricData" in simulated
267272
assert "ecr:GetAuthorizationToken" in simulated
@@ -634,6 +639,137 @@ def test_all_role_types_have_source_account_placeholder(self):
634639
), f"{role_type} trust policy missing aws:SourceAccount placeholder"
635640

636641

642+
class TestEcrPolicyScopedCorrectly:
643+
"""Regression tests for ECR policy scoping (V2287033493).
644+
645+
Repository-level ECR actions (BatchGetImage, GetDownloadUrlForLayer,
646+
BatchCheckLayerAvailability) must be scoped to repository ARNs, NOT
647+
Resource: "*". Only GetAuthorizationToken is account-level and belongs
648+
under "*". When all four are under "*", _get_smoke_test_actions includes
649+
the repo-level ones and SimulatePrincipalPolicy (called without ResourceArns)
650+
returns implicitDeny for roles with least-privilege ECR policies — a false
651+
positive that blocks deploys/training/pipelines.
652+
"""
653+
654+
ECR_REPO_ACTIONS = {
655+
"ecr:GetDownloadUrlForLayer",
656+
"ecr:BatchGetImage",
657+
"ecr:BatchCheckLayerAvailability",
658+
}
659+
660+
@pytest.mark.parametrize("role_type", ["training", "serving", "hyperpod"])
661+
def test_ecr_repo_actions_not_in_smoke_test(self, role_type):
662+
"""Repository-level ECR actions must NOT appear in the smoke test set."""
663+
smoke_actions = set(_get_smoke_test_actions(role_type))
664+
overlap = smoke_actions & self.ECR_REPO_ACTIONS
665+
assert not overlap, (
666+
f"role_type={role_type}: repo-level ECR actions {overlap} should not be "
667+
f"in smoke test (would cause false implicitDeny)"
668+
)
669+
670+
@pytest.mark.parametrize("role_type", ["training", "serving", "hyperpod"])
671+
def test_ecr_get_authorization_token_in_smoke_test(self, role_type):
672+
"""GetAuthorizationToken is account-level and SHOULD be in the smoke test."""
673+
smoke_actions = set(_get_smoke_test_actions(role_type))
674+
assert "ecr:GetAuthorizationToken" in smoke_actions
675+
676+
@pytest.mark.parametrize("role_type", ["training", "serving", "hyperpod"])
677+
def test_ecr_repo_actions_scoped_to_repository_arn(self, role_type):
678+
"""Repository-level ECR actions must have a repository/* resource scope."""
679+
config = _load_policy_config()
680+
ecr_stmts = config[role_type]["policies"]["ecr_policy"]["Statement"]
681+
# Find the statement containing BatchGetImage
682+
repo_stmt = None
683+
for stmt in ecr_stmts:
684+
actions = stmt.get("Action", [])
685+
if isinstance(actions, str):
686+
actions = [actions]
687+
if "ecr:BatchGetImage" in actions:
688+
repo_stmt = stmt
689+
break
690+
assert repo_stmt is not None, "No statement with ecr:BatchGetImage found"
691+
resource = repo_stmt["Resource"]
692+
assert resource == "arn:aws:ecr:*:*:repository/*", (
693+
f"Expected repository/* scope, got: {resource}"
694+
)
695+
696+
@pytest.mark.parametrize("role_type", ["training", "serving", "hyperpod"])
697+
def test_ecr_get_authorization_token_resource_is_wildcard(self, role_type):
698+
"""GetAuthorizationToken must remain under Resource: '*'."""
699+
config = _load_policy_config()
700+
ecr_stmts = config[role_type]["policies"]["ecr_policy"]["Statement"]
701+
auth_stmt = None
702+
for stmt in ecr_stmts:
703+
actions = stmt.get("Action", [])
704+
if isinstance(actions, str):
705+
actions = [actions]
706+
if "ecr:GetAuthorizationToken" in actions:
707+
auth_stmt = stmt
708+
break
709+
assert auth_stmt is not None, "No statement with ecr:GetAuthorizationToken found"
710+
assert auth_stmt["Resource"] == "*"
711+
712+
@pytest.mark.parametrize("role_type", ["training", "serving", "hyperpod"])
713+
def test_all_ecr_actions_still_in_required_actions(self, role_type):
714+
"""All four ECR actions must remain in the full required actions list."""
715+
all_actions = set(_get_required_actions(role_type))
716+
expected = self.ECR_REPO_ACTIONS | {"ecr:GetAuthorizationToken"}
717+
assert expected.issubset(all_actions), (
718+
f"Missing ECR actions from required set: {expected - all_actions}"
719+
)
720+
721+
def test_least_privilege_ecr_role_passes_validation(self):
722+
"""A role with ECR permissions scoped to specific repos must not be blocked.
723+
724+
This is the actual customer scenario from V2287033493: the customer's role
725+
grants BatchGetImage/GetDownloadUrlForLayer/BatchCheckLayerAvailability on
726+
specific repository ARNs, not '*'. The smoke test should only simulate
727+
GetAuthorizationToken (which the customer grants on '*'), so the validation
728+
should pass.
729+
"""
730+
mock_session, mock_iam, _ = _make_session(
731+
"arn:aws:sts::123456789012:assumed-role/MyTrainingRole/sess"
732+
)
733+
mock_iam.get_role.return_value = {
734+
"Role": {
735+
"Arn": "arn:aws:iam::123456789012:role/MyTrainingRole",
736+
"AssumeRolePolicyDocument": _trusted_doc(),
737+
}
738+
}
739+
740+
# Simulate: all smoke-test actions are allowed (customer has them on *)
741+
captured = {}
742+
743+
def paginate(**kwargs):
744+
captured["actions"] = kwargs.get("ActionNames", [])
745+
return [
746+
{
747+
"EvaluationResults": [
748+
{"EvalActionName": a, "EvalDecision": "allowed"}
749+
for a in kwargs["ActionNames"]
750+
]
751+
}
752+
]
753+
754+
paginator = MagicMock()
755+
paginator.paginate.side_effect = paginate
756+
mock_iam.get_paginator.return_value = paginator
757+
758+
# Should succeed without raising
759+
result = resolve_and_validate_role(
760+
provided_role=None, role_type="training", sagemaker_session=mock_session
761+
)
762+
assert result == "arn:aws:iam::123456789012:role/MyTrainingRole"
763+
764+
# Verify repo-level ECR actions were NOT simulated
765+
simulated = set(captured["actions"])
766+
assert "ecr:BatchGetImage" not in simulated
767+
assert "ecr:GetDownloadUrlForLayer" not in simulated
768+
assert "ecr:BatchCheckLayerAvailability" not in simulated
769+
# But GetAuthorizationToken WAS simulated
770+
assert "ecr:GetAuthorizationToken" in simulated
771+
772+
637773
class TestReplacePlaceholders:
638774
"""Tests for the pure-data _replace_placeholders() helper."""
639775

0 commit comments

Comments
 (0)