git.iko.soy/typeable/nixpkgs
on master help sign in
Merge #56922: nixos/knot: init basic service + tests
commit3aecf21239d105aadad229595348a0da4bcdbcd8 authorVladimír Čunát <v@cunat.cz> dateSat 16 Mar 2019, 09:17 (8 years ago) parent056b9d0a978d3d
4 files changed+294
nixos/modules/module-list.nix+1 nixos/modules/services/networking/knot.nix+95 nixos/tests/all-tests.nix+1 nixos/tests/knot.nix+197
nixos/modules/module-list.nix+1
@@ -578,6 +578,7 @@
578578 ./services/networking/keepalived/default.nix
579579 ./services/networking/keybase.nix
580580 ./services/networking/kippo.nix
581+ ./services/networking/knot.nix
581582 ./services/networking/kresd.nix
582583 ./services/networking/lambdabot.nix
583584 ./services/networking/libreswan.nix
nixos/modules/services/networking/knot.nixnew file+95
@@ -0,0 +1,95 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+
5+let
6+ cfg = config.services.knot;
7+
8+ configFile = pkgs.writeText "knot.conf" cfg.extraConfig;
9+ socketFile = "/run/knot/knot.sock";
10+
11+ knotConfCheck = file: pkgs.runCommand "knot-config-checked"
12+ { buildInputs = [ cfg.package ]; } ''
13+ ln -s ${configFile} $out
14+ knotc --config=${configFile} conf-check
15+ '';
16+
17+ knot-cli-wrappers = pkgs.stdenv.mkDerivation {
18+ name = "knot-cli-wrappers";
19+ buildInputs = [ pkgs.makeWrapper ];
20+ buildCommand = ''
21+ mkdir -p $out/bin
22+ makeWrapper ${cfg.package}/bin/knotc "$out/bin/knotc" \
23+ --add-flags "--config=${configFile}" \
24+ --add-flags "--socket=${socketFile}"
25+ makeWrapper ${cfg.package}/bin/keymgr "$out/bin/keymgr" \
26+ --add-flags "--config=${configFile}"
27+ for executable in kdig khost kjournalprint knsec3hash knsupdate kzonecheck
28+ do
29+ ln -s "${cfg.package}/bin/$executable" "$out/bin/$executable"
30+ done
31+ mkdir -p "$out/share"
32+ ln -s '${cfg.package}/share/man' "$out/share/"
33+ '';
34+ };
35+in {
36+ options = {
37+ services.knot = {
38+ enable = mkEnableOption "Knot authoritative-only DNS server";
39+
40+ extraArgs = mkOption {
41+ type = types.listOf types.str;
42+ default = [];
43+ description = ''
44+ List of additional command line paramters for knotd
45+ '';
46+ };
47+
48+ extraConfig = mkOption {
49+ type = types.lines;
50+ default = "";
51+ description = ''
52+ Extra lines to be added verbatim to knot.conf
53+ '';
54+ };
55+
56+ package = mkOption {
57+ type = types.package;
58+ default = pkgs.knot-dns;
59+ description = ''
60+ Which Knot DNS package to use
61+ '';
62+ };
63+ };
64+ };
65+
66+ config = mkIf config.services.knot.enable {
67+ systemd.services.knot = {
68+ unitConfig.Documentation = "man:knotd(8) man:knot.conf(5) man:knotc(8) https://www.knot-dns.cz/docs/${cfg.package.version}/html/";
69+ description = cfg.package.meta.description;
70+ wantedBy = [ "multi-user.target" ];
71+ wants = [ "network.target" ];
72+ after = ["network.target" ];
73+
74+ serviceConfig = {
75+ Type = "notify";
76+ ExecStart = "${cfg.package}/bin/knotd --config=${knotConfCheck configFile} --socket=${socketFile} ${concatStringsSep " " cfg.extraArgs}";
77+ ExecReload = "${knot-cli-wrappers}/bin/knotc reload";
78+ CapabilityBoundingSet = "CAP_NET_BIND_SERVICE CAP_SETPCAP";
79+ AmbientCapabilities = "CAP_NET_BIND_SERVICE CAP_SETPCAP";
80+ NoNewPrivileges = true;
81+ DynamicUser = "yes";
82+ RuntimeDirectory = "knot";
83+ StateDirectory = "knot";
84+ StateDirectoryMode = "0700";
85+ PrivateDevices = true;
86+ RestrictAddressFamilies = "AF_UNIX AF_INET AF_INET6";
87+ SystemCallArchitectures = "native";
88+ Restart = "on-abort";
89+ };
90+ };
91+
92+ environment.systemPackages = [ knot-cli-wrappers ];
93+ };
94+}
95+
nixos/tests/all-tests.nix+1
@@ -117,6 +117,7 @@ in
117117 kernel-latest = handleTest ./kernel-latest.nix {};
118118 kernel-lts = handleTest ./kernel-lts.nix {};
119119 keymap = handleTest ./keymap.nix {};
120+ knot = handleTest ./knot.nix {};
120121 kubernetes.dns = handleTestOn ["x86_64-linux"] ./kubernetes/dns.nix {};
121122 # kubernetes.e2e should eventually replace kubernetes.rbac when it works
122123 #kubernetes.e2e = handleTestOn ["x86_64-linux"] ./kubernetes/e2e.nix {};
nixos/tests/knot.nixnew file+197
@@ -0,0 +1,197 @@
1+import ./make-test.nix ({ pkgs, lib, ...} :
2+let
3+ common = {
4+ networking.firewall.enable = false;
5+ networking.useDHCP = false;
6+ };
7+ exampleZone = pkgs.writeTextDir "example.com.zone" ''
8+ @ SOA ns.example.com. noc.example.com. 2019031301 86400 7200 3600000 172800
9+ @ NS ns1
10+ @ NS ns2
11+ ns1 A 192.168.0.1
12+ ns1 AAAA fd00::1
13+ ns2 A 192.168.0.2
14+ ns2 AAAA fd00::2
15+ www A 192.0.2.1
16+ www AAAA 2001:DB8::1
17+ sub NS ns.example.com.
18+ '';
19+ delegatedZone = pkgs.writeTextDir "sub.example.com.zone" ''
20+ @ SOA ns.example.com. noc.example.com. 2019031301 86400 7200 3600000 172800
21+ @ NS ns1.example.com.
22+ @ NS ns2.example.com.
23+ @ A 192.0.2.2
24+ @ AAAA 2001:DB8::2
25+ '';
26+
27+ knotZonesEnv = pkgs.buildEnv {
28+ name = "knot-zones";
29+ paths = [ exampleZone delegatedZone ];
30+ };
31+in {
32+ name = "knot";
33+
34+ nodes = {
35+ master = { lib, ... }: {
36+ imports = [ common ];
37+ networking.interfaces.eth1 = {
38+ ipv4.addresses = lib.mkForce [
39+ { address = "192.168.0.1"; prefixLength = 24; }
40+ ];
41+ ipv6.addresses = lib.mkForce [
42+ { address = "fd00::1"; prefixLength = 64; }
43+ ];
44+ };
45+ services.knot.enable = true;
46+ services.knot.extraArgs = [ "-v" ];
47+ services.knot.extraConfig = ''
48+ server:
49+ listen: 0.0.0.0@53
50+ listen: ::@53
51+
52+ acl:
53+ - id: slave_acl
54+ address: 192.168.0.2
55+ action: transfer
56+
57+ remote:
58+ - id: slave
59+ address: 192.168.0.2@53
60+
61+ template:
62+ - id: default
63+ storage: ${knotZonesEnv}
64+ notify: [slave]
65+ acl: [slave_acl]
66+ dnssec-signing: on
67+ # Input-only zone files
68+ # https://www.knot-dns.cz/docs/2.8/html/operation.html#example-3
69+ # prevents modification of the zonefiles, since the zonefiles are immutable
70+ zonefile-sync: -1
71+ zonefile-load: difference
72+ journal-content: changes
73+ # move databases below the state directory, because they need to be writable
74+ journal-db: /var/lib/knot/journal
75+ kasp-db: /var/lib/knot/kasp
76+ timer-db: /var/lib/knot/timer
77+
78+ zone:
79+ - domain: example.com
80+ file: example.com.zone
81+
82+ - domain: sub.example.com
83+ file: sub.example.com.zone
84+
85+ log:
86+ - target: syslog
87+ any: info
88+ '';
89+ };
90+
91+ slave = { lib, ... }: {
92+ imports = [ common ];
93+ networking.interfaces.eth1 = {
94+ ipv4.addresses = lib.mkForce [
95+ { address = "192.168.0.2"; prefixLength = 24; }
96+ ];
97+ ipv6.addresses = lib.mkForce [
98+ { address = "fd00::2"; prefixLength = 64; }
99+ ];
100+ };
101+ services.knot.enable = true;
102+ services.knot.extraArgs = [ "-v" ];
103+ services.knot.extraConfig = ''
104+ server:
105+ listen: 0.0.0.0@53
106+ listen: ::@53
107+
108+ acl:
109+ - id: notify_from_master
110+ address: 192.168.0.1
111+ action: notify
112+
113+ remote:
114+ - id: master
115+ address: 192.168.0.1@53
116+
117+ template:
118+ - id: default
119+ master: master
120+ acl: [notify_from_master]
121+ # zonefileless setup
122+ # https://www.knot-dns.cz/docs/2.8/html/operation.html#example-2
123+ zonefile-sync: -1
124+ zonefile-load: none
125+ journal-content: all
126+ # move databases below the state directory, because they need to be writable
127+ journal-db: /var/lib/knot/journal
128+ kasp-db: /var/lib/knot/kasp
129+ timer-db: /var/lib/knot/timer
130+
131+ zone:
132+ - domain: example.com
133+ file: example.com.zone
134+
135+ - domain: sub.example.com
136+ file: sub.example.com.zone
137+
138+ log:
139+ - target: syslog
140+ any: info
141+ '';
142+ };
143+ client = { lib, nodes, ... }: {
144+ imports = [ common ];
145+ networking.interfaces.eth1 = {
146+ ipv4.addresses = [
147+ { address = "192.168.0.3"; prefixLength = 24; }
148+ ];
149+ ipv6.addresses = [
150+ { address = "fd00::3"; prefixLength = 64; }
151+ ];
152+ };
153+ environment.systemPackages = [ pkgs.knot-dns ];
154+ };
155+ };
156+
157+ testScript = { nodes, ... }: let
158+ master4 = (lib.head nodes.master.config.networking.interfaces.eth1.ipv4.addresses).address;
159+ master6 = (lib.head nodes.master.config.networking.interfaces.eth1.ipv6.addresses).address;
160+
161+ slave4 = (lib.head nodes.slave.config.networking.interfaces.eth1.ipv4.addresses).address;
162+ slave6 = (lib.head nodes.slave.config.networking.interfaces.eth1.ipv6.addresses).address;
163+ in ''
164+ startAll;
165+
166+ $client->waitForUnit("network.target");
167+ $master->waitForUnit("knot.service");
168+ $slave->waitForUnit("knot.service");
169+
170+ sub assertResponse {
171+ my ($knot, $query_type, $query, $expected) = @_;
172+ my $out = $client->succeed("khost -t $query_type $query $knot");
173+ $client->log("$knot replies with: $out");
174+ chomp $out;
175+ die "DNS query for $query ($query_type) against $knot gave '$out' instead of '$expected'"
176+ if ($out !~ $expected);
177+ }
178+
179+ foreach ("${master4}", "${master6}", "${slave4}", "${slave6}") {
180+ subtest $_, sub {
181+ assertResponse($_, "SOA", "example.com", qr/start of authority.*?noc\.example\.com/);
182+ assertResponse($_, "A", "example.com", qr/has no [^ ]+ record/);
183+ assertResponse($_, "AAAA", "example.com", qr/has no [^ ]+ record/);
184+
185+ assertResponse($_, "A", "www.example.com", qr/address 192.0.2.1$/);
186+ assertResponse($_, "AAAA", "www.example.com", qr/address 2001:db8::1$/);
187+
188+ assertResponse($_, "NS", "sub.example.com", qr/nameserver is ns\d\.example\.com.$/);
189+ assertResponse($_, "A", "sub.example.com", qr/address 192.0.2.2$/);
190+ assertResponse($_, "AAAA", "sub.example.com", qr/address 2001:db8::2$/);
191+
192+ assertResponse($_, "RRSIG", "www.example.com", qr/RR set signature is/);
193+ assertResponse($_, "DNSKEY", "example.com", qr/DNSSEC key is/);
194+ };
195+ }
196+ '';
197+})