mirror of
https://github.com/Mic92/sops-nix.git
synced 2026-07-17 06:25:16 +08:00
Refuse age keyfile paths that are in the nix store
This commit is contained in:
parent
7f49111254
commit
74f03c1a51
3 changed files with 62 additions and 10 deletions
|
|
@ -62,6 +62,14 @@ let
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
pathNotInStore = lib.mkOptionType {
|
||||||
|
name = "pathNotInStore";
|
||||||
|
description = "path not in the Nix store";
|
||||||
|
descriptionClass = "noun";
|
||||||
|
check = x: !lib.path.hasStorePathPrefix x;
|
||||||
|
merge = lib.mergeEqualOption;
|
||||||
|
};
|
||||||
|
|
||||||
manifestFor = suffix: secrets: pkgs.writeTextFile {
|
manifestFor = suffix: secrets: pkgs.writeTextFile {
|
||||||
name = "manifest${suffix}.json";
|
name = "manifest${suffix}.json";
|
||||||
text = builtins.toJSON {
|
text = builtins.toJSON {
|
||||||
|
|
@ -166,7 +174,7 @@ in {
|
||||||
|
|
||||||
age = {
|
age = {
|
||||||
keyFile = lib.mkOption {
|
keyFile = lib.mkOption {
|
||||||
type = lib.types.nullOr lib.types.path;
|
type = lib.types.nullOr pathNotInStore;
|
||||||
default = null;
|
default = null;
|
||||||
example = "/home/someuser/.age-key.txt";
|
example = "/home/someuser/.age-key.txt";
|
||||||
description = ''
|
description = ''
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,14 @@ let
|
||||||
};
|
};
|
||||||
manifest = manifestFor "" regularSecrets {};
|
manifest = manifestFor "" regularSecrets {};
|
||||||
|
|
||||||
|
pathNotInStore = lib.mkOptionType {
|
||||||
|
name = "pathNotInStore";
|
||||||
|
description = "path not in the Nix store";
|
||||||
|
descriptionClass = "noun";
|
||||||
|
check = x: !lib.path.hasStorePathPrefix (/. + x);
|
||||||
|
merge = lib.mergeEqualOption;
|
||||||
|
};
|
||||||
|
|
||||||
regularSecrets = lib.filterAttrs (_: v: !v.neededForUsers) cfg.secrets;
|
regularSecrets = lib.filterAttrs (_: v: !v.neededForUsers) cfg.secrets;
|
||||||
|
|
||||||
sysusersEnabled = options.systemd ? sysusers && config.systemd.sysusers.enable;
|
sysusersEnabled = options.systemd ? sysusers && config.systemd.sysusers.enable;
|
||||||
|
|
@ -237,7 +245,7 @@ in {
|
||||||
|
|
||||||
age = {
|
age = {
|
||||||
keyFile = lib.mkOption {
|
keyFile = lib.mkOption {
|
||||||
type = lib.types.nullOr lib.types.path;
|
type = lib.types.nullOr pathNotInStore;
|
||||||
default = null;
|
default = null;
|
||||||
example = "/var/lib/sops-nix/key.txt";
|
example = "/var/lib/sops-nix/key.txt";
|
||||||
description = ''
|
description = ''
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ let
|
||||||
extraConfig
|
extraConfig
|
||||||
];
|
];
|
||||||
sops = {
|
sops = {
|
||||||
age.keyFile = ./test-assets/age-keys.txt;
|
age.keyFile = "/run/age-keys.txt";
|
||||||
defaultSopsFile = ./test-assets/secrets.yaml;
|
defaultSopsFile = ./test-assets/secrets.yaml;
|
||||||
secrets.test_key.neededForUsers = true;
|
secrets.test_key.neededForUsers = true;
|
||||||
secrets."nested/test/file".owner = "example-user";
|
secrets."nested/test/file".owner = "example-user";
|
||||||
|
|
@ -70,12 +70,18 @@ in {
|
||||||
nodes.machine = { lib, ... }: {
|
nodes.machine = { lib, ... }: {
|
||||||
imports = [ ../../modules/sops ];
|
imports = [ ../../modules/sops ];
|
||||||
sops = {
|
sops = {
|
||||||
age.keyFile = ./test-assets/age-keys.txt;
|
age.keyFile = "/run/age-keys.txt";
|
||||||
defaultSopsFile = ./test-assets/secrets.yaml;
|
defaultSopsFile = ./test-assets/secrets.yaml;
|
||||||
secrets.test_key = { };
|
secrets.test_key = { };
|
||||||
keepGenerations = lib.mkDefault 0;
|
keepGenerations = lib.mkDefault 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
# must run before sops sets up keys
|
||||||
|
boot.initrd.postDeviceCommands = ''
|
||||||
|
cp -r ${./test-assets/age-keys.txt} /run/age-keys.txt
|
||||||
|
chmod -R 700 /run/age-keys.txt
|
||||||
|
'';
|
||||||
|
|
||||||
specialisation.pruning.configuration.sops.keepGenerations = 10;
|
specialisation.pruning.configuration.sops.keepGenerations = 10;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -108,13 +114,19 @@ in {
|
||||||
|
|
||||||
age-keys = makeTest {
|
age-keys = makeTest {
|
||||||
name = "sops-age-keys";
|
name = "sops-age-keys";
|
||||||
nodes.machine = {
|
nodes.machine = { lib, ... }: {
|
||||||
imports = [ ../../modules/sops ];
|
imports = [ ../../modules/sops ];
|
||||||
sops = {
|
sops = {
|
||||||
age.keyFile = ./test-assets/age-keys.txt;
|
age.keyFile = "/run/age-keys.txt";
|
||||||
defaultSopsFile = ./test-assets/secrets.yaml;
|
defaultSopsFile = ./test-assets/secrets.yaml;
|
||||||
secrets.test_key = { };
|
secrets.test_key = { };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
# must run before sops sets up keys
|
||||||
|
boot.initrd.postDeviceCommands = ''
|
||||||
|
cp -r ${./test-assets/age-keys.txt} /run/age-keys.txt
|
||||||
|
chmod -R 700 /run/age-keys.txt
|
||||||
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
testScript = ''
|
testScript = ''
|
||||||
|
|
@ -213,14 +225,20 @@ in {
|
||||||
|
|
||||||
templates = makeTest {
|
templates = makeTest {
|
||||||
name = "sops-templates";
|
name = "sops-templates";
|
||||||
nodes.machine = { config, ... }: {
|
nodes.machine = { config, lib, ... }: {
|
||||||
imports = [ ../../modules/sops ];
|
imports = [ ../../modules/sops ];
|
||||||
sops = {
|
sops = {
|
||||||
age.keyFile = ./test-assets/age-keys.txt;
|
age.keyFile = "/run/age-keys.txt";
|
||||||
defaultSopsFile = ./test-assets/secrets.yaml;
|
defaultSopsFile = ./test-assets/secrets.yaml;
|
||||||
secrets.test_key = { };
|
secrets.test_key = { };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
# must run before sops sets up keys
|
||||||
|
boot.initrd.postDeviceCommands = ''
|
||||||
|
cp -r ${./test-assets/age-keys.txt} /run/age-keys.txt
|
||||||
|
chmod -R 700 /run/age-keys.txt
|
||||||
|
'';
|
||||||
|
|
||||||
sops.templates.test_template = {
|
sops.templates.test_template = {
|
||||||
content = ''
|
content = ''
|
||||||
This line is not modified.
|
This line is not modified.
|
||||||
|
|
@ -275,7 +293,7 @@ in {
|
||||||
imports = [ ../../modules/sops ];
|
imports = [ ../../modules/sops ];
|
||||||
|
|
||||||
sops = {
|
sops = {
|
||||||
age.keyFile = ./test-assets/age-keys.txt;
|
age.keyFile = "/run/age-keys.txt";
|
||||||
defaultSopsFile = ./test-assets/secrets.yaml;
|
defaultSopsFile = ./test-assets/secrets.yaml;
|
||||||
secrets.test_key = {
|
secrets.test_key = {
|
||||||
restartUnits = [ "restart-unit.service" "reload-unit.service" ];
|
restartUnits = [ "restart-unit.service" "reload-unit.service" ];
|
||||||
|
|
@ -283,6 +301,12 @@ in {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
# must run before sops sets up keys
|
||||||
|
boot.initrd.postDeviceCommands = ''
|
||||||
|
cp -r ${./test-assets/age-keys.txt} /run/age-keys.txt
|
||||||
|
chmod -R 700 /run/age-keys.txt
|
||||||
|
'';
|
||||||
|
|
||||||
systemd.services."restart-unit" = {
|
systemd.services."restart-unit" = {
|
||||||
description = "Restart unit";
|
description = "Restart unit";
|
||||||
# not started on boot
|
# not started on boot
|
||||||
|
|
@ -380,7 +404,13 @@ in {
|
||||||
inherit (pkgs) system;
|
inherit (pkgs) system;
|
||||||
};
|
};
|
||||||
|
|
||||||
user-passwords = userPasswordTest "sops-user-passwords" {};
|
user-passwords = userPasswordTest "sops-user-passwords" {
|
||||||
|
# must run before sops sets up keys
|
||||||
|
boot.initrd.postDeviceCommands = ''
|
||||||
|
cp -r ${./test-assets/age-keys.txt} /run/age-keys.txt
|
||||||
|
chmod -R 700 /run/age-keys.txt
|
||||||
|
'';
|
||||||
|
};
|
||||||
} // pkgs.lib.optionalAttrs (pkgs.lib.versionAtLeast (pkgs.lib.versions.majorMinor pkgs.lib.version) "24.05") {
|
} // pkgs.lib.optionalAttrs (pkgs.lib.versionAtLeast (pkgs.lib.versions.majorMinor pkgs.lib.version) "24.05") {
|
||||||
user-passwords-sysusers = userPasswordTest "sops-user-passwords-sysusers" {
|
user-passwords-sysusers = userPasswordTest "sops-user-passwords-sysusers" {
|
||||||
systemd.sysusers.enable = true;
|
systemd.sysusers.enable = true;
|
||||||
|
|
@ -388,5 +418,11 @@ in {
|
||||||
system.etc.overlay.enable = true;
|
system.etc.overlay.enable = true;
|
||||||
boot.initrd.systemd.enable = true;
|
boot.initrd.systemd.enable = true;
|
||||||
boot.kernelPackages = pkgs.linuxPackages_latest;
|
boot.kernelPackages = pkgs.linuxPackages_latest;
|
||||||
|
|
||||||
|
# must run before sops sets up keys
|
||||||
|
systemd.services."sops-install-secrets-for-users".preStart = ''
|
||||||
|
printf '${builtins.readFile ./test-assets/age-keys.txt}' > /run/age-keys.txt
|
||||||
|
chmod -R 700 /run/age-keys.txt
|
||||||
|
'';
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue