-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
03-plugins_triggering_spec.lua
374 lines (327 loc) · 9.91 KB
/
03-plugins_triggering_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
local helpers = require "spec.helpers"
describe("Plugins triggering", function()
local client
setup(function()
helpers.run_migrations()
local consumer1 = assert(helpers.dao.consumers:insert {
username = "consumer1"
})
assert(helpers.dao.keyauth_credentials:insert {
key = "secret1",
consumer_id = consumer1.id
})
local consumer2 = assert(helpers.dao.consumers:insert {
username = "consumer2"
})
assert(helpers.dao.keyauth_credentials:insert {
key = "secret2",
consumer_id = consumer2.id
})
local consumer3 = assert(helpers.dao.consumers:insert {
username = "anonymous"
})
-- Global configuration
assert(helpers.dao.apis:insert {
name = "global1",
hosts = { "global1.com" },
upstream_url = helpers.mock_upstream_url,
})
assert(helpers.dao.plugins:insert {
name = "key-auth",
config = {},
})
assert(helpers.dao.plugins:insert {
name = "rate-limiting",
config = {
hour = 1,
},
})
-- API Specific Configuration
local api1 = assert(helpers.dao.apis:insert {
name = "api1",
hosts = { "api1.com" },
upstream_url = helpers.mock_upstream_url,
})
assert(helpers.dao.plugins:insert {
name = "rate-limiting",
api_id = api1.id,
config = {
hour = 2,
},
})
-- Consumer Specific Configuration
assert(helpers.dao.plugins:insert {
name = "rate-limiting",
consumer_id = consumer2.id,
config = {
hour = 3,
},
})
-- API and Consumer Configuration
local api2 = assert(helpers.dao.apis:insert {
name = "api2",
hosts = { "api2.com" },
upstream_url = helpers.mock_upstream_url,
})
assert(helpers.dao.plugins:insert {
name = "rate-limiting",
api_id = api2.id,
consumer_id = consumer2.id,
config = {
hour = 4,
},
})
-- API with anonymous configuration
local api3 = assert(helpers.dao.apis:insert {
name = "api3",
hosts = { "api3.com" },
upstream_url = helpers.mock_upstream_url,
})
assert(helpers.dao.plugins:insert {
name = "key-auth",
config = {
anonymous = consumer3.id,
},
api_id = api3.id,
})
assert(helpers.dao.plugins:insert {
name = "rate-limiting",
consumer_id = consumer3.id,
api_id = api3.id,
config = {
hour = 5,
}
})
assert(helpers.start_kong({
nginx_conf = "spec/fixtures/custom_nginx.template",
}))
client = helpers.proxy_client()
end)
teardown(function()
if client then client:close() end
helpers.stop_kong()
end)
it("checks global configuration without credentials", function()
local res = assert(client:send {
method = "GET",
path = "/status/200",
headers = { Host = "global1.com" }
})
assert.res_status(401, res)
end)
it("checks global api configuration", function()
local res = assert(client:send {
method = "GET",
path = "/status/200?apikey=secret1",
headers = { Host = "global1.com" }
})
assert.res_status(200, res)
assert.equal("1", res.headers["x-ratelimit-limit-hour"])
end)
it("checks api specific configuration", function()
local res = assert(client:send {
method = "GET",
path = "/status/200?apikey=secret1",
headers = { Host = "api1.com" }
})
assert.res_status(200, res)
assert.equal("2", res.headers["x-ratelimit-limit-hour"])
end)
it("checks global consumer configuration", function()
local res = assert(client:send {
method = "GET",
path = "/status/200?apikey=secret2",
headers = { Host = "global1.com" }
})
assert.res_status(200, res)
assert.equal("3", res.headers["x-ratelimit-limit-hour"])
end)
it("checks consumer specific configuration", function()
local res = assert(client:send {
method = "GET",
path = "/status/200?apikey=secret2",
headers = { Host = "api2.com" }
})
assert.res_status(200, res)
assert.equal("4", res.headers["x-ratelimit-limit-hour"])
end)
it("checks anonymous consumer specific configuration", function()
local res = assert(client:send {
method = "GET",
path = "/status/200",
headers = { Host = "api3.com" }
})
assert.res_status(200, res)
assert.equal("5", res.headers["x-ratelimit-limit-hour"])
end)
describe("short-circuited requests", function()
local FILE_LOG_PATH = os.tmpname()
setup(function()
if client then
client:close()
end
helpers.stop_kong()
helpers.dao:truncate_tables()
local api = assert(helpers.dao.apis:insert {
name = "example",
hosts = { "mock_upstream" },
upstream_url = helpers.mock_upstream_url,
})
-- plugin able to short-circuit a request
assert(helpers.dao.plugins:insert {
name = "key-auth",
api_id = api.id,
})
-- response/body filter plugin
assert(helpers.dao.plugins:insert {
name = "dummy",
api_id = api.id,
config = {
append_body = "appended from body filtering",
}
})
-- log phase plugin
assert(helpers.dao.plugins:insert {
name = "file-log",
api_id = api.id,
config = {
path = FILE_LOG_PATH,
},
})
assert(helpers.start_kong {
nginx_conf = "spec/fixtures/custom_nginx.template",
})
client = helpers.proxy_client()
end)
teardown(function()
if client then
client:close()
end
os.remove(FILE_LOG_PATH)
helpers.stop_kong()
end)
it("execute a log plugin", function()
local utils = require "kong.tools.utils"
local cjson = require "cjson"
local pl_path = require "pl.path"
local pl_file = require "pl.file"
local pl_stringx = require "pl.stringx"
local uuid = utils.uuid()
local res = assert(client:send {
method = "GET",
path = "/status/200",
headers = {
["Host"] = "mock_upstream",
["X-UUID"] = uuid,
-- /!\ no key credential
}
})
assert.res_status(401, res)
-- TEST: ensure that our logging plugin was executed and wrote
-- something to disk.
helpers.wait_until(function()
return pl_path.exists(FILE_LOG_PATH) and pl_path.getsize(FILE_LOG_PATH) > 0
end, 3)
local log = pl_file.read(FILE_LOG_PATH)
local log_message = cjson.decode(pl_stringx.strip(log))
assert.equal("127.0.0.1", log_message.client_ip)
assert.equal(uuid, log_message.request.headers["x-uuid"])
end)
it("execute a header_filter plugin", function()
local res = assert(client:send {
method = "GET",
path = "/status/200",
headers = {
["Host"] = "mock_upstream",
}
})
assert.res_status(401, res)
-- TEST: ensure that the dummy plugin was executed by checking
-- that headers have been injected in the header_filter phase
-- Plugins such as CORS need to run on short-circuited requests
-- as well.
assert.not_nil(res.headers["dummy-plugin"])
end)
it("execute a body_filter plugin", function()
local res = assert(client:send {
method = "GET",
path = "/status/200",
headers = {
["Host"] = "mock_upstream",
}
})
local body = assert.res_status(401, res)
-- TEST: ensure that the dummy plugin was executed by checking
-- that the body filtering phase has run
assert.matches("appended from body filtering", body, nil, true)
end)
end)
describe("anonymous reports execution", function()
-- anonymous reports are implemented as a plugin which is being executed
-- by the plugins runloop, but which doesn't have a schema
--
-- This is a regression test after:
-- https://github.com/Kong/kong/issues/2756
-- to ensure that this plugin plays well when it is being executed by
-- the runloop (which accesses plugins schemas and is vulnerable to
-- Lua indexing errors)
--
-- At the time of this test, the issue only arises when a request is
-- authenticated via an auth plugin, and the runloop runs again, and
-- tries to evaluate is the `schema.no_consumer` flag is set.
-- Since the reports plugin has no `schema`, this indexing fails.
setup(function()
if client then
client:close()
end
helpers.stop_kong()
helpers.dao:truncate_tables()
local api = assert(helpers.dao.apis:insert {
name = "example",
hosts = { "mock_upstream" },
upstream_url = helpers.mock_upstream_url
})
assert(helpers.dao.plugins:insert {
name = "key-auth",
api_id = api.id,
})
local consumer = assert(helpers.dao.consumers:insert {
username = "bob",
})
assert(helpers.dao.keyauth_credentials:insert {
key = "abcd",
consumer_id = consumer.id,
})
assert(helpers.start_kong {
nginx_conf = "spec/fixtures/custom_nginx.template",
anonymous_reports = true,
})
client = helpers.proxy_client()
end)
teardown(function()
if client then
client:close()
end
helpers.stop_kong()
end)
it("runs without causing an internal error", function()
local res = assert(client:send {
method = "GET",
path = "/status/200",
headers = {
["Host"] = "mock_upstream",
},
})
assert.res_status(401, res)
res = assert(client:send {
method = "GET",
path = "/status/200",
headers = {
["Host"] = "mock_upstream",
["apikey"] = "abcd",
},
})
assert.res_status(200, res)
end)
end)
end)