1 import ./make-test-python.nix ({ pkgs, ... }: let
2 snakeOil = pkgs.runCommand "snakeoil-certs" {
3 outputs = [ "out" "cacert" "cert" "key" "crl" ];
4 buildInputs = [ pkgs.gnutls.bin ];
5 caTemplate = pkgs.writeText "snakeoil-ca.template" ''
11 certTemplate = pkgs.writeText "snakeoil-cert.template" ''
18 crlTemplate = pkgs.writeText "snakeoil-crl.template" ''
21 userCertTemplate = pkgs.writeText "snakeoil-user-cert.template" ''
22 organization = snakeoil
30 certtool -p --bits 4096 --outfile ca.key
31 certtool -s --template "$caTemplate" --load-privkey ca.key \
33 certtool -p --bits 4096 --outfile "$key"
34 certtool -c --template "$certTemplate" \
35 --load-ca-privkey ca.key \
36 --load-ca-certificate "$cacert" \
37 --load-privkey "$key" \
39 certtool --generate-crl --template "$crlTemplate" \
40 --load-ca-privkey ca.key \
41 --load-ca-certificate "$cacert" \
46 # Stripping key information before the actual PEM-encoded values is solely
47 # to make test output a bit less verbose when copying the client key to the
49 certtool -p --bits 4096 | sed -n \
50 -e '/^----* *BEGIN/,/^----* *END/p' > "$out/alice.key"
52 certtool -c --template "$userCertTemplate" \
53 --load-privkey "$out/alice.key" \
54 --load-ca-privkey ca.key \
55 --load-ca-certificate "$cacert" \
56 --outfile "$out/alice.cert"
64 services.taskserver.enable = true;
65 services.taskserver.listenHost = "::";
66 services.taskserver.openFirewall = true;
67 services.taskserver.fqdn = "server";
68 services.taskserver.organisations = {
69 testOrganisation.users = [ "alice" "foo" ];
70 anotherOrganisation.users = [ "bob" ];
73 specialisation.manual_config.configuration = {
74 services.taskserver.pki.manual = {
75 ca.cert = snakeOil.cacert;
76 server.cert = snakeOil.cert;
77 server.key = snakeOil.key;
78 server.crl = snakeOil.crl;
83 client1 = { pkgs, ... }: {
84 environment.systemPackages = [ pkgs.taskwarrior2 pkgs.gnutls ];
85 users.users.alice.isNormalUser = true;
86 users.users.bob.isNormalUser = true;
87 users.users.foo.isNormalUser = true;
88 users.users.bar.isNormalUser = true;
94 testScript = { nodes, ... }: let
95 cfg = nodes.server.config.services.taskserver;
96 portStr = toString cfg.listenPort;
97 specialisations = "${nodes.server.system.build.toplevel}/specialisation";
98 newServerSystem = "${specialisations}/manual_config";
99 switchToNewServer = "${newServerSystem}/bin/switch-to-configuration test";
101 from shlex import quote
105 return f"su - {user} -c {quote(cmd)}"
108 def no_extra_init(client, org, user):
112 def setup_clients_for(org, user, extra_init=no_extra_init):
113 for client in [client1, client2]:
114 with client.nested(f"initialize client for user {user}"):
116 su(user, f"rm -rf /home/{user}/.task"),
117 su(user, "task rc.confirmation=no config confirmation no"),
120 exportinfo = server.succeed(f"nixos-taskserver user export {org} {user}")
122 with client.nested("importing taskwarrior configuration"):
123 client.succeed(su(user, f"eval {quote(exportinfo)} >&2"))
125 extra_init(client, org, user)
127 client.succeed(su(user, "task config taskd.server server:${portStr} >&2"))
129 client.succeed(su(user, "task sync init >&2"))
132 def restart_server():
133 server.systemctl("restart taskserver.service")
134 server.wait_for_open_port(${portStr})
137 def re_add_imperative_user():
138 with server.nested("(re-)add imperative user bar"):
139 server.execute("nixos-taskserver org remove imperativeOrg")
141 "nixos-taskserver org add imperativeOrg",
142 "nixos-taskserver user add imperativeOrg bar",
144 setup_clients_for("imperativeOrg", "bar")
148 with subtest(f"sync for user {user}"):
149 client1.succeed(su(user, "task add foo >&2"))
150 client1.succeed(su(user, "task sync >&2"))
151 client2.fail(su(user, "task list >&2"))
152 client2.succeed(su(user, "task sync >&2"))
153 client2.succeed(su(user, "task list >&2"))
156 def check_client_cert(user):
157 # debug level 3 is a workaround for gnutls issue https://gitlab.com/gnutls/gnutls/-/issues/1040
160 f" --x509cafile=/home/{user}/.task/keys/ca.cert"
161 f" --x509keyfile=/home/{user}/.task/keys/private.key"
162 f" --x509certfile=/home/{user}/.task/keys/public.cert"
163 f" --port=${portStr} server < /dev/null"
168 # Explicitly start the VMs so that we don't accidentally start newServer
173 server.wait_for_unit("taskserver.service")
176 "nixos-taskserver user list testOrganisation | grep -qxF alice",
177 "nixos-taskserver user list testOrganisation | grep -qxF foo",
178 "nixos-taskserver user list anotherOrganisation | grep -qxF bob",
181 server.wait_for_open_port(${portStr})
183 client1.wait_for_unit("multi-user.target")
184 client2.wait_for_unit("multi-user.target")
186 setup_clients_for("testOrganisation", "alice")
187 setup_clients_for("testOrganisation", "foo")
188 setup_clients_for("anotherOrganisation", "bob")
190 for user in ["alice", "bob", "foo"]:
193 server.fail("nixos-taskserver user add imperativeOrg bar")
194 re_add_imperative_user()
198 with subtest("checking certificate revocation of user bar"):
199 client1.succeed(check_client_cert("bar"))
201 server.succeed("nixos-taskserver user remove imperativeOrg bar")
204 client1.fail(check_client_cert("bar"))
206 client1.succeed(su("bar", "task add destroy everything >&2"))
207 client1.fail(su("bar", "task sync >&2"))
209 re_add_imperative_user()
211 with subtest("checking certificate revocation of org imperativeOrg"):
212 client1.succeed(check_client_cert("bar"))
214 server.succeed("nixos-taskserver org remove imperativeOrg")
217 client1.fail(check_client_cert("bar"))
219 client1.succeed(su("bar", "task add destroy even more >&2"))
220 client1.fail(su("bar", "task sync >&2"))
222 re_add_imperative_user()
224 with subtest("check whether declarative config overrides user bar"):
229 def init_manual_config(client, org, user):
230 cfgpath = f"/home/{user}/.task"
232 client.copy_from_host(
233 "${snakeOil.cacert}",
234 f"{cfgpath}/ca.cert",
236 for file in ["alice.key", "alice.cert"]:
237 client.copy_from_host(
238 f"${snakeOil}/{file}",
242 for file in [f"{user}.key", f"{user}.cert"]:
243 client.copy_from_host(
244 f"${snakeOil}/{file}",
249 su("alice", f"task config taskd.ca {cfgpath}/ca.cert"),
250 su("alice", f"task config taskd.key {cfgpath}/{user}.key"),
251 su(user, f"task config taskd.certificate {cfgpath}/{user}.cert"),
255 with subtest("check manual configuration"):
256 # Remove the keys from automatic CA creation, to make sure the new
257 # generation doesn't use keys from before.
258 server.succeed("rm -rf ${cfg.dataDir}/keys/* >&2")
261 "${switchToNewServer} >&2"
263 server.wait_for_unit("taskserver.service")
264 server.wait_for_open_port(${portStr})
267 "nixos-taskserver org add manualOrg",
268 "nixos-taskserver user add manualOrg alice",
271 setup_clients_for("manualOrg", "alice", init_manual_config)