diff options
| author | tv <tv@krebsco.de> | 2026-11-21 15:09:49 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-21 15:09:49 +0000 |
| commit | ab8c63531ca4a5d8eb185fcdafbe02122649416b (patch) | |
| tree | 3f56fda69675e59e8eab6c84361b6dfe57267feb | |
| parent | 4e480218c0777ba9342ce2edd3b00ff3488229b2 (diff) | |
run-firecracker-vm: secrets, --secret NAME=FILE and fc.secrets
Secrets for a guest, like API keys, had no way in but the store or
/xchg, which other VMs started in the same directory see. Now a layer
declares the secrets its guest needs (fc.secrets.NAME, with where it
goes, default /run/secrets/NAME, and whose it is, default root's, 0400),
and the run script gets them with --secret NAME=FILE (FILE read once,
so a pipe will do), refusing to start the guest without the declared
ones. Like the SSH host key, it copies them into its private temporary
directory and serves that via 9P on vsock port 568, to that guest only;
there, fc-secrets copies them into place, before sysinit.target (so
before any service with the default dependencies), but after the users
get created, and unmounts the share again. A secret the guest doesn't
declare goes to /run/secrets/NAME, root's, with a warning.
| -rw-r--r-- | README.md | 20 | ||||
| -rw-r--r-- | pkgs/run-firecracker-vm/default.nix | 104 |
2 files changed, 120 insertions, 4 deletions
@@ -156,6 +156,26 @@ Who may log in goes in a layer, e.g.: A guest's address is its VM's, by name (`vms`, see host setup), or else its tap's own (see `fc-egress vms`). +Secrets, like API keys, come from the host at boot, too, never through +the store or `/xchg` (which other VMs started in the same directory +see): a layer declares what it needs, where it goes (default: +`/run/secrets/NAME`), and whose it is (default: root's, 0400): + +```nix +{ fc.secrets."hermes.env" = { owner = "hermes"; }; } +``` + +and the run script, which doesn't start the guest without them, gets +them with `--secret NAME=FILE` (FILE read once, so a pipe will do): + +```sh +./result/bin/run-firecracker-vm --secret hermes.env=<(pass show hermes/env) +``` + +They're only that guest's, and only in its memory (`/run`), until it +stops; to change one, restart the guest. A secret the guest doesn't +declare ends up in `/run/secrets/NAME`, root's, with a warning. + The guest's system can be built in layers, each a NixOS module on top of the ones before (the first on top of the base system): diff --git a/pkgs/run-firecracker-vm/default.nix b/pkgs/run-firecracker-vm/default.nix index fa4abc2..f78aaa5 100644 --- a/pkgs/run-firecracker-vm/default.nix +++ b/pkgs/run-firecracker-vm/default.nix @@ -135,6 +135,25 @@ let internal = true; description = "mount.vsock9p, for layers/nix.nix's initrd."; }; + # Secrets the guest needs, from the run script (--secret + # NAME=FILE, which it requires for each of these), never in the + # store: fc-secrets puts them into place, below /run, at boot. + options.fc.secrets = lib.mkOption { + type = lib.types.attrsOf (lib.types.submodule ({ name, ... }: { + options = { + path = lib.mkOption { + type = lib.types.str; + default = "/run/secrets/${name}"; + description = "Where the secret goes, below /run."; + }; + owner = lib.mkOption { type = lib.types.str; default = "root"; }; + group = lib.mkOption { type = lib.types.str; default = "root"; }; + mode = lib.mkOption { type = lib.types.str; default = "0400"; }; + }; + })); + default = { }; + description = "The secrets the guest needs, by name (--secret NAME=FILE)."; + }; } ] ++ lib.take n layers; @@ -144,7 +163,11 @@ let assertions = [{ assertion = config.nix.enable == config.fc.nix; message = "nix.enable: use run-firecracker-vm's layers/nix.nix instead."; - }]; + }] ++ lib.mapAttrsToList (name: s: { + assertion = builtins.match "[A-Za-z0-9._-]+" name != null + && builtins.match "/run/[^ ]+" s.path != null; + message = "fc.secrets.${name}: a name of letters, digits, '.', '_' and '-', with a path below /run"; + }) config.fc.secrets; # Activate and switch root from the initrd with a single program, # instead of bash running prepare-root and the activation script. @@ -223,6 +246,39 @@ let ''; }; + # The secrets (fc.secrets), which the run script serves via 9P on + # vsock port 568 (--secret NAME=FILE; never in the store), copied + # into place, each with its owner and mode, before sysinit.target + # (so before any service with the default dependencies), but after + # the users get created, for the owners; any other secret the run + # script was given goes to /run/secrets/NAME, root's. The share + # gets unmounted again. + systemd.services.fc-secrets = { + wantedBy = [ "sysinit.target" ]; + before = [ "sysinit.target" "shutdown.target" ]; + after = [ "userborn.service" "systemd-sysusers.service" ]; + conflicts = [ "shutdown.target" ]; + unitConfig.DefaultDependencies = false; + serviceConfig.Type = "oneshot"; + serviceConfig.RemainAfterExit = true; + script = '' + share=$(mktemp -d -p /run) + ${mountVsock9p} 568 "$share" -o ro + trap '${pkgs.util-linux}/bin/umount "$share"; rmdir "$share"' EXIT + install -d -m 0755 /run/secrets + ${lib.concatStrings (lib.mapAttrsToList (name: s: '' + install -D -o ${lib.escapeShellArg s.owner} -g ${lib.escapeShellArg s.group} -m ${s.mode} "$share"/${name} ${lib.escapeShellArg s.path} + '') config.fc.secrets)} + for f in "$share"/*; do + test -f "$f" || continue + case ''${f##*/} in + ${lib.concatStringsSep " | " ([ "/" ] ++ lib.attrNames config.fc.secrets)}) ;; # / is no name + *) install -m 0400 "$f" /run/secrets/ + esac + done + ''; + }; + # A fixed machine-id, so systemd doesn't consider every boot the first. environment.etc.machine-id.text = builtins.hashString "md5" "firecracker" + "\n"; @@ -458,7 +514,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' # [--mem-size MIB] # [--nixpkgs DIR] [--nixpkgs-cache MODE] # [--egress LIST] [--name NAME] [--vcpus N] - # [--ssh-host-key FILE] + # [--ssh-host-key FILE] [--secret NAME=FILE]... # The guest gets the directory xchg (in the current working directory) # at /xchg, via 9P over vsock. VMs started in the same directory share # it. Its store is read-only, unless it has nix (layers/nix.nix). @@ -486,11 +542,15 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' # --ssh-host-key: the guest's sshd's (ed25519) host key, by default # ~/.local/share/run-firecracker-vm/ssh_host_ed25519_key, created if it # doesn't exist, so all of the user's guests have the same one. + # --secret NAME=FILE: a secret for the guest, NAME letters, digits, '.', + # '_' and '-', FILE read once (it may be a pipe, like <(...)), and handed + # to the guest only, never in the store (see fc-secrets); a guest + # doesn't start without the secrets it declares (fc.secrets). set -efu export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.e2fsprogs pkgs.findutils pkgs.gnugrep pkgs.gnused pkgs.jq pkgs.openssh pkgs.util-linux pkgs.firecracker pkgs.unpfs ]} usage() { - echo "usage: $0 [--store FILE] [--store-size SIZE] [--home FILE] [--home-size SIZE] [--mem-size MIB] [--nixpkgs DIR] [--nixpkgs-cache MODE] [--egress LIST] [--name NAME] [--vcpus N] [--ssh-host-key FILE]" + echo "usage: $0 [--store FILE] [--store-size SIZE] [--home FILE] [--home-size SIZE] [--mem-size MIB] [--nixpkgs DIR] [--nixpkgs-cache MODE] [--egress LIST] [--name NAME] [--vcpus N] [--ssh-host-key FILE] [--secret NAME=FILE]..." } args= rw_store= @@ -504,6 +564,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' ssh_host_key=''${XDG_DATA_HOME:-$HOME/.local/share}/run-firecracker-vm/ssh_host_ed25519_key nixpkgs=${lib.escapeShellArg (toString pkgs.path)} nix_opt= # the last option that's only for a guest with nix + secrets=() # NAME=FILE, as given while test $# -gt 0; do case $1 in --*=*) set -- "''${1%%=*}" "''${1#*=}" "''${@:2}"; continue ;; @@ -518,6 +579,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' --name) name=$2; shift 2 ;; --vcpus) vcpus=$2; shift 2 ;; --ssh-host-key) ssh_host_key=$2; shift 2 ;; + --secret) secrets+=("$2"); shift 2 ;; -h|--help) usage; exit ;; *) usage >&2; exit 1 esac @@ -572,6 +634,39 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' exit 1 fi + # The secrets, copies, likewise, for the guest to copy (see fc-secrets), + # each FILE read once. The guest's own (fc.secrets) are required. + mkdir -m 700 "$tmp"/secrets + for secret in ''${secrets[@]+"''${secrets[@]}"}; do + s=''${secret%%=*} + file=''${secret#*=} + if test "$s" = "$secret" || ! printf %s "$s" | grep -qxE '[A-Za-z0-9._-]+'; then + echo "$0: --secret $secret: not NAME=FILE (NAME: letters, digits, '.', '_' and '-')" >&2 + exit 1 + fi + if test -e "$tmp/secrets/$s"; then + echo "$0: --secret $s: given twice" >&2 + exit 1 + fi + if ! cp "$file" "$tmp/secrets/$s"; then + echo "$0: --secret $s: can't read $file" >&2 + exit 1 + fi + chmod 600 "$tmp/secrets/$s" + done + for s in ${lib.escapeShellArgs (lib.attrNames nixos.config.fc.secrets)}; do + if ! test -e "$tmp/secrets/$s"; then + echo "$0: the guest needs the secret $s: --secret $s=FILE is required" >&2 + exit 1 + fi + done + for s in $(ls "$tmp"/secrets); do + case $s in + ${lib.concatStringsSep " | " ([ "/" ] ++ lib.attrNames nixos.config.fc.secrets)}) ;; # / is no name + *) echo "$0: --secret $s: the guest doesn't ask for it (fc.secrets), it goes to /run/secrets/$s" >&2 + esac + done + # disk VAR FILE SIZE: sets VAR to the absolute path of FILE, an ext4 # image, created (sparse, of SIZE) if it doesn't exist; locked while the # guest runs, as two guests with the same file would wreck it. @@ -700,7 +795,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' vm= fi - # The 9P servers, for /xchg, the SSH host key, and with nix, the store + # The 9P servers, for /xchg, the SSH host key, the secrets, and with nix, the store # and nixpkgs, one each per VM. They run as the user running this # script: xchg's files end up owned by the user, and the store is # read-only to them. When the guest connects to vsock port PORT on the @@ -721,6 +816,7 @@ pkgs.writeShellScriptBin "run-firecracker-vm" /* sh */ '' mkdir -p xchg serve 564 xchg serve 567 "$tmp"/ssh + serve 568 "$tmp"/secrets ${lib.optionalString nix '' serve 565 /nix/store serve 566 "$nixpkgs" |
