Review ECR repository access policies

Limit access to the principals and image operations that need it.

Description

An IAM identity policy can grant access to a private ECR repository in the same account. A repository with no repository policy remains private.

Potential impact

An overly broad repository policy can give unintended roles access to images.

Remediation

Check existing IAM permissions first. If a repository policy is needed, name the role and allowed actions. Grant ecr:GetAuthorizationToken in the caller’s IAM policy for registry sign-in.

Examples

The statement on bar lets ImagePuller retrieve an image manifest and a layer download URL. Use the actual role ARN and verify that the role can pull the image it needs.

Before

hcl
resource "aws_ecr_repository" "example" {
  name = "bar"
}

After

hcl
resource "aws_ecr_repository" "example" {
  name = "bar"
}

resource "aws_ecr_repository_policy" "example_policy" {
  repository = aws_ecr_repository.example.name

  policy = <<EOF
{
  "Version": "2008-10-17",
  "Statement": [
    {
      "Sid": "new policy",
      "Effect": "Allow",
      "Principal": {"AWS": "arn:aws:iam::123456789012:role/ImagePuller"},
      "Action": [
        "ecr:GetDownloadUrlForLayer",
        "ecr:BatchGetImage"
      ]
    }
  ]
}
EOF
}

References