Skip to content

Commit a2f6808

Browse files
Add the CONFIG_JSON environment variable (#5)
* Add the CONFIG_JSON environment variable as an alternative to creating a file
1 parent 02ecaa6 commit a2f6808

File tree

4 files changed

+54
-12
lines changed

4 files changed

+54
-12
lines changed

.github/workflows/e2e_test.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ on:
88

99
jobs:
1010
test:
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
profile: ["proxy", "proxy2"]
1115
name: Test
1216
runs-on: "ubuntu-latest"
1317
steps:
1418
- uses: actions/checkout@v3
1519
- name: Run test server
1620
working-directory: ./test
17-
run: docker compose up --build --detach --wait --wait-timeout 30
21+
run: docker compose --profile ${{matrix.profile}} up --build --detach --wait --wait-timeout 60
1822
- name: querying http returns redirect
1923
run: |
2024
output=$(curl -s -o /dev/null -w "%{http_code}" http://localhost)
@@ -39,7 +43,7 @@ jobs:
3943
fi
4044
- name: Copy the SSL key
4145
working-directory: ./test
42-
run: docker compose cp proxy:/etc/reverse_proxy/data/certs/localhost/fullchain.pem .
46+
run: docker compose --profile ${{matrix.profile}} cp ${{matrix.profile}}:/etc/reverse_proxy/data/certs/localhost/fullchain.pem .
4347
- name: Querying the https route returns 200
4448
working-directory: ./test
4549
run: |

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ So. that's basically it :)
5353
- `SKIP_RENEW_CERTS=1` - don't call acme --install-cronjob to renew the certificates
5454
- `SKIP_WRITE_NGINX_CONF=1` - that /etc/reverse_proxy/nginx.conf is not overriden during the config process
5555
- `DEBUG=1` - add verbose logging (set -x) to figure out what's going wrong
56+
- `CONFIG_JSON={...}` - Instead of using a config.json file, you can instead set it as an environment variable instead
5657

5758
# Advanced configuration
5859

bootstrap.sh

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,22 @@ bootstrap_fn() {
1717
exit 1
1818
fi
1919

20-
# First, validate the config file
21-
if [ ! -f "$config_file" ]; then
22-
echo "Missing $config_file. Did you forget to mount the config file?"
20+
if [ -z "${CONFIG_JSON:-}" ]; then
21+
if [ -f "$config_file" ]; then
22+
CONFIG_JSON=$(cat "$config_file")
23+
else
24+
echo "Missing $config_file. Did you forget to mount the config file?"
25+
exit 1
26+
fi
27+
fi
28+
29+
echo "$CONFIG_JSON" | jq empty 2>/dev/null
30+
if [ $? -ne 0 ]; then
31+
echo "Failed to parse the config file"
2332
exit 1
2433
fi
2534

26-
num_domains=$(jq -e -r '.domains | length' "$config_file")
35+
num_domains=$(echo "$CONFIG_JSON" | jq -e -r '.domains | length')
2736
if [ $? -ne 0 ] || [ "$num_domains" -lt 1 ]; then
2837
echo "No domains listed in the config"
2938
exit 1
@@ -39,9 +48,9 @@ bootstrap_fn() {
3948
else
4049
# Install acme.sh with the email in the config, ensure the account_thumbprint
4150
if [ ! -d "$acme_dir" ]; then
42-
email=$(jq -e -r '.email' "$config_file")
51+
email=$(echo "$CONFIG_JSON" | jq -e -r '.email')
4352
if [ $? -ne 0 ]; then
44-
echo "$config_file is missing the email to use when registering the SSL certificates"
53+
echo "The config is missing the email to use when registering the SSL certificates"
4554
exit 1
4655
fi
4756
echo "Installing acme.sh"
@@ -74,8 +83,8 @@ bootstrap_fn() {
7483
echo "Creating the self-signed certificate"
7584

7685
mkdir -p "$cert_dir" || exit 1
77-
subject=$(jq -e -r '.domains[0].name' "$config_file")
78-
alt_names=$(jq -e -r '.domains | map([.name] + .aliases) | flatten | map("DNS:" + .) | join(",")' "$config_file")
86+
subject=$(echo "$CONFIG_JSON" | jq -e -r '.domains[0].name')
87+
alt_names=$(echo "$CONFIG_JSON" | jq -e -r '.domains | map([.name] + .aliases) | flatten | map("DNS:" + .) | join(",")')
7988
echo "subject: $subject"
8089
echo "alt_names: $alt_names"
8190
openssl req -x509 -newkey rsa:4096 -sha256 -days 365 -nodes \
@@ -85,7 +94,7 @@ bootstrap_fn() {
8594
-addext "subjectAltName=$alt_names" || exit 1
8695
fi
8796

88-
domains=$(jq -e -r '.domains[].name' "$config_file")
97+
domains=$(echo "$CONFIG_JSON" | jq -e -r '.domains[].name')
8998
# Note that this script assumes that the config.json is trusted input
9099
# and the domain doesn't have e.g. ../../ in it
91100
for domain in $domains; do
@@ -105,7 +114,7 @@ bootstrap_fn() {
105114
cat /dev/null > "$data_dir/nginx_generated.conf"
106115
i=0
107116
while [ "$i" -lt "$num_domains" ]; do
108-
domain_json=$(jq -e ".domains[$i]" "$config_file")
117+
domain_json=$(echo "$CONFIG_JSON" | jq -e ".domains[$i]")
109118
domain=$(echo "$domain_json" | jq -e -r '.name')
110119
if [ $? -ne 0 ]; then
111120
echo "Failed to get the name for $domain_json"

test/docker-compose.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
services:
22
proxy:
3+
profiles:
4+
- proxy
35
build:
46
context: ./reverse-proxy
57
volumes:
@@ -19,8 +21,34 @@ services:
1921
timeout: 5s
2022
interval: 5s
2123
retries: 6
24+
proxy2:
25+
profiles:
26+
- proxy2
27+
build:
28+
context: ./reverse-proxy
29+
volumes:
30+
- reverse-proxy-test:/etc/reverse_proxy/data
31+
environment:
32+
- SKIP_CREATE_CERTS=1
33+
- SKIP_RENEW_CERTS=1
34+
- DEBUG=1
35+
- 'CONFIG_JSON={ "email": "test@example.com", "domains": [ { "name": "localhost", "dest": "http://hello:80" } ] }'
36+
37+
ports:
38+
- 80:80
39+
- 443:443
40+
networks:
41+
- web
42+
healthcheck:
43+
test: ['CMD-SHELL', 'curl -so /dev/null http://localhost/ || exit 1']
44+
timeout: 5s
45+
interval: 5s
46+
retries: 6
2247

2348
hello:
49+
profiles:
50+
- proxy
51+
- proxy2
2452
image: nginxdemos/hello:plain-text
2553
networks:
2654
- web

0 commit comments

Comments
 (0)