git.delta.rocks / jrsonnet / refs/commits / 064468b85673

difftreelog

source

modules/secrets.nix5.0 KiBsourcehistory
1{lib, config, ...}: let2  inherit (lib.options) mkOption;3  inherit (lib.types) unspecified nullOr listOf str bool attrsOf submodule;4  inherit (lib.strings) concatStringsSep;5  inherit (lib.attrsets) mapAttrs;67  sharedSecret = {config, ...}: {8    options = {9      expectedOwners = mkOption {10        type = nullOr (listOf str);11        description = ''12          List of hosts to encrypt secret for. null if managed by user (= via owners field from fleet.nix)1314          Secrets would be decrypted and stored to /run/secrets/$\{name} on owners15        '';16        default = null;17      };18      # TODO: Aren't those options may be just desugared to data/expectedData?19      regenerateOnOwnerAdded = mkOption {20        type = bool;21        description = ''22          Is this secret owner-dependent, and needs to be regenerated on ownership set change, or it may be just reencrypted.2324          You want to have this option set to true, when this secret contains some reference to its owners, i.e x509 SANs.25        '';26      };27      regenerateOnOwnerRemoved = mkOption {28        default = config.regenerateOnOwnerAdded;29        type = bool;30        description = ''31          Should this secret be removed on owner removal, or it may be just reencrypted3233          Most probably its value should be equal to regenerateOnOwnerAdded, override only if you know what are you doing.34          Contrary to regenerateOnOwnerAdded, you may want to set this option to false, when host permissions are revoked35          in some other way than by this secret ownership, I.e by firewall/etc.36        '';37      };38      generator = mkOption {39        type = nullOr unspecified;40        description = "Derivation to evaluate for secret generation";41        default = null;42      };43    };44  };45in {46  options = {47    sharedSecrets = mkOption {48      type = attrsOf (submodule sharedSecret);49      default = {};50      description = "Shared secrets";51    };52  };53  config = {54    hosts = mapAttrs (_: secretMap: {55      nixos.secrets = mapAttrs (_: s: removeAttrs s ["createdAt" "expiresAt"]) secretMap;56    }) config.data.hostSecrets;57    nixpkgs.overlays = [58      (final: prev: {59        mkSecretGenerators = {recipients}: rec {60          # TODO: Merge both generators to one with consistent options syntax?61          # Impure generator is built on local machine, then built closure is copied to remote machine,62          # and then it is ran in inpure context, so that this generator may access HSMs and other things.63          mkImpureSecretGenerator = {64            script,65            # If set - script will be run on remote machine, otherwise it will be run with fleet project in CWD66            # (Some secrets-encryption-in-git/managed PKI solution is expected)67            impureOn ? null,68          }:69            (prev.writeShellScript "impureGenerator.sh" ''70              #!/bin/sh71              set -eu7273              export GENERATOR_HELPER_IDENTITIES="${concatStringsSep "\n" recipients}";74              export PATH=${final.fleet-generator-helper}/bin:$PATH7576              # TODO: Provide tempdir from outside, to make it securely erasurable as needed?77              tmp=$(mktemp -d)78              cd $tmp79              # cd /var/empty8081              created_at=$(date -u +"%Y-%m-%dT%H:%M:%S.%NZ")8283              ${script}8485              if ! test -d $out; then86                echo "impure generator script did not produce expected \$out output"87                exit 188              fi8990              echo -n $created_at > $out/created_at91              echo -n SUCCESS > $out/marker92            '')93            .overrideAttrs (old: {94              passthru = {95                inherit impureOn;96                generatorKind = "impure";97              };98            });99          # Pure generators are disabled for now100          mkSecretGenerator = {script}: mkImpureSecretGenerator {inherit script;};101102          # TODO: Implement consistent naming103          # Pure secret generator is supposed to be run entirely by nix, using `__impure` derivation type...104          # But for now, it is ran the same way as `impureSecretGenerator`, but on the local machine.105          # mkSecretGenerator = {script}:106          #   (prev.writeShellScript "generator.sh" ''107          #     #!/bin/sh108          #     set -eu109          #     # TODO: make nix daemon build secret, not just the script.110          #     cd /var/empty111          #112          #     created_at=$(date -u +"%Y-%m-%dT%H:%M:%S.%NZ")113          #114          #     ${script}115          #     if ! test -d $out; then116          #       echo "impure generator script did not produce expected \$out output"117          #       exit 1118          #     fi119          #120          #     echo -n $created_at > $out/created_at121          #     echo -n SUCCESS > $out/marker122          #   '')123          #   .overrideAttrs (old: {124          #     passthru = {125          #       generatorKind = "pure";126          #     };127          #     # TODO: make nix daemon build secret, not just the script.128          #     # __impure = true;129          #   });130        };131      })132    ];133  };134}