Skip to content

Commit 4eda03c

Browse files
committed
Back to upstream and upgrade deps, refactor. Reinitialization
1 parent 1ca9844 commit 4eda03c

File tree

17 files changed

+211
-237
lines changed

17 files changed

+211
-237
lines changed

.gitignore

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

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [Unreleased]
9+
10+
### Added
11+
- Support for AWS WebIdentity Token
12+
- Support for AWS EC2 metadata
13+
- Log of the AWS identity used
14+
- Support to set custom renewal interval
15+
- Fallback to AWS_REGION, AWS_DEFAULT_REGION and region in ECR URL
16+
17+
### Changed
18+
- Upgraded to OpenResty 1.21.4.1
19+
- Upgraded AWS CLI to 1.34.21
20+
- Environment variables names simplified
21+
- Cleaned up the repository structure

Dockerfile

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
FROM openresty/openresty:1.13.6.1-alpine
1+
FROM openresty/openresty:1.21.4.1-0-alpine
2+
23
USER root
34

4-
RUN apk add -v --no-cache bind-tools python py-pip supervisor \
5-
&& mkdir /cache \
6-
&& addgroup -g 101 nginx \
7-
&& adduser -u 100 -D -S -h /cache -s /sbin/nologin -G nginx nginx \
8-
&& pip install --upgrade pip awscli==1.11.183 \
9-
&& apk -v --purge del py-pip
5+
RUN apk add -v --no-cache bind-tools python3 py-pip py3-urllib3 py3-colorama supervisor
6+
RUN mkdir /cache
7+
RUN mkdir /etc/crontab
8+
RUN addgroup -g 110 nginx && adduser -u 110 -D -S -h /cache -s /sbin/nologin -G nginx nginx
9+
RUN pip install --upgrade pip awscli==1.34.21
1010

11-
COPY files/startup.sh files/renew_token.sh /
12-
COPY files/ecr.ini /etc/supervisor.d/ecr.ini
13-
COPY files/root /etc/crontabs/root
11+
COPY scripts/startup.sh /
12+
COPY scripts/renew_token.sh /
13+
COPY config/supervisord/programs.ini /etc/supervisor.d/programs.ini
1414

15-
COPY files/nginx.conf /usr/local/openresty/nginx/conf/nginx.conf
16-
COPY files/ssl.conf /usr/local/openresty/nginx/conf/ssl.conf
15+
COPY config/nginx/nginx.conf /usr/local/openresty/nginx/conf/nginx.conf
16+
COPY config/nginx/ssl.conf /usr/local/openresty/nginx/conf/ssl.conf
1717

1818
ENV PORT 5000
1919
RUN chmod a+x /startup.sh /renew_token.sh

LICENSE

Lines changed: 0 additions & 21 deletions
This file was deleted.

README.md

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,70 @@
1-
aws-ecr-http-proxy
2-
===========
1+
# aws-ecr-http-proxy
32

4-
A very simple nginx proxy that forwards requests to AWS ECR and caches the responses locally.
3+
A very simple nginx push/pull proxy that forwards requests to AWS ECR and caches the responses locally.
54

6-
Run it like this, replace UPSTREAM with your target address with following required params:
7-
- `AWS_REGION`
8-
- `AWS_ACCESS_KEY_ID`
9-
- `AWS_SECRET_ACCESS_KEY`
5+
### Differences Between Fork and Upstream Repository
106

11-
It is also possible to define `CACHE_MAX_SIZE` env to limit maximum cache size on provided volume
7+
- Added support for AWS WebIdentity Token
8+
- Added support for AWS EC2 metadata
9+
- Added log of the AWS identity used
10+
- Added support to set custom renewal interval
11+
- Fallback to AWS_REGION, AWS_DEFAULT_REGION and region in ECR URL
12+
- Upgraded to OpenResty 1.21.4.1
13+
- Upgraded AWS CLI to 1.34.21
14+
- Environment variables names simplified
15+
- Cleaned up the repository structure
1216

13-
For example:
17+
### Configuration:
18+
The proxy is packaged in a docker container and can be configured with following environment variables:
19+
20+
| Environment Variable | Description | Status | Default |
21+
| :---------------------------------: | :--------------------------------------------: | :-------------------------------: | :--------: |
22+
| `ECR` | URL for AWS ECR | Required | |
23+
| `RESOLVER` | DNS server to be used by proxy | Required | |
24+
| `PORT` | Port on which proxy listens | Required | |
25+
| `CACHE_MAX_SIZE` | Maximum size for cache volume | Optional | `75g` |
26+
| `CACHE_KEY` | Cache key used for the content by nginx | Optional | `$uri` |
27+
| `RENEW_INTERVAL_HOURS` | Interval for renewing the AWS credentials | Optional | `6` |
28+
| `ENABLE_SSL` | Used to enable SSL/TLS for proxy | Optional | `false` |
29+
| `SSL_KEY` | Path to TLS key in the container | Required with SSL | |
30+
| `SSL_CERTIFICATE` | Path to TLS cert in the container | Required with SSL | |
31+
32+
33+
AWS identity can be passed:
34+
- using environment variables
35+
- using AWS credentials file (mounted in the container)
36+
- using WebIdentity Token (mounted in the container)
37+
- on AWS EC2 via metadata
38+
39+
If `AWS_REGION` is not set, it will be deduced from ECR URL.
40+
41+
| Environment Variable | Description | Status | Default |
42+
| :---------------------------------: | :--------------------------------------------: | :-------------------------------: | :--------: |
43+
| `AWS_REGION` | Region | Optional | |
44+
| `AWS_ACCESS_KEY_ID` | Access key | Optional | |
45+
| `AWS_SECRET_ACCESS_KEY` | Secret key | Optional | |
46+
47+
48+
### Example:
1449

1550
```sh
16-
docker run --rm --name docker-registry-proxy --net=host \
17-
-v /local-storage/cache:/cache \
51+
docker run -d --name docker-registry-proxy --net=host \
52+
-v /registry/local-storage/cache:/cache \
53+
-v /registry/certificate.pem:/opt/ssl/certificate.pem \
54+
-v /registry/key.pem:/opt/ssl/key.pem \
1855
-e PORT=5000 \
1956
-e RESOLVER=8.8.8.8 \
20-
-e UPSTREAM=https://XXXXXXXXXX.dkr.ecr.eu-central-1.amazonaws.com \
21-
-e AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY} \
22-
-e AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID} \
23-
-e AWS_REGION=${AWS_DEFAULT_REGION} \
57+
-e ECR=https://XXXXXXXXXX.dkr.ecr.eu-central-1.amazonaws.com \
2458
-e CACHE_MAX_SIZE=100g \
25-
esailors/aws-ecr-http-proxy:latest
59+
-e ENABLE_SSL=true \
60+
-e SSL_KEY=/opt/ssl/key.pem \
61+
-e SSL_CERTIFICATE=/opt/ssl/certificate.pem \
62+
aws-ecr-http-proxy:latest
2663
```
2764

2865
If you ran this command on "registry-proxy.example.com" you can now get your images using `docker pull registry-proxy.example.com:5000/repo/image`.
2966

30-
### Deploying the proxy
31-
Modify the ansible role variables according to your need and run the playbook as follow:
32-
```sh
33-
ansible-playbook -i hosts playbook-docker-registry-proxy.yaml
34-
```
35-
The docker registry for project is available [here](https://hub.docker.com/r/esailors/aws-ecr-http-proxy)
36-
37-
### Note
38-
The proxy has `HTTP` endpoint so in order to avoid docker client complaining about it either mark the registry host as insecure in your [deamon config](https://docs.docker.com/registry/insecure/) or add [SSL/TLS termination](https://docs.docker.com/registry/recipes/nginx)
67+
### Note on SSL/TLS
68+
The proxy is using `HTTP` (plain text) as default protocol for now. So in order to avoid docker client complaining either:
69+
- (**Recommended**) Enable SSL/TLS using `ENABLE_SSL` configuration. For that you will have to mount your **valid** certificate/key in the container and pass the paths using `SSL_*` variables.
70+
- Mark the registry host as insecure in your client [deamon config](https://docs.docker.com/registry/insecure/).

files/nginx.conf renamed to config/nginx/nginx.conf

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
user nginx;
2-
worker_processes 1;
3-
41
events {
52
worker_connections 1024;
63
}
74

5+
user nginx;
6+
worker_processes 1;
7+
8+
89
http {
910
include mime.types;
1011
default_type application/octet-stream;
1112

1213
keepalive_timeout 65;
1314
sendfile on;
1415

15-
proxy_cache_path /cache/cache levels=1:2 keys_zone=cache:16m inactive=1y max_size=CACHE_MAX_SIZE use_temp_path=off;
16-
resolver RESOLVER valid=30s;
16+
proxy_cache_path /cache/cache levels=1:2 keys_zone=cache:16m inactive=1y max_size=__CACHE_MAX_SIZE__ use_temp_path=off;
17+
resolver __RESOLVER__ valid=30s;
1718

1819
# this is necessary for us to be able to disable request buffering in all cases
1920
proxy_http_version 1.1;
2021

21-
#SSLCONFIG
22-
2322
# will run before forking out nginx worker processes
2423
init_by_lua_block { require "cjson" }
2524

@@ -29,9 +28,9 @@ http {
2928
}
3029

3130
server {
32-
listen LISTEN default_server;
31+
listen __PORT__ __SSL_LISTEN__ default_server;
3332

34-
#AUTHCONFIG
33+
__SSL_INCLUDE__
3534

3635
# Cache
3736
add_header X-Cache-Status $upstream_cache_status;
@@ -52,45 +51,37 @@ http {
5251

5352
# disable proxy request buffering
5453
proxy_request_buffering off;
55-
proxy_cache cache;
56-
proxy_cache_key $scheme$uri$args$request_method;
57-
proxy_cache_valid 200 1s;
58-
proxy_cache_use_stale error timeout invalid_header updating
59-
http_500 http_502 http_503 http_504;
60-
proxy_cache_lock on;
6154

6255
add_header 'Docker-Distribution-Api-Version' $docker_distribution_api_version always;
6356
add_header "Access-Control-Allow-Origin" "*";
6457

6558
location / {
66-
set $url UPSTREAM;
59+
set $url __ECR__;
6760
proxy_pass $url;
68-
proxy_redirect $url http://$host:PORT;
61+
proxy_redirect $url __SCHEME__://$host:__PORT__;
6962

7063
# Add AWS ECR authentication headers
7164
proxy_set_header X-Real-IP $remote_addr;
7265
proxy_set_header X-Forwarded-For $remote_addr;
7366
proxy_set_header X-Forwarded-User "Basic $http_authorization";
7467
proxy_set_header Authorization "Basic $http_authorization";
7568
proxy_set_header X-Forwarded-Proto $scheme;
76-
proxy_set_header Authorization "";
7769

7870
}
7971

8072
# Content addressable files like blobs.
8173
# https://docs.docker.com/registry/spec/api/#blob
8274
location ~ ^/v2/.*/blobs/[a-z0-9]+:[a-f0-9]+$ {
83-
set $url UPSTREAM;
75+
set $url __ECR__;
8476
proxy_pass $url;
85-
proxy_redirect $url http://$host:PORT;
77+
proxy_redirect $url __SCHEME__://$host:__PORT__;
8678

8779
# Add AWS ECR authentication headers
8880
proxy_set_header X-Real-IP $remote_addr;
8981
proxy_set_header X-Forwarded-For $remote_addr;
9082
proxy_set_header X-Forwarded-User "Basic $http_authorization";
9183
proxy_set_header Authorization "Basic $http_authorization";
9284
proxy_set_header X-Forwarded-Proto $scheme;
93-
proxy_set_header Authorization "";
9485

9586
# When accessing image blobs using HTTP GET AWS ECR redirects with
9687
# s3 buckets uri to download the image. This needs to handled by
@@ -106,7 +97,7 @@ http {
10697
set $saved_redirect_location '$upstream_http_location';
10798
proxy_pass $saved_redirect_location;
10899
proxy_cache cache;
109-
proxy_cache_key $scheme$uri$args$request_method;
100+
proxy_cache_key __CACHE_KEY__;
110101
proxy_cache_valid 200 1y;
111102
proxy_cache_use_stale error timeout invalid_header updating
112103
http_500 http_502 http_503 http_504;
@@ -115,7 +106,6 @@ http {
115106

116107
location ~ ^/v2/.*/.*/tags/list+$ {
117108
# get paginated list of tags
118-
proxy_set_header Authorization "";
119109
content_by_lua_block {
120110
local location, tags, cjson = ngx.var.uri, {}, require "cjson"
121111
while true do
@@ -146,8 +136,7 @@ http {
146136
location /get_tags {
147137
internal;
148138
set_unescape_uri $req_uri $arg_req_uri;
149-
proxy_pass UPSTREAM$req_uri;
150-
proxy_set_header Authorization "";
139+
proxy_pass __ECR__$req_uri;
151140

152141
# Add AWS ECR authentication headers
153142
proxy_set_header X-Real-IP $remote_addr;
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
ssl_certificate_key REGISTRY_HTTP_TLS_KEY;
2-
ssl_certificate REGISTRY_HTTP_TLS_CERTIFICATE;
1+
ssl_certificate_key __SSL_KEY__;
2+
ssl_certificate __SSL_CERTIFICATE__;
33

44
ssl_protocols TLSv1.2;
55
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
66
ssl_prefer_server_ciphers on;
77

88
add_header Strict-Transport-Security max-age=31536000;
9-
File renamed without changes.

files/renew_token.sh

Lines changed: 0 additions & 13 deletions
This file was deleted.

files/root

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)