1 import ./make-test-python.nix ({ lib, pkgs, ... }:
3 name = "systemd-journal-gateway";
4 meta = with pkgs.lib.maintainers; {
5 maintainers = [ minijackson raitobezarius ];
8 # Named client for coherence with the systemd-journal-upload test, and for
9 # certificate validation
11 services.journald.gateway = {
13 cert = "/run/secrets/client/cert.pem";
14 key = "/run/secrets/client/key.pem";
15 trust = "/run/secrets/ca.cert.pem";
24 tmpdir_o = tempfile.TemporaryDirectory()
25 tmpdir = tmpdir_o.name
27 def generate_pems(domain: str):
30 "${pkgs.minica}/bin/minica",
31 "--ca-key=ca.key.pem",
32 "--ca-cert=ca.cert.pem",
33 f"--domains={domain}",
38 with subtest("Creating keys and certificates"):
39 generate_pems("server")
40 generate_pems("client")
42 client.wait_for_unit("multi-user.target")
44 def copy_pem(file: str):
45 machine.copy_from_host(source=f"{tmpdir}/{file}", target=f"/run/secrets/{file}")
46 machine.succeed(f"chmod 644 /run/secrets/{file}")
48 with subtest("Copying keys and certificates"):
49 machine.succeed("mkdir -p /run/secrets/{client,server}")
50 copy_pem("server/cert.pem")
51 copy_pem("server/key.pem")
52 copy_pem("client/cert.pem")
53 copy_pem("client/key.pem")
54 copy_pem("ca.cert.pem")
56 client.wait_for_unit("multi-user.target")
58 curl = '${pkgs.curl}/bin/curl'
59 accept_json = '--header "Accept: application/json"'
60 cacert = '--cacert /run/secrets/ca.cert.pem'
61 cert = '--cert /run/secrets/server/cert.pem'
62 key = '--key /run/secrets/server/key.pem'
63 base_url = 'https://client:19531'
65 curl_cli = f"{curl} {accept_json} {cacert} {cert} {key} --fail"
67 machine_info = client.succeed(f"{curl_cli} {base_url}/machine")
68 assert json.loads(machine_info)["hostname"] == "client", "wrong machine name"
70 # The HTTP request should have started the gateway service, triggered by
72 client.wait_for_unit("systemd-journal-gatewayd.service")
74 identifier = "nixos-test"
75 message = "Hello from NixOS test infrastructure"
77 client.succeed(f"systemd-cat --identifier={identifier} <<< '{message}'")
79 # max-time is a workaround against a bug in systemd-journal-gatewayd where
80 # if TLS is enabled, the connection is never closed. Since it will timeout,
81 # we ignore the return code.
82 entries = client.succeed(
83 f"{curl_cli} --max-time 5 {base_url}/entries?SYSLOG_IDENTIFIER={identifier} || true"
86 # Number of entries should be only 1
87 added_entry = json.loads(entries)
88 assert added_entry["SYSLOG_IDENTIFIER"] == identifier and added_entry["MESSAGE"] == message, "journal entry does not correspond"