aboutsummaryrefslogtreecommitdiffstats
path: root/tests/common
diff options
context:
space:
mode:
Diffstat (limited to '')
-rwxr-xr-xtests/common/acme/certs/generate66
-rwxr-xr-xtests/common/acme/certs/generate.ca38
-rwxr-xr-xtests/common/acme/certs/generate.client (renamed from system/services/taskserver/certs/generate.client)46
-rw-r--r--tests/common/acme/certs/output/acme.nixos_test.cert.pem11
-rw-r--r--tests/common/acme/certs/output/acme.nixos_test.key.pem25
-rw-r--r--tests/common/acme/certs/output/acme.nixos_test.template6
-rw-r--r--tests/common/acme/certs/output/ca.cert.pem10
-rw-r--r--tests/common/acme/certs/output/ca.key.pem25
-rw-r--r--tests/common/acme/certs/output/ca.template5
-rw-r--r--tests/common/acme/certs/snakeoil-certs.nix13
-rw-r--r--tests/common/acme/client.nix21
-rw-r--r--tests/common/acme/default.nix44
-rw-r--r--tests/common/acme/server.nix91
-rw-r--r--tests/common/dns/client.nix16
-rw-r--r--tests/common/dns/server.nix37
-rw-r--r--tests/common/email/dkim/alice.com/private.age11
-rw-r--r--tests/common/email/dkim/alice.com/public1
-rw-r--r--tests/common/email/dkim/bob.com/private.age12
-rw-r--r--tests/common/email/dkim/bob.com/public1
-rwxr-xr-xtests/common/email/dkim/gen_key.sh35
-rw-r--r--tests/common/email/dkim/mail.server1/private.age13
-rw-r--r--tests/common/email/dkim/mail.server1/public1
-rw-r--r--tests/common/email/dkim/mail.server2/private.age11
-rw-r--r--tests/common/email/dkim/mail.server2/public1
-rw-r--r--tests/common/email/functions.nix218
-rw-r--r--tests/common/email/hostKey7
26 files changed, 737 insertions, 28 deletions
diff --git a/tests/common/acme/certs/generate b/tests/common/acme/certs/generate
new file mode 100755
index 0000000..ffce74b
--- /dev/null
+++ b/tests/common/acme/certs/generate
@@ -0,0 +1,66 @@
+#! /usr/bin/env nix-shell
+#! nix-shell -p gnutls -p dash -i dash --impure
+# shellcheck shell=dash
+
+# For development and testing.
+# Create a CA key and cert, and use that to generate a server key and cert.
+# Creates:
+# ca.key.pem
+# ca.cert.pem
+# server.key.pem
+# server.cert.pem
+
+export SEC_PARAM=ultra
+export EXPIRATION_DAYS=123456
+export ORGANIZATION="Vhack.eu Test Keys"
+export COUNTRY=EU
+export SAN="acme.nixos_test"
+export KEY_TYPE="ed25519"
+
+BASEDIR="$(dirname "$0")"
+GENERATION_LOCATION="$BASEDIR/output"
+cd "$BASEDIR" || {
+ echo "(BUG?) No basedir ('$BASEDIR')" 1>&2
+ exit 1
+}
+
+ca=false
+clients=false
+
+usage() {
+ echo "Usage: $0 --ca|--clients"
+ exit 2
+}
+
+if [ "$#" -eq 0 ]; then
+ usage
+fi
+
+for arg in "$@"; do
+ case "$arg" in
+ "--ca")
+ ca=true
+ ;;
+ "--clients")
+ clients=true
+ ;;
+ *)
+ usage
+ ;;
+ esac
+done
+
+[ -d "$GENERATION_LOCATION" ] || mkdir --parents "$GENERATION_LOCATION"
+cd "$GENERATION_LOCATION" || echo "(BUG?) No generation location fould!" 1>&2
+
+[ "$ca" = true ] && ../generate.ca
+
+# Creates:
+# <client_name>.key.pem
+# <client_name>.cert.pem
+#
+[ "$clients" = true ] && ../generate.client "acme.nixos_test"
+
+echo "(INFO) Look for the keys at: $GENERATION_LOCATION"
+
+# vim: ft=sh
diff --git a/tests/common/acme/certs/generate.ca b/tests/common/acme/certs/generate.ca
new file mode 100755
index 0000000..92832c5
--- /dev/null
+++ b/tests/common/acme/certs/generate.ca
@@ -0,0 +1,38 @@
+#! /usr/bin/env sh
+
+# Take the correct binary to create the certificates
+CERTTOOL=$(command -v gnutls-certtool 2>/dev/null || command -v certtool 2>/dev/null)
+if [ -z "$CERTTOOL" ]; then
+ echo "ERROR: No certtool found" >&2
+ exit 1
+fi
+
+# Create a CA key.
+$CERTTOOL \
+ --generate-privkey \
+ --sec-param "$SEC_PARAM" \
+ --key-type "$KEY_TYPE" \
+ --outfile ca.key.pem
+
+chmod 600 ca.key.pem
+
+# Sign a CA cert.
+cat <<EOF >ca.template
+country = $COUNTRY
+dns_name = "$SAN"
+expiration_days = $EXPIRATION_DAYS
+organization = $ORGANIZATION
+ca
+EOF
+#state = $STATE
+#locality = $LOCALITY
+
+$CERTTOOL \
+ --generate-self-signed \
+ --load-privkey ca.key.pem \
+ --template ca.template \
+ --outfile ca.cert.pem
+
+chmod 600 ca.cert.pem
+
+# vim: ft=sh
diff --git a/system/services/taskserver/certs/generate.client b/tests/common/acme/certs/generate.client
index 4f0e503..5930298 100755
--- a/system/services/taskserver/certs/generate.client
+++ b/tests/common/acme/certs/generate.client
@@ -1,54 +1,44 @@
-#!/bin/sh
+#! /usr/bin/env sh
# Take the correct binary to create the certificates
CERTTOOL=$(command -v gnutls-certtool 2>/dev/null || command -v certtool 2>/dev/null)
-if [ -z "$CERTTOOL" ]
-then
- echo "ERROR: No certtool found" >&2
- exit 1
+if [ -z "$CERTTOOL" ]; then
+ echo "ERROR: No certtool found" >&2
+ exit 1
fi
-. ./vars
-
NAME=client
-if [ $# -gt 0 ]
-then
- NAME=$1
+if [ $# -gt 0 ]; then
+ NAME="$1"
fi
-if ! [ -f "$NAME".key.pem ]
-then
- # Create a client key.
- $CERTTOOL \
+# Create a client key.
+$CERTTOOL \
--generate-privkey \
- --sec-param $SEC_PARAM \
+ --sec-param "$SEC_PARAM" \
+ --key-type "$KEY_TYPE" \
--outfile "$NAME".key.pem
-fi
chmod 600 "$NAME".key.pem
-if ! [ -f "$NAME".template ]
-then
- # Sign a client cert with the key.
- cat <<EOF >"$NAME".template
-organization = $ORGANIZATION
-cn = $CN
+# Sign a client cert with the key.
+cat <<EOF >"$NAME".template
+dns_name = "$NAME"
+dns_name = "$SAN"
expiration_days = $EXPIRATION_DAYS
-tls_www_client
+organization = $ORGANIZATION
encryption_key
signing_key
EOF
-fi
-if ! [ -f "$NAME".cert.pem ]
-then
- $CERTTOOL \
+$CERTTOOL \
--generate-certificate \
--load-privkey "$NAME".key.pem \
--load-ca-certificate ca.cert.pem \
--load-ca-privkey ca.key.pem \
--template "$NAME".template \
--outfile "$NAME".cert.pem
-fi
chmod 600 "$NAME".cert.pem
+
+# vim: ft=sh
diff --git a/tests/common/acme/certs/output/acme.nixos_test.cert.pem b/tests/common/acme/certs/output/acme.nixos_test.cert.pem
new file mode 100644
index 0000000..58b4de1
--- /dev/null
+++ b/tests/common/acme/certs/output/acme.nixos_test.cert.pem
@@ -0,0 +1,11 @@
+-----BEGIN CERTIFICATE-----
+MIIBmjCCAUygAwIBAgIUFdLqU4Nnr7S1XID2wC9T77hCOFwwBQYDK2VwMCoxCzAJ
+BgNVBAYTAkVVMRswGQYDVQQKExJWaGFjay5ldSBUZXN0IEtleXMwIBcNMjYxMTI1
+MjEyNzIxWhgPMjM2NDExMjkyMTI3MjFaMB0xGzAZBgNVBAoTElZoYWNrLmV1IFRl
+c3QgS2V5czAqMAUGAytlcAMhAMkG/QPjjHnBb+hbKplpVFB6H+UgW/ZvBvlBT1aE
+0AvDo4GOMIGLMAwGA1UdEwEB/wQCMAAwKwYDVR0RBCQwIoIPYWNtZS5uaXhvc190
+ZXN0gg9hY21lLm5peG9zX3Rlc3QwDgYDVR0PAQH/BAQDAgeAMB0GA1UdDgQWBBRo
+0ehOg4DRs1umtKC2Ao1flvI0HjAfBgNVHSMEGDAWgBQixZzNSQAtj5IS+2AEQBEa
+PJoG3jAFBgMrZXADQQAL7KGrXQvFi5Qf68VQmTYush5rKCaplI9Dyx6q2bjY/CdL
+0Ru36eyHML25kHdH1O/uATOUzcQ2ktQr/32rYeEF
+-----END CERTIFICATE-----
diff --git a/tests/common/acme/certs/output/acme.nixos_test.key.pem b/tests/common/acme/certs/output/acme.nixos_test.key.pem
new file mode 100644
index 0000000..6917ca3
--- /dev/null
+++ b/tests/common/acme/certs/output/acme.nixos_test.key.pem
@@ -0,0 +1,25 @@
+Public Key Info:
+ Public Key Algorithm: EdDSA (Ed25519)
+ Key Security Level: High (256 bits)
+
+curve: Ed25519
+private key:
+ 32:d5:dd:08:b1:9f:8e:6d:6a:dc:7d:95:56:ea:93:2e
+ 8a:58:95:24:ea:83:81:1a:f1:a0:91:67:98:d2:db:58
+
+
+x:
+ c9:06:fd:03:e3:8c:79:c1:6f:e8:5b:2a:99:69:54:50
+ 7a:1f:e5:20:5b:f6:6f:06:f9:41:4f:56:84:d0:0b:c3
+
+
+
+Public Key PIN:
+ pin-sha256:eavyvA0AGilDRJLrGi6ZQbJsvmglZJbNNtu43TccUUU=
+Public Key ID:
+ sha256:79abf2bc0d001a29434492eb1a2e9941b26cbe68256496cd36dbb8dd371c5145
+ sha1:68d1e84e8380d1b35ba6b4a0b6028d5f96f2341e
+
+-----BEGIN PRIVATE KEY-----
+MC4CAQAwBQYDK2VwBCIEIDLV3Qixn45tatx9lVbqky6KWJUk6oOBGvGgkWeY0ttY
+-----END PRIVATE KEY-----
diff --git a/tests/common/acme/certs/output/acme.nixos_test.template b/tests/common/acme/certs/output/acme.nixos_test.template
new file mode 100644
index 0000000..4a8ffc2
--- /dev/null
+++ b/tests/common/acme/certs/output/acme.nixos_test.template
@@ -0,0 +1,6 @@
+dns_name = "acme.nixos_test"
+dns_name = "acme.nixos_test"
+expiration_days = 123456
+organization = Vhack.eu Test Keys
+encryption_key
+signing_key
diff --git a/tests/common/acme/certs/output/ca.cert.pem b/tests/common/acme/certs/output/ca.cert.pem
new file mode 100644
index 0000000..80bcfba
--- /dev/null
+++ b/tests/common/acme/certs/output/ca.cert.pem
@@ -0,0 +1,10 @@
+-----BEGIN CERTIFICATE-----
+MIIBZjCCARigAwIBAgIUJOXiI/fNb+f0jg9A5C8R422F1fEwBQYDK2VwMCoxCzAJ
+BgNVBAYTAkVVMRswGQYDVQQKExJWaGFjay5ldSBUZXN0IEtleXMwIBcNMjYxMTI1
+MjEyNzIxWhgPMjM2NDExMjkyMTI3MjFaMCoxCzAJBgNVBAYTAkVVMRswGQYDVQQK
+ExJWaGFjay5ldSBUZXN0IEtleXMwKjAFBgMrZXADIQBB0LTe8yXxLY1AGtwTYnR8
+FKRi443xDAnMzvlgHkPEM6NOMEwwDwYDVR0TAQH/BAUwAwEB/zAaBgNVHREEEzAR
+gg9hY21lLm5peG9zX3Rlc3QwHQYDVR0OBBYEFCLFnM1JAC2PkhL7YARAERo8mgbe
+MAUGAytlcANBAJ8BFuFmb3uCLqi4R0fIXfPIZ11dGpfGZH53mo/rHZjag2LZp2s3
+vFOposan7fyE1AR8SWcAM29jDUODEEbt/gc=
+-----END CERTIFICATE-----
diff --git a/tests/common/acme/certs/output/ca.key.pem b/tests/common/acme/certs/output/ca.key.pem
new file mode 100644
index 0000000..63f4ce3
--- /dev/null
+++ b/tests/common/acme/certs/output/ca.key.pem
@@ -0,0 +1,25 @@
+Public Key Info:
+ Public Key Algorithm: EdDSA (Ed25519)
+ Key Security Level: High (256 bits)
+
+curve: Ed25519
+private key:
+ d4:e3:c9:e9:66:c5:b9:4c:cd:ff:d0:ae:3a:ae:de:69
+ 62:8b:17:3e:92:57:fd:87:06:03:f1:c1:38:65:a2:fd
+
+
+x:
+ 41:d0:b4:de:f3:25:f1:2d:8d:40:1a:dc:13:62:74:7c
+ 14:a4:62:e3:8d:f1:0c:09:cc:ce:f9:60:1e:43:c4:33
+
+
+
+Public Key PIN:
+ pin-sha256:KRMUGfl9oe0XIaRXC0iXU8vAYUe1txWgHhIScds/CrA=
+Public Key ID:
+ sha256:29131419f97da1ed1721a4570b489753cbc06147b5b715a01e121271db3f0ab0
+ sha1:22c59ccd49002d8f9212fb600440111a3c9a06de
+
+-----BEGIN PRIVATE KEY-----
+MC4CAQAwBQYDK2VwBCIEINTjyelmxblMzf/Qrjqu3mliixc+klf9hwYD8cE4ZaL9
+-----END PRIVATE KEY-----
diff --git a/tests/common/acme/certs/output/ca.template b/tests/common/acme/certs/output/ca.template
new file mode 100644
index 0000000..80c74c0
--- /dev/null
+++ b/tests/common/acme/certs/output/ca.template
@@ -0,0 +1,5 @@
+country = EU
+dns_name = "acme.nixos_test"
+expiration_days = 123456
+organization = Vhack.eu Test Keys
+ca
diff --git a/tests/common/acme/certs/snakeoil-certs.nix b/tests/common/acme/certs/snakeoil-certs.nix
new file mode 100644
index 0000000..1b4328c
--- /dev/null
+++ b/tests/common/acme/certs/snakeoil-certs.nix
@@ -0,0 +1,13 @@
+let
+ domain = "acme.nixos_test";
+in {
+ inherit domain;
+ ca = {
+ cert = ./output/ca.cert.pem;
+ key = ./output/ca.key.pem;
+ };
+ "${domain}" = {
+ cert = ./output/. + "/${domain}.cert.pem";
+ key = ./output/. + "/${domain}.key.pem";
+ };
+}
diff --git a/tests/common/acme/client.nix b/tests/common/acme/client.nix
new file mode 100644
index 0000000..2b870e8
--- /dev/null
+++ b/tests/common/acme/client.nix
@@ -0,0 +1,21 @@
+{
+ nodes,
+ lib,
+ ...
+}: let
+ inherit (nodes.acme.test-support.acme) caCert;
+ inherit (nodes.acme.test-support.acme) caDomain;
+in {
+ security = {
+ acme = {
+ acceptTerms = true;
+ defaults = {
+ server = "https://${caDomain}/dir";
+ };
+ };
+
+ pki = {
+ certificateFiles = lib.mkForce [caCert];
+ };
+ };
+}
diff --git a/tests/common/acme/default.nix b/tests/common/acme/default.nix
new file mode 100644
index 0000000..dffe40c
--- /dev/null
+++ b/tests/common/acme/default.nix
@@ -0,0 +1,44 @@
+{pkgs}: let
+ add_pebble_ca_certs = pkgs.writeShellScript "fetch-and-set-ca" ''
+ set -xe
+
+ # Fetch the randomly generated ca certificate
+ curl https://acme.nixos_test:15000/roots/0 > /tmp/ca.crt
+ curl https://acme.nixos_test:15000/intermediates/0 >> /tmp/ca.crt
+
+ # Append it to the various system stores
+ # The file paths are from <nixpgks>/modules/security/ca.nix
+ for cert_path in "ssl/certs/ca-certificates.crt" "ssl/certs/ca-bundle.crt" "pki/tls/certs/ca-bundle.crt"; do
+ cert_path="/etc/$cert_path"
+
+ mv "$cert_path" "$cert_path.old"
+ cat "$cert_path.old" > "$cert_path"
+ cat /tmp/ca.crt >> "$cert_path"
+ done
+
+ export NIX_SSL_CERT_FILE=/tmp/ca.crt
+ export SSL_CERT_FILE=/tmp/ca.crt
+
+ # TODO
+ # # P11-Kit trust source.
+ # environment.etc."ssl/trust-source".source = "$${cacertPackage.p11kit}/etc/ssl/trust-source";
+ '';
+in {
+ prepare = clients: extra:
+ # The parens are needed for the syntax highlighting to work.
+ ( # python
+ ''
+ # Start dependencies for the other services
+ start_all()
+
+ acme.wait_for_unit("pebble.service")
+ name_server.wait_for_unit("nsd.service")
+
+ with subtest("Add pebble ca key to all services"):
+ for node in [name_server, ${builtins.concatStringsSep "," clients}]:
+ node.wait_until_succeeds("curl https://acme.nixos_test:15000/roots/0")
+ node.succeed("${add_pebble_ca_certs}")
+ ''
+ )
+ + extra;
+}
diff --git a/tests/common/acme/server.nix b/tests/common/acme/server.nix
new file mode 100644
index 0000000..997c944
--- /dev/null
+++ b/tests/common/acme/server.nix
@@ -0,0 +1,91 @@
+# Add this node as acme server.
+# This also needs a DNS server.
+{
+ config,
+ pkgs,
+ lib,
+ ...
+}: let
+ testCerts = import ./certs/snakeoil-certs.nix;
+ inherit (testCerts) domain;
+
+ pebbleConf.pebble = {
+ listenAddress = "0.0.0.0:443";
+ managementListenAddress = "0.0.0.0:15000";
+
+ # The cert and key are used only for the Web Front End (WFE)
+ certificate = testCerts.${domain}.cert;
+ privateKey = testCerts.${domain}.key;
+
+ httpPort = 80;
+ tlsPort = 443;
+ ocspResponderURL = "http://${domain}:4002";
+ strict = true;
+ };
+
+ pebbleConfFile = pkgs.writeText "pebble.conf" (builtins.toJSON pebbleConf);
+in {
+ options.test-support.acme = {
+ caDomain = lib.mkOption {
+ type = lib.types.str;
+ default = domain;
+ readOnly = true;
+ description = ''
+ A domain name to use with the `nodes` attribute to
+ identify the CA server in the `client` config.
+ '';
+ };
+ caCert = lib.mkOption {
+ type = lib.types.path;
+ readOnly = true;
+ default = testCerts.ca.cert;
+ description = ''
+ A certificate file to use with the `nodes` attribute to
+ inject the test CA certificate used in the ACME server into
+ {option}`security.pki.certificateFiles`.
+ '';
+ };
+ };
+
+ config = {
+ networking = {
+ # This has priority 140, because modules/testing/test-instrumentation.nix
+ # already overrides this with priority 150.
+ nameservers = lib.mkOverride 140 ["127.0.0.1"];
+ firewall.allowedTCPPorts = [
+ 80
+ 443
+ 15000
+ 4002
+ ];
+
+ extraHosts = ''
+ 127.0.0.1 ${domain}
+ ${config.networking.primaryIPAddress} ${domain}
+ '';
+ };
+
+ systemd.services = {
+ pebble = {
+ enable = true;
+ description = "Pebble ACME server";
+ wantedBy = ["network.target"];
+ environment = {
+ # We're not testing lego, we're just testing our configuration.
+ # No need to sleep.
+ PEBBLE_VA_NOSLEEP = "1";
+ };
+
+ serviceConfig = {
+ RuntimeDirectory = "pebble";
+ WorkingDirectory = "/run/pebble";
+
+ # Required to bind on privileged ports.
+ AmbientCapabilities = ["CAP_NET_BIND_SERVICE"];
+
+ ExecStart = "${pkgs.pebble}/bin/pebble -config ${pebbleConfFile}";
+ };
+ };
+ };
+ };
+}
diff --git a/tests/common/dns/client.nix b/tests/common/dns/client.nix
new file mode 100644
index 0000000..384f1c9
--- /dev/null
+++ b/tests/common/dns/client.nix
@@ -0,0 +1,16 @@
+{
+ nodes,
+ lib,
+ ...
+}: {
+ # In the test environment, we cannot use the root dns servers to perform recursive dns
+ # resolving, as there is no internet access.
+ # Instead of doing the dance with root dns servers and what-not, we just force them
+ # to directly connect to our authoritative dns server for the zones we have.
+ networking.nameservers = lib.mkForce nodes.name_server.vhack.nsd.interfaces;
+
+ # `unbound` might be active, but it's utterly pointless in a test environment (there is
+ # no network access). So we disable it, which means that the nameservers, configured
+ # above, actually mean something.
+ vhack.unbound.enable = lib.mkForce false;
+}
diff --git a/tests/common/dns/server.nix b/tests/common/dns/server.nix
new file mode 100644
index 0000000..a3a603f
--- /dev/null
+++ b/tests/common/dns/server.nix
@@ -0,0 +1,37 @@
+{
+ nodes,
+ config,
+ ...
+}: {
+ vhack = {
+ # Enable unbound, so we can resolve our own zones.
+ unbound.enable = true;
+
+ nsd = {
+ enable = true;
+ openFirewall = true;
+ interfaces = [
+ config.networking.primaryIPAddress
+ config.networking.primaryIPv6Address
+ ];
+
+ zones = {
+ "acme.nixos_test" = {
+ SOA = {
+ nameServer = "ns";
+ adminEmail = "admin@server.com";
+ serial = 2025012301;
+ };
+ useOrigin = false;
+
+ A = [
+ nodes.acme.networking.primaryIPAddress
+ ];
+ AAAA = [
+ nodes.acme.networking.primaryIPv6Address
+ ];
+ };
+ };
+ };
+ };
+}
diff --git a/tests/common/email/dkim/alice.com/private.age b/tests/common/email/dkim/alice.com/private.age
new file mode 100644
index 0000000..cb590c6
--- /dev/null
+++ b/tests/common/email/dkim/alice.com/private.age
@@ -0,0 +1,11 @@
+-----BEGIN AGE ENCRYPTED FILE-----
+YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IHNzaC1lZDI1NTE5IFJzZ1dwUSA3bVJS
+NjBTaitjUEhxMVZmejI1L3Fmd0xGK3ZFVlM2eVBaellDYWs3ZW5nClZOckc5cFFZ
+dVlXYmxWZXpQU3lPTnlwTC9KUGNZak9VVS9aNzVEaFkrUG8KLT4gUy1ncmVhc2UK
+Y21CMkZlQVdUUHVIclRpMk5yNmRTb3lwUVQzTWM2a0YwSjhUQU1kbTRqK1lrVUtk
+ZkVVCi0tLSByRDVCeTJIK2JQWittWjlUWklSOWJaenluL1FLdXN2ZEM4bVlnU2U1
+RFk0CiAxOLq5ePeM8MemSES9Q86mBix3hooazHZZ7xCIQKnLvZmiHWCKUYw83PWl
+QpmWI7S3wgdguOncKirQccJbNtVLYb/noYEBA1y7lOzO1FELPcFgevtB3kt5EAOA
+6OaPsFKSsz71t++K011dgFUcnYmOtrqiQeKKEkZ0gey4+jNwf4buDygdiPOL/fQS
+uJiIUwsLPqZI/FQ=
+-----END AGE ENCRYPTED FILE-----
diff --git a/tests/common/email/dkim/alice.com/public b/tests/common/email/dkim/alice.com/public
new file mode 100644
index 0000000..be30723
--- /dev/null
+++ b/tests/common/email/dkim/alice.com/public
@@ -0,0 +1 @@
+LviKY6SBD2mtLJlUTon9AkWPYzB2D+Ivk0XnJOalAZM= \ No newline at end of file
diff --git a/tests/common/email/dkim/bob.com/private.age b/tests/common/email/dkim/bob.com/private.age
new file mode 100644
index 0000000..70eddca
--- /dev/null
+++ b/tests/common/email/dkim/bob.com/private.age
@@ -0,0 +1,12 @@
+-----BEGIN AGE ENCRYPTED FILE-----
+YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IHNzaC1lZDI1NTE5IFJzZ1dwUSAvSmk4
+YlQ3SElud2c3TWgxaUZCaC9mYytlbmNVdlVRcS9VTjlneFBBQ2hrCnE3b1dNdytR
+Z0l5dVlMYUhVT0ZIMkFBYkNRKzZ6U1pBYStJTUJsNUEvN28KLT4gcX5kMWtnXi1n
+cmVhc2UgczdieGAKNjNwRmVMYmN2cE5LaGFEOS9QZHFtaVhtZXI4dTFpazZhdUN4
+bUZ0cjQ4Y09nOVRjM2IzTGgweS9NbHkxMXlMSwpjdWhQekk0a0VnMTNFTXdjTmdl
+bHluNDZWT0R5L25zbXlTL3l3NnRuTWhtNWs0dVByMWQ2TlEKLS0tIEZMK2dqRktZ
+Y0hlN2l0ZVB4c3Exd3NsanRuTitrVklaaS9ZTU5ZWVF0cGsKuTNM9zBVFmOHkEqx
+jMZaJQd/XqA4WzOi/iFi4WmDUDb8XcVsS0s/37fyZfeMW3YjoCzDb/treKMXyTDC
+8l5N/xMG20UGC/Z16W24c/MXMFzpDLuFrjHiUtpQvBzwi6JUDMlKC8t0YWNcqQRr
+2vu7qij0XgasxrGDg86hoc72YmYPmpHnE86O1il0PWy9sDRlkn6Ah/DUNg==
+-----END AGE ENCRYPTED FILE-----
diff --git a/tests/common/email/dkim/bob.com/public b/tests/common/email/dkim/bob.com/public
new file mode 100644
index 0000000..915188c
--- /dev/null
+++ b/tests/common/email/dkim/bob.com/public
@@ -0,0 +1 @@
+6USjzSE5zuUNrMdgsjAHFnq0BRKmyZh2IoXFHBCv3To= \ No newline at end of file
diff --git a/tests/common/email/dkim/gen_key.sh b/tests/common/email/dkim/gen_key.sh
new file mode 100755
index 0000000..48b4434
--- /dev/null
+++ b/tests/common/email/dkim/gen_key.sh
@@ -0,0 +1,35 @@
+#! /usr/bin/env nix-shell
+#! nix-shell -p rage -p openssl -p dash -i dash --impure
+
+# shellcheck shell=dash
+
+cd "$(dirname "$0")" || {
+ echo "No basedir?!"
+ exit 1
+}
+
+key_name="$1"
+[ -z "$key_name" ] && {
+ echo "Usage: $0 KEY_NAME"
+ exit 2
+}
+
+[ -d "$key_name" ] || mkdir "$key_name"
+cd "$key_name" || {
+ echo "Just created."
+ exit 1
+}
+
+openssl genpkey -algorithm ed25519 -out "private"
+openssl pkey -in "private" -pubout -out "public.tmp"
+
+openssl asn1parse -in "public.tmp" -offset 12 -noout -out /dev/stdout | base64 --wrap 0 >"public"
+rm "public.tmp"
+
+rage --encrypt \
+ --armor \
+ --recipient "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILxdvBk/PC9fC7B5vqe9TvygZKY6LgDQ2mXRdVrthBM/" \
+ "private" >"private.age"
+rm "private"
+
+# vim: ft=sh
diff --git a/tests/common/email/dkim/mail.server1/private.age b/tests/common/email/dkim/mail.server1/private.age
new file mode 100644
index 0000000..a97c70b
--- /dev/null
+++ b/tests/common/email/dkim/mail.server1/private.age
@@ -0,0 +1,13 @@
+-----BEGIN AGE ENCRYPTED FILE-----
+YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IHNzaC1lZDI1NTE5IFJzZ1dwUSBndElV
+WFFTcEZpTGwrTUg5WHk1MElLSU5ZZWp1NU5ocit4VGtnM09KZFNBCjNvaWVPR3Zt
+RG14N2E3b0RwK21IUzBZZmg1UzFwakMwTVVOdDgrTTd4U1UKLT4geD9OYm0tZ3Jl
+YXNlIDwgcjkKQUJtaDlVbzk3Slk3S1ozNS9STGwrdzhvWTlodWgxNVd1WVRadTV5
+VmFVUmRmSTV3ckIzYmpQYUFsMUxkdGNzUgo1UXozYzBCK3UxU1BPSnlvVVVxQjI2
+QVdWY0tBbUg1R3p5NmNaZ2RiVGNlL3pWN3FHZU5QOEN2VVdraDBzM1R2CgotLS0g
+TElGejJDYUk4WlhxajdXekI4S0tUMVlrNmdxelpjUnBUa1VCU2krSFloawpUfMj8
+iqVP45yV8fON6fDUur6q/UYqTaaHhCW07LTsBZQpReG8LA+PiOhHw1TmPSJlc/JH
+yEkwLachwvLGCGEO2yoFUFhYwoNhl3vrBsuDLXpqfNuoPrg3wcFXBdrg9zBjZ5Pc
+C/rdzpysjW/52gKRCdGgFj2WyTW2wO2yVmmUk1RWhMabmdd+XLkKhynaMtWsBF0j
+NQ3F
+-----END AGE ENCRYPTED FILE-----
diff --git a/tests/common/email/dkim/mail.server1/public b/tests/common/email/dkim/mail.server1/public
new file mode 100644
index 0000000..6c9e653
--- /dev/null
+++ b/tests/common/email/dkim/mail.server1/public
@@ -0,0 +1 @@
+3/bsVrnWag3eMCuj78VpsHDQSONi97fIzY+WPKvAYWM= \ No newline at end of file
diff --git a/tests/common/email/dkim/mail.server2/private.age b/tests/common/email/dkim/mail.server2/private.age
new file mode 100644
index 0000000..fbd88fc
--- /dev/null
+++ b/tests/common/email/dkim/mail.server2/private.age
@@ -0,0 +1,11 @@
+-----BEGIN AGE ENCRYPTED FILE-----
+YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IHNzaC1lZDI1NTE5IFJzZ1dwUSBTcktS
+L0ovODhxd2tyWFdINWRtbXNNYksyNzl6RUxRcTU2T251eVNhQlg4CjVzTVpnc0FC
+eHBNaUtEQnJVRXVpTjg2c3gxcC90VXJ3cUJVaHZJY1pnb00KLT4gKDEkKS1ncmVh
+c2UgYnYqQSBAVSU+CnVRbkRhdUhRS0Z6NGIybll1WVdmSmxzS0lxYlAwSXp0TnA4
+anRDSmQKLS0tIGxxSXRaM0lKeXRoeHFxWW9qN3Y1OE55b1FBVGJTZzB2RmFmZVk5
+eW94aW8KleHhm6fKalehWR0IfHYiC+0bsm0SeYjnJeA60hePdwikmr0QQv5D2xCX
+Nf2QRuai/BpsiJpnvKUiP1uwkm5x6a2Zgwa7t8BwJ7SHqD4oJ+VgCsWMffLs55uC
+A1pKLRmAI26cNt7oibVxNSCCuxZRWeCn+xAsQ393jVCUuDW2FkgyDsKFYHvKRpnC
+BNfwDWWHJ+pifPPrqg==
+-----END AGE ENCRYPTED FILE-----
diff --git a/tests/common/email/dkim/mail.server2/public b/tests/common/email/dkim/mail.server2/public
new file mode 100644
index 0000000..40b6ff7
--- /dev/null
+++ b/tests/common/email/dkim/mail.server2/public
@@ -0,0 +1 @@
+QmbJbp/b671OCOQa0Qx1XGl7c1jzFuASuzBr1f5ATpE= \ No newline at end of file
diff --git a/tests/common/email/functions.nix b/tests/common/email/functions.nix
new file mode 100644
index 0000000..0628a2f
--- /dev/null
+++ b/tests/common/email/functions.nix
@@ -0,0 +1,218 @@
+{
+ pkgs,
+ vhackPackages,
+}: {
+ scripts = {
+ expectNoNewMail = pkgs.writeShellScript "assert-empty-emails" ''
+ set -xe
+
+ # fetchmail returns EXIT_CODE 1 when no new mail
+ fetchmail --verbose >&2 || [ "$?" -eq 1 ] || {
+ echo "Mail was not empty" >&2
+ exit 1
+ }
+ '';
+ expectNewMail = pkgs.writeShellScript "assert-new-emails" ''
+ set -xe
+
+ # fetchmail returns EXIT_CODE 1 when no new mail
+ fetchmail --verbose >&2 || [ "$?" -ne 1 ] || {
+ echo "No new mail" >&2
+ exit 1
+ }
+ '';
+ expectNewSpamMail = pkgs.writeShellScript "assert-new-spam-emails" ''
+ set -xe
+
+ # fetchmail returns EXIT_CODE 1 when no new mail
+ fetchmail --folder JUNK --verbose >&2 || [ "$?" -ne 1 ] || {
+ echo "No new spam mail" >&2
+ exit 1
+ }
+ '';
+ };
+
+ mkMailServer = serverName: userName: {
+ config,
+ lib,
+ nodes,
+ ...
+ }: {
+ age.identityPaths = ["${../../common/email/hostKey}"];
+
+ systemd.tmpfiles.rules = [
+ #- path mode user group - content
+ "f /passwords/${userName} 777 root root - ${userName}-password"
+ ];
+
+ age.secrets = {
+ "mailDkim" = {
+ file = ../../common/email/dkim + "/${serverName}/private.age";
+ mode = "600";
+ owner = "rspamd";
+ group = "rspamd";
+ };
+ };
+
+ # Disable rspamd. rspamd can't do the dns lookups in the sandbox for
+ # determining if something is spam or not. Therefore it just rejects every mail with
+ # 'Try again later.'.
+ # services.rspamd.enable = lib.mkForce false;
+ # users.users.rspamd.group = "rspamd";
+ # users.groups.rspamd = {};
+ #
+ # services.postfix.settings.main = {
+ # smtpd_milters = lib.mkForce [];
+ # non_smtpd_milters = lib.mkForce [];
+ # };
+ # mailserver.dkim.enable = false;
+ services = {
+ rspamd = {
+ locals = {
+ "asn.conf".text = lib.mkForce ''
+ enabled = false;
+ '';
+ "rbl.conf".text = lib.mkForce ''
+ enabled = false;
+ '';
+ "greylist.conf".text = lib.mkForce ''
+ enabled = false;
+ '';
+ };
+ };
+ };
+
+ vhack = {
+ mail = {
+ enable = true;
+ fqdn = serverName;
+ domains = [
+ serverName
+ ];
+
+ security.dkimKeys."${serverName}" = {
+ dkimPrivateKeyPath = config.age.secrets."mailDkim".path;
+ dkimPublicKey =
+ builtins.readFile (../../common/email/dkim + "/${serverName}/public");
+ keyAlgorithm = "rsa-sha-256";
+ };
+
+ accounts = {
+ "${userName}@${serverName}" = {
+ passwordFile = "/passwords/${userName}";
+ };
+ };
+ };
+ };
+ };
+
+ mkDnsZone = serverName: {config, ...}: let
+ cfg = config.vhack.mail;
+
+ keyAlgoToKeyType = keyAlgo:
+ if keyAlgo == "ed25519-sha256"
+ then "ed25519"
+ else if keyAlgo == "rsa-sha-256" || keyAlgo == "rsa-sha-1"
+ then "rsa"
+ else builtins.throw "Impossible";
+ in {
+ "${serverName}" = {
+ MX = [
+ {
+ preference = 10;
+ exchange = "${serverName}.";
+ }
+ ];
+
+ # https://www.rfc-editor.org/rfc/rfc6376.html#section-3.6.1
+ # https://www.rfc-editor.org/rfc/rfc6376.html#section-7.5
+ DKIM = [
+ {
+ selector = "mail";
+ k = keyAlgoToKeyType cfg.security.dkimKeys."${serverName}".keyAlgorithm;
+ p = cfg.security.dkimKeys."${serverName}".dkimPublicKey;
+ s = ["email"];
+ t = ["s"];
+ }
+ ];
+
+ # https://www.rfc-editor.org/rfc/rfc7489.html#section-6.3
+ DMARC = [
+ {
+ adkim = "strict";
+ aspf = "strict";
+ fo = ["0" "1" "d" "s"];
+ p = "quarantine";
+ rua = "admin@vhack.eu";
+ ruf = ["admin@vhack.eu"];
+ }
+ ];
+
+ # https://www.rfc-editor.org/rfc/rfc7208.html
+ # NOTE(@bpeetz): This server might not be directly sending mail, but it is still required for
+ # the SMTP EHLO check. <2025-02-25>
+ TXT = [
+ (builtins.concatStringsSep " "
+ [
+ "v=spf1" # The version.
+ "+mx" # Allow mail from this domain MX record.
+ "-all" # Reject all other emails if the previous mechanism did not match.
+ ])
+ ];
+ };
+ };
+
+ mkUser = user: mailServer: {nodes, ...}: {
+ environment.systemPackages = [
+ vhackPackages.fetchmail-common-name
+ pkgs.msmtp
+ pkgs.procmail
+ ];
+
+ users.users."${user}" = {isNormalUser = true;};
+
+ systemd.tmpfiles.rules = [
+ "d /home/${user}/mail 0700 ${user} users - -"
+ "L /home/${user}/.fetchmailrc - - - - /etc/homeSetup/.fetchmailrc"
+ "L /home/${user}/.procmailrc - - - - /etc/homeSetup/.procmailrc"
+ "L /home/${user}/.msmtprc - - - - /etc/homeSetup/.msmtprc"
+ ];
+
+ environment.etc = {
+ "homeSetup/.fetchmailrc" = {
+ text = ''
+ poll "${mailServer}" protocol IMAP
+ username "${user}@${mailServer}"
+ password "${user}-password"
+ ssl
+ mda procmail;
+ '';
+ mode = "0600";
+ inherit user;
+ };
+ "homeSetup/.procmailrc" = {
+ text = ''
+ DEFAULT=$HOME/mail
+ '';
+ mode = "0600";
+ inherit user;
+ };
+ "homeSetup/.msmtprc" = {
+ text = ''
+ account ${user}
+ host ${mailServer}
+ domain ${mailServer}
+ port 465
+ from ${user}@${mailServer}
+ user ${user}@${mailServer}
+ password ${user}-password
+ auth on
+ tls on
+ tls_starttls off
+ '';
+ mode = "0600";
+ inherit user;
+ };
+ };
+ };
+}
diff --git a/tests/common/email/hostKey b/tests/common/email/hostKey
new file mode 100644
index 0000000..79c9d6c
--- /dev/null
+++ b/tests/common/email/hostKey
@@ -0,0 +1,7 @@
+-----BEGIN OPENSSH PRIVATE KEY-----
+b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
+QyNTUxOQAAACC8XbwZPzwvXwuweb6nvU78oGSmOi4A0Npl0XVa7YQTPwAAAJjFZPqHxWT6
+hwAAAAtzc2gtZWQyNTUxOQAAACC8XbwZPzwvXwuweb6nvU78oGSmOi4A0Npl0XVa7YQTPw
+AAAEA9D5AP+Uqhrg8rPx2DjgucjfnJknkk7lkeKHMV04ZZv7xdvBk/PC9fC7B5vqe9Tvyg
+ZKY6LgDQ2mXRdVrthBM/AAAAFSAnUHVibGljIHRlc3Rpbmcga2V5Jw==
+-----END OPENSSH PRIVATE KEY-----