/
fields.lua
435 lines (314 loc) · 9.95 KB
/
fields.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
local buffer = require("string.buffer")
local type = type
local ipairs = ipairs
local assert = assert
local tonumber = tonumber
local tb_sort = table.sort
local tb_concat = table.concat
local replace_dashes_lower = require("kong.tools.string").replace_dashes_lower
local var = ngx.var
local get_method = ngx.req.get_method
local get_headers = ngx.req.get_headers
local get_uri_args = ngx.req.get_uri_args
local server_name = require("ngx.ssl").server_name
local PREFIX_LEN = 13 -- #"http.headers."
local HTTP_HEADERS_PREFIX = "http.headers."
local HTTP_QUERIES_PREFIX = "http.queries."
local FIELDS_FUNCS = {
-- http.*
["http.method"] =
function(params)
if not params.method then
params.method = get_method()
end
return params.method
end,
["http.path"] =
function(params)
return params.uri
end,
["http.host"] =
function(params)
return params.host
end,
-- net.*
["net.src.ip"] =
function(params)
if not params.src_ip then
params.src_ip = var.remote_addr
end
return params.src_ip
end,
["net.src.port"] =
function(params)
if not params.src_port then
params.src_port = tonumber(var.remote_port, 10)
end
return params.src_port
end,
-- below are atc context only
["net.protocol"] =
function(params)
return params.scheme
end,
}
local is_http = ngx.config.subsystem == "http"
if is_http then
-- tls.*
FIELDS_FUNCS["tls.sni"] =
function(params)
if not params.sni then
params.sni = server_name()
end
return params.sni
end
-- net.*
FIELDS_FUNCS["net.dst.ip"] =
function(params)
if not params.dst_ip then
params.dst_ip = var.server_addr
end
return params.dst_ip
end
FIELDS_FUNCS["net.dst.port"] =
function(params, ctx)
if params.port then
return params.port
end
if not params.dst_port then
params.dst_port = tonumber((ctx or ngx.ctx).host_port, 10) or
tonumber(var.server_port, 10)
end
return params.dst_port
end
else -- stream
-- tls.*
-- error value for non-TLS connections ignored intentionally
-- fallback to preread SNI if current connection doesn't terminate TLS
FIELDS_FUNCS["tls.sni"] =
function(params)
if not params.sni then
params.sni = server_name() or var.ssl_preread_server_name
end
return params.sni
end
-- net.*
-- when proxying TLS request in second layer or doing TLS passthrough
-- rewrite the dst_ip, port back to what specified in proxy_protocol
FIELDS_FUNCS["net.dst.ip"] =
function(params)
if not params.dst_ip then
if var.kong_tls_passthrough_block == "1" or var.ssl_protocol then
params.dst_ip = var.proxy_protocol_server_addr
else
params.dst_ip = var.server_addr
end
end
return params.dst_ip
end
FIELDS_FUNCS["net.dst.port"] =
function(params, ctx)
if not params.dst_port then
if var.kong_tls_passthrough_block == "1" or var.ssl_protocol then
params.dst_port = tonumber(var.proxy_protocol_server_port)
else
params.dst_port = tonumber((ctx or ngx.ctx).host_port, 10) or
tonumber(var.server_port, 10)
end
end
return params.dst_port
end
end -- is_http
if is_http then
local fmt = string.format
local ngx_null = ngx.null
local re_split = require("ngx.re").split
local HTTP_SEGMENTS_PREFIX = "http.path.segments."
local HTTP_SEGMENTS_PREFIX_LEN = #HTTP_SEGMENTS_PREFIX
local HTTP_SEGMENTS_REG_CTX = { pos = 2, } -- skip first '/'
local HTTP_SEGMENTS_OFFSET = 1
-- func => get_headers or get_uri_args
-- name => "headers" or "queries"
-- max_config_option => "lua_max_req_headers" or "lua_max_uri_args"
local function get_http_params(func, name, max_config_option)
local params, err = func()
if err == "truncated" then
local max = kong and kong.configuration and kong.configuration[max_config_option] or 100
ngx.log(ngx.ERR,
fmt("router: not all request %s were read in order to determine the route " ..
"as the request contains more than %d %s, " ..
"route selection may be inaccurate, " ..
"consider increasing the '%s' configuration value " ..
"(currently at %d)",
name, max, name, max_config_option, max))
end
return params
end
setmetatable(FIELDS_FUNCS, {
__index = function(_, field)
local prefix = field:sub(1, PREFIX_LEN)
if prefix == HTTP_HEADERS_PREFIX then
return function(params)
if not params.headers then
params.headers = get_http_params(get_headers, "headers", "lua_max_req_headers")
end
return params.headers[field:sub(PREFIX_LEN + 1)]
end
elseif prefix == HTTP_QUERIES_PREFIX then
return function(params)
if not params.queries then
params.queries = get_http_params(get_uri_args, "queries", "lua_max_uri_args")
end
return params.queries[field:sub(PREFIX_LEN + 1)]
end
elseif field:sub(1, HTTP_SEGMENTS_PREFIX_LEN) == HTTP_SEGMENTS_PREFIX then
return function(params)
if not params.segments then
HTTP_SEGMENTS_REG_CTX.pos = 2 -- reset ctx, skip first '/'
params.segments = re_split(params.uri, "/", "jo", HTTP_SEGMENTS_REG_CTX)
end
local segments = params.segments
local range = field:sub(HTTP_SEGMENTS_PREFIX_LEN + 1)
local value = segments[range]
if value then
return value ~= ngx_null and value or nil
end
-- "/a/b/c" => 1="a", 2="b", 3="c"
-- http.path.segments.0 => params.segments[1 + 0] => a
-- http.path.segments.1_2 => b/c
local p = range:find("_", 1, true)
-- only one segment, e.g. http.path.segments.1
if not p then
local pos = tonumber(range)
value = pos and segments[HTTP_SEGMENTS_OFFSET + pos] or nil
segments[range] = value or ngx_null
return value
end
-- (pos1, pos2) defines a segment range, e.g. http.path.segments.1_2
local pos1 = tonumber(range:sub(1, p - 1))
local pos2 = tonumber(range:sub(p + 1))
local segs_count = #segments - HTTP_SEGMENTS_OFFSET
if not pos1 or not pos2 or
pos1 >= pos2 or pos1 > segs_count or pos2 > segs_count
then
segments[range] = ngx_null
return nil
end
local buf = buffer.new()
for p = pos1, pos2 - 1 do
buf:put(segments[HTTP_SEGMENTS_OFFSET + p], "/")
end
buf:put(segments[HTTP_SEGMENTS_OFFSET + pos2])
value = buf:get()
segments[range] = value
return value
end
end -- if prefix
-- others return nil
end
})
end -- is_http
local function get_value(field, params, ctx)
local func = FIELDS_FUNCS[field]
if not func then -- unknown field
error("unknown router matching schema field: " .. field)
end -- if func
return func(params, ctx)
end
local function fields_visitor(fields, params, ctx, cb)
for _, field in ipairs(fields) do
local value = get_value(field, params, ctx)
local res, err = cb(field, value)
if not res then
return nil, err
end
end -- for fields
return true
end
-- cache key string
local str_buf = buffer.new(64)
local function get_cache_key(fields, params, ctx)
str_buf:reset()
local res =
fields_visitor(fields, params, ctx, function(field, value)
-- these fields were not in cache key
if field == "net.protocol" then
return true
end
local headers_or_queries = field:sub(1, PREFIX_LEN)
if headers_or_queries == HTTP_HEADERS_PREFIX then
headers_or_queries = true
field = replace_dashes_lower(field)
elseif headers_or_queries == HTTP_QUERIES_PREFIX then
headers_or_queries = true
else
headers_or_queries = false
end
if not headers_or_queries then
str_buf:put(value or ""):put("|")
else -- headers or queries
if type(value) == "table" then
tb_sort(value)
value = tb_concat(value, ",")
end
str_buf:putf("%s=%s|", field, value or "")
end
return true
end) -- fields_visitor
assert(res)
return str_buf:get()
end
local function fill_atc_context(context, fields, params)
local c = context
local res, err =
fields_visitor(fields, params, nil, function(field, value)
local prefix = field:sub(1, PREFIX_LEN)
if prefix == HTTP_HEADERS_PREFIX or prefix == HTTP_QUERIES_PREFIX then
local v_type = type(value)
-- multiple values for a single query parameter, like /?foo=bar&foo=baz
if v_type == "table" then
for _, v in ipairs(value) do
local res, err = c:add_value(field, v)
if not res then
return nil, err
end
end
return true
end -- if v_type
-- the query parameter has only one value, like /?foo=bar
-- the query parameter has no value, like /?foo,
-- get_uri_arg will get a boolean `true`
-- we think it is equivalent to /?foo=
if v_type == "boolean" then
value = ""
end
end
return c:add_value(field, value)
end) -- fields_visitor
if not res then
return nil, err
end
return c
end
local function _set_ngx(mock_ngx)
if mock_ngx.var then
var = mock_ngx.var
end
if type(mock_ngx.req) == "table" then
if mock_ngx.req.get_method then
get_method = mock_ngx.req.get_method
end
if mock_ngx.req.get_headers then
get_headers = mock_ngx.req.get_headers
end
if mock_ngx.req.get_uri_args then
get_uri_args = mock_ngx.req.get_uri_args
end
end
end
return {
get_value = get_value,
get_cache_key = get_cache_key,
fill_atc_context = fill_atc_context,
_set_ngx = _set_ngx,
}