Skip to content

Commit

Permalink
fix: avoid realizing store paths which do not yet exist
Browse files Browse the repository at this point in the history
- while secrets generation is not yet out of band, the realizations cannot
  depend on paths that are yet to be generated
  • Loading branch information
blaggacao committed Jan 12, 2022
1 parent 88e04a5 commit 2175494
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 18 deletions.
6 changes: 0 additions & 6 deletions modules/secrets.nix
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,6 @@ in {
};
};

config.assertions = lib.flip lib.mapAttrsToList config.secrets.install
(name: cfg: {
assertion = cfg.source == null || builtins.pathExists cfg.source;
message = ''secrets: source path "${cfg.source}" must exist.'';
});

config.systemd.services = lib.flip lib.mapAttrs' config.secrets.install
(name: cfg:
lib.nameValuePair "secret-${name}" {
Expand Down
2 changes: 1 addition & 1 deletion profiles/auxiliaries/builder.nix
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ in {
builder = {
isSystemUser = true;
openssh.authorizedKeys.keyFiles =
[ (config.secrets.encryptedRoot + "/nix-builder-key.pub") ];
[ ((toString config.secrets.encryptedRoot) + "/nix-builder-key.pub") ];
shell = pkgs.bashInteractive;
};
};
Expand Down
4 changes: 2 additions & 2 deletions profiles/auxiliaries/docker-registry.nix
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
'';

secrets.install.redis-password = {
source = config.secrets.encryptedRoot + "/redis-password.json";
source = (toString config.secrets.encryptedRoot) + "/redis-password.json";
target = /run/keys/redis-password;
inputType = "binary";
outputType = "binary";
Expand All @@ -67,7 +67,7 @@
'';

secrets.install.docker-passwords = {
source = config.secrets.encryptedRoot + "/docker-passwords.json";
source = (toString config.secrets.encryptedRoot) + "/docker-passwords.json";
target = /run/keys/docker-passwords-decrypted;
script = ''
export PATH="${lib.makeBinPath (with pkgs; [ coreutils jq ])}"
Expand Down
2 changes: 1 addition & 1 deletion profiles/auxiliaries/oauth.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
secrets.install.oauth.script = ''
export PATH="${lib.makeBinPath (with pkgs; [ sops coreutils ])}"
cat ${config.secrets.encryptedRoot + "/oauth-secrets"} \
cat ${(toString config.secrets.encryptedRoot) + "/oauth-secrets"} \
| sops -d /dev/stdin \
> /run/keys/oauth-secrets
Expand Down
10 changes: 6 additions & 4 deletions profiles/auxiliaries/secrets.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ let
"${pkgs.sops}/bin/sops --encrypt --input-type json --kms '${config.cluster.kms}' /dev/stdin";

sopsDecrypt = path:
# NB: we can't work on store paths that don't yet exist before they are generated
assert lib.assertMsg (builtins.isString path) "sopsDecrypt: path must be a string ${toString path}";
"${pkgs.sops}/bin/sops --decrypt --input-type json ${path}";

isInstance = config.currentCoreNode != null;
Expand Down Expand Up @@ -92,17 +94,17 @@ in {
'';

secrets.install.nomad-server = lib.mkIf isInstance {
source = config.secrets.encryptedRoot + "/nomad.json";
source = (toString config.secrets.encryptedRoot) + "/nomad.json";
target = gossipEncryptionMaterial.nomad;
};

secrets.install.consul-server = lib.mkIf isInstance {
source = config.secrets.encryptedRoot + "/consul-core.json";
source = (toString config.secrets.encryptedRoot) + "/consul-core.json";
target = gossipEncryptionMaterial.consul;
};

secrets.install.consul-clients = lib.mkIf (!isInstance) {
source = config.secrets.encryptedRoot + "/consul-clients.json";
source = (toString config.secrets.encryptedRoot) + "/consul-clients.json";
target = gossipEncryptionMaterial.consul;
script = ''
${pkgs.systemd}/bin/systemctl restart consul.service
Expand Down Expand Up @@ -182,7 +184,7 @@ in {
secrets.install.certs = lib.mkIf isInstance {
script = ''
export PATH="${lib.makeBinPath (with pkgs; [ cfssl jq coreutils ])}"
cert="$(${sopsDecrypt (config.secrets.encryptedRoot + "/cert.json")})"
cert="$(${sopsDecrypt ((toString config.secrets.encryptedRoot) + "/cert.json")})"
echo "$cert" | cfssljson -bare cert
cp ${builtins.baseNameOf pkiFiles.certFile} ${pkiFiles.certFile}
cp ${builtins.baseNameOf pkiFiles.keyFile} ${pkiFiles.keyFile}
Expand Down
6 changes: 3 additions & 3 deletions profiles/bootstrap/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ let

initialVaultSecrets = ''
sops --decrypt --extract '["encrypt"]' ${
config.secrets.encryptedRoot + "/consul-clients.json"
(toString config.secrets.encryptedRoot) + "/consul-clients.json"
} | vault kv put kv/bootstrap/clients/consul encrypt=-
sops --decrypt --extract '["server"]["encrypt"]' ${
config.secrets.encryptedRoot + "/nomad.json"
(toString config.secrets.encryptedRoot) + "/nomad.json"
} | vault kv put kv/bootstrap/clients/nomad encrypt=-
sops --decrypt ${
config.secrets.encryptedRoot + "/nix-cache.json"
(toString config.secrets.encryptedRoot) + "/nix-cache.json"
} | vault kv put kv/bootstrap/cache/nix-key -
'';

Expand Down
2 changes: 1 addition & 1 deletion profiles/monitoring.nix
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
mkdir -p /var/lib/grafana
cat ${config.secrets.encryptedRoot + "/grafana-password.json"} \
cat ${(toString config.secrets.encryptedRoot) + "/grafana-password.json"} \
| sops -d /dev/stdin \
> /var/lib/grafana/password
'';
Expand Down

0 comments on commit 2175494

Please sign in to comment.