]> git.immae.eu Git - perso/Immae/Config/Nix.git/commitdiff
Add keys handling for bind9
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Sat, 1 May 2021 19:07:09 +0000 (21:07 +0200)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Sat, 1 May 2021 19:07:09 +0000 (21:07 +0200)
modules/private/dns.nix
modules/private/environment.nix
nixops/secrets

index cb900ffad6ca725b9853666d6055c095117b716f..1149daf6787508f36befea23903b7360774f80e6 100644 (file)
@@ -3,6 +3,15 @@
   options.myServices.dns.enable = lib.mkEnableOption "enable DNS resolver";
   config = let
     cfg = config.services.bind;
+    keyIncludes = builtins.concatStringsSep "\n" (map (v: "include \"/var/secrets/bind/${v}.key\";") (builtins.attrNames config.myEnv.dns.keys));
+    toKeyList = servers: keys: builtins.concatStringsSep "\n" (map (s: ''
+      server ${s} {
+        keys { ${builtins.concatStringsSep ";" keys}; };
+      };
+    '') servers);
+    serverIncludes = builtins.concatStringsSep "\n" (map (v:
+      lib.optionalString (builtins.length v.keys > 0) (toKeyList (lib.flatten (map (n: builtins.attrValues config.myEnv.dns.ns."${n}") v.masters)) v.keys)
+    ) config.myEnv.dns.slaveZones);
     configFile = pkgs.writeText "named.conf" ''
       include "/etc/bind/rndc.key";
       controls {
@@ -24,6 +33,9 @@
         ${cfg.extraOptions}
       };
 
+      ${keyIncludes}
+      ${serverIncludes}
+
       ${cfg.extraConfig}
 
       ${ lib.concatMapStrings
   in lib.mkIf config.myServices.dns.enable {
     networking.firewall.allowedUDPPorts = [ 53 ];
     networking.firewall.allowedTCPPorts = [ 53 ];
+    users.users.named.extraGroups = [ "keys" ];
+    secrets.keys = lib.mapAttrsToList (k: v:
+      {
+        dest = "bind/${k}.key";
+        permissions = "0400";
+        user = "named";
+        text = ''
+          key "${k}"
+            {
+              algorithm ${v.algorithm};
+              secret "${v.secret}";
+            };
+        '';
+      }
+    ) config.myEnv.dns.keys;
     services.bind = {
       enable = true;
       cacheNetworks = ["any"];
index 9cd591ef081585b874013af41dd3c37c5fe66b90..32af33989b6d5af31d53029485c9b73da4ceda06 100644 (file)
@@ -384,6 +384,16 @@ in
             };
             type = attrsOf (attrsOf (listOf str));
           };
+          keys = mkOption {
+            default = {};
+            description = "DNS keys";
+            type = attrsOf (submodule {
+              options = {
+                algorithm = mkOption { type = str; description = "Algorithm"; };
+                secret    = mkOption { type = str; description = "Secret"; };
+              };
+            });
+          };
           slaveZones = mkOption {
             description = "List of slave zones";
             type = listOf (submodule {
@@ -393,6 +403,11 @@ in
                   description = "NS master groups of this zone";
                   type = listOf str;
                 };
+                keys = mkOption {
+                  default = [];
+                  description = "Keys associated to the server";
+                  type = listOf str;
+                };
               };
             });
           };
index e816753f9ffc851a6f1e35cfa267a03e2a0ad2aa..bbc6606211e970d0df974f0f74693f48186aea9c 160000 (submodule)
@@ -1 +1 @@
-Subproject commit e816753f9ffc851a6f1e35cfa267a03e2a0ad2aa
+Subproject commit bbc6606211e970d0df974f0f74693f48186aea9c