-
Notifications
You must be signed in to change notification settings - Fork 0
/
003-prefix_handler_spec.lua
474 lines (448 loc) · 19.9 KB
/
003-prefix_handler_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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
local helpers = require "spec.helpers"
local conf_loader = require "kong.conf_loader"
local prefix_handler = require "kong.cmd.utils.prefix_handler"
local exists = helpers.path.exists
local join = helpers.path.join
describe("NGINX conf compiler", function()
describe("gen_default_ssl_cert()", function()
local conf = assert(conf_loader(helpers.test_conf_path, {
prefix = "ssl_tmp",
ssl = true,
ssl_cert = "spec/fixtures/kong_spec.crt",
ssl_cert_key = "spec/fixtures/kong_spec.key",
admin_ssl = true,
admin_ssl_cert = "spec/fixtures/kong_spec.crt",
admin_ssl_cert_key = "spec/fixtures/kong_spec.key",
}))
before_each(function()
helpers.dir.makepath("ssl_tmp")
end)
after_each(function()
pcall(helpers.dir.rmtree, "ssl_tmp")
end)
describe("proxy", function()
it("auto-generates SSL certificate and key", function()
assert(prefix_handler.gen_default_ssl_cert(conf))
assert(exists(conf.ssl_cert_default))
assert(exists(conf.ssl_cert_key_default))
end)
it("does not re-generate if they already exist", function()
assert(prefix_handler.gen_default_ssl_cert(conf))
local cer = helpers.file.read(conf.ssl_cert_default)
local key = helpers.file.read(conf.ssl_cert_key_default)
assert(prefix_handler.gen_default_ssl_cert(conf))
assert.equal(cer, helpers.file.read(conf.ssl_cert_default))
assert.equal(key, helpers.file.read(conf.ssl_cert_key_default))
end)
end)
describe("admin", function()
it("auto-generates SSL certificate and key", function()
assert(prefix_handler.gen_default_ssl_cert(conf, true))
assert(exists(conf.admin_ssl_cert_default))
assert(exists(conf.admin_ssl_cert_key_default))
end)
it("does not re-generate if they already exist", function()
assert(prefix_handler.gen_default_ssl_cert(conf, true))
local cer = helpers.file.read(conf.admin_ssl_cert_default)
local key = helpers.file.read(conf.admin_ssl_cert_key_default)
assert(prefix_handler.gen_default_ssl_cert(conf, true))
assert.equal(cer, helpers.file.read(conf.admin_ssl_cert_default))
assert.equal(key, helpers.file.read(conf.admin_ssl_cert_key_default))
end)
end)
end)
describe("compile_kong_conf()", function()
it("compiles the Kong NGINX conf chunk", function()
local kong_nginx_conf = prefix_handler.compile_kong_conf(helpers.test_conf)
assert.matches("lua_package_path './?.lua;./?/init.lua;;;'", kong_nginx_conf, nil, true)
assert.matches("lua_code_cache on;", kong_nginx_conf, nil, true)
assert.matches("listen 0.0.0.0:9000;", kong_nginx_conf, nil, true)
assert.matches("listen 0.0.0.0:9001;", kong_nginx_conf, nil, true)
assert.matches("server_name kong;", kong_nginx_conf, nil, true)
assert.matches("server_name kong_admin;", kong_nginx_conf, nil, true)
assert.not_matches("lua_ssl_trusted_certificate", kong_nginx_conf, nil, true)
end)
it("compiles with custom conf", function()
local conf = assert(conf_loader(helpers.test_conf_path, {
lua_code_cache = false,
mem_cache_size = "128k",
proxy_listen = "0.0.0.0:80",
admin_listen = "127.0.0.1:8001"
}))
local kong_nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.matches("lua_code_cache off;", kong_nginx_conf, nil, true)
assert.matches("lua_shared_dict kong_cache%s+128k;", kong_nginx_conf)
assert.matches("listen 0.0.0.0:80;", kong_nginx_conf, nil, true)
assert.matches("listen 127.0.0.1:8001;", kong_nginx_conf, nil, true)
end)
it("enables HTTP/2", function()
local conf = assert(conf_loader(helpers.test_conf_path, {
http2 = true,
admin_http2 = true
}))
local kong_nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.matches("listen 0.0.0.0:9000;", kong_nginx_conf, nil, true)
assert.matches("listen 0.0.0.0:9443 ssl http2;", kong_nginx_conf, nil, true)
assert.matches("listen 0.0.0.0:9001;", kong_nginx_conf, nil, true)
assert.matches("listen 0.0.0.0:8444 ssl http2;", kong_nginx_conf, nil, true)
conf = assert(conf_loader(helpers.test_conf_path, {
http2 = true,
}))
kong_nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.matches("listen 0.0.0.0:9000;", kong_nginx_conf, nil, true)
assert.matches("listen 0.0.0.0:9443 ssl http2;", kong_nginx_conf, nil, true)
assert.matches("listen 0.0.0.0:9001;", kong_nginx_conf, nil, true)
assert.matches("listen 0.0.0.0:8444 ssl;", kong_nginx_conf, nil, true)
conf = assert(conf_loader(helpers.test_conf_path, {
admin_http2 = true
}))
kong_nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.matches("listen 0.0.0.0:9000;", kong_nginx_conf, nil, true)
assert.matches("listen 0.0.0.0:9443 ssl;", kong_nginx_conf, nil, true)
assert.matches("listen 0.0.0.0:9001;", kong_nginx_conf, nil, true)
assert.matches("listen 0.0.0.0:8444 ssl http2;", kong_nginx_conf, nil, true)
end)
it("enables proxy_protocol", function()
local conf = assert(conf_loader(helpers.test_conf_path, {
real_ip_header = "proxy_protocol",
}))
local kong_nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.matches("listen 0.0.0.0:9000 proxy_protocol;", kong_nginx_conf, nil, true)
assert.matches("listen 0.0.0.0:9443 ssl proxy_protocol;", kong_nginx_conf, nil, true)
end)
it("disables SSL", function()
local conf = assert(conf_loader(helpers.test_conf_path, {
ssl = false,
admin_ssl = false
}))
local kong_nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.not_matches("listen %d+%.%d+%.%d+%.%d+:%d+ ssl;", kong_nginx_conf)
assert.not_matches("ssl_certificate", kong_nginx_conf)
assert.not_matches("ssl_certificate_key", kong_nginx_conf)
assert.not_matches("ssl_protocols", kong_nginx_conf)
assert.not_matches("ssl_certificate_by_lua_block", kong_nginx_conf)
end)
describe("handles client_ssl", function()
it("on", function()
local conf = assert(conf_loader(helpers.test_conf_path, {
client_ssl = true,
client_ssl_cert = "spec/fixtures/kong_spec.crt",
client_ssl_cert_key = "spec/fixtures/kong_spec.key",
}))
local kong_nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.matches("proxy_ssl_certificate.*spec/fixtures/kong_spec.crt", kong_nginx_conf)
assert.matches("proxy_ssl_certificate_key.*spec/fixtures/kong_spec.key", kong_nginx_conf)
end)
it("off", function()
local conf = assert(conf_loader(helpers.test_conf_path, {
client_ssl = false,
}))
local kong_nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.not_matches("proxy_ssl_certificate.*spec/fixtures/kong_spec.crt", kong_nginx_conf)
assert.not_matches("proxy_ssl_certificate_key.*spec/fixtures/kong_spec.key", kong_nginx_conf)
end)
end)
it("does not include lua_ssl_trusted_certificate/lua_ssl_verify_depth by default", function()
local conf = assert(conf_loader(helpers.test_conf_path, {
lua_ssl_verify_depth = "2"
}))
local kong_nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.not_matches("lua_ssl_trusted_certificate", kong_nginx_conf, nil, true)
assert.not_matches("lua_ssl_verify_depth", kong_nginx_conf, nil, true)
end)
it("sets lua_ssl_trusted_certificate/lua_ssl_verify_depth", function()
local conf = assert(conf_loader(helpers.test_conf_path, {
lua_ssl_trusted_certificate = "/path/to/ca.cert",
lua_ssl_verify_depth = "2"
}))
local kong_nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.matches("lua_ssl_trusted_certificate '/path/to/ca.cert';", kong_nginx_conf, nil, true)
assert.matches("lua_ssl_verify_depth 2;", kong_nginx_conf, nil, true)
end)
it("compiles without anonymous reports", function()
local conf = assert(conf_loader(nil, {
anonymous_reports = false,
}))
local nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.not_matches("error_log syslog:server=.+ error;", nginx_conf)
end)
it("compiles with anonymous reports", function()
local conf = assert(conf_loader(nil, {
anonymous_reports = true,
}))
local nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.matches("error_log syslog:server=.+:61828 error;", nginx_conf)
end)
it("defines the client_max_body_size by default", function()
local conf = assert(conf_loader(nil, {}))
local nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.matches("client_max_body_size 0", nginx_conf, nil, true)
end)
it("writes the client_max_body_size as defined", function()
local conf = assert(conf_loader(nil, {
client_max_body_size = "1m",
}))
local nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.matches("client_max_body_size 1m", nginx_conf, nil, true)
end)
it("defines the client_body_buffer_size directive by default", function()
local conf = assert(conf_loader(nil, {}))
local nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.matches("client_body_buffer_size 8k", nginx_conf, nil, true)
end)
it("writes the client_body_buffer_size directive as defined", function()
local conf = assert(conf_loader(nil, {
client_body_buffer_size = "128k",
}))
local nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.matches("client_body_buffer_size 128k", nginx_conf, nil, true)
end)
it("writes kong_cassandra shm if using Cassandra", function()
local conf = assert(conf_loader(nil, {
database = "cassandra",
}))
local nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.matches("lua_shared_dict%s+kong_cassandra", nginx_conf)
end)
it("does not write kong_cassandra shm if not using Cassandra", function()
local conf = assert(conf_loader(nil, {
database = "postgres",
}))
local nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.not_matches("lua_shared_dict%s+kong_cassandra", nginx_conf)
end)
describe("user directive", function()
it("is not included by default", function()
local conf = assert(conf_loader(helpers.test_conf_path))
local nginx_conf = prefix_handler.compile_nginx_conf(conf)
assert.not_matches("user[^;]*;", nginx_conf, nil, true)
end)
it("is not included when 'nobody'", function()
local conf = assert(conf_loader(helpers.test_conf_path, {
nginx_user = "nobody"
}))
local nginx_conf = prefix_handler.compile_nginx_conf(conf)
assert.not_matches("user[^;]*;", nginx_conf, nil, true)
end)
it("is not included when 'nobody nobody'", function()
local conf = assert(conf_loader(helpers.test_conf_path, {
nginx_user = "nobody nobody"
}))
local nginx_conf = prefix_handler.compile_nginx_conf(conf)
assert.not_matches("user[^;]*;", nginx_conf, nil, true)
end)
it("is included when otherwise", function()
local conf = assert(conf_loader(helpers.test_conf_path, {
nginx_user = "www_data www_data"
}))
local nginx_conf = prefix_handler.compile_nginx_conf(conf)
assert.matches("user www_data www_data;", nginx_conf, nil, true)
end)
end)
describe("ngx_http_realip_module settings", function()
it("defaults", function()
local conf = assert(conf_loader())
local nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.matches("real_ip_header%s+X%-Real%-IP;", nginx_conf)
assert.matches("real_ip_recursive%s+off;", nginx_conf)
assert.not_matches("set_real_ip_from", nginx_conf)
end)
it("real_ip_recursive on", function()
local conf = assert(conf_loader(nil, {
real_ip_recursive = true,
}))
local nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.matches("real_ip_recursive%s+on;", nginx_conf)
end)
it("real_ip_recursive off", function()
local conf = assert(conf_loader(nil, {
real_ip_recursive = false,
}))
local nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.matches("real_ip_recursive%s+off;", nginx_conf)
end)
it("set_real_ip_from", function()
local conf = assert(conf_loader(nil, {
trusted_ips = "192.168.1.0/24,192.168.2.1,2001:0db8::/32"
}))
local nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.matches("set_real_ip_from%s+192.168.1.0/24", nginx_conf)
assert.matches("set_real_ip_from%s+192.168.1.0", nginx_conf)
assert.matches("set_real_ip_from%s+2001:0db8::/32", nginx_conf)
end)
it("proxy_protocol", function()
local conf = assert(conf_loader(nil, {
real_ip_header = "proxy_protocol"
}))
local nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.matches("real_ip_header%s+proxy_protocol", nginx_conf)
assert.matches("listen 0.0.0.0:8000 proxy_protocol;", nginx_conf)
assert.matches("listen 0.0.0.0:8443 ssl proxy_protocol;", nginx_conf)
end)
end)
end)
describe("compile_nginx_conf()", function()
it("compiles a main NGINX conf", function()
local nginx_conf = prefix_handler.compile_nginx_conf(helpers.test_conf)
assert.matches("worker_processes 1;", nginx_conf, nil, true)
assert.matches("daemon on;", nginx_conf, nil, true)
end)
it("compiles with custom conf", function()
local conf = assert(conf_loader(helpers.test_conf_path, {
nginx_daemon = "off"
}))
local nginx_conf = prefix_handler.compile_nginx_conf(conf)
assert.matches("daemon off;", nginx_conf, nil, true)
end)
it("compiles without opinionated nginx optimizations", function()
local conf = assert(conf_loader(nil, {
nginx_optimizations = false,
}))
local nginx_conf = prefix_handler.compile_nginx_conf(conf)
assert.not_matches("worker_rlimit_nofile %d+;", nginx_conf)
assert.not_matches("worker_connections %d+;", nginx_conf)
assert.not_matches("multi_accept on;", nginx_conf)
end)
it("compiles with opinionated nginx optimizations", function()
local conf = assert(conf_loader(nil, {
nginx_optimizations = true,
}))
local nginx_conf = prefix_handler.compile_nginx_conf(conf)
assert.matches("worker_rlimit_nofile %d+;", nginx_conf)
assert.matches("worker_connections %d+;", nginx_conf)
assert.matches("multi_accept on;", nginx_conf)
end)
it("converts dns_resolver to string", function()
local nginx_conf = prefix_handler.compile_nginx_conf({
dns_resolver = { "8.8.8.8", "8.8.4.4" }
}, [[
"resolver ${{DNS_RESOLVER}} ipv6=off;"
]])
assert.matches("resolver 8.8.8.8 8.8.4.4 ipv6=off;", nginx_conf, nil, true)
end)
end)
describe("prepare_prefix()", function()
local tmp_config = conf_loader(helpers.test_conf_path, {
prefix = "servroot_tmp"
})
before_each(function()
pcall(helpers.dir.rmtree, tmp_config.prefix)
helpers.dir.makepath(tmp_config.prefix)
end)
after_each(function()
pcall(helpers.dir.rmtree, tmp_config.prefix)
end)
it("creates inexistent prefix", function()
finally(function()
pcall(helpers.dir.rmtree, "inexistent")
end)
local config = assert(conf_loader(helpers.test_conf_path, {
prefix = "inexistent"
}))
assert(prefix_handler.prepare_prefix(config))
assert.truthy(exists("inexistent"))
end)
it("ensures prefix is a directory", function()
local tmp = os.tmpname()
finally(function()
os.remove(tmp)
end)
local config = assert(conf_loader(helpers.test_conf_path, {
prefix = tmp
}))
local ok, err = prefix_handler.prepare_prefix(config)
assert.equal(tmp .. " is not a directory", err)
assert.is_nil(ok)
end)
it("creates pids folder", function()
assert(prefix_handler.prepare_prefix(tmp_config))
assert.truthy(exists(join(tmp_config.prefix, "pids")))
end)
it("creates NGINX conf and log files", function()
assert(prefix_handler.prepare_prefix(tmp_config))
assert.truthy(exists(tmp_config.kong_env))
assert.truthy(exists(tmp_config.nginx_kong_conf))
assert.truthy(exists(tmp_config.nginx_err_logs))
assert.truthy(exists(tmp_config.nginx_acc_logs))
assert.truthy(exists(tmp_config.nginx_admin_acc_logs))
end)
it("dumps Kong conf", function()
assert(prefix_handler.prepare_prefix(tmp_config))
local in_prefix_kong_conf = assert(conf_loader(tmp_config.kong_env))
assert.same(tmp_config, in_prefix_kong_conf)
end)
it("dump Kong conf (custom conf)", function()
local conf = assert(conf_loader(nil, {
pg_database = "foobar",
prefix = tmp_config.prefix
}))
assert.equal("foobar", conf.pg_database)
assert(prefix_handler.prepare_prefix(conf))
local in_prefix_kong_conf = assert(conf_loader(tmp_config.kong_env))
assert.same(conf, in_prefix_kong_conf)
end)
it("writes custom plugins in Kong conf", function()
local conf = assert(conf_loader(nil, {
custom_plugins = { "foo", "bar" },
prefix = tmp_config.prefix
}))
assert(prefix_handler.prepare_prefix(conf))
local in_prefix_kong_conf = assert(conf_loader(tmp_config.kong_env))
assert.True(in_prefix_kong_conf.plugins.foo)
assert.True(in_prefix_kong_conf.plugins.bar)
end)
describe("ssl", function()
it("does not create SSL dir if disabled", function()
local conf = conf_loader(nil, {
prefix = tmp_config.prefix,
ssl = false,
admin_ssl = false
})
assert(prefix_handler.prepare_prefix(conf))
assert.falsy(exists(join(conf.prefix, "ssl")))
end)
it("does not create SSL dir if using custom cert", function()
local conf = conf_loader(nil, {
prefix = tmp_config.prefix,
ssl = true,
ssl_cert = "spec/fixtures/kong_spec.crt",
ssl_cert_key = "spec/fixtures/kong_spec.key",
admin_ssl = true,
admin_ssl_cert = "spec/fixtures/kong_spec.crt",
admin_ssl_cert_key = "spec/fixtures/kong_spec.key",
})
assert(prefix_handler.prepare_prefix(conf))
assert.falsy(exists(join(conf.prefix, "ssl")))
end)
it("generates default SSL cert", function()
local conf = conf_loader(nil, {
prefix = tmp_config.prefix,
ssl = true,
admin_ssl = true
})
assert(prefix_handler.prepare_prefix(conf))
assert.truthy(exists(join(conf.prefix, "ssl")))
assert.truthy(exists(conf.ssl_cert_default))
assert.truthy(exists(conf.ssl_cert_key_default))
assert.truthy(exists(conf.admin_ssl_cert_default))
assert.truthy(exists(conf.admin_ssl_cert_key_default))
end)
end)
describe("custom template", function()
local templ_fixture = "spec/fixtures/custom_nginx.template"
it("accepts a custom NGINX conf template", function()
assert(prefix_handler.prepare_prefix(tmp_config, templ_fixture))
assert.truthy(exists(tmp_config.nginx_conf))
local contents = helpers.file.read(tmp_config.nginx_conf)
assert.matches("# This is a custom nginx configuration template for Kong specs", contents, nil, true)
assert.matches("daemon on;", contents, nil, true)
assert.matches("listen 0.0.0.0:9000;", contents, nil, true)
end)
it("errors on non-existing file", function()
local ok, err = prefix_handler.prepare_prefix(tmp_config, "spec/fixtures/inexistent.template")
assert.is_nil(ok)
assert.equal("no such file: spec/fixtures/inexistent.template", err)
end)
end)
end)
end)