|
14 | 14 | HYPERPOD_CLI_CONNECT_ACTIONS, |
15 | 15 | _load_policy_config, |
16 | 16 | _get_required_actions, |
| 17 | + _get_smoke_test_actions, |
17 | 18 | _replace_placeholders, |
18 | 19 | _get_boto_session, |
19 | 20 | _simulate_denied_actions, |
@@ -262,6 +263,10 @@ def paginate(**kwargs): |
262 | 263 | assert not any(a.startswith("sagemaker-mlflow:") for a in simulated) |
263 | 264 | assert "sagemaker:DescribeHubContent" not in simulated |
264 | 265 | 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 |
265 | 270 | # ...but *-resource smoke-test actions are included. |
266 | 271 | assert "cloudwatch:PutMetricData" in simulated |
267 | 272 | assert "ecr:GetAuthorizationToken" in simulated |
@@ -634,6 +639,137 @@ def test_all_role_types_have_source_account_placeholder(self): |
634 | 639 | ), f"{role_type} trust policy missing aws:SourceAccount placeholder" |
635 | 640 |
|
636 | 641 |
|
| 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 | + |
637 | 773 | class TestReplacePlaceholders: |
638 | 774 | """Tests for the pure-data _replace_placeholders() helper.""" |
639 | 775 |
|
|
0 commit comments