For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/iac_security/iac_rules/ansible-aws-ecr-repository-is-publicly-accessible.md. A documentation index is available at /llms.txt.
This product is not supported for your selected Datadog site. ().

Metadata

Id: ansible-aws-ecr-repository-is-publicly-accessible

Provider: AWS

Platform: Ansible

Severity: Critical

Category: Access Control

Learn More

Description

ECR repository policies must not grant Allow permissions to the wildcard principal (*). This makes repositories publicly accessible and allows unauthorized accounts to pull or push container images, increasing the risk of data exposure and supply-chain compromise.

Check Ansible ECS/ECR tasks using the community.aws.ecs_ecr or ecs_ecr modules: in the resource policy document, any statement with "Effect": "Allow" and Principal equal to "*" is flagged unless it includes a Condition that genuinely scopes the caller — such as aws:PrincipalOrgID, aws:PrincipalAccount, or aws:SourceArn. Conditions that only restrict how a call is made (e.g. aws:SecureTransport) or check key presence (e.g. Null) are not considered scoping and the statement is still flagged. Instead, specify explicit principals such as AWS account ARNs, IAM role ARNs, or service principals, or restrict access using a scoping condition key.

Secure example with an explicit AWS account principal:

{
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": { "AWS": "arn:aws:iam::123456789012:root" },
      "Action": [ "ecr:GetDownloadUrlForLayer", "ecr:BatchGetImage" ],
      "Resource": "arn:aws:ecr:us-east-1:123456789012:repository/my-repo"
    }
  ]
}

Compliant Code Examples

- name: set-policy as object
  community.aws.ecs_ecr:
    name: needs-policy-object
    policy:
      Version: '2008-10-17'
      Statement:
      - Sid: read-only
        Effect: Allow
        Action:
        - ecr:GetDownloadUrlForLayer
        - ecr:BatchGetImage
        - ecr:BatchCheckLayerAvailability
- name: set-policy with org-restricted wildcard principal
  community.aws.ecs_ecr:
    name: org-pull-only
    policy:
      Version: '2012-10-17'
      Statement:
      - Sid: AllowOrgPull
        Effect: Allow
        Principal: "*"
        Action:
        - ecr:BatchCheckLayerAvailability
        - ecr:BatchGetImage
        - ecr:GetDownloadUrlForLayer
        Condition:
          StringEquals:
            aws:PrincipalOrgID: "o-abc12defgh"

Non-Compliant Code Examples

- 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
- name: set-policy as string
  community.aws.ecs_ecr:
    name: needs-policy-string
    policy: >
        {
          "Id": "id113",
          "Version": "2012-10-17",
          "Statement": [
            {
              "Action": [
                "s3:put"
              ],
              "Effect": "Allow",
              "Resource": "arn:aws:s3:::S3B_181355/*",
              "Principal": "*"
            }
          ]
        }
- name: set-policy with non-scoping condition
  community.aws.ecs_ecr:
    name: secure-transport-only
    policy:
      Version: '2012-10-17'
      Statement:
      - Sid: AllowPullOverHttps
        Effect: Allow
        Principal: "*"
        Action:
        - ecr:BatchCheckLayerAvailability
        - ecr:BatchGetImage
        - ecr:GetDownloadUrlForLayer
        Condition:
          Bool:
            aws:SecureTransport: "true"
- name: set-policy with forallvalues condition
  community.aws.ecs_ecr:
    name: forallvalues-org
    policy:
      Version: '2012-10-17'
      Statement:
      - Sid: AllowPullForAllOrgValues
        Effect: Allow
        Principal: "*"
        Action:
        - ecr:BatchCheckLayerAvailability
        - ecr:BatchGetImage
        - ecr:GetDownloadUrlForLayer
        Condition:
          ForAllValues:StringEquals:
            aws:PrincipalOrgID: "o-abc12defgh"