1 { system ? builtins.currentSystem
3 , pkgs ? import ../.. { inherit system config; }
7 with import ../lib/testing-python.nix { inherit system pkgs; };
12 # Makes a test for a PostgreSQL package, given by name and looked up from `pkgs`.
13 makeTestAttribute = name:
16 value = makePostgresqlTlsClientCertTest pkgs."${name}";
19 makePostgresqlTlsClientCertTest = pkg:
21 runWithOpenSSL = file: cmd: pkgs.runCommand file
23 buildInputs = [ pkgs.openssl ];
26 caKey = runWithOpenSSL "ca.key" "openssl ecparam -name prime256v1 -genkey -noout -out $out";
27 caCert = runWithOpenSSL
30 openssl req -new -x509 -sha256 -key ${caKey} -out $out -subj "/CN=test.example" -days 36500
33 runWithOpenSSL "server.key" "openssl ecparam -name prime256v1 -genkey -noout -out $out";
34 serverKeyPath = "/var/lib/postgresql";
36 runWithOpenSSL "server.crt" ''
37 openssl req -new -sha256 -key ${serverKey} -out server.csr -subj "/CN=db.test.example"
38 openssl x509 -req -in server.csr -CA ${caCert} -CAkey ${caKey} \
39 -CAcreateserial -out $out -days 36500 -sha256
42 runWithOpenSSL "client.key" "openssl ecparam -name prime256v1 -genkey -noout -out $out";
44 runWithOpenSSL "client.crt" ''
45 openssl req -new -sha256 -key ${clientKey} -out client.csr -subj "/CN=test"
46 openssl x509 -req -in client.csr -CA ${caCert} -CAkey ${caKey} \
47 -CAcreateserial -out $out -days 36500 -sha256
49 clientKeyPath = "/root";
53 name = "postgresql-tls-client-cert-${pkg.name}";
54 meta.maintainers = with lib.maintainers; [ erictapen ];
56 nodes.server = { ... }: {
57 system.activationScripts = {
58 keyPlacement.text = ''
59 mkdir -p '${serverKeyPath}'
60 cp '${serverKey}' '${serverKeyPath}/server.key'
61 chown postgres:postgres '${serverKeyPath}/server.key'
62 chmod 600 '${serverKeyPath}/server.key'
65 services.postgresql = {
72 ensureDBOwnership = true;
75 ensureDatabases = [ "test" ];
78 ssl_ca_file = toString caCert;
79 ssl_cert_file = toString serverCert;
80 ssl_key_file = "${serverKeyPath}/server.key";
83 hostssl test test ::/0 cert clientcert=verify-full
89 { address = "fc00::1"; prefixLength = 120; }
92 firewall.allowedTCPPorts = [ 5432 ];
96 nodes.client = { ... }: {
97 system.activationScripts = {
98 keyPlacement.text = ''
99 mkdir -p '${clientKeyPath}'
100 cp '${clientKey}' '${clientKeyPath}/client.key'
101 chown root:root '${clientKeyPath}/client.key'
102 chmod 600 '${clientKeyPath}/client.key'
107 PGHOST = "db.test.example";
111 PGSSLMODE = "verify-full";
112 PGSSLCERT = clientCert;
113 PGSSLKEY = "${clientKeyPath}/client.key";
114 PGSSLROOTCERT = caCert;
116 systemPackages = [ pkg ];
121 { address = "fc00::2"; prefixLength = 120; }
124 hosts = { "fc00::1" = [ "db.test.example" ]; };
129 server.wait_for_unit("multi-user.target")
130 client.wait_for_unit("multi-user.target")
131 client.succeed("psql -c \"SELECT 1;\"")
136 if package == null then
137 # all-tests.nix: Maps the generic function over all attributes of PostgreSQL packages
138 builtins.listToAttrs (map makeTestAttribute (builtins.attrNames (import ../../pkgs/servers/sql/postgresql pkgs)))
140 # Called directly from <package>.tests
141 makePostgresqlTlsClientCertTest package