Skip to content

Commit aaf7fa0

Browse files
committed
introduce env DEBUG_HUB; to help understand #54
- injects a mitmproxy between the caching layer and DockerHub's registry-1.docker.io - it also disables VERIFY_SSL so the mitmproxy can be used; - DEBUG_HUB's mitmweb is exposed on port 8082 - both DEBUG and DEBUG_HUB can be enabled independently
1 parent ffc7540 commit aaf7fa0

File tree

3 files changed

+50
-15
lines changed

3 files changed

+50
-15
lines changed

Dockerfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ RUN chmod +x /create_ca_cert.sh /entrypoint.sh
4848
# Clients should only use 3128, not anything else.
4949
EXPOSE 3128
5050

51-
# In debug mode, 8081 exposes the mitmweb interface.
51+
# In debug mode, 8081 exposes the mitmweb interface (for incoming requests from Docker clients)
5252
EXPOSE 8081
53+
# In debug-hub mode, 8082 exposes the mitmweb interface (for outgoing requests to DockerHub)
54+
EXPOSE 8082
5355

5456
## Default envs.
5557
# A space delimited list of registries we should proxy and cache; this is in addition to the central DockerHub.
@@ -60,6 +62,8 @@ ENV AUTH_REGISTRIES="some.authenticated.registry:oneuser:onepassword another.reg
6062
ENV VERIFY_SSL="true"
6163
# Enable debugging mode; this inserts mitmproxy/mitmweb between the CONNECT proxy and the caching layer
6264
ENV DEBUG="false"
65+
# Enable debugging mode; this inserts mitmproxy/mitmweb between the caching layer and DockerHub's registry
66+
ENV DEBUG_HUB="false"
6367
# Enable nginx debugging mode; this uses nginx-debug binary and enabled debug logging, which is VERY verbose so separate setting
6468
ENV DEBUG_NGINX="false"
6569

entrypoint.sh

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ done
3131
export ALLDOMAINS=${ALLDOMAINS:1} # remove the first comma and export
3232
/create_ca_cert.sh # This uses ALLDOMAINS to generate the certificates.
3333

34+
# Target host interception. Empty by default. Used to intercept outgoing requests
35+
# from the proxy to the registries.
36+
echo -n "" > /etc/nginx/docker.targetHost.map
37+
3438
# Now handle the auth part.
3539
echo -n "" > /etc/nginx/docker.auth.map
3640

@@ -63,19 +67,6 @@ if [ "$AUTH_REGISTRIES" ]; then
6367
done
6468
fi
6569

66-
echo "" > /etc/nginx/docker.verify.ssl.conf
67-
if [[ "a${VERIFY_SSL}" == "atrue" ]]; then
68-
cat << EOD > /etc/nginx/docker.verify.ssl.conf
69-
# We actually wanna be secure and avoid mitm attacks.
70-
# Fitting, since this whole thing is a mitm...
71-
# We'll accept any cert signed by a CA trusted by Mozilla (ca-certificates-bundle in alpine)
72-
proxy_ssl_verify on;
73-
proxy_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt;
74-
proxy_ssl_verify_depth 2;
75-
EOD
76-
echo "Upstream SSL certificate verification enabled."
77-
fi
78-
7970
# create default config for the caching layer to listen on 443.
8071
echo " listen 443 ssl default_server;" > /etc/nginx/caching.layer.listen
8172
echo "error_log /var/log/nginx/error.log warn;" > /etc/nginx/error.log.debug.warn
@@ -99,7 +90,7 @@ if [[ "a${DEBUG}" == "atrue" ]]; then
9990
# in debug mode, change caching layer to listen on 444, so that mitmproxy can sit in the middle.
10091
echo " listen 444 ssl default_server;" > /etc/nginx/caching.layer.listen
10192

102-
echo "Starting in DEBUG MODE (mitmproxy)."
93+
echo "Starting in DEBUG MODE (mitmproxy)." >&2
10394
echo "Run mitmproxy with reverse pointing to the same certs..."
10495
mitmweb --no-web-open-browser --web-iface 0.0.0.0 --web-port 8081 \
10596
--set keep_host_header=true --set ssl_insecure=true \
@@ -108,6 +99,28 @@ if [[ "a${DEBUG}" == "atrue" ]]; then
10899
echo "Access mitmweb via http://127.0.0.1:8081/ "
109100
fi
110101

102+
if [[ "a${DEBUG_HUB}" == "atrue" ]]; then
103+
if [[ ! -f /usr/bin/mitmweb ]]; then
104+
echo "To debug, you need the -debug version of this image, eg: :latest-debug"
105+
exit 3
106+
fi
107+
108+
# in debug hub mode, we remap targetHost to point to mitmproxy below
109+
echo "\"registry-1.docker.io\" \"127.0.0.1:445\";" > /etc/nginx/docker.targetHost.map
110+
111+
echo "Debugging outgoing DockerHub connections via mitmproxy on 8082." >&2
112+
# this one has keep_host_header=false so we don't need to modify nginx config
113+
mitmweb --no-web-open-browser --web-iface 0.0.0.0 --web-port 8082 \
114+
--set keep_host_header=false --set ssl_insecure=true \
115+
--mode reverse:https://registry-1.docker.io --listen-host 0.0.0.0 \
116+
--listen-port 445 --certs /certs/fullchain_with_key.pem &> /dev/null &
117+
118+
echo "Warning, DockerHub outgoing debugging disables upstream SSL verification for all upstreams." >&2
119+
VERIFY_SSL=false
120+
121+
echo "Access mitmweb for outgoing DockerHub requests via http://127.0.0.1:8082/ "
122+
fi
123+
111124
if [[ "a${DEBUG_NGINX}" == "atrue" ]]; then
112125
if [[ ! -f /usr/sbin/nginx-debug ]]; then
113126
echo "To debug, you need the -debug version of this image, eg: :latest-debug"
@@ -120,6 +133,23 @@ if [[ "a${DEBUG_NGINX}" == "atrue" ]]; then
120133
NGINX_BIN="/usr/sbin/nginx-debug"
121134
fi
122135

136+
# Upstream SSL verification.
137+
echo "" > /etc/nginx/docker.verify.ssl.conf
138+
if [[ "a${VERIFY_SSL}" == "atrue" ]]; then
139+
cat << EOD > /etc/nginx/docker.verify.ssl.conf
140+
# We actually wanna be secure and avoid mitm attacks.
141+
# Fitting, since this whole thing is a mitm...
142+
# We'll accept any cert signed by a CA trusted by Mozilla (ca-certificates-bundle in alpine)
143+
proxy_ssl_verify on;
144+
proxy_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt;
145+
proxy_ssl_verify_depth 2;
146+
EOD
147+
echo "Upstream SSL certificate verification enabled."
148+
else
149+
echo "Upstream SSL certificate verification is DISABLED."
150+
fi
151+
152+
123153
echo "Testing nginx config..."
124154
${NGINX_BIN} -t
125155

nginx.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ http {
8181
# Just in case you want to rewrite some hosts. Default maps directly.
8282
map $host $targetHost {
8383
hostnames;
84+
include /etc/nginx/docker.targetHost.map;
8485
default $host;
8586
}
8687

0 commit comments

Comments
 (0)