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" ];
74 # New generation of the server with manual config
75 newServer = { lib, nodes, ... }: {
77 services.taskserver.pki.manual = {
78 ca.cert = snakeOil.cacert;
79 server.cert = snakeOil.cert;
80 server.key = snakeOil.key;
81 server.crl = snakeOil.crl;
83 # This is to avoid assigning a different network address to the new
85 networking = lib.mapAttrs (lib.const lib.mkForce) {
86 interfaces.eth1.ipv4 = nodes.server.config.networking.interfaces.eth1.ipv4;
87 inherit (nodes.server.config.networking)
88 hostName primaryIPAddress extraHosts;
92 client1 = { pkgs, ... }: {
93 environment.systemPackages = [ pkgs.taskwarrior pkgs.gnutls ];
94 users.users.alice.isNormalUser = true;
95 users.users.bob.isNormalUser = true;
96 users.users.foo.isNormalUser = true;
97 users.users.bar.isNormalUser = true;
103 testScript = { nodes, ... }: let
104 cfg = nodes.server.config.services.taskserver;
105 portStr = toString cfg.listenPort;
106 newServerSystem = nodes.newServer.config.system.build.toplevel;
107 switchToNewServer = "${newServerSystem}/bin/switch-to-configuration test";
109 from shlex import quote
113 return f"su - {user} -c {quote(cmd)}"
116 def no_extra_init(client, org, user):
120 def setup_clients_for(org, user, extra_init=no_extra_init):
121 for client in [client1, client2]:
122 with client.nested(f"initialize client for user {user}"):
124 su(user, f"rm -rf /home/{user}/.task"),
125 su(user, "task rc.confirmation=no config confirmation no"),
128 exportinfo = server.succeed(f"nixos-taskserver user export {org} {user}")
130 with client.nested("importing taskwarrior configuration"):
131 client.succeed(su(user, f"eval {quote(exportinfo)} >&2"))
133 extra_init(client, org, user)
135 client.succeed(su(user, "task config taskd.server server:${portStr} >&2"))
137 client.succeed(su(user, "task sync init >&2"))
140 def restart_server():
141 server.systemctl("restart taskserver.service")
142 server.wait_for_open_port(${portStr})
145 def re_add_imperative_user():
146 with server.nested("(re-)add imperative user bar"):
147 server.execute("nixos-taskserver org remove imperativeOrg")
149 "nixos-taskserver org add imperativeOrg",
150 "nixos-taskserver user add imperativeOrg bar",
152 setup_clients_for("imperativeOrg", "bar")
156 with subtest(f"sync for user {user}"):
157 client1.succeed(su(user, "task add foo >&2"))
158 client1.succeed(su(user, "task sync >&2"))
159 client2.fail(su(user, "task list >&2"))
160 client2.succeed(su(user, "task sync >&2"))
161 client2.succeed(su(user, "task list >&2"))
164 def check_client_cert(user):
165 # debug level 3 is a workaround for gnutls issue https://gitlab.com/gnutls/gnutls/-/issues/1040
168 f" --x509cafile=/home/{user}/.task/keys/ca.cert"
169 f" --x509keyfile=/home/{user}/.task/keys/private.key"
170 f" --x509certfile=/home/{user}/.task/keys/public.cert"
171 f" --port=${portStr} server < /dev/null"
176 # Explicitly start the VMs so that we don't accidentally start newServer
181 server.wait_for_unit("taskserver.service")
184 "nixos-taskserver user list testOrganisation | grep -qxF alice",
185 "nixos-taskserver user list testOrganisation | grep -qxF foo",
186 "nixos-taskserver user list anotherOrganisation | grep -qxF bob",
189 server.wait_for_open_port(${portStr})
191 client1.wait_for_unit("multi-user.target")
192 client2.wait_for_unit("multi-user.target")
194 setup_clients_for("testOrganisation", "alice")
195 setup_clients_for("testOrganisation", "foo")
196 setup_clients_for("anotherOrganisation", "bob")
198 for user in ["alice", "bob", "foo"]:
201 server.fail("nixos-taskserver user add imperativeOrg bar")
202 re_add_imperative_user()
206 with subtest("checking certificate revocation of user bar"):
207 client1.succeed(check_client_cert("bar"))
209 server.succeed("nixos-taskserver user remove imperativeOrg bar")
212 client1.fail(check_client_cert("bar"))
214 client1.succeed(su("bar", "task add destroy everything >&2"))
215 client1.fail(su("bar", "task sync >&2"))
217 re_add_imperative_user()
219 with subtest("checking certificate revocation of org imperativeOrg"):
220 client1.succeed(check_client_cert("bar"))
222 server.succeed("nixos-taskserver org remove imperativeOrg")
225 client1.fail(check_client_cert("bar"))
227 client1.succeed(su("bar", "task add destroy even more >&2"))
228 client1.fail(su("bar", "task sync >&2"))
230 re_add_imperative_user()
232 with subtest("check whether declarative config overrides user bar"):
237 def init_manual_config(client, org, user):
238 cfgpath = f"/home/{user}/.task"
240 client.copy_from_host(
241 "${snakeOil.cacert}",
242 f"{cfgpath}/ca.cert",
244 for file in ["alice.key", "alice.cert"]:
245 client.copy_from_host(
246 f"${snakeOil}/{file}",
250 for file in [f"{user}.key", f"{user}.cert"]:
251 client.copy_from_host(
252 f"${snakeOil}/{file}",
257 su("alice", f"task config taskd.ca {cfgpath}/ca.cert"),
258 su("alice", f"task config taskd.key {cfgpath}/{user}.key"),
259 su(user, f"task config taskd.certificate {cfgpath}/{user}.cert"),
263 with subtest("check manual configuration"):
264 # Remove the keys from automatic CA creation, to make sure the new
265 # generation doesn't use keys from before.
266 server.succeed("rm -rf ${cfg.dataDir}/keys/* >&2")
269 "${switchToNewServer} >&2"
271 server.wait_for_unit("taskserver.service")
272 server.wait_for_open_port(${portStr})
275 "nixos-taskserver org add manualOrg",
276 "nixos-taskserver user add manualOrg alice",
279 setup_clients_for("manualOrg", "alice", init_manual_config)