-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathsockproc_file_descriptors_spec.lua
150 lines (131 loc) · 4.6 KB
/
sockproc_file_descriptors_spec.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
local cjson = require "cjson.safe"
local http = require "resty.http"
local server = require "spec.support.server"
local shell_blocking = require "shell-games"
local function get_sockproc_file_descriptors(as_user, expect_no_results)
local result, shell_err = shell_blocking.capture({ "sudo", "-u", as_user, "lsof", "-n", "-P", "-l", "-R", "-c", "sockproc", "-a", "-d", "0-255", "-F", "pnf" })
if expect_no_results and shell_err and result["output"] == "" then
return {}
end
assert.equal(nil, shell_err)
local lines = {}
local index = 1
for line in string.gmatch(result["output"], "[^\n]+") do
if index > 1 then
local _, err
line, _, err = ngx.re.sub(line, "\\s*type=STREAM", "")
assert.equal(nil, err)
line, _, err = ngx.re.sub(line, "^n/.*logs/error.log$", "n/dev/null")
assert.equal(nil, err)
table.insert(lines, line)
end
index = index + 1
end
return lines
end
describe("sockproc file descriptors", function()
before_each(server.stop)
after_each(server.stop)
it("does not inherit nginx file descriptors", function()
server.start({
auto_ssl_http_server_config = [[
location /get-lua-root {
content_by_lua_block {
local cjson = require "cjson.safe"
ngx.print(cjson.encode({
lua_root = auto_ssl.lua_root,
}))
}
}
]],
})
local httpc = http.new()
local res, err = httpc:request_uri("http://127.0.0.1:9080/get-lua-root")
assert.equal(nil, err)
assert.equal(200, res.status)
local data, json_err = cjson.decode(res.body)
assert.equal(nil, json_err)
local lua_root = data["lua_root"]
assert.String(lua_root)
-- Already running
assert.same({
"f0", "n/dev/null",
"f1", "n/dev/null",
"f2", "n/dev/null",
"f3", "n/tmp/shell.sock",
}, get_sockproc_file_descriptors("root"))
-- sockproc not running
server.stop_sockproc()
assert.same({}, get_sockproc_file_descriptors("root", true))
-- current dir as current user
server.stop_sockproc()
shell_blocking.capture_combined({ lua_root .. "/bin/resty-auto-ssl/start_sockproc" }, { umask = "0022" })
assert.same({
"f0", "n/dev/null",
"f1", "n/dev/null",
"f2", "n/dev/null",
"f3", "n/tmp/shell.sock",
}, get_sockproc_file_descriptors("root"))
-- /tmp dir as current user
server.stop_sockproc()
shell_blocking.capture_combined({ lua_root .. "/bin/resty-auto-ssl/start_sockproc" }, { umask = "0022", chdir = "/tmp" })
assert.same({
"f0", "n/dev/null",
"f1", "n/dev/null",
"f2", "n/dev/null",
"f3", "n/tmp/shell.sock",
}, get_sockproc_file_descriptors("root"))
end)
it("does not inherit nginx file descriptors when nginx started from directory workers don't have permissions to", function()
server.start({
pwd = "/root",
master_process = "on",
auto_ssl_http_server_config = [[
location /get-lua-root {
content_by_lua_block {
local cjson = require "cjson.safe"
ngx.print(cjson.encode({
lua_root = auto_ssl.lua_root,
}))
}
}
]],
})
local httpc = http.new()
local res, err = httpc:request_uri("http://127.0.0.1:9080/get-lua-root")
assert.equal(nil, err)
assert.equal(200, res.status)
local data, json_err = cjson.decode(res.body)
assert.equal(nil, json_err)
local lua_root = data["lua_root"]
assert.String(lua_root)
-- Already running
assert.same({
"f0", "n/dev/null",
"f1", "n/dev/null",
"f2", "n/dev/null",
"f3", "n/tmp/shell.sock",
}, get_sockproc_file_descriptors(server.nobody_user))
-- sockproc not running
server.stop_sockproc()
assert.same({}, get_sockproc_file_descriptors(server.nobody_user, true))
-- current dir as current user
server.stop_sockproc()
shell_blocking.capture_combined({ "sudo", "-u", server.nobody_user, lua_root .. "/bin/resty-auto-ssl/start_sockproc" }, { umask = "0022" })
assert.same({
"f0", "n/dev/null",
"f1", "n/dev/null",
"f2", "n/dev/null",
"f3", "n/tmp/shell.sock",
}, get_sockproc_file_descriptors(server.nobody_user))
-- /tmp dir as current user
server.stop_sockproc()
shell_blocking.capture_combined({ "sudo", "-u", server.nobody_user, lua_root .. "/bin/resty-auto-ssl/start_sockproc" }, { umask = "0022", chdir = "/tmp" })
assert.same({
"f0", "n/dev/null",
"f1", "n/dev/null",
"f2", "n/dev/null",
"f3", "n/tmp/shell.sock",
}, get_sockproc_file_descriptors(server.nobody_user))
end)
end)