Compare commits

..

3 Commits

Author SHA1 Message Date
holger krekel
4ba5cd0ce3 better output, and allowing to use the lua script as is 2023-10-12 17:23:33 +02:00
holger krekel
e918b3a507 adding a lua test for chatctl.py 2023-10-12 17:19:51 +02:00
holger krekel
16680014ff add a lua test script running directly against chatctl.py 2023-10-12 17:18:08 +02:00
2 changed files with 82 additions and 13 deletions

View File

@@ -1,16 +1,8 @@
from pathlib import Path import importlib.resources
from pyinfra.operations import apt, files, systemd, server from pyinfra.operations import apt, files, systemd, server
def openfile(basename):
# on newer python versions:
# importlib.resources.files(__package__).joinpath(basename).open("rb")
# but here we use a way supported on old pythons
dirpath = Path(__path__[0])
return dirpath.joinpath(basename).open("rb")
def deploy_acmetool(nginx_hook=False, email="", domains=[]): def deploy_acmetool(nginx_hook=False, email="", domains=[]):
"""Deploy acmetool.""" """Deploy acmetool."""
apt.packages( apt.packages(
@@ -19,7 +11,7 @@ def deploy_acmetool(nginx_hook=False, email="", domains=[]):
) )
files.put( files.put(
src=openfile("acmetool.cron"), src=importlib.resources.files(__package__).joinpath("acmetool.cron").open("rb"),
dest="/etc/cron.d/acmetool", dest="/etc/cron.d/acmetool",
user="root", user="root",
group="root", group="root",
@@ -28,7 +20,9 @@ def deploy_acmetool(nginx_hook=False, email="", domains=[]):
if nginx_hook: if nginx_hook:
files.put( files.put(
src=openfile("acmetool.hook"), src=importlib.resources.files(__package__)
.joinpath("acmetool.hook")
.open("rb"),
dest="/usr/lib/acme/hooks/nginx", dest="/usr/lib/acme/hooks/nginx",
user="root", user="root",
group="root", group="root",
@@ -36,7 +30,7 @@ def deploy_acmetool(nginx_hook=False, email="", domains=[]):
) )
files.template( files.template(
src=openfile("response-file.yaml.j2"), src=importlib.resources.files(__package__).joinpath("response-file.yaml.j2"),
dest="/var/lib/acme/conf/responses", dest="/var/lib/acme/conf/responses",
user="root", user="root",
group="root", group="root",
@@ -45,7 +39,9 @@ def deploy_acmetool(nginx_hook=False, email="", domains=[]):
) )
service_file = files.put( service_file = files.put(
src=openfile("acmetool-redirector.service"), src=importlib.resources.files(__package__)
.joinpath("acmetool-redirector.service")
.open("rb"),
dest="/etc/systemd/system/acmetool-redirector.service", dest="/etc/systemd/system/acmetool-redirector.service",
user="root", user="root",
group="root", group="root",

View File

@@ -0,0 +1,73 @@
-- To run this test: run "lua test.lua" while in the same directory as chatctl.py
if dovecot == nil then
dovecot = {
auth = {
PASSDB_RESULT_OK="OK",
PASSDB_RESULT_PASSWORD_MISMATCH="MISMATCH"
}
}
end
-- Escape shell argument by hex encoding it and wrapping in quotes.
function escape(data)
b16 = data:gsub(".", function(char) return string.format("%2X", char:byte()) end)
return ("'"..b16.."'")
end
-- call out to python program to actually manage authentication for dovecot
function chatctl_verify(user, password)
return os.execute("python chatctl.py hexauth "..escape(user).." "..escape(password))
end
function chatctl_lookup(hex, user)
return os.execute("python chatctl.py hexlookup "..escape(user))
end
function auth_password_verify(request, password)
if chatctl_verify(request.user, password) then
return dovecot.auth.PASSDB_RESULT_OK, {}
end
return dovecot.auth.PASSDB_RESULT_PASSWORD_MISMATCH, ""
end
function auth_passdb_lookup(request)
if chatctl_lookup(request.user) then
return dovecot.auth.PASSDB_RESULT_OK, {}
end
return dovecot.auth.PASSDB_RESULT_USER_UNKNOWN, "no such user"
end
function auth_userdb_lookup(request)
if chatctl_lookup(request.user) then
return dovecot.auth.USERDB_RESULT_OK, "uid=vmail gid=vmail"
end
return dovecot.auth.USERDB_RESULT_USER_UNKNOWN, "no such user"
end
function split_chatctl_results(output)
local ret = {}
for key, value in output:gmatch "(%w+)%s*=%s*(%w+)" do
ret[key] = value
end
return ret
end
function test_ok(user, password)
local res = auth_password_verify({user=user}, password)
assert(res=="OK")
print("OK test_ok "..user.." "..password)
end
function test_mismatch(user, password)
local res = auth_password_verify({user=user}, password)
assert(res == "MISMATCH")
print("OK test_mismatch "..user.." "..password)
end
test_ok("link2xt@instant2.testrun.org", "Ahyei6ie")
test_mismatch("link2xt@instant2.testrun.org", "Aqwlek")