Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: txt records for too long values #14

Merged
merged 3 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ jobs:
run: nix build .#docs

- name: "Try building example"
run: nix build ./example#octodns
run: nix build --override-input nixos-dns $(pwd)/ ./example#octodns

- name: "Check zonefile with bind"
run: nix build --override-input nixos-dns $(pwd)/ ./example#zoneFiles && nix shell nixpkgs#bind --command named-checkzone example.com result/example.com
18 changes: 18 additions & 0 deletions example/dns.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,32 @@
zones = {
"example.com" = {
"" = {
soa = {
data = {
rname = "admin.example.invalid";
mname = "ns.example.invalid";
serial = 1970010100;
refresh = 7200;
retry = 3600;
ttl = 60;
expire = 1209600;
};
};
ns = {
data = [
"ns1.invalid"
"ns2.invalid"
"ns3.invalid"
];
};
txt = {
data = [
"meow"
"v=spf1 a:mail.example.com -all"
];
};
};
"mail._domainkey".txt.data = "v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2WJ46bl9UqBY9ZxqkVCBdSiysIJMUbWS3BK10Lupe4T5+jWAcdzJraznWeaVF/mR/9TyiB7lE79ZB6WxHxTwwJ5UZjURwImKAKqSGPXPACIj+LHyx5j2nHN4CawC6bkCmpGT99B7I/5bCelekoAHV9U/4pE2YEjgA0VxvlSKHB2Y7cPWL303DInYGaTrvMczuwLYoEwIiBirffYNqHyrOJE9A+ZQRdLjM8DFOxegAOV9mcHb3MwneJuu86Czz45UIrQ7AxkMUNKgHitqTSnXzLWd4BF6Kf3XUh/lED7WPdviBLJo/1H0Cgch8RRlinTeDVliHDQ6/zLWpk6+k3iKkQIDAQAB; s=*;";
};
"example.net" = {
"" = {
Expand Down
51 changes: 44 additions & 7 deletions example/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions utils/tests/zonefiles.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
utils,
}:
{
testFormatTxtRecordShorter255 = {
expr = utils.zonefiles.formatTxtRecord "meow";
expected = "\"meow\"";
};
testFormatTxtRecordLonger255 = {
expr = utils.zonefiles.formatTxtRecord "v=DKIM1; k=rsa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc";
expected = "\"v=DKIM1; k=rsa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" \"aaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\" \"bbbbbbbbbbbbbbbcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc\" \"ccccccccccccccc\"";
};
testWriteZoneFile = {
expr = builtins.readFile (
utils.zonefiles.write "example.com" {
Expand Down
44 changes: 43 additions & 1 deletion utils/zonefiles.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,41 @@
{ lib, utils }:
{
/*
Converts a string into a valid txt record so it's compliant with RFC 4408
This means it splits the string every 255 chars and surrounds it with quotation marks

Type:
utils.zonefiles.formatTxtRecord :: String -> String
*/
formatTxtRecord =
# The String of a txt resource record
txtString:
let
format =
{
acc ? [ ],
chars,
}:
let
rest = [ (lib.concatStrings (lib.take 255 chars)) ];
in
if (lib.length chars) > 255 then
format {
acc = acc ++ rest;
chars = lib.drop 255 chars;
}
else if acc != [ ] then
acc ++ rest
else
rest;
resolve = lib.concatStringsSep "\" \"" (format {
chars = lib.stringToCharacters txtString;
});
in
"\"${resolve}\"";

/*
attributeset
Takes any record from the module and converts it to a fitting zonefile string

Type:
Expand All @@ -24,10 +59,17 @@
else if record == "soa" then
"SOA ${value.mname}. ${value.rname}. ( ${builtins.toString value.serial} ${builtins.toString value.refresh} ${builtins.toString value.retry} ${builtins.toString value.expire} ${builtins.toString value.ttl} )"
else if record == "txt" then
"TXT \"${value}\""
"TXT ${utils.zonefiles.formatTxtRecord value}"
else
"${lib.toUpper record} ${value}";
/*
Converts a zone attributeset into a zonefile and returns a multiline string

Type:
utils.zonefiles.mkZoneString :: Attr -> String
*/
mkZoneString =
# Takes dnsConfig."your-domain.invalid"
entries:
''${lib.concatLines (
lib.flatten (
Expand Down
Loading