ECR repository policy uses wildcard principals

Check whether wildcard principals grant unintended access to an ECR repository, and restrict permissions to the required roles and actions.

Description

Allowing Principal: "*" in a private ECR repository policy can grant access beyond the intended accounts and roles. Registry sign-in still requires ecr:GetAuthorizationToken in an IAM policy.

Potential impact

The first policy permits reads, uploads, deletion and policy changes. Effective access also depends on conditions, applicable IAM policies and explicit denies.

Remediation

Grant each reader, publisher and administrator only the actions they need. An account’s :root ARN delegates authority to the whole account. Test that the deployment role can pull images and unapproved principals are denied.

Examples

The second example applies its policy to a differently named repository. To protect an existing repository, change the policy attached to that repository.

Before

hcl
resource "aws_ecr_repository" "public_example" {
  name = "public-example"
}

resource "aws_ecr_repository_policy" "public_policy" {
  repository = aws_ecr_repository.public_example.name

  policy = <<EOF
{
    "Version": "2008-10-17",
    "Statement": [
        {
            "Sid": "RepositoryAccess",
            "Effect": "Allow",
            "Principal": "*",
            "Action": [
                "ecr:GetDownloadUrlForLayer",
                "ecr:BatchGetImage",
                "ecr:BatchCheckLayerAvailability",
                "ecr:PutImage",
                "ecr:InitiateLayerUpload",
                "ecr:UploadLayerPart",
                "ecr:CompleteLayerUpload",
                "ecr:DescribeRepositories",
                "ecr:GetRepositoryPolicy",
                "ecr:ListImages",
                "ecr:DeleteRepository",
                "ecr:BatchDeleteImage",
                "ecr:SetRepositoryPolicy",
                "ecr:DeleteRepositoryPolicy"
            ]
        }
    ]
}
EOF
}

After

hcl
resource "aws_ecr_repository" "private_example" {
  name = "private-example"
}

resource "aws_ecr_repository_policy" "private_policy" {
  repository = aws_ecr_repository.private_example.name

  policy = <<EOF
{
    "Version": "2008-10-17",
    "Statement": [
        {
            "Sid": "ImageRead",
            "Effect": "Allow",
            "Principal": {
                "AWS": "${var.trusted_role_arn}"
            },
            "Action": [
                "ecr:GetDownloadUrlForLayer",
                "ecr:BatchGetImage",
                "ecr:BatchCheckLayerAvailability"
            ]
        }
    ]
}
EOF
}

Set trusted_role_arn to the ARN of an approved role. This statement grants that role only the three image-read actions.

References