Skip to content

Commit 7777ff2

Browse files
authored
Merge pull request #89 from Joxit/custom-headers
Supports custom headers when the ui is used as proxy ## Background Headers can be useful in some cases such as avoid sending credentials when you are on the UI (like #87). Or give to the registry server other properties such as `X-Forward-For` or `Server` headers for monitoring. ## How to use ? This is compatible only with static version of the UI and used with `REGISTRY_URL` variable. When you want to add a custom header, add to the registry ui a environment variable or entry in `/etc/nginx/.env` which looks like `NGINX_PROXY_HEADER_Custom_Header`. All underscores (`_`) will be replaced by hyphens (`-`). Some example of custom headers as variable: - `NGINX_PROXY_HEADER_Authorization` for Basic auth credentials - `NGINX_PROXY_HEADER_X_Forwarded_For` for identifying the originating IP address of a client An example is bundled with this PR closes: #87
2 parents 1321d9b + 4fee7b4 commit 7777ff2

File tree

8 files changed

+97
-4
lines changed

8 files changed

+97
-4
lines changed

bin/entrypoint

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/sh
2-
$@
2+
33
sed -i "s,\${URL},${URL}," scripts/docker-registry-ui.js
44
sed -i "s,\${REGISTRY_TITLE},${REGISTRY_TITLE}," scripts/docker-registry-ui.js
55
sed -i "s,\${PULL_URL},${PULL_URL}," scripts/docker-registry-ui.js
@@ -8,13 +8,31 @@ if [ -z "${DELETE_IMAGES}" ] || [ "${DELETE_IMAGES}" = false ] ; then
88
sed -i -r "s/(isImageRemoveActivated[:=])[^,;]*/\1false/" scripts/docker-registry-ui.js
99
fi
1010

11+
get_nginx_proxy_headers() {
12+
(
13+
env &&
14+
if [ -f "/etc/nginx/.env" ]; then
15+
cat /etc/nginx/.env
16+
# Force new line
17+
echo ""
18+
fi
19+
) | while read e; do
20+
if [ -n "$(echo $e | grep -o '^NGINX_PROXY_HEADER_')" ]; then
21+
key=$(echo ${e%%=*} | sed 's/^NGINX_PROXY_HEADER_//' | sed 's/_/-/g')
22+
value=${e#*=}
23+
echo -n "proxy_set_header ${key} \"${value}\"; "
24+
fi
25+
done
26+
}
27+
1128
if [ -n "${REGISTRY_URL}" ] ; then
1229
sed -i "s,\${REGISTRY_URL},${REGISTRY_URL}," /etc/nginx/conf.d/default.conf
30+
sed -i "s^\${NGINX_PROXY_HEADERS}^$(get_nginx_proxy_headers)^" /etc/nginx/conf.d/default.conf
1331
sed -i "s,#!,," /etc/nginx/conf.d/default.conf
1432
fi
1533

1634
if [ -z "$@" ]; then
17-
nginx -g "daemon off;"
35+
exec nginx -g "daemon off;"
1836
else
19-
$@
37+
exec $@
2038
fi

examples/proxy-headers/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Set custom headers to the registry example
2+
3+
The interface and the docker registry will be accessible with <http://localhost>.
4+
5+
This example highlight the usage of custom headers when the UI is used as a proxy. When you wants to use a header name with hyphens, replace them by underscores in the variable. You can put headers in environment variable or in config file `/etc/nginx/.env`. They have the same writing style.
6+
7+
Headers can be useful in some cases such as avoid sending credentials when you are on the UI. Or give to the registry server other properties such as X-Forward-For header.
8+
9+
I will set these two headers in this example. X-Forward-For by environment variable and Authorization by file.
10+
11+
In order to set your credentials in the header, you need to know how [Authorization](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization) header works. Here we use the `Basic` authentication scheme, the credentials are constructed like this:
12+
- The username and the password are combined with a colon (`registry:ui`).
13+
- The resulting string is base64 encoded (`cmVnaXN0cnk6dWk=`). You can simply run `echo -n "registry:ui" | base64`.
14+
- In your header, put this value `Basic cmVnaXN0cnk6dWk=`
15+
- In your `/etc/nginx/.env`, the file will contains `NGINX_PROXY_HEADER_Authorization=Basic cmVnaXN0cnk6dWk=`
16+
17+
For X-Forward-For, replace all hyphens by underscores, and the value will be a nginx variable which is `$proxy_add_x_forwarded_for`. In your docker compose you will need to duplicate the `$` character. In your docker-compose, your environment will look like `NGINX_PROXY_HEADER_X_Forwarded_For=$$proxy_add_x_forwarded_for`
18+
19+
As usual, run the project with `docker-compose up -d` (for background mode)
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
version: '2.0'
2+
services:
3+
registry:
4+
image: registry:2.7
5+
volumes:
6+
- ./registry-data:/var/lib/registry
7+
- ./registry-config/credentials.yml:/etc/docker/registry/config.yml
8+
- ./registry-config/htpasswd:/etc/docker/registry/htpasswd
9+
networks:
10+
- registry-ui-net
11+
12+
ui:
13+
image: joxit/docker-registry-ui:static
14+
ports:
15+
- 80:80
16+
environment:
17+
- REGISTRY_TITLE=My Private Docker Registry
18+
- REGISTRY_URL=http://registry:5000
19+
- NGINX_PROXY_HEADER_X_Forwarded_For=$$proxy_add_x_forwarded_for
20+
volumes:
21+
- ./nginx.env:/etc/nginx/.env
22+
depends_on:
23+
- registry
24+
networks:
25+
- registry-ui-net
26+
27+
networks:
28+
registry-ui-net:

examples/proxy-headers/nginx.env

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NGINX_PROXY_HEADER_Authorization=Basic cmVnaXN0cnk6dWk=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
version: 0.1
2+
log:
3+
fields:
4+
service: registry
5+
storage:
6+
delete:
7+
enabled: true
8+
cache:
9+
blobdescriptor: inmemory
10+
filesystem:
11+
rootdirectory: /var/lib/registry
12+
http:
13+
addr: :5000
14+
headers:
15+
X-Content-Type-Options: [nosniff]
16+
Access-Control-Allow-Origin: ['http://localhost']
17+
Access-Control-Allow-Methods: ['HEAD', 'GET', 'OPTIONS', 'DELETE']
18+
Access-Control-Allow-Headers: ['Authorization']
19+
Access-Control-Max-Age: [1728000]
20+
Access-Control-Allow-Credentials: [true]
21+
Access-Control-Expose-Headers: ['Docker-Content-Digest']
22+
auth:
23+
htpasswd:
24+
realm: basic-realm
25+
path: /etc/docker/registry/htpasswd
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
registry:$2y$11$1bmuJLK8HrQl5ACS/WeqRuJLUArUZfUcP2R23asmozEpfN76.pCHy

nginx/default.conf

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ server {
2525
#! return 404;
2626
#! }
2727
#! proxy_set_header Host $http_host;
28+
#! ${NGINX_PROXY_HEADERS}
2829
#! proxy_pass ${REGISTRY_URL};
2930
#! }
3031

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"dependencies": {},
1515
"devDependencies": {
1616
"del": "^3.0.0",
17-
"gulp": "^4.0.1",
17+
"gulp": "^4.0.2",
1818
"gulp-clean-css": "^4.2.0",
1919
"gulp-concat": "^2.6.0",
2020
"gulp-filter": "^5.1.0",

0 commit comments

Comments
 (0)