Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/libexec/nuageinit/tests/adddoas.lua
288923 views
1
#!/usr/libexec/flua
2
---
3
-- SPDX-License-Identifier: BSD-2-Clause
4
--
5
-- Copyright (c) 2026 Baptiste Daroussin <[email protected]>
6
7
local n = require("nuage")
8
9
local root = os.getenv("NUAGE_FAKE_ROOTDIR")
10
if not root then
11
root = ""
12
end
13
14
local function get_localbase()
15
local f = io.popen("sysctl -in user.localbase 2> /dev/null")
16
local lb = f:read("*l")
17
f:close()
18
if lb == nil or lb:len() == 0 then
19
lb = "/usr/local"
20
end
21
return lb
22
end
23
24
local function read_doasconf()
25
local path = root .. get_localbase() .. "/etc/doas.conf"
26
local f = io.open(path, "r")
27
if not f then
28
return nil
29
end
30
local content = f:read("*a")
31
f:close()
32
return content
33
end
34
35
-- test with a single string rule with %u substitution
36
n.adddoas({ name = "testuser", doas = "permit persist %u as root" })
37
local content = read_doasconf()
38
if not content then
39
n.err("doas.conf not created")
40
end
41
if content ~= "permit persist testuser as root\n" then
42
n.err("unexpected doas.conf content with %u: '" .. content .. "'")
43
end
44
45
-- remove file for next test
46
os.remove(root .. get_localbase() .. "/etc/doas.conf")
47
48
-- test with a table of rules
49
n.adddoas({
50
name = "testuser",
51
doas = {
52
"deny %u as foobar",
53
"permit persist %u as root cmd whoami"
54
}
55
})
56
content = read_doasconf()
57
if not content then
58
n.err("doas.conf not created for table")
59
end
60
if content ~= "deny testuser as foobar\npermit persist testuser as root cmd whoami\n" then
61
n.err("unexpected doas.conf content for table: '" .. content .. "'")
62
end
63
64
os.exit(0)
65
66