Every container I run is described by a compose file. The compose files live in git, and Adze redeploys them when a new image lands. That works nicely right up until a container needs a password. Git is a lovely place for compose files and a terrible place for secrets.
Environment Variables
The easy option, and where everyone starts, is putting them straight in the environment section:
services:
app:
image: example/app
environment:
DATABASE_PASSWORD: hunter2
It works, every application under the sun supports environment variables, and it's right there next to everything else about the service. It's also stored in plaintext in a file I want to commit, and anyone who can run docker inspect can read it (which is only me, but that's not the point).
.env Files
The next step is moving them out of the compose file. Compose will read a .env file sitting next to the compose file and interpolate variables from it, or you can point a service at one with env_file:
services:
app:
image: example/app
env_file: app.env
The compose file can now be committed, and app.env goes in .gitignore. This is what most people do, and what I did for years. The secrets are still plaintext on disk, though, and they're still in docker inspect. They just aren't in git, which also means they aren't anywhere else either.
Every server ends up with its own little collection of .env files that only exist on that server, and that I have to remember to copy around when something moves. I could encrypt them and commit them to git, but then I'd still have to decrypt them every time I moved something to another server. That's exactly the sort of hand-tended state that will bug me when I come to deploy, especially with the hands-off nature of my new server install.
So I started looking for something better. Most of the secrets management tools I found focused on getting decrypted secrets into containers as files, which is fine if the application knows to read them. I've generally tried to follow The Twelve-Factor App principles1, though, so most of my services expect all their configuration to come from the environment. So there was going to be a little tangential yak first.
What I needed was a way to get secrets from files into environment variables without them appearing in docker inspect. I appreciate that anyone with unrestricted access to the Docker daemon effectively has root anyway, so this isn't intended to protect secrets from a compromised host. It does, however, avoid unnecessarily exposing secrets through Docker's configuration and API.
secret-env
secret-env fixes the files problem, and nothing else. It's a small launcher that reads files into environment variables and then directly execs the real application. There's no shell, no supervisor. It knows nothing about Docker, and it doesn't encrypt anything. It's written in Rust with a single dependency, because I wanted something tiny and static that could sit in front of anything.
You can give it explicit mappings, or point it at a directory and every file in it becomes a variable with the same name. This site's config is now just:
[exec]
path = /usr/libexec/greboid.com.real
[discovery]
directory = /run/secrets
The real binary gets moved to /usr/libexec/<name>.real and secret-env takes its place, so the image's entrypoint doesn't change and the application doesn't need touching. A missing directory just means there are no secrets, so the same image works with or without them. Nothing ends up in the container's config or in docker inspect. The secrets are still in the process's environment2, but that's unavoidable for anything that only reads environment variables.
It also means any delivery mechanism that produces files is good enough. I now just needed a nice way of getting the secrets into Docker.
Getting Secrets into Containers
Docker does have secrets built in, and I really did want to just use those. The useful version, though, where Docker stores the secrets encrypted and only hands them to the containers that need them, is Swarm-only. Plain Compose has a secrets section too, but outside of Swarm the file-backed variety is really just a nicer way of writing a bind mount for a file that already exists on the host, which puts me straight back to plaintext on disk. It's also entirely possible to initialise Swarm and run each server as a single node Swarm, but that felt like a big management overhead and a lot of confusion just to get secrets.
Other options seemed to want additional binaries, or full-blown sidecar containers, neither of which felt appealing to me. Obviously the next step was to not bother looking any further and to just go write my own solution.
SOPS and age seemed like the answer to the encryption and decryption side of things. I'm using SOPS with YAML (it's not all good...), and SOPS leaves the keys readable so you can still see what each secret is for. The files can live in git and the diffs are reasonably readable.
It also supports multiple recipients3, so each server can have its own age key, along with a couple of backup keys in case things die or explode. I'm not copying another private key between servers, and the secrets can just live alongside the compose file in git.
My servers all run systemd, and systemd can do everything, so let's go see what it can do. systemd-creds can handle encrypting the age key at rest and decrypting it onto a tmpfs at boot. This means the age key can remain encrypted at rest and be made available to decrypt the secrets themselves at runtime. This can be done with or without a TPM, which is good, as some of my servers don't have one4.
But how do I get the secrets into the containers securely and automatically?
Attempt 1
Compose has an extension mechanism for exactly this sort of thing: providers. A service can be a provider rather than an image, and Compose runs a binary for it during up and down. So the first version of compose-sops was a provider. On up, it decrypted the SOPS file and wrote each secret as a file on a tmpfs, and the real services picked them up as normal Compose secrets:
services:
secrets:
provider:
type: compose-sops
options:
file: ./secrets.yaml
age-key-file: /run/compose-sops/age-key.txt
runtime-dir: /dev/shm/demo
app:
image: example/app
depends_on: [secrets]
secrets: [db_password]
secrets:
db_password:
file: /dev/shm/demo/secrets/db_password
The containers worked, secrets were handled, you couldn't inspect them or see them in git. Awesome.
Then I rebooted a server.
Compose isn't involved at all when it comes to bringing the containers back up. Docker restarts the containers itself based on their restart policies, and Docker has never heard of compose-sops. The tmpfs had been wiped, the provider never ran, the files the containers were expecting to mount didn't exist, and so the containers didn't start. Sad.
This was, in hindsight, very obvious, so I'm not really sure how I didn't think of it, and nothing I could do in a Compose provider was going to fix it.
Attempt 2
So Docker needed to be doing this, not Compose. Docker calls the volume driver whenever a container using the volume starts, including after a reboot. So compose-sops became a Docker volume plugin. Mounting the volume decrypts the document and publishes the secrets as read-only files on a private tmpfs, and the last unmount throws it all away again. On reboot, it all happens again.
services:
app:
image: example/app
volumes:
- type: volume
source: app-secrets
target: /run/secrets
read_only: true
volume:
nocopy: true
volumes:
app-secrets:
driver: compose-sops
driver_opts:
file: myproject/secrets.sops.yaml
keys: db_password,api_key
uid: "65532"
gid: "65532"
It worked. It was verbose and not obvious. There was a lot of cruft for "this container gets these secrets", and it needed to be repeated verbatim in basically all compose stacks. It also needed a managed plugin running with CAP_SYS_ADMIN, three host directories that had to exist before Docker enabled it, systemd units to get everything starting in the right order around Docker, and a separate volume for every distinct set of secrets. Whilst I didn't hate it, I didn't like it.
Thinking It Through
Let's do some thinking this time before rushing into attempt 3. Attempt 2 was working; I just didn't like it.
YAML Tags
My next idea was to extend the compose syntax itself. Compose already understands a couple of custom YAML tags (!reset and !override), so something along these lines seemed plausible:
services:
app:
image: example/app
secrets: !sops secrets.sops.yaml
It reads really nicely. The problem is the same one the provider had: the tag is only ever evaluated by Compose. I'd either need a wrapper that generated a regular compose file and handed it off to Compose, or I'd have to replace Compose entirely.
I've got at least three other projects running on my servers that use the existing Compose libraries to manage container stacks in various ways, and making this work with all of those didn't feel worth the hassle.
I'd also still need to solve the reboot problem without leaving decrypted secrets on disk or putting them into Docker's configuration. So whilst it looks great, it never really got past the thinking stage.
runc
Underneath Docker and containerd, containers are actually started by runc. When Docker starts a container, including after a reboot, an OCI bundle (a root filesystem plus a config.json describing the mounts, environment, namespaces and so on) is passed to runc. The OCI spec has hooks at various points in a container's lifecycle, and runc runs them.
So all I needed to do was inject a couple of hooks at this point and I could get secrets into the container, no Compose needed.
Unfortunately, Docker doesn't expose any of that. There's no way to add hooks or touch the bundle from a compose file. What Docker does let you do is swap the runtime: you can register alternative OCI runtimes in daemon.json and pick one per container with runtime:. It's how things like gVisor work.
Attempt 3
A wrapper around runc. runcage registers as a runtime, decrypts the SOPS document, gives the container its secrets as files in /run/secrets on a read-only tmpfs of its own, and leaves everything else to runc. The configuration is just plain compose secrets with a slightly different target so runcage knows what to intercept. I probably could have just left this as is, but it felt easier to have a distinct boundary from a normal Docker secret.
services:
app:
image: example/app
runtime: runcage
secrets:
- source: app
target: /run/sops/secrets.yaml
secrets:
app:
file: ./secrets.sops.yaml
The encrypted document goes into the container through a perfectly normal Compose secret, and runcage turns it into one file per key. Forget runtime: runcage and the container only gets the ciphertext, which is a much nicer way to fail than leaking anything.
If the age key is missing runcage fails to start the container, if decryption fails it fails to start the container. It doesn't fail open. There's no daemon, no plugin, no additional systemd unit, and nothing in plaintext5 on disk or in docker inspect.
With secret-env in the image, the application just gets its environment variables and never knows any of this happened. If the application already supports reading secrets from files, you don't even need secret-env.
The compose files and their encrypted secrets can all live in git again. There's no collection of manually maintained .env files on each server, and no separate secrets service that needs to be running before the containers can start. The servers just need runcage installed and their own age key available.
Docker can restart the containers itself, Adze can carry on redeploying them as before, and the applications don't need to know anything about how their secrets got there6.
It's taken three implementations and two new projects to get what amounts to a couple of extra lines in a compose file, but that's exactly where I wanted to end up.
Next up is to work on my mini orchestrator so I can leave all my servers' compose files in a single repository and only run the ones for the correct server.
-
I'm not suggesting that environment variables are necessarily the best way to handle secrets, but they're the interface most of my applications already support and I find them the nicest way to deploy with containers. ↩︎
-
The environment can still be read through
/proc/<pid>/environ, subject to the usual process access permissions, and may be inherited by child processes. The intention here is to keep secrets out of Docker's metadata, not to make environment variables a secure storage mechanism. ↩︎ -
Each recipient can independently decrypt the document, it doesn't mean they all need to be available to decrypt it. Otherwise it would be a bad backup. ↩︎
-
Without a TPM, the host key is stored on disk and protected by root-only filesystem permissions. Whilst this provides some protection against unauthorised access, it does not offer hardware-backed security. ↩︎
-
Ordinarily, tmpfs contents could end up on disk because swapping exists. runcage requires Linux 6.4 or later because it uses the
noswapmount option introduced in that version. Secrets could, of course, still end up on disk through hibernation, crash dumps, core dumps, swapping of process memory and so on. ↩︎ -
Secrets are decrypted when the container starts, not continuously. Changing the encrypted document requires restarting the container before the application sees the new values. ↩︎