Skip to content

Commit 8e43333

Browse files
author
ricardop
committed
Merge branch 'pull/16'
2 parents bb8ab75 + df8c057 commit 8e43333

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ for this to work it requires inserting a root CA certificate into system trusted
2121
- Map volume `/docker_mirror_cache` for up to 32gb of cached images from all registries
2222
- Map volume `/ca`, the proxy will store the CA certificate here across restarts
2323
- Env `REGISTRIES`: space separated list of registries to cache; no need to include Docker Hub, its already there.
24-
- Env `AUTH_REGISTRIES`: space separated list of `hostname:username:password` authentication info.
24+
- Env `AUTH_REGISTRIES`: space separated list of `hostname:username:password` authentication info.
2525
- `hostname`s listed here should be listed in the REGISTRIES environment as well, so they can be intercepted.
2626
- For Docker Hub authentication, `hostname` should be `auth.docker.io`, username should NOT be an email, use the regular username.
2727
- For regular registry auth (HTTP Basic), `hostname` here should be the same... unless your registry uses a different auth server. This should work for quay.io also, but I have no way to test.
28-
- For Google Container Registry (GCR), username should be `_json_key` and the password should be the contents of the service account JSON. Check out [GCR docs](https://cloud.google.com/container-registry/docs/advanced-authentication#json_key_file)
29-
28+
- Env `AUTH_REGISTRIES_DELIMITER` to change the separator between authentication info. By default, a space: "` `". If you use keys that contain spaces (as with Google Cloud Registry), you should update this variable, e.g. setting it to `AUTH_REGISTRIES_DELIMITER=";;;"`. In that case, `AUTH_REGISTRIES` could contain something like `registry1.com:user1:pass1;;;registry2.com:user2:pass2`.
29+
- Env `AUTH_REGISTRY_DELIMITER` to change the separator between authentication info *parts*. By default, a colon: "`:`". If you use keys that contain single colons, you should update this variable, e.g. setting it to `AUTH_REGISTRIES_DELIMITER=":::"`. In that case, `AUTH_REGISTRIES` could contain something like `registry1.com:::user1:::pass1 registry2.com:::user2:::pass2`.
30+
- For Google Container Registry (GCR), username should be `_json_key` and the password should be the contents of the service account JSON. Check out [GCR docs](https://cloud.google.com/container-registry/docs/advanced-authentication#json_key_file). The service account key is in JSON format, it contains spaces ("` `") and colons ("`:`"). To be able to use GCR you should set `AUTH_REGISTRIES_DELIMITER` to something different than space (e.g. `AUTH_REGISTRIES_DELIMITER=";;;"`) and `AUTH_REGISTRY_DELIMITER` to something different than a single colon (e.g. `AUTH_REGISTRY_DELIMITER=":::"`).
3031

3132
```bash
3233
docker run --rm --name docker_registry_proxy -it \
@@ -38,6 +39,20 @@ docker run --rm --name docker_registry_proxy -it \
3839
rpardini/docker-registry-proxy:0.2.4
3940
```
4041

42+
Example with GCR using credentials from a service account from a key file `servicekey.json`:
43+
44+
```bash
45+
docker run --rm --name docker_registry_proxy -it \
46+
-p 0.0.0.0:3128:3128 \
47+
-v $(pwd)/docker_mirror_cache:/docker_mirror_cache \
48+
-v $(pwd)/docker_mirror_certs:/ca \
49+
-e REGISTRIES="k8s.gcr.io gcr.io quay.io your.own.registry another.public.registry" \
50+
-e AUTH_REGISTRIES_DELIMITER=";;;" \
51+
-e AUTH_REGISTRY_DELIMITER=":::" \
52+
-e AUTH_REGISTRIES="gcr.io:::_json_key:::$(cat servicekey.json);;;auth.docker.io:::dockerhub_username:::dockerhub_password" \
53+
rpardini/docker-registry-proxy:0.2.4
54+
```
55+
4156
Let's say you did this on host `192.168.66.72`, you can then `curl http://192.168.66.72:3128/ca.crt` and get the proxy CA certificate.
4257

4358
#### Configuring the Docker clients / Kubernetes nodes

entrypoint.sh

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,35 @@ export ALLDOMAINS=${ALLDOMAINS:1} # remove the first comma and export
3434
# Now handle the auth part.
3535
echo -n "" > /etc/nginx/docker.auth.map
3636

37-
for ONEREGISTRYIN in ${AUTH_REGISTRIES}; do
38-
ONEREGISTRY=$(echo -n ${ONEREGISTRYIN} | xargs) # Remove whitespace
39-
AUTH_HOST=$(echo -n ${ONEREGISTRY} | cut -d ":" -f 1 | xargs)
40-
AUTH_USER=$(echo -n ${ONEREGISTRY} | cut -d ":" -f 2 | xargs)
41-
AUTH_PASS=$(echo -n ${ONEREGISTRY} | cut -d ":" -f 3 | xargs)
42-
AUTH_BASE64=$(echo -n ${AUTH_USER}:${AUTH_PASS} | base64 | xargs)
43-
echo "Adding Auth for registry '${AUTH_HOST}' with user '${AUTH_USER}'."
44-
echo "\"${AUTH_HOST}\" \"${AUTH_BASE64}\";" >> /etc/nginx/docker.auth.map
45-
done
37+
# Only configure auth registries if the env var contains values
38+
if [ "$AUTH_REGISTRIES" ]; then
39+
# Ref: https://stackoverflow.com/a/47633817/219530
40+
AUTH_REGISTRIES_DELIMITER=${AUTH_REGISTRIES_DELIMITER:-" "}
41+
s=$AUTH_REGISTRIES$AUTH_REGISTRIES_DELIMITER
42+
auth_array=();
43+
while [[ $s ]]; do
44+
auth_array+=( "${s%%"$AUTH_REGISTRIES_DELIMITER"*}" );
45+
s=${s#*"$AUTH_REGISTRIES_DELIMITER"};
46+
done
47+
48+
AUTH_REGISTRY_DELIMITER=${AUTH_REGISTRY_DELIMITER:-":"}
49+
50+
for ONEREGISTRY in "${auth_array[@]}"; do
51+
s=$ONEREGISTRY$AUTH_REGISTRY_DELIMITER
52+
registry_array=();
53+
while [[ $s ]]; do
54+
registry_array+=( "${s%%"$AUTH_REGISTRY_DELIMITER"*}" );
55+
s=${s#*"$AUTH_REGISTRY_DELIMITER"};
56+
done
57+
AUTH_HOST="${registry_array[0]}"
58+
AUTH_USER="${registry_array[1]}"
59+
AUTH_PASS="${registry_array[2]}"
60+
# make base64 not wrap lines: https://superuser.com/a/1225334
61+
AUTH_BASE64=$(echo -n "${AUTH_USER}:${AUTH_PASS}" | base64 | tr -d \\n )
62+
echo "Adding Auth for registry '${AUTH_HOST}' with user '${AUTH_USER}'."
63+
echo "\"${AUTH_HOST}\" \"${AUTH_BASE64}\";" >> /etc/nginx/docker.auth.map
64+
done
65+
fi
4666

4767
echo "" > /etc/nginx/docker.verify.ssl.conf
4868
if [[ "a${VERIFY_SSL}" == "atrue" ]]; then

0 commit comments

Comments
 (0)