Skip to content
This repository was archived by the owner on Dec 9, 2021. It is now read-only.

Commit f4c6753

Browse files
committed
Initial import
0 parents  commit f4c6753

File tree

12 files changed

+384
-0
lines changed

12 files changed

+384
-0
lines changed

.dockerignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cronjob.yaml
2+
LICENSE
3+
NOTICE
4+
README.md
5+
serviceaccount.yaml
6+
examples
7+
iam-permission.json

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
.vscode/*

CONTRIBUTING.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Contributing Guidelines
2+
3+
Thank you for your interest in contributing to our project. Whether it's a bug report, new feature, correction, or additional
4+
documentation, we greatly value feedback and contributions from our community.
5+
6+
Please read through this document before submitting any issues or pull requests to ensure we have all the necessary
7+
information to effectively respond to your bug report or contribution.
8+
9+
## Reporting Bugs/Feature Requests
10+
11+
We welcome you to use the GitHub issue tracker to report bugs or suggest features.
12+
13+
When filing an issue, please check existing open, or recently closed, issues to make sure somebody else hasn't already
14+
reported the issue. Please try to include as much information as you can. Details like these are incredibly useful:
15+
16+
* A reproducible test case or series of steps
17+
* The version of our code being used
18+
* Any modifications you've made relevant to the bug
19+
* Anything unusual about your environment or deployment
20+
21+
## Contributing via Pull Requests
22+
23+
Contributions via pull requests are much appreciated. Before sending us a pull request, please ensure that:
24+
25+
1. You are working against the latest source on the *main* branch.
26+
2. You check existing open, and recently merged, pull requests to make sure someone else hasn't addressed the problem already.
27+
3. You open an issue to discuss any significant work - we would hate for your time to be wasted.
28+
29+
To send us a pull request, please:
30+
31+
1. Fork the repository.
32+
2. Modify the source; please focus on the specific change you are contributing. If you also reformat all the code, it will be hard for us to focus on your change.
33+
3. Ensure local tests pass.
34+
4. Commit to your fork using clear commit messages.
35+
5. Send us a pull request, answering any default questions in the pull request interface.
36+
6. Pay attention to any automated CI failures reported in the pull request, and stay involved in the conversation.
37+
38+
GitHub provides additional document on [forking a repository](https://help.github.com/articles/fork-a-repo/) and
39+
[creating a pull request](https://help.github.com/articles/creating-a-pull-request/).
40+
41+
## Finding contributions to work on
42+
43+
Looking at the existing issues is a great way to find something to contribute on. As our projects, by default, use the default GitHub issue labels (enhancement/bug/duplicate/help wanted/invalid/question/wontfix), looking at any 'help wanted' issues is a great place to start.
44+
45+
## Code of Conduct
46+
47+
This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct).
48+
For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact
49+
[email protected] with any additional questions or comments.
50+
51+
## Security issue notifications
52+
53+
If you discover a potential security issue in this project we ask that you notify AWS/Amazon Security via our [vulnerability reporting page](http://aws.amazon.com/security/vulnerability-reporting/). Please do **not** create a public github issue.
54+
55+
## Licensing
56+
57+
See the [LICENSE](LICENSE) file for our project's licensing. We will ask you to confirm the licensing of your contribution.

Dockerfile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: MIT-0
3+
4+
ARG AWS_CLI_VERSION=2.1.12
5+
6+
FROM amazon/aws-cli:${AWS_CLI_VERSION}
7+
8+
RUN yum update -y \
9+
&& yum install -y openssl \
10+
&& yum clean all
11+
12+
# See https://docs.aws.amazon.com/eks/latest/userguide/install-kubectl.html for other kubectl versions or AWS China regions
13+
ARG KUBECTL_DOWNLOAD_URL=https://amazon-eks.s3.us-west-2.amazonaws.com/1.18.9/2020-11-02/bin/linux/amd64/kubectl
14+
ARG KUBECTL_SHA256_URL=https://amazon-eks.s3.us-west-2.amazonaws.com/1.18.9/2020-11-02/bin/linux/amd64/kubectl.sha256
15+
16+
RUN curl -o kubectl ${KUBECTL_DOWNLOAD_URL} \
17+
&& curl -o kubectl.sha256 ${KUBECTL_SHA256_URL} \
18+
&& openssl sha1 -sha256 kubectl \
19+
&& chmod +x ./kubectl \
20+
&& mv ./kubectl /usr/local/bin/kubectl
21+
22+
COPY entrypoint.sh /
23+
24+
ENTRYPOINT ["/entrypoint.sh"]

LICENSE

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this
4+
software and associated documentation files (the "Software"), to deal in the Software
5+
without restriction, including without limitation the rights to use, copy, modify,
6+
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
7+
permit persons to whom the Software is furnished to do so.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
10+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
11+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
12+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
13+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
14+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
# Amazon ECR "Public" credentials helper script for Kubernetes
2+
3+
Amazon ECR "Public" credentials helper script for Kubernetes (`ecr-public-creds-helper-for-k8s` for short) allows your Kubernetes clusters pull public container images from [Amazon ECR Public](https://aws.amazon.com/blogs/aws/amazon-ecr-public-a-new-public-container-registry/) registries **as authenticated users** to get the limit upgraded to `10` pulls per second which is `1` for unauthenticated users as described [here](https://docs.aws.amazon.com/AmazonECR/latest/public/public-service-quotas.html), and unlimited data bandwidth as described [here](https://aws.amazon.com/ecr/pricing/).
4+
5+
`ecr-public-creds-helper-for-k8s` is one of the workarounds to access ECR Public as authenticated users from your Kubernetes clusters until 1) Amazon ECR Public get supported by the upstream Kubernetes project and/or 2) Official Amazon ECR Public support for AWS Fargate by Amazon EKS.
6+
7+
`ecr-public-creds-helper-for-k8s` runs in your cluster as a Kubernetes CronJob every 8 hours by default. It authenticates against ECR Public and stores the auth token as Kubernetes Secrets within namespaces you specified.
8+
9+
Each pod (even on AWS Fargate) will reference that Kubernetes Secret in its namespace by specifying the `imagePullSecrets` field in the PodSpec. You may also want to patch the `default` service account in each namespace to avoid writing `imagePullSecrets` in all PodSpecs, see the comments at the [entrypoint.sh#L21](entrypoint.sh#L21) for further details.
10+
11+
See the "[Create a Secret by providing credentials on the command line](https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/#create-a-secret-by-providing-credentials-on-the-command-line)" section and the "[Create a Pod that uses your Secret](https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/#create-a-pod-that-uses-your-secret)" in the Kubernetes documentation to understand how it works.
12+
13+
## Motivations
14+
15+
This project aims to fill the gap between Amazon EKS (Kubernetes, both EC2 and Fargate) and Amazon ECR Public. There are two pain points for users at the time of release of this repository as follows:
16+
17+
1. Kubernetes supports Amazon ECR (the private one) to pull private ECR images with automatic auth via IAM roles, but still the latest version (1.20, at this moment) of Kubernetes doesn't support ECR Public yet. Thus there is no straightforward way to pull ECR Public container images from EKS/Kubernetes clusters as authenticated users. This means users are forced to access ECR Public as unauthenticated users from their Kubernetes clusters, resulting that pulls from ECR Public could be throttled easily and frequently.
18+
1. [A PR in the "awslabs/amazon-ecr-credential-helper" GitHub repository](https://github.com/awslabs/amazon-ecr-credential-helper/pull/253) could solve the pain point #1 someday, but the "awslabs/amazon-ecr-credential-helper" itself cannot be used for EKS/Fargate workloads by its design. EKS/Fargate users, not only EKS/EC2 users, obviously need a way to use ECR Public as authenticated users, so this is also the pain point that this project addresses.
19+
20+
## Installation
21+
22+
### Step 0 - Clone repo
23+
24+
```shell
25+
$ git clone https://github.com/aws-containers/amazon-ecr-public-creds-helper-for-k8s.git
26+
$ cd amazon-ecr-public-creds-helper-for-k8s
27+
```
28+
29+
### Step 1 - Build and Push creds-helper container image
30+
31+
```shell
32+
$ export CREDS_HELPER_CONTAINER_IMAGE=<your-creds-helper-container-image-name-here>
33+
34+
$ docker build -t ${CREDS_HELPER_CONTAINER_IMAGE} .
35+
36+
$ docker push ${CREDS_HELPER_CONTAINER_IMAGE}
37+
```
38+
39+
### Step 2 - Create namespace
40+
41+
Create a namespace for `ecr-public-creds-helper-for-k8s` to run as a CronJob in your Kubernetes cluster.
42+
43+
```shell
44+
$ kubectl apply -f namespace.yaml
45+
namespace/ecr-public-creds-helper created
46+
```
47+
48+
### Step 3 - Create service account
49+
50+
Create a service account to allow `ecr-public-creds-helper-for-k8s` to edit Kubernetes secrets.
51+
52+
```shell
53+
$ kubectl apply -f serviceaccount.yaml
54+
serviceaccount/sa-secrets-editor created
55+
clusterrole.rbac.authorization.k8s.io/secrets-editor created
56+
clusterrolebinding.rbac.authorization.k8s.io/edit-secrets created
57+
```
58+
59+
### Step 4 - Create IAM role
60+
61+
Create an AWS IAM role to allow `ecr-public-creds-helper-for-k8s` to authenticate against Amazon ECR Public. We use the mechanism called [IAM Roles for Service Accounts (IRSA)](https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html) to map it to the service account which you created in the previous step.
62+
63+
If you have not enabled IRSA in your Kubernetes cluster yet, please follow the [IRSA documentation](https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html) and/or the [blog post](https://aws.amazon.com/blogs/opensource/introducing-fine-grained-iam-roles-service-accounts/) for enabling IRSA for your Kubernetes cluster.
64+
65+
#### Steps with eksctl
66+
67+
We're going to use `eksctl` here to show the steps to create and map the IAM role in an EKS cluster, but you can also use the AWS CLI, the AWS management console, CloudFormation, Terraform or whatever you want to use.
68+
69+
```shell
70+
$ export POLICY_ARN=$(aws iam create-policy --policy-name AmazonECRPublicAuthOnlyPolicy --policy-document file://iam-permission.json --query Policy.Arn --output text)
71+
## Check you've created the policy successfully
72+
$ echo ${POLICY_ARN}
73+
arn:aws:iam::YOUR_AWS_ACCOUNT_ID:policy/AmazonECRPublicAuthOnlyPolicy
74+
75+
$ export EKS_CLUSTER_NAME=<your-eks-cluster-name-here>
76+
77+
$ eksctl create iamserviceaccount --cluster=${EKS_CLUSTER_NAME} \
78+
--name=sa-secrets-editor \
79+
--namespace=ecr-public-creds-helper \
80+
--attach-policy-arn=${POLICY_ARN} \
81+
--override-existing-serviceaccounts \
82+
--approve
83+
```
84+
85+
### Step 5 - Configure and Apply
86+
87+
#### Configure cronjob.yaml
88+
89+
Edit the [cronjob.yaml](cronjob.yaml) before running `ecr-public-creds-helper-for-k8s` in your Kubernetes cluster.
90+
91+
```shell
92+
$ vim cronjob.yaml
93+
```
94+
95+
There are two **required** fields to change.
96+
97+
##### 1. `image` field
98+
99+
Replace `${CREDS_HELPER_CONTAINER_IMAGE}` in [line.22 in the cronjob.yaml](cronjob.yaml#L22) with your creds helper container image name which you built and pushed in the Step 1.
100+
101+
##### 2. `env.value` field
102+
103+
Replace the value (`default foo bar`) of `TARGET_NAMESPACES` environment variable in [line.26 in the cronjob.yaml](cronjob.yaml#L26) with a space-delimited list which includes one or multiple Kuberentes namespaces where your pods need the auth token for ECR Public.
104+
105+
Let's say you want to pull ECR Public container images in three namespaces (`default`, `prometheus`, `my-app`) with auth token, then the `env.value` field will look like: `value: "default prometheus my-app"`.
106+
107+
`ecr-public-creds-helper-for-k8s` will store the auth token as Kubernetes Secrets in these namespaces.
108+
109+
#### Apply cronjob.yaml
110+
111+
Run `ecr-public-creds-helper-for-k8s` in your Kubernetes cluster.
112+
113+
```shell
114+
$ kubectl apply -f cronjob.yaml
115+
cronjob.batch/ecr-public-creds-helper created
116+
```
117+
118+
### Step 6 - (Optional but recommended) Run Job manually
119+
120+
Create an initial auth token manually to let your pods use it without waiting the initial cronjob to be started. Note that `ecs-public-creds-helper-for-k8s` [refreshes the auth token in every 8 hours by default](cronjob.yaml#L8).
121+
122+
```shell
123+
$ kubectl create job initial-creds-job \
124+
-n ecr-public-creds-helper \
125+
--from=cronjob/ecr-public-creds-helper
126+
job.batch/initial-creds-job created
127+
128+
## Check the pod log to make sure it works as expected
129+
$ export POD_NAME=$(kubectl get pods --selector=job-name=initial-creds-job -n ecr-public-creds-helper -o jsonpath='{.items[0].metadata.name}')
130+
131+
$ echo ${POD_NAME}
132+
initial-creds-job-r4fbp # you'll see something like this
133+
134+
$ kubectl logs ${POD_NAME} -n ecr-public-creds-helper
135+
### You'll see the same number of lines as the namespaces you specified in the "TARGET_NAMESPACES" in the cronjob.yaml
136+
secret/ecr-public-token created
137+
secret/ecr-public-token created
138+
secret/ecr-public-token created
139+
140+
$ kubectl delete job initial-creds-job -n ecr-public-creds-helper
141+
job.batch "initial-creds-job" deleted
142+
```
143+
144+
## Use auth tokens in Pods
145+
146+
Now your pod can use the auth token (Kubernetes secret) created by `ecr-public-creds-helper-for-k8s` to pull public container images as an authenticated user from Amazon ECR Public registries.
147+
148+
You can reference the auth token from your pods like:
149+
150+
```yaml
151+
apiVersion: v1
152+
kind: Pod
153+
# ~ snip ~
154+
spec:
155+
# ~ snip ~
156+
imagePullSecrets:
157+
- name: ecr-public-token
158+
# ~ snip ~
159+
```
160+
161+
See also [examples/pod.yaml](examples/pod.yaml) for full example.
162+
163+
If you don't want to add `imagePullSecrets` in each PodSpec, you may want to see the comments in the [entrypoint.sh](entrypoint.sh#L18).
164+
165+
## Security
166+
167+
See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information.
168+
169+
## License
170+
171+
Licensed under the MIT-0 License. See the [LICENSE](LICENSE) file.

cronjob.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
apiVersion: batch/v1beta1
3+
kind: CronJob
4+
metadata:
5+
name: ecr-public-creds-helper
6+
namespace: ecr-public-creds-helper
7+
spec:
8+
schedule: "0 */8 * * *" # Every 8 hours, Note: An auth token for ECR Public is valid for 12 hours
9+
successfulJobsHistoryLimit: 2
10+
failedJobsHistoryLimit: 2
11+
jobTemplate:
12+
spec:
13+
backoffLimit: 3
14+
template:
15+
spec:
16+
serviceAccountName: sa-secrets-editor
17+
terminationGracePeriodSeconds: 0
18+
restartPolicy: Never
19+
containers:
20+
- name: kubectl
21+
imagePullPolicy: Always
22+
image: ${CREDS_HELPER_CONTAINER_IMAGE} ### <- 1. EDIT HERE !!!
23+
env:
24+
# Specify "existing" namespaces as a space-delimited list where you want to create Kubernetes secrets with auth token for ECR Public
25+
- name: TARGET_NAMESPACES
26+
value: "default foo bar" ### <- 2. EDIT HERE !!!

entrypoint.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: MIT-0
3+
4+
#!/usr/bin/env bash
5+
set -euo pipefail
6+
7+
REGISTRY=https://public.ecr.aws
8+
AUTH_USER=AWS
9+
AUTH_TOKEN=$(aws ecr-public get-authorization-token --region us-east-1 --output=text --query 'authorizationData.authorizationToken' | base64 -d | cut -d: -f2)
10+
11+
for n in $(echo ${TARGET_NAMESPACES})
12+
do
13+
kubectl delete secret ecr-public-token \
14+
-n "${n}" \
15+
--ignore-not-found
16+
kubectl create secret docker-registry ecr-public-token \
17+
-n "${n}" \
18+
--docker-server=${REGISTRY} \
19+
--docker-username=${AUTH_USER} \
20+
--docker-password=${AUTH_TOKEN}
21+
# Uncomment the following lines if you want to make all pods use this auth token without specifying it in each PodSpec.
22+
#kubectl patch serviceaccount default \
23+
# -n "${n}"
24+
# -p '{"imagePullSecrets":[{"name":"ecr-public-token"}]}'
25+
done

examples/pod.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: ecr-public-authenticated-access-example
5+
namespace: default
6+
spec:
7+
containers:
8+
- name: my-container
9+
image: public.ecr.aws/r6p5x8p7/everlasting-hey-yo:latest
10+
# Use the Kubernetes secret created by ecr-public-creds-helper-for-k8s
11+
imagePullSecrets:
12+
- name: ecr-public-token

iam-permission.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": [
4+
{
5+
"Effect": "Allow",
6+
"Action": [
7+
"ecr-public:GetAuthorizationToken",
8+
"sts:GetServiceBearerToken"
9+
],
10+
"Resource": "*"
11+
}
12+
]
13+
}

0 commit comments

Comments
 (0)