39/53
Sign inSign up free

850. Render Config in the Entrypoint, Safely

hard 40 XP ~14 minHands-on lab

The payouts image renders its config at start-up with /home/student/projects/payouts/docker-entrypoint.sh, from the template config.json.tmpl next to it. Problems seen in production:

  • a database password containing / crashed the container with a sed error, one with & produced a wrong password,
  • "$schema" and the price format "$ 0.00" in the template got mangled,
  • a container with no password started anyway and failed much later,
  • the app never receives SIGTERM and its arguments lose their quoting.

Task

Fix docker-entrypoint.sh. It reads CONFIG_TEMPLATE (default /app/config.json.tmpl) and writes CONFIG_OUT (default /tmp/app/config.json, creating the directory if needed), then:

  • if DB_HOST or DB_PASSWORD is unset or empty, prints missing required variable: NAME to stderr for each and exits 64 without running anything,
  • PORT defaults to 8080,
  • replaces exactly the placeholders ${PORT}, ${DB_HOST} and ${DB_PASSWORD} with the values as they are, and changes nothing else,
  • makes the rendered file readable by its owner only (mode 600),
  • finally replaces itself with the command it was given, all arguments intact.

Test it locally, for example: CONFIG_TEMPLATE=config.json.tmpl CONFIG_OUT=/tmp/t/config.json DB_HOST=db DB_PASSWORD='a/b&c' ./docker-entrypoint.sh cat /tmp/t/config.json.

About this machine

Ubuntu 24.04 with no internet access. You are student with passwordless sudo. The grader checks the final state of the machine, not the commands you typed. Values like names and ports are generated for you.

Teaching or writing about this? Embed this lab on your page

TerminalNot started

Your machine is ready to start

A fresh, isolated Ubuntu machine boots in a few seconds with the task already set up.

No account needed: 3 of 3 free tries left. Guest machines last 30 minutes.