More Info:

ECR repository policies should not enable global or public access to images. ECR repository policies should limit access to images to known IAM entities and AWS accounts and avoid the use of account-level wildcards.

Risk Level

High

Address

Security

Compliance Standards

HIPAA, PCIDSS, SOC2, AWSWAF, HITRUST, NISTCSF

Remediation

Using Console

Sure, here are the step-by-step instructions to remediate the misconfiguration of ECR repositories not being private in AWS:

  1. Login to your AWS console.
  2. Navigate to the Amazon Elastic Container Registry (ECR) service.
  3. Select the repository that you want to make private.
  4. Click on the “Permissions” tab.
  5. Under the “Repository policy” section, click on the “Edit” button.
  6. In the JSON editor, replace the existing policy with the following policy:
{
  "Version": "2008-10-17",
  "Statement": [
    {
      "Sid": "AccessDenied",
      "Effect": "Deny",
      "Principal": "*",
      "Action": [
        "ecr:GetDownloadUrlForLayer",
        "ecr:BatchGetImage",
        "ecr:BatchCheckLayerAvailability",
        "ecr:PutImage",
        "ecr:InitiateLayerUpload",
        "ecr:UploadLayerPart",
        "ecr:CompleteLayerUpload"
      ]
    },
    {
      "Sid": "AllowPushPull",
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::AWS_ACCOUNT_ID:root"
      },
      "Action": [
        "ecr:GetAuthorizationToken",
        "ecr:BatchCheckLayerAvailability",
        "ecr:GetDownloadUrlForLayer",
        "ecr:BatchGetImage",
        "ecr:PutImage",
        "ecr:InitiateLayerUpload",
        "ecr:UploadLayerPart",
        "ecr:CompleteLayerUpload"
      ]
    }
  ]
}

Note: Replace “AWS_ACCOUNT_ID” with your AWS account ID.

  1. Click on the “Save” button to save the policy.
  2. Verify that the repository is now private by checking the “Repository visibility” under the “Overview” tab.

That’s it! You have successfully remediated the misconfiguration of ECR repositories not being private in AWS.

Using CLI

To remediate the misconfiguration of ECR repositories being public, you can follow the below steps using AWS CLI:

  1. Open the AWS CLI and run the following command to list all ECR repositories in your account:

    aws ecr describe-repositories
    
  2. For each repository that is public, run the following command to modify its permissions:

    aws ecr set-repository-policy --repository-name <repository-name> --policy-text '{"Version": "2008-10-17", "Statement": [{"Sid": "DenyPublicPull", "Effect": "Deny", "Principal": "*", "Action": ["ecr:BatchGetImage", "ecr:GetDownloadUrlForLayer", "ecr:GetAuthorizationToken", "ecr:DescribeRepositories", "ecr:ListImages"], "Condition": {"Bool": {"aws:SecureTransport": "false"}}}]}' 
    

    Replace <repository-name> with the name of the repository that you want to make private.

  3. Verify that the repository is now private by running the following command:

    aws ecr describe-repositories --repository-names <repository-name>
    

    The output should include "repositoryPolicyText": "{\"Version\": \"2008-10-17\", \"Statement\": [{\"Sid\": \"DenyPublicPull\", \"Effect\": \"Deny\", \"Principal\": \"*\", \"Action\": [\"ecr:BatchGetImage\", \"ecr:GetDownloadUrlForLayer\", \"ecr:GetAuthorizationToken\", \"ecr:DescribeRepositories\", \"ecr:ListImages\"], \"Condition\": {\"Bool\": {\"aws:SecureTransport\": \"false\"}}}]}"

  4. Repeat the above steps for all ECR repositories that are public in your account.

By following these steps, you can remediate the misconfiguration of ECR repositories being public and make them private.

Using Python

To remediate the misconfiguration “ECR Repositories Should Be Private” for AWS using python, you can follow these steps:

  1. Import the necessary AWS SDK and Boto3 library in your python code.
import boto3
  1. Create a Boto3 ECR client object to interact with ECR.
ecr_client = boto3.client('ecr')
  1. Get a list of all the ECR repositories in your AWS account using the describe_repositories method.
response = ecr_client.describe_repositories()
repositories = response['repositories']
  1. For each repository, check if it is public or not using the describe_images method. If the repository is public, update its permissions to make it private using the set_repository_policy method.
for repo in repositories:
    response = ecr_client.describe_images(repositoryName=repo['repositoryName'])
    if 'imageDetails' in response and len(response['imageDetails']) > 0:
        image_detail = response['imageDetails'][0]
        if 'imageTags' in image_detail and len(image_detail['imageTags']) > 0:
            image_tag = image_detail['imageTags'][0]
            policy_text = '{"Version": "2008-10-17","Statement": [{"Sid": "AllowPushPull","Effect": "Allow","Principal": {"AWS": "*"},"Action": ["ecr:GetDownloadUrlForLayer","ecr:BatchGetImage","ecr:BatchCheckLayerAvailability","ecr:PutImage","ecr:InitiateLayerUpload","ecr:UploadLayerPart","ecr:CompleteLayerUpload"]},{"Sid": "DenyAll","Effect": "Deny","Principal": {"AWS": "*"},"Action": "ecr:*"}]}'
            ecr_client.set_repository_policy(repositoryName=repo['repositoryName'], policyText=policy_text)
  1. Once the permissions have been updated, print a message to confirm that the repositories have been made private.
print("All ECR repositories have been made private.")

This code will remediate the misconfiguration “ECR Repositories Should Be Private” for AWS using python.

Additional Reading: