diff options
Diffstat (limited to 'pkgs')
| -rw-r--r-- | pkgs/run-firecracker-vm/default.nix | 104 |
1 files changed, 100 insertions, 4 deletions
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" |
