pdns icon indicating copy to clipboard operation
pdns copied to clipboard

Feature request: Global include of LUA scripts

Open veitw opened this issue 2 years ago • 6 comments

Description

Please add a feature to authoritative server that allows a common LUA script to be loaded when the server starts.

Primary use case

Creating new LUA functions that work just like the built-in functions.

Example

Switch RR value on a given time for a planned migration scenario.

Solution A so far

Zone:

migratethis     LUA A "os.time() < 1654174980 and '127.0.0.1' or '127.0.1.1'"
migratethat     LUA A "os.time() < 1654174980 and '127.0.0.2' or '127.0.1.2'"
migratewhatever LUA A "os.time() < 1654174980 and '127.0.0.3' or '127.0.1.3'"

Solution B so far

Zone:

switch LUA LUA (" function switchontime(time, before, after) "
                "   if(os.time() < time) then                "
                "     return before                          "
                "   else                                     "
                "     return after                           "
                "   end                                      "
                " end                                        ")

migratethis     LUA A ";include('switch') return switchontime(1654174980, '127.0.0.1', '127.0.1.1')"
migratethat     LUA A ";include('switch') return switchontime(1654174980, '127.0.0.2', '127.0.1.2')"
migratewhatever LUA A ";include('switch') return switchontime(1654174980, '127.0.0.3', '127.0.1.3')"

Proposed solution with new feature

/etc/pdns/pdns.conf:

lua-include-common = /etc/pdns/common-lua.d/*.lua

/etc/pdns/common-lua.d/switchontime.lua

function switchontime(time, before, after)
  if(os.time() < time) then
    return before
  else
    return after
  end
end

Zone:

migratethis     LUA A "switchontime(1654174980, '127.0.0.1', '127.0.1.1')"
migratethat     LUA A "switchontime(1654174980, '127.0.0.2', '127.0.1.2')"
migratewhatever LUA A "switchontime(1654174980, '127.0.0.3', '127.0.1.3')"

veitw avatar Jun 03 '22 09:06 veitw

lua-include-common = /etc/pdns/common-lua.d/*.lua

we'd probably take a dir, or a file (plus offering a function to include a dir), instead of handling wildcards here, but that's a detail

Habbie avatar Jun 03 '22 09:06 Habbie

If there are multiple scripts in the directory, the order may matter, which can lead to problems

paddg avatar Jun 03 '22 09:06 paddg

If there are multiple scripts in the directory, the order may matter, which can lead to problems

Good point. Sorting alphabetically before loading would be a good idea, so one could prefix files with e.g. "00-" to "99-" to fix the order.

veitw avatar Jun 03 '22 09:06 veitw

we'd probably take a dir, or a file (plus offering a function to include a dir), instead of handling wildcards here, but that's a detail

My intention was to use a wildcard to give the admin a configurable possibility to only use files with a given extension.

If including a directory, only files with names ending in .lua should be loaded, so packagers may ship inactive .example files or a README in this directory, and package managers could rename them on uninstall/reinstall or upgrade when one had been altered (for rpm based systems e.g. to *.rpmsave or *.rpmnew), so they are not loaded by default.

veitw avatar Jun 03 '22 09:06 veitw

package managers could rename them on uninstall/reinstall or upgrade when one had been altered (for rpm based systems e.g. to *.rpmsave or *.rpmnew), so they are not loaded by default

This is especially important in case this feature leads to LUA files as extension packages. For the example described above I could think of an RPM package pdns-lua-switchontime that includes the code as e.g. /usr/lib/pdns/lua-extensions/switchontime.lua, and a configuration file /etc/pdns/common-lua.d/switchontime.lua that either loads the code as include or is a symlink, or /usr/lib/pdns/lua-extensions/*.lua are loaded by a default configuration directive in pdns.conf. So doing a dnf install pdns-lua-switchontime would be enough to supply this feature.

veitw avatar Jun 03 '22 10:06 veitw

only files with names ending in .lua should be loaded

yes!

Habbie avatar Jun 03 '22 10:06 Habbie

I'm looking to do something exactly like this. Currently using recursor as it can reference lua files which has over 100 lines of code but running into issues with Google due to no AA bit. Reading this, it seems all lua is supported (e.f. preresolve) in authorative but without this feature I need to insert row by row in between ""?

smck83 avatar Oct 19 '23 06:10 smck83

but without this feature I need to insert row by row in between ""

you can already use dofile or require

Habbie avatar Oct 19 '23 07:10 Habbie

Thank you for confirming. That's good news, so I can use the full lua script in a lua records on pdns Authoritative and use ;require("/opt/myscript.lua")

in the record?

smck83 avatar Oct 19 '23 09:10 smck83