2022-11-11 00:50:06 +00:00
|
|
|
import ./make-test-python.nix {
|
2018-04-26 04:09:05 +00:00
|
|
|
name = "dhparams";
|
|
|
|
|
2022-11-11 00:50:06 +00:00
|
|
|
nodes.machine =
|
|
|
|
{ pkgs, ... }:
|
|
|
|
{
|
|
|
|
security.dhparams.enable = true;
|
|
|
|
environment.systemPackages = [ pkgs.openssl ];
|
2024-12-10 19:26:33 +00:00
|
|
|
|
2022-11-11 00:50:06 +00:00
|
|
|
specialisation = {
|
|
|
|
gen1.configuration =
|
|
|
|
{ config, ... }:
|
|
|
|
{
|
|
|
|
security.dhparams.params = {
|
|
|
|
# Use low values here because we don't want the test to run for ages.
|
|
|
|
foo.bits = 1024;
|
|
|
|
# Also use the old format to make sure the type is coerced in the right
|
|
|
|
# way.
|
|
|
|
bar = 1025;
|
|
|
|
};
|
2024-12-10 19:26:33 +00:00
|
|
|
|
2022-11-11 00:50:06 +00:00
|
|
|
systemd.services.foo = {
|
|
|
|
description = "Check systemd Ordering";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2023-11-30 23:45:52 +00:00
|
|
|
before = [ "shutdown.target" ];
|
|
|
|
conflicts = [ "shutdown.target" ];
|
2022-11-11 00:50:06 +00:00
|
|
|
unitConfig = {
|
|
|
|
# This is to make sure that the dhparams generation of foo occurs
|
|
|
|
# before this service so we need this service to start as early as
|
|
|
|
# possible to provoke a race condition.
|
|
|
|
DefaultDependencies = false;
|
2024-12-10 19:26:33 +00:00
|
|
|
|
2022-11-11 00:50:06 +00:00
|
|
|
# We check later whether the service has been started or not.
|
|
|
|
ConditionPathExists = config.security.dhparams.params.foo.path;
|
2024-12-10 19:26:33 +00:00
|
|
|
};
|
2022-11-11 00:50:06 +00:00
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
|
|
|
# The reason we only provide an ExecStop here is to ensure that we don't
|
|
|
|
# accidentally trigger an error because a file system is not yet ready
|
|
|
|
# during very early startup (we might not even have the Nix store
|
|
|
|
# available, for example if future changes in NixOS use systemd mount
|
|
|
|
# units to do early file system initialisation).
|
|
|
|
serviceConfig.ExecStop = "${pkgs.coreutils}/bin/true";
|
2024-12-10 19:26:33 +00:00
|
|
|
};
|
2022-11-11 00:50:06 +00:00
|
|
|
};
|
|
|
|
gen2.configuration = {
|
|
|
|
security.dhparams.params.foo.bits = 1026;
|
2024-12-10 19:26:33 +00:00
|
|
|
};
|
2022-11-11 00:50:06 +00:00
|
|
|
gen3.configuration = { };
|
|
|
|
gen4.configuration = {
|
|
|
|
security.dhparams.stateful = false;
|
|
|
|
security.dhparams.params.foo2.bits = 1027;
|
|
|
|
security.dhparams.params.bar2.bits = 1028;
|
2024-12-10 19:26:33 +00:00
|
|
|
};
|
2022-11-11 00:50:06 +00:00
|
|
|
gen5.configuration = {
|
|
|
|
security.dhparams.defaultBitSize = 1029;
|
|
|
|
security.dhparams.params.foo3 = { };
|
|
|
|
security.dhparams.params.bar3 = { };
|
|
|
|
};
|
2018-04-26 04:09:05 +00:00
|
|
|
};
|
|
|
|
};
|
2019-12-06 06:05:06 +00:00
|
|
|
|
|
|
|
testScript =
|
|
|
|
{ nodes, ... }:
|
|
|
|
let
|
|
|
|
getParamPath =
|
|
|
|
gen: name:
|
2022-11-11 00:50:06 +00:00
|
|
|
let
|
|
|
|
node = "gen${toString gen}";
|
2024-12-10 19:26:33 +00:00
|
|
|
in
|
2022-11-11 00:50:06 +00:00
|
|
|
nodes.machine.config.specialisation.${node}.configuration.security.dhparams.params.${name}.path;
|
2024-12-10 19:26:33 +00:00
|
|
|
|
2018-04-26 04:09:05 +00:00
|
|
|
switchToGeneration =
|
2024-12-10 19:26:33 +00:00
|
|
|
gen:
|
2019-12-06 06:05:06 +00:00
|
|
|
let
|
|
|
|
switchCmd = "${nodes.machine.config.system.build.toplevel}/specialisation/gen${toString gen}/bin/switch-to-configuration test";
|
2024-12-10 19:26:33 +00:00
|
|
|
in
|
|
|
|
''
|
2019-12-06 06:05:06 +00:00
|
|
|
with machine.nested("switch to generation ${toString gen}"):
|
|
|
|
machine.succeed("${switchCmd}")
|
2024-12-10 19:26:33 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
in
|
|
|
|
''
|
2019-12-06 06:05:06 +00:00
|
|
|
import re
|
2024-12-10 19:26:33 +00:00
|
|
|
|
|
|
|
|
2019-12-06 06:05:06 +00:00
|
|
|
def assert_param_bits(path, bits):
|
|
|
|
with machine.nested(f"check bit size of {path}"):
|
2022-11-11 00:50:06 +00:00
|
|
|
output = machine.succeed(f"openssl dhparam -in {path} -text")
|
|
|
|
pattern = re.compile(r"^\s*DH Parameters:\s+\((\d+)\s+bit\)\s*$", re.M)
|
2019-12-06 06:05:06 +00:00
|
|
|
match = pattern.match(output)
|
|
|
|
if match is None:
|
2022-11-11 00:50:06 +00:00
|
|
|
raise Exception("bla")
|
|
|
|
if match[1] != str(bits):
|
2019-12-06 06:05:06 +00:00
|
|
|
raise Exception(f"bit size should be {bits} but it is {match[1]} instead.")
|
2024-12-10 19:26:33 +00:00
|
|
|
|
2019-12-06 06:05:06 +00:00
|
|
|
machine.wait_for_unit("multi-user.target")
|
|
|
|
${switchToGeneration 1}
|
2024-12-10 19:26:33 +00:00
|
|
|
|
2019-12-06 06:05:06 +00:00
|
|
|
with subtest("verify startup order"):
|
|
|
|
machine.succeed("systemctl is-active foo.service")
|
2024-12-10 19:26:33 +00:00
|
|
|
|
2019-12-06 06:05:06 +00:00
|
|
|
with subtest("check bit sizes of dhparam files"):
|
|
|
|
assert_param_bits("${getParamPath 1 "foo"}", 1024)
|
|
|
|
assert_param_bits("${getParamPath 1 "bar"}", 1025)
|
2024-12-10 19:26:33 +00:00
|
|
|
|
2018-04-26 04:09:05 +00:00
|
|
|
${switchToGeneration 2}
|
2024-12-10 19:26:33 +00:00
|
|
|
|
2019-12-06 06:05:06 +00:00
|
|
|
with subtest("check whether bit size has changed"):
|
|
|
|
assert_param_bits("${getParamPath 2 "foo"}", 1026)
|
2024-12-10 19:26:33 +00:00
|
|
|
|
2019-12-06 06:05:06 +00:00
|
|
|
with subtest("ensure that dhparams file for 'bar' was deleted"):
|
2022-11-11 00:50:06 +00:00
|
|
|
machine.fail("test -e ${getParamPath 1 "bar"}")
|
2024-12-10 19:26:33 +00:00
|
|
|
|
2022-11-11 00:50:06 +00:00
|
|
|
${switchToGeneration 3}
|
2024-12-10 19:26:33 +00:00
|
|
|
|
2022-11-11 00:50:06 +00:00
|
|
|
with subtest("ensure that 'security.dhparams.path' has been deleted"):
|
|
|
|
machine.fail("test -e ${nodes.machine.config.specialisation.gen3.configuration.security.dhparams.path}")
|
2024-12-10 19:26:33 +00:00
|
|
|
|
2018-04-26 04:38:02 +00:00
|
|
|
${switchToGeneration 4}
|
2024-12-10 19:26:33 +00:00
|
|
|
|
2019-12-06 06:05:06 +00:00
|
|
|
with subtest("check bit sizes dhparam files"):
|
|
|
|
assert_param_bits(
|
2022-11-11 00:50:06 +00:00
|
|
|
"${getParamPath 4 "foo2"}", 1027
|
2019-12-06 06:05:06 +00:00
|
|
|
)
|
|
|
|
assert_param_bits(
|
2022-11-11 00:50:06 +00:00
|
|
|
"${getParamPath 4 "bar2"}", 1028
|
2019-12-06 06:05:06 +00:00
|
|
|
)
|
2024-12-10 19:26:33 +00:00
|
|
|
|
2019-12-06 06:05:06 +00:00
|
|
|
with subtest("check whether dhparam files are in the Nix store"):
|
|
|
|
machine.succeed(
|
|
|
|
"expr match ${getParamPath 4 "foo2"} ${builtins.storeDir}",
|
|
|
|
"expr match ${getParamPath 4 "bar2"} ${builtins.storeDir}",
|
|
|
|
)
|
2024-12-10 19:26:33 +00:00
|
|
|
|
2018-05-07 02:33:56 +00:00
|
|
|
${switchToGeneration 5}
|
2024-12-10 19:26:33 +00:00
|
|
|
|
2019-12-06 06:05:06 +00:00
|
|
|
with subtest("check whether defaultBitSize works as intended"):
|
2022-11-11 00:50:06 +00:00
|
|
|
assert_param_bits("${getParamPath 5 "foo3"}", 1029)
|
|
|
|
assert_param_bits("${getParamPath 5 "bar3"}", 1029)
|
2018-04-26 04:09:05 +00:00
|
|
|
'';
|
|
|
|
}
|