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

Pass OIDC config from .env to Backend and Frontend #458

Merged
merged 10 commits into from
Sep 1, 2023
6 changes: 6 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ HTTPS_PORT=443
# EMAIL_USER=
# EMAIL_PASSWORD=

# Optional: configure Single Sign-on with OpenID Connect
# OIDC_ENABLED=
matthew-white marked this conversation as resolved.
Show resolved Hide resolved
# OIDC_DISCOVERY_URL=
# OIDC_CLIENT_ID=
# OIDC_CLIENT_SECRET=

# Optional: configure error reporting
# SENTRY_ORG_SUBDOMAIN=
# SENTRY_KEY=
Expand Down
6 changes: 6 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ services:
- EMAIL_IGNORE_TLS=${EMAIL_IGNORE_TLS:-true}
- EMAIL_USER=${EMAIL_USER:-''}
- EMAIL_PASSWORD=${EMAIL_PASSWORD:-''}
- OIDC_ENABLED=${OIDC_ENABLED:-false}
- OIDC_DISCOVERY_URL=${OIDC_DISCOVERY_URL:-''}
- OIDC_CLIENT_ID=${OIDC_CLIENT_ID:-''}
- OIDC_CLIENT_SECRET=${OIDC_CLIENT_SECRET:-''}
- SENTRY_ORG_SUBDOMAIN=${SENTRY_ORG_SUBDOMAIN:-o130137}
- SENTRY_KEY=${SENTRY_KEY:-3cf75f54983e473da6bd07daddf0d2ee}
- SENTRY_PROJECT=${SENTRY_PROJECT:-1298632}
Expand All @@ -74,6 +78,8 @@ services:
nginx:
build:
context: .
args:
- OIDC_ENABLED=${OIDC_ENABLED:-false}
dockerfile: nginx.dockerfile
depends_on:
- service
Expand Down
2 changes: 1 addition & 1 deletion files/prebuild/build-frontend.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash -eu
cd client
npm clean-install --no-audit --fund=false --update-notifier=false
npm run build
VUE_APP_OIDC_ENABLED="$OIDC_ENABLED" npm run build
6 changes: 6 additions & 0 deletions files/service/config.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
"domain": "${BASE_URL}",
"sysadminAccount": "${SYSADMIN_EMAIL}"
},
"oidc": {
"enabled": ${OIDC_ENABLED},
"discoveryUrl": "${OIDC_DISCOVERY_URL}",
"clientId": "${OIDC_CLIENT_ID}",
"clientSecret": "${OIDC_CLIENT_SECRET}"
matthew-white marked this conversation as resolved.
Show resolved Hide resolved
},
"external": {
"sentry": {
"orgSubdomain": "${SENTRY_ORG_SUBDOMAIN}",
Expand Down
2 changes: 1 addition & 1 deletion files/service/scripts/start-odk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ echo "generating local service configuration.."

ENKETO_API_KEY=$(cat /etc/secrets/enketo-api-key) \
BASE_URL=$( [ "${HTTPS_PORT}" = 443 ] && echo https://"${DOMAIN}" || echo https://"${DOMAIN}":"${HTTPS_PORT}" ) \
envsubst '$DOMAIN $BASE_URL $SYSADMIN_EMAIL $ENKETO_API_KEY $DB_HOST $DB_USER $DB_PASSWORD $DB_NAME $DB_SSL $EMAIL_FROM $EMAIL_HOST $EMAIL_PORT $EMAIL_SECURE $EMAIL_IGNORE_TLS $EMAIL_USER $EMAIL_PASSWORD $SENTRY_ORG_SUBDOMAIN $SENTRY_KEY $SENTRY_PROJECT' \
envsubst '$DOMAIN $BASE_URL $SYSADMIN_EMAIL $ENKETO_API_KEY $DB_HOST $DB_USER $DB_PASSWORD $DB_NAME $DB_SSL $EMAIL_FROM $EMAIL_HOST $EMAIL_PORT $EMAIL_SECURE $EMAIL_IGNORE_TLS $EMAIL_USER $EMAIL_PASSWORD $OIDC_ENABLED $OIDC_DISCOVERY_URL $OIDC_CLIENT_ID $OIDC_CLIENT_SECRET $SENTRY_ORG_SUBDOMAIN $SENTRY_KEY $SENTRY_PROJECT' \
< /usr/share/odk/config.json.template \
> /usr/odk/config/local.json

Expand Down
3 changes: 2 additions & 1 deletion nginx.dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
ARG OIDC_ENABLED
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most examples of ARG that I've seen have ARG after FROM. But service.dockerfile has ARG before FROM. Maybe because there are two FROMs? Things seem to be working here with ARG before FROM, but I could look more into this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a strange use of ARG, or perhaps a strange position for it in the dockerfile. Will setting it here mean every single layer in the dockerfile needs rebuilding when OIDC_ENABLED is toggled?

Is it even necessary to set this in the dockerfile at all? Presumably RUN files/prebuild/build-frontend.sh can't pick up standard env vars set in docker-compose.yml(?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps a strange position for it in the dockerfile

I've moved ARG to after the first FROM. Based on how I understand the docs, I think that's what's needed here.

Will setting it here mean every single layer in the dockerfile needs rebuilding when OIDC_ENABLED is toggled?

This question makes me wonder, should I move ARG even further down, to right before RUN files/prebuild/build-frontend.sh? I don't think what comes before build-frontend.sh takes much time, so I doubt there'd be a big difference. But to be explicit, if an OIDC_* config is changed, the Vue build will need to be rerun.

Presumably RUN files/prebuild/build-frontend.sh can't pick up standard env vars set in docker-compose.yml(?)

That's my impression from reading some of the docs, but I'm definitely no Docker expert. I tried it out in a couple of commits here, and it does look like variables set in environment aren't available in the build. From the build for 735cbe7:

$OIDC_DISCOVERY_URL in the Dockerfile: [blank]
...
$OIDC_DISCOVERY_URL in build-frontend.sh: [blank]

From the build for 8171f6c (using build.args instead):

$OIDC_DISCOVERY_URL in the Dockerfile: [foobar]
...
$OIDC_DISCOVERY_URL in build-frontend.sh: [foobar]

Copy link
Contributor

@alxndrsn alxndrsn Aug 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like it matters where the ARG is declared:

Impact on build caching

ARG variables are not persisted into the built image as ENV variables are. However, ARG variables do impact the build cache in similar ways. If a Dockerfile defines an ARG variable whose value is different from a previous build, then a “cache miss” occurs upon its first usage, not its definition. In particular, all RUN instructions following an ARG instruction use the ARG variable implicitly (as an environment variable), thus can cause a cache miss. All predefined ARG variables are exempt from caching unless there is a matching ARG statement in the Dockerfile.
-https://docs.docker.com/engine/reference/builder/#impact-on-build-caching

I think declaring directly before use is probably better:

  • while build times may not be affected much, the number of local images will be, and associated disk usage
  • it may make it clearer to future maintainers where the ARG is used

Copy link
Member Author

@matthew-white matthew-white Aug 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! I've moved ARG to right before build-frontend.sh.

FROM node:18.17 as intermediate

COPY ./ ./
RUN files/prebuild/write-version.sh
RUN files/prebuild/build-frontend.sh
RUN OIDC_ENABLED="$OIDC_ENABLED" files/prebuild/build-frontend.sh

# when upgrading, look for upstream changes to redirector.conf
# also, confirm setup-odk.sh strips out HTTP-01 ACME challenge location
Expand Down