X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=modules%2Fsecrets.nix;h=86d276a27e1ebb6947963da3b95343b63a4b4c59;hb=bd5c5d4e23ebd3863a960976767ed4a83dfd07fe;hp=a149f02ca9baeb873b26d6732102b5944caf8c4d;hpb=34abd6afa44c620a56416bd423a2438a09bd1ce4;p=perso%2FImmae%2FConfig%2FNix.git diff --git a/modules/secrets.nix b/modules/secrets.nix index a149f02..86d276a 100644 --- a/modules/secrets.nix +++ b/modules/secrets.nix @@ -6,11 +6,36 @@ default = []; description = "Keys to upload to server"; }; + gpgKeys = lib.mkOption { + type = lib.types.listOf lib.types.path; + default = []; + description = "GPG public keys files to encrypt to"; + }; + ageKeys = lib.mkOption { + type = lib.types.listOf lib.types.str; + default = []; + description = "AGE keys to encrypt to"; + }; + decryptKey = lib.mkOption { + type = lib.types.str; + default = "/etc/ssh/ssh_host_ed25519_key"; + description = "ed25519 key used to decrypt with AGE"; + }; location = lib.mkOption { type = lib.types.path; default = "/var/secrets"; description = "Location where to put the keys"; }; + secretsVars = lib.mkOption { + type = lib.types.path; + default = "/run/keys/vars.yml"; + description = "Location where the secrets variables are defined, to be used to fill the templates in secrets"; + }; + deleteSecretsVars = lib.mkOption { + type = lib.types.bool; + default = false; + description = "Delete secrets file after deployment"; + }; # Read-only variables fullPaths = lib.mkOption { type = lib.types.attrsOf lib.types.path; @@ -25,49 +50,64 @@ location = config.secrets.location; keys = config.secrets.keys; empty = pkgs.runCommand "empty" { preferLocalBuild = true; } "mkdir -p $out && touch $out/done"; + fpath = v: "secrets/${v.dest}${lib.optionalString (v.isTemplated or true) ".gucci.tpl"}"; dumpKey = v: '' mkdir -p secrets/$(dirname ${v.dest}) - echo -n ${lib.strings.escapeShellArg v.text} > secrets/${v.dest} + echo -n ${lib.strings.escapeShellArg v.text} > ${fpath v} cat >> mods </dev/null + fingerprints=$fingerprints,$(cat $key | gpg --with-colons --import-options show-only --import 2>/dev/null | grep ^fpr | cut -d: -f10 | head -n1) + done + + sops --age ${builtins.concatStringsSep "," config.secrets.ageKeys} --pgp ''${fingerprints#,} --input-type binary -i -e $out 2>/dev/null ''; in lib.mkIf (builtins.length keys > 0) { system.activationScripts.secrets = { deps = [ "users" "wrappers" ]; text = '' install -m0750 -o root -g keys -d ${location} - if [ -f /run/keys/secrets.tar ]; then - if [ ! -f ${location}/currentSecrets ] || ! sha512sum -c --status "${location}/currentSecrets"; then - echo "rebuilding secrets" - TMP=$(${pkgs.coreutils}/bin/mktemp -d) - if [ -n "$TMP" ]; then - install -m0750 -o root -g keys -d $TMP - ${pkgs.gnutar}/bin/tar --strip-components 1 -C $TMP -xf /run/keys/secrets.tar - sha512sum /run/keys/secrets.tar > $TMP/currentSecrets - find $TMP -type d -exec chown root:keys {} \; -exec chmod o-rx {} \; - ${pkgs.rsync}/bin/rsync -O -c -av --delete $TMP/ ${location} - rm -rf $TMP - fi + TMP=$(${pkgs.coreutils}/bin/mktemp -d) + TMPWORK=$(${pkgs.coreutils}/bin/mktemp -d) + chmod go-rwx $TMPWORK + if [ -n "$TMP" -a -n "$TMPWORK" ]; then + install -m0750 -o root -g keys -d $TMP + ${pkgs.ssh-to-age}/bin/ssh-to-age -private-key -i ${config.secrets.decryptKey} -o $TMPWORK/keys.txt + SOPS_AGE_KEY_FILE=$TMPWORK/keys.txt ${pkgs.sops}/bin/sops -d ${secrets} | ${pkgs.gnutar}/bin/tar --strip-components 1 -C $TMP -x + if [ -f ${config.secrets.secretsVars} ]; then + SOPS_AGE_KEY_FILE=$TMPWORK/keys.txt ${pkgs.sops}/bin/sops -d ${config.secrets.secretsVars} > $TMPWORK/vars.yml fi + if [ -f $TMPWORK/vars.yml ]; then + find $TMP -name "*.gucci.tpl" -exec \ + /bin/sh -c 'f="{}"; ${pkgs.gucci}/bin/gucci -f '$TMPWORK'/vars.yml "$f" > "''${f%.gucci.tpl}"; touch --reference "$f" ''${f%.gucci.tpl} ; chmod --reference="$f" ''${f%.gucci.tpl} ; chown --reference="$f" ''${f%.gucci.tpl}' \; + fi + find $TMP -type d -exec chown root:keys {} \; -exec chmod o-rx {} \; + ${pkgs.rsync}/bin/rsync --exclude="*.gucci.tpl" -O -c -av --delete $TMP/ ${location} + rm -rf $TMP $TMPWORK ${lib.optionalString config.secrets.deleteSecretsVars config.secrets.secretsVars} fi ''; }; - system.extraDependencies = [ secrets ]; - deployment.secrets."secrets.tar" = { - source = "${secrets}"; - destination = "/run/keys/secrets.tar"; + + deployment.secrets."secret_vars.yml" = { + source = builtins.toString ../nixops/secrets/vars.yml; + destination = config.secrets.secretsVars; owner.user = "root"; owner.group = "root"; permissions = "0400"; - }; + }; }; }