1{ pkgs, lib, ... }: {
2 name = "aesmd";
3 meta = {
4 maintainers = with lib.maintainers; [ trundle veehaitch ];
5 };
6
7 nodes.machine = { lib, ... }: {
8 services.aesmd = {
9 enable = true;
10 settings = {
11 defaultQuotingType = "ecdsa_256";
12 proxyType = "direct";
13 whitelistUrl = "http://nixos.org";
14 };
15 };
16
17 # Should have access to the AESM socket
18 users.users."sgxtest" = {
19 isNormalUser = true;
20 extraGroups = [ "sgx" ];
21 };
22
23 # Should NOT have access to the AESM socket
24 users.users."nosgxtest".isNormalUser = true;
25
26 # We don't have a real SGX machine in NixOS tests
27 systemd.services.aesmd.unitConfig.AssertPathExists = lib.mkForce [ ];
28
29 specialisation = {
30 withQuoteProvider.configuration = { ... }: {
31 services.aesmd = {
32 quoteProviderLibrary = pkgs.sgx-azure-dcap-client;
33 environment = {
34 AZDCAP_DEBUG_LOG_LEVEL = "INFO";
35 };
36 };
37 };
38 };
39 };
40
41 testScript = { nodes, ... }:
42 let
43 specialisations = "${nodes.machine.system.build.toplevel}/specialisation";
44 in
45 ''
46 def get_aesmd_pid():
47 status, main_pid = machine.systemctl("show --property MainPID --value aesmd.service")
48 assert status == 0, "Could not get MainPID of aesmd.service"
49 return main_pid.strip()
50
51 with subtest("aesmd.service starts"):
52 machine.wait_for_unit("aesmd.service")
53
54 main_pid = get_aesmd_pid()
55
56 with subtest("aesmd.service runtime directory permissions"):
57 runtime_dir = "/run/aesmd";
58 res = machine.succeed(f"stat -c '%a %U %G' {runtime_dir}").strip()
59 assert "750 aesmd sgx" == res, f"{runtime_dir} does not have the expected permissions: {res}"
60
61 with subtest("aesm.socket available on host"):
62 socket_path = "/var/run/aesmd/aesm.socket"
63 machine.wait_until_succeeds(f"test -S {socket_path}")
64 machine.succeed(f"test 777 -eq $(stat -c '%a' {socket_path})")
65 for op in [ "-r", "-w", "-x" ]:
66 machine.succeed(f"sudo -u sgxtest test {op} {socket_path}")
67 machine.fail(f"sudo -u nosgxtest test {op} {socket_path}")
68
69 with subtest("Copies white_list_cert_to_be_verify.bin"):
70 whitelist_path = "/var/opt/aesmd/data/white_list_cert_to_be_verify.bin"
71 whitelist_perms = machine.succeed(
72 f"nsenter -m -t {main_pid} ${pkgs.coreutils}/bin/stat -c '%a' {whitelist_path}"
73 ).strip()
74 assert "644" == whitelist_perms, f"white_list_cert_to_be_verify.bin has permissions {whitelist_perms}"
75
76 with subtest("Writes and binds aesm.conf in service namespace"):
77 aesmd_config = machine.succeed(f"nsenter -m -t {main_pid} ${pkgs.coreutils}/bin/cat /etc/aesmd.conf")
78
79 assert aesmd_config == "whitelist url = http://nixos.org\nproxy type = direct\ndefault quoting type = ecdsa_256\n", "aesmd.conf differs"
80
81 with subtest("aesmd.service without quote provider library has correct LD_LIBRARY_PATH"):
82 status, environment = machine.systemctl("show --property Environment --value aesmd.service")
83 assert status == 0, "Could not get Environment of aesmd.service"
84 env_by_name = dict(entry.split("=", 1) for entry in environment.split())
85 assert not env_by_name["LD_LIBRARY_PATH"], "LD_LIBRARY_PATH is not empty"
86
87 with subtest("aesmd.service with quote provider library starts"):
88 machine.succeed('${specialisations}/withQuoteProvider/bin/switch-to-configuration test')
89 machine.wait_for_unit("aesmd.service")
90
91 main_pid = get_aesmd_pid()
92
93 with subtest("aesmd.service with quote provider library has correct LD_LIBRARY_PATH"):
94 ld_library_path = machine.succeed(f"xargs -0 -L1 -a /proc/{main_pid}/environ | grep LD_LIBRARY_PATH")
95 assert ld_library_path.startswith("LD_LIBRARY_PATH=${pkgs.sgx-azure-dcap-client}/lib:"), \
96 "LD_LIBRARY_PATH is not set to the configured quote provider library"
97
98 with subtest("aesmd.service with quote provider library has set AZDCAP_DEBUG_LOG_LEVEL"):
99 azdcp_debug_log_level = machine.succeed(f"xargs -0 -L1 -a /proc/{main_pid}/environ | grep AZDCAP_DEBUG_LOG_LEVEL")
100 assert azdcp_debug_log_level == "AZDCAP_DEBUG_LOG_LEVEL=INFO\n", "AZDCAP_DEBUG_LOG_LEVEL is not set to INFO"
101 '';
102}