Skip to content

Commit 741a5e3

Browse files
Merge pull request #9 from Scalingo/feat/target_scheme
feat: multiple fixes and features
2 parents 9db22a6 + 382dd53 commit 741a5e3

14 files changed

+464
-73
lines changed

.github/workflows/docker-image.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
name: CI
1+
name: "build-docker-image"
22

33
on:
44
push:
55
tags:
6-
- 'v[0-9]+.[0-9]+.[0-9]+*'
6+
- "v[0-9]+.[0-9]+.[0-9]+*"
77

88
jobs:
99
bake:

.github/workflows/tests.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: "tests"
2+
3+
on:
4+
pull_request_target: # Use pull_request_target
5+
branches: [main]
6+
7+
jobs:
8+
run-specs:
9+
name: run specs
10+
runs-on: ubuntu-22.04
11+
steps:
12+
- name: deploy
13+
run: ./vagrant-deploy.sh
14+
- name: tests
15+
run: ./vagrant-tests.sh

.markdownlint.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Default state for all rules
2+
default: true
3+
4+
# MD013/line-length - Line length
5+
MD013:
6+
line_length: 200

Dockerfile

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ ARG BASE_IMAGE="openresty-proxy-connect"
22

33
FROM ${BASE_IMAGE}
44

5-
ADD generate-certificate.sh /generate-certificate.sh
6-
ADD entrypoint.sh /entrypoint.sh
5+
COPY generate-certificate.sh /generate-certificate.sh
6+
COPY entrypoint.sh /entrypoint.sh
77

8-
ADD nginx.conf /opt/openresty/nginx/conf/nginx.conf
9-
ADD nginx.manifest.common.conf /opt/openresty/nginx/conf/nginx.manifest.common.conf
10-
ADD nginx.manifest.stale.conf /opt/openresty/nginx/conf/nginx.manifest.stale.conf
11-
ADD proxy_auth.lua /opt/openresty/nginx/conf/proxy_auth.lua
8+
COPY nginx.conf /opt/openresty/nginx/conf/nginx.conf
9+
COPY nginx.manifest.common.conf /opt/openresty/nginx/conf/nginx.manifest.common.conf
10+
COPY nginx.manifest.stale.conf /opt/openresty/nginx/conf/nginx.manifest.stale.conf
11+
COPY proxy_auth.lua /opt/openresty/nginx/conf/proxy_auth.lua
1212

1313
RUN apk add --no-cache --update bash openssl \
1414
&& mkdir -p /docker_mirror_cache /certs /opt/openresty/nginx/tmp \
@@ -79,6 +79,9 @@ ENV PROXY_CONNECT_SEND_TIMEOUT="60s"
7979
# Allow disabling IPV6 resolution, default to false
8080
ENV DISABLE_IPV6="false"
8181

82+
# Forbid unknown registries
83+
ENV ALLOW_UNKNOWN_REGISTRIES="false"
84+
8285
USER 1001
8386

8487
# Did you want a shell? Sorry, the entrypoint never returns, because it runs nginx itself. Use 'docker exec' if you need to mess around internally.

Dockerfile.openresty

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,37 @@ ARG NGX_HTTP_PROXY_CONNECT_MODULE_VERSION=0.0.7
66
ARG OPENRESTY_VERSION=1.27.1.2
77
ARG PATCH_VERSION=proxy_connect_rewrite_102101.patch
88

9+
ARG OPENRESTY_HASH=74f076f7e364b2a99a6c5f9bb531c27610c78985abe956b442b192a2295f7548
10+
ARG NGX_HTTP_PROXY_CONNECT_MODULE_HASH=b1502309b0afc3e24bdfb02aa8e7028014d06f3f4e3c99a4a867de81d9c0e47d
11+
912
RUN apk add --no-cache --update --virtual .build-deps gcc libc-dev make openssl-dev pcre-dev zlib-dev linux-headers patch curl git perl \
1013
&& curl -fSL https://github.com/chobits/ngx_http_proxy_connect_module/archive/refs/tags/v${NGX_HTTP_PROXY_CONNECT_MODULE_VERSION}.tar.gz -o ngx_http_proxy_connect_module.tar.gz \
14+
&& (echo "${NGX_HTTP_PROXY_CONNECT_MODULE_HASH} ngx_http_proxy_connect_module.tar.gz" | sha256sum -c -s) \
1115
&& mkdir -p /usr/src \
1216
&& tar zxC /usr/src/ -f ngx_http_proxy_connect_module.tar.gz \
1317
&& curl -fSL https://openresty.org/download/openresty-${OPENRESTY_VERSION}.tar.gz -o openresty.tar.gz \
18+
&& (echo "${OPENRESTY_HASH} openresty.tar.gz" | sha256sum -c -s) \
1419
&& tar zxC /usr/src/ -f openresty.tar.gz \
1520
&& patch -d /usr/src/openresty-${OPENRESTY_VERSION}/bundle/nginx-${OPENRESTY_VERSION%.*}/ -p 1 < /usr/src/ngx_http_proxy_connect_module-${NGX_HTTP_PROXY_CONNECT_MODULE_VERSION}/patch/${PATCH_VERSION} \
1621
&& cd /usr/src/openresty-${OPENRESTY_VERSION} \
1722
&& ./configure --prefix=/opt/openresty \
18-
--without-http_ssi_module \
19-
--without-http_userid_module \
20-
--without-mail_pop3_module \
21-
--without-mail_imap_module \
22-
--without-mail_smtp_module \
23-
--with-http_sub_module \
24-
--with-http_ssl_module \
25-
--with-http_v2_module \
26-
--with-http_gzip_static_module \
27-
--with-http_gunzip_module \
28-
--with-http_realip_module \
29-
--with-http_stub_status_module \
30-
--with-select_module \
31-
--with-poll_module \
32-
--with-file-aio \
33-
--with-pcre-jit \
34-
--add-module=/usr/src/ngx_http_proxy_connect_module-${NGX_HTTP_PROXY_CONNECT_MODULE_VERSION} \
23+
--without-http_ssi_module \
24+
--without-http_userid_module \
25+
--without-mail_pop3_module \
26+
--without-mail_imap_module \
27+
--without-mail_smtp_module \
28+
--with-http_sub_module \
29+
--with-http_ssl_module \
30+
--with-http_v2_module \
31+
--with-http_gzip_static_module \
32+
--with-http_gunzip_module \
33+
--with-http_realip_module \
34+
--with-http_stub_status_module \
35+
--with-select_module \
36+
--with-poll_module \
37+
--with-file-aio \
38+
--with-pcre-jit \
39+
--add-module=/usr/src/ngx_http_proxy_connect_module-${NGX_HTTP_PROXY_CONNECT_MODULE_VERSION} \
3540
&& make install \
3641
&& strip /opt/openresty/nginx/sbin/nginx \
3742
&& ln -sf /dev/stdout /opt/openresty/nginx/logs/access.log \

README.md

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
# Build with bake
1+
# Docker registry proxy
2+
3+
## Build with bake
4+
25
```bash
36
docker buildx bake --file docker-bake.hcl
47
```
58

6-
# Create .env file
9+
## Create .env file
10+
711
```bash
812
cp .env.sample .env
913
vim .env
1014
```
1115

12-
# Run with docker
16+
## Run with docker
17+
1318
```bash
1419
docker run --name openresty_docker_registry_proxy \
1520
--rm -it \
@@ -20,31 +25,48 @@ docker run --name openresty_docker_registry_proxy \
2025
docker-registry-proxy-cache:latest
2126
```
2227

23-
# Run with docker compose
28+
## Run with docker compose
29+
2430
```bash
2531
docker compose up
2632
```
2733

2834
The `HTPASSWD` environment variable activates basic authentication. In this example, we define two users:
29-
* user1:user1
30-
* user2:user2
35+
36+
* user1:password1
37+
* user2:password2
3138

3239
The `HTPASSWD_DELIMITER` environment variable can be used to specify a custom delimiter. By default, a `space` is used.
3340

3441
By default, `generate-certificate.sh` generates a self-signed certificate. You can override this by mounting a volume with your own certificates at `/certs`.
42+
3543
* server.crt
3644
* server.key
45+
* proxy_server.crt
46+
* proxy_server.key
47+
48+
## Configure docker to use a proxy
3749

38-
# Configure docker to use a proxy
3950
```json
4051
{
4152
...
4253
"proxies": {
43-
"http-proxy": "http://user1:user1@127.0.0.1:3128",
44-
"https-proxy": "http://user1:user1@127.0.0.1:3128"
54+
"https-proxy": "https://user1:user1@127.0.0.1:3128"
4555
},
4656
"insecure-registries" : ["own-registry.sample.com:443"]
4757
}
4858
```
4959

5060
The `insecure-registries` setting should be configured if your proxy is using an invalid or self-signed certificate.
61+
62+
## Run a test environment
63+
64+
Vagrant will create a VM and will run 2 scripts:
65+
66+
* vagrant-deploy.sh => Execute the complete deployment process, including package installation, certificate generation, Docker image builds, and container execution.
67+
* vagrant-tests.sh => Run integration tests
68+
69+
```bash
70+
vagrant up --provision docker-registry-cache
71+
vagrant ssh docker-registry-cache
72+
```

Vagrantfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Vagrant.configure('2') do |config|
2+
config.vm.box = 'ubuntu/focal64'
3+
4+
config.vm.define 'docker-registry-cache' do |node|
5+
node.vm.hostname = 'docker-registry-cache'
6+
node.vm.provision 'shell', inline: '/vagrant/tests/vagrant-deploy.sh'
7+
node.vm.provision 'shell', inline: '/vagrant/tests/vagrant-tests.sh', run: 'always'
8+
node.vm.provider 'virtualbox' do |v|
9+
v.memory = 2048
10+
end
11+
end
12+
end

entrypoint.sh

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,39 @@ if [[ ${AUTH_REGISTRIES+x} ]]; then
9090
done
9191
fi
9292

93-
# create default config for the caching layer to listen on 8443.
94-
echo " listen 8443 ssl default_server;" >/opt/openresty/nginx/conf/caching.layer.listen
93+
# Target scheme interception. Used to force the http scheme of a registry host
94+
echo -n "" >/opt/openresty/nginx/conf/docker.targetScheme.map
95+
96+
if [[ ${FORCE_HTTP_REGISTRIES+x} ]]; then
97+
for ONEREGISTRYIN in ${FORCE_HTTP_REGISTRIES}; do
98+
ONEREGISTRY=$(echo ${ONEREGISTRYIN} | xargs) # Remove whitespace
99+
echo "${ONEREGISTRY} http;" >>/opt/openresty/nginx/conf/docker.targetScheme.map
100+
done
101+
fi
102+
103+
# Enforce ssl
104+
echo -n "" > /opt/openresty/nginx/conf/ssl.conf
105+
if [[ ${SSL_CIPHERS_LIST+x} ]]; then
106+
# Most secured
107+
echo "ssl_ciphers ${SSL_CIPHERS_LIST};" >> /opt/openresty/nginx/conf/ssl.conf
108+
fi
109+
if [[ ${SSL_PROTOCOLS_LIST+x} ]]; then
110+
# Most secured
111+
echo "ssl_protocols ${SSL_PROTOCOLS_LIST};" >> /opt/openresty/nginx/conf/ssl.conf
112+
fi
113+
114+
# Forbid unknown registries
115+
echo -n "" > /opt/openresty/nginx/conf/forbid_unknown_registries.conf
116+
if [[ "a${ALLOW_UNKNOWN_REGISTRIES}" != "atrue" ]]; then
117+
echo 'if ($interceptedHost != "127.0.0.1:8443") {' >> /opt/openresty/nginx/conf/forbid_unknown_registries.conf
118+
echo ' return 403 "docker-registry-proxy: remote request not authorized!";' >> /opt/openresty/nginx/conf/forbid_unknown_registries.conf
119+
echo '}' >> /opt/openresty/nginx/conf/forbid_unknown_registries.conf
120+
#cat >> /opt/openresty/nginx/conf/forbid_unknown_registries.conf <<-"EOF"
121+
#if ($interceptedHost != '127.0.0.1:8443') {
122+
# return 403 "docker-registry-proxy: remote request not authorized!";
123+
#}
124+
#EOF
125+
fi
95126

96127
# Set Docker Registry cache size, by default, 32 GB ('32g')
97128
CACHE_MAX_SIZE=${CACHE_MAX_SIZE:-32g}

generate-certificate.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,12 @@ if [ ! -f /certs/server.crt ]; then
88
-subj "/C=FR/ST=Grand-Est/L=Strasbourg/O=Scalingo/OU=IT Department/CN=scalingo.com"
99
openssl x509 -req -days 365 -in /certs/server.csr -signkey /certs/server.key -out /certs/server.crt
1010
fi
11+
12+
if [ ! -f /certs/proxy_server.crt ]; then
13+
openssl genrsa -des3 -passout pass:x -out /certs/proxy_server.pass.key 2048
14+
openssl rsa -passin pass:x -in /certs/proxy_server.pass.key -out /certs/proxy_server.key
15+
rm /certs/proxy_server.pass.key
16+
openssl req -new -key /certs/proxy_server.key -out /certs/proxy_server.csr \
17+
-subj "/C=FR/ST=Grand-Est/L=Strasbourg/O=Scalingo/OU=IT Department/CN=scalingo.com"
18+
openssl x509 -req -days 365 -in /certs/proxy_server.csr -signkey /certs/proxy_server.key -out /certs/proxy_server.crt
19+
fi

0 commit comments

Comments
 (0)