Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the CONFIG_JSON environment variable #5

Merged
merged 7 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .github/workflows/e2e_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ on:

jobs:
test:
strategy:
fail-fast: false
matrix:
profile: ["proxy", "proxy2"]
name: Test
runs-on: "ubuntu-latest"
steps:
- uses: actions/checkout@v3
- name: Run test server
working-directory: ./test
run: docker compose up --build --detach --wait --wait-timeout 30
run: docker compose --profile ${{matrix.profile}} up --build --detach --wait --wait-timeout 60
- name: querying http returns redirect
run: |
output=$(curl -s -o /dev/null -w "%{http_code}" http://localhost)
Expand All @@ -39,7 +43,7 @@ jobs:
fi
- name: Copy the SSL key
working-directory: ./test
run: docker compose cp proxy:/etc/reverse_proxy/data/certs/localhost/fullchain.pem .
run: docker compose --profile ${{matrix.profile}} cp ${{matrix.profile}}:/etc/reverse_proxy/data/certs/localhost/fullchain.pem .
- name: Querying the https route returns 200
working-directory: ./test
run: |
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ So. that's basically it :)
- `SKIP_RENEW_CERTS=1` - don't call acme --install-cronjob to renew the certificates
- `SKIP_WRITE_NGINX_CONF=1` - that /etc/reverse_proxy/nginx.conf is not overriden during the config process
- `DEBUG=1` - add verbose logging (set -x) to figure out what's going wrong
- `CONFIG_JSON={...}` - Instead of using a config.json file, you can instead set it as an environment variable instead

# Advanced configuration

Expand Down
29 changes: 19 additions & 10 deletions bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,22 @@ bootstrap_fn() {
exit 1
fi

# First, validate the config file
if [ ! -f "$config_file" ]; then
echo "Missing $config_file. Did you forget to mount the config file?"
if [ -z "${CONFIG_JSON:-}" ]; then
if [ -f "$config_file" ]; then
CONFIG_JSON=$(cat "$config_file")
else
echo "Missing $config_file. Did you forget to mount the config file?"
exit 1
fi
fi

echo "$CONFIG_JSON" | jq empty 2>/dev/null
if [ $? -ne 0 ]; then
echo "Failed to parse the config file"
exit 1
fi

num_domains=$(jq -e -r '.domains | length' "$config_file")
num_domains=$(echo "$CONFIG_JSON" | jq -e -r '.domains | length')
if [ $? -ne 0 ] || [ "$num_domains" -lt 1 ]; then
echo "No domains listed in the config"
exit 1
Expand All @@ -39,9 +48,9 @@ bootstrap_fn() {
else
# Install acme.sh with the email in the config, ensure the account_thumbprint
if [ ! -d "$acme_dir" ]; then
email=$(jq -e -r '.email' "$config_file")
email=$(echo "$CONFIG_JSON" | jq -e -r '.email')
if [ $? -ne 0 ]; then
echo "$config_file is missing the email to use when registering the SSL certificates"
echo "The config is missing the email to use when registering the SSL certificates"
exit 1
fi
echo "Installing acme.sh"
Expand Down Expand Up @@ -74,8 +83,8 @@ bootstrap_fn() {
echo "Creating the self-signed certificate"

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

domains=$(jq -e -r '.domains[].name' "$config_file")
domains=$(echo "$CONFIG_JSON" | jq -e -r '.domains[].name')
# Note that this script assumes that the config.json is trusted input
# and the domain doesn't have e.g. ../../ in it
for domain in $domains; do
Expand All @@ -105,7 +114,7 @@ bootstrap_fn() {
cat /dev/null > "$data_dir/nginx_generated.conf"
i=0
while [ "$i" -lt "$num_domains" ]; do
domain_json=$(jq -e ".domains[$i]" "$config_file")
domain_json=$(echo "$CONFIG_JSON" | jq -e ".domains[$i]")
domain=$(echo "$domain_json" | jq -e -r '.name')
if [ $? -ne 0 ]; then
echo "Failed to get the name for $domain_json"
Expand Down
28 changes: 28 additions & 0 deletions test/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
services:
proxy:
profiles:
- proxy
build:
context: ./reverse-proxy
volumes:
Expand All @@ -19,8 +21,34 @@ services:
timeout: 5s
interval: 5s
retries: 6
proxy2:
profiles:
- proxy2
build:
context: ./reverse-proxy
volumes:
- reverse-proxy-test:/etc/reverse_proxy/data
environment:
- SKIP_CREATE_CERTS=1
- SKIP_RENEW_CERTS=1
- DEBUG=1
- 'CONFIG_JSON={ "email": "[email protected]", "domains": [ { "name": "localhost", "dest": "http://hello:80" } ] }'

ports:
- 80:80
- 443:443
networks:
- web
healthcheck:
test: ['CMD-SHELL', 'curl -so /dev/null http://localhost/ || exit 1']
timeout: 5s
interval: 5s
retries: 6

hello:
profiles:
- proxy
- proxy2
image: nginxdemos/hello:plain-text
networks:
- web
Expand Down
Loading