ECR repository policy uses a wildcard principal

Review wildcard principals in private ECR repository policies and the image operations they permit.

Description

In community.aws.ecs_ecr, an allow statement with Principal: "*" in policy can grant access to unintended AWS principals. The registry remains private and sign-in requires ecr:GetAuthorizationToken through IAM.

Potential impact

Read access can expose internal images. Uploads or deletions require additional actions in the effective permissions.

Remediation

Name the accounts, roles or services that need the images and grant each only the required actions. Review conditions and the caller’s IAM permissions for cross-account access. Test the deployment’s image pull. Use 2012-10-17 for new or revised policies.

Examples

The second task grants the ci-ecr-reader role read access on a different repository, backend-api. To restrict the original repository, use its name and the actual role ARN.

Before

yaml
- name: set-policy as object
  community.aws.ecs_ecr:
    name: needs-policy-object
    policy:
      Version: "2008-10-17"
      Statement:
        - Sid: read-only
          Effect: Allow
          Principal: "*"
          Action:
            - ecr:GetDownloadUrlForLayer
            - ecr:BatchGetImage
            - ecr:BatchCheckLayerAvailability

After

yaml
- name: set restricted policy
  community.aws.ecs_ecr:
    name: backend-api
    policy:
      Version: "2008-10-17"
      Statement:
        - Sid: ci-read-only
          Effect: Allow
          Principal:
            AWS: "arn:aws:iam::123456789012:role/ci-ecr-reader"
          Action:
            - ecr:GetDownloadUrlForLayer
            - ecr:BatchGetImage
            - ecr:BatchCheckLayerAvailability

In community.aws 11.2.0, omitting image_tag_mutability and scan_on_push applies their defaults, mutable and false, to existing repositories too. Specify the intended values when changing only the policy.

References