Sub topic 8
Repository policies and sharing across accounts
This chapter assumes you know how IAM users, roles, and policies work, and that you can create an ECR repository and push an image to it. You will learn how a repository policy lets another AWS account pull from your repository, and why that is often not enough on its own.
Two places to grant access
ECR checks two kinds of policy:
- Identity-based policies are IAM policies attached to a user or role. They say what that principal may do.
- Repository policies are resource-based policies attached to one repository. They say who may use that repository.
Within a single account, either policy can grant access. Across accounts, both sides must agree. The repository policy in the owning account must allow the other account, and the IAM policy in the other account must allow its own principal to call ECR.
A cross-account pull policy
Suppose account 444455556666 owns my-app, and account 111122223333 needs to pull from it. This repository policy goes on the repository in the owning account:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowCrossAccountPull",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::111122223333:root"
},
"Action": [
"ecr:BatchGetImage",
"ecr:GetDownloadUrlForLayer"
]
}
]
}Save it as policy.json and apply it:
aws ecr set-repository-policy \
--repository-name my-app \
--policy-text file://policy.json \
--region us-east-1The two actions are what a pull needs. ecr:BatchGetImage reads the image manifest. ecr:GetDownloadUrlForLayer lets the client download each layer.
Using :root as the principal names the whole account. It does not give every user in that account access. It only makes the account eligible, and each principal there still needs its own IAM permission.
The other half: IAM in the pulling account
A principal in account 111122223333 needs an IAM policy like this one:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "ecr:GetAuthorizationToken",
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"ecr:BatchGetImage",
"ecr:GetDownloadUrlForLayer"
],
"Resource": "arn:aws:ecr:us-east-1:444455556666:repository/my-app"
}
]
}ecr:GetAuthorizationToken applies to the registry as a whole, so its resource must be *. The pull actions can be scoped to the repository ARN in the owning account.
The pulling account then logs in against its own registry host or the owner's. It pulls using the owner's registry URI:
aws ecr get-login-password --region us-east-1 | \
docker login --username AWS --password-stdin 444455556666.dkr.ecr.us-east-1.amazonaws.com
docker pull 444455556666.dkr.ecr.us-east-1.amazonaws.com/my-app:1.0.0Keeping the policy narrow
Follow least privilege when you write repository policies:
- Grant only the actions the consumer needs. Pulling needs the two actions above. Pushing needs more, such as
ecr:PutImage,ecr:InitiateLayerUpload,ecr:UploadLayerPart, andecr:CompleteLayerUpload. - Prefer naming a specific role ARN as the principal instead of
:rootwhen you know which role will pull. - Never use
"Principal": "*"without a condition. That opens the repository to anyone.
Common errors
| Symptom | Likely cause |
|---|---|
| `no basic auth credentials` | The `docker login` step was skipped or the token expired. |
| `denied: User is not authorized to perform: ecr:GetAuthorizationToken` | The IAM policy in the pulling account is missing that action. |
| `pull access denied` or an `AccessDeniedException` on `BatchGetImage` | The repository policy does not name the pulling account or role. |
| Policy rejected when applied | Invalid JSON, or a principal that does not exist. |
Exercise
A role in account 111122223333 gets "AccessDeniedException" when it runs docker pull against a repository in account 444455556666. The repository policy already lists arn:aws:iam::111122223333:root with ecr:BatchGetImage and ecr:GetDownloadUrlForLayer. Name two things you should check in account 111122223333.
Show answer
Check that the role's IAM policy allows ecr:GetAuthorizationToken on *, and that it allows ecr:BatchGetImage and ecr:GetDownloadUrlForLayer on the repository ARN in account 444455556666. Cross-account access needs permission on both sides, so the repository policy alone is not enough.