Sub topic 11
Least-privilege access for people and automation
This chapter assumes you can push and pull images with GHCR and ECR, and that you know basic GitHub and AWS IAM concepts. Least privilege means each person or system gets only the permissions its job needs, on only the repositories it needs.
Who needs what
Most registry users fall into three roles. Separating them keeps a leaked credential from becoming a full compromise.
| Role | Needs | Should not have |
|---|---|---|
| Developer | Pull from shared repositories, push to dev repositories | Push to production repositories |
| CI pipeline | Push to one or a few repositories | Pull from or delete in unrelated repositories |
| Runtime (cluster, node) | Pull from the repositories it runs | Any push or delete permission |
GHCR
GHCR permissions attach to a package, not to a whole account. Three practices matter most:
- Prefer the workflow token. In GitHub Actions,
GITHUB_TOKENis created per run and expires when the run ends. Grant itpackages: writeonly in the job that publishes. Other jobs should keeppackages: reador no package access. - Scope personal access tokens (classic) tightly. Use
read:packagesfor pulling. Addwrite:packagesonly for the account that pushes. Avoiddelete:packagesunless a cleanup job truly needs it. - Link packages to repositories. A package connected to a repository can inherit that repository's access. Set explicit access for teams under the package settings, and keep visibility private unless the image must be public.
A token that only pulls, like the one in the Kubernetes pull secret, should never be able to push.
ECR
ECR uses IAM policies, so you can restrict by action and by repository. Authentication itself is separate from authorization. ecr:GetAuthorizationToken cannot be limited to a repository, so it uses Resource: "*". The data actions are limited to a repository ARN.
This policy lets a CI role push to one repository:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "LoginToRegistry",
"Effect": "Allow",
"Action": "ecr:GetAuthorizationToken",
"Resource": "*"
},
{
"Sid": "PushToMyApp",
"Effect": "Allow",
"Action": [
"ecr:BatchCheckLayerAvailability",
"ecr:InitiateLayerUpload",
"ecr:UploadLayerPart",
"ecr:CompleteLayerUpload",
"ecr:PutImage"
],
"Resource": "arn:aws:ecr:us-east-1:123456789012:repository/my-app"
}
]
}A runtime role needs only ecr:BatchGetImage and ecr:GetDownloadUrlForLayer, plus the same login action. Leave out ecr:PutImage and ecr:BatchDeleteImage.
For an EKS node role, the AWS managed policy AmazonEC2ContainerRegistryReadOnly covers pull access to all repositories in the account. That is convenient, but it is broader than needed if some repositories are sensitive. Write a custom policy scoped to specific repository ARNs in that case.
Automation habits
- Use short-lived credentials. In GitHub Actions, assume an AWS IAM role through OpenID Connect (OIDC) instead of storing long-lived access keys as secrets.
- Use one role or token per purpose. A push role and a pull role are easier to audit and revoke than one shared credential.
- Combine access limits with tag immutability, so even an allowed pusher cannot overwrite a released tag.
- Rotate and review. Remove tokens belonging to people who left, and check IAM access reports for unused permissions.
Exercise
A Kubernetes node role uses AmazonEC2ContainerRegistryReadOnly, and your team stores a sensitive internal repository in the same account. Name the risk and one way to reduce it.
Show answer
The managed policy allows pulling from every repository in the account, so any workload on those nodes can pull the sensitive images. Replace it with a custom IAM policy that allows ecr:BatchGetImage and ecr:GetDownloadUrlForLayer only on the ARNs of the repositories those nodes run, plus ecr:GetAuthorizationToken on "*".