/
access.lua
396 lines (312 loc) · 10.8 KB
/
access.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
local constants = require "kong.constants"
local sha256 = require "resty.sha256"
local openssl_hmac = require "openssl.hmac"
local utils = require "kong.tools.utils"
local ngx = ngx
local kong = kong
local time = ngx.time
local abs = math.abs
local decode_base64 = ngx.decode_base64
local encode_base64 = ngx.encode_base64
local parse_time = ngx.parse_http_time
local re_gmatch = ngx.re.gmatch
local hmac_sha1 = ngx.hmac_sha1
local ipairs = ipairs
local fmt = string.format
local AUTHORIZATION = "authorization"
local PROXY_AUTHORIZATION = "proxy-authorization"
local DATE = "date"
local X_DATE = "x-date"
local DIGEST = "digest"
local SIGNATURE_NOT_VALID = "HMAC signature cannot be verified"
local SIGNATURE_NOT_SAME = "HMAC signature does not match"
local hmac = {
["hmac-sha1"] = function(secret, data)
return hmac_sha1(secret, data)
end,
["hmac-sha256"] = function(secret, data)
return openssl_hmac.new(secret, "sha256"):final(data)
end,
["hmac-sha384"] = function(secret, data)
return openssl_hmac.new(secret, "sha384"):final(data)
end,
["hmac-sha512"] = function(secret, data)
return openssl_hmac.new(secret, "sha512"):final(data)
end,
}
local function list_as_set(list)
local set = kong.table.new(0, #list)
for _, v in ipairs(list) do
set[v] = true
end
return set
end
local function validate_params(params, conf)
-- check username and signature are present
if not params.username and params.signature then
return nil, "username or signature missing"
end
-- check enforced headers are present
if conf.enforce_headers and #conf.enforce_headers >= 1 then
local enforced_header_set = list_as_set(conf.enforce_headers)
if params.hmac_headers then
for _, header in ipairs(params.hmac_headers) do
enforced_header_set[header] = nil
end
end
for _, header in ipairs(conf.enforce_headers) do
if enforced_header_set[header] then
return nil, "enforced header not used for signature creation"
end
end
end
-- check supported alorithm used
for _, algo in ipairs(conf.algorithms) do
if algo == params.algorithm then
return true
end
end
return nil, fmt("algorithm %s not supported", params.algorithm)
end
local function retrieve_hmac_fields(authorization_header)
local hmac_params = {}
-- parse the header to retrieve hamc parameters
if authorization_header then
local iterator, iter_err = re_gmatch(authorization_header,
"\\s*[Hh]mac\\s*username=\"(.+)\"," ..
"\\s*algorithm=\"(.+)\",\\s*header" ..
"s=\"(.+)\",\\s*signature=\"(.+)\"")
if not iterator then
kong.log.err(iter_err)
return
end
local m, err = iterator()
if err then
kong.log.err(err)
return
end
if m and #m >= 4 then
hmac_params.username = m[1]
hmac_params.algorithm = m[2]
hmac_params.hmac_headers = utils.split(m[3], " ")
hmac_params.signature = m[4]
end
end
return hmac_params
end
-- plugin assumes the request parameters being used for creating
-- signature by client are not changed by core or any other plugin
local function create_hash(request_uri, hmac_params)
local signing_string = ""
local hmac_headers = hmac_params.hmac_headers
local count = #hmac_headers
for i = 1, count do
local header = hmac_headers[i]
local header_value = kong.request.get_header(header)
if not header_value then
if header == "request-line" then
-- request-line in hmac headers list
local request_line = fmt("%s %s HTTP/%s", kong.request.get_method(),
request_uri, kong.request.get_http_version())
signing_string = signing_string .. request_line
else
signing_string = signing_string .. header .. ":"
end
else
signing_string = signing_string .. header .. ":" .. " " .. header_value
end
if i < count then
signing_string = signing_string .. "\n"
end
end
return hmac[hmac_params.algorithm](hmac_params.secret, signing_string)
end
local function validate_signature(hmac_params)
local signature_1 = create_hash(kong.request.get_path_with_query(), hmac_params)
local signature_2 = decode_base64(hmac_params.signature)
if signature_1 == signature_2 then
return true
end
-- DEPRECATED BY: https://github.com/Kong/kong/pull/3339
local signature_1_deprecated = create_hash(ngx.var.uri, hmac_params)
return signature_1_deprecated == signature_2
end
local function load_credential_into_memory(username)
local key, err = kong.db.hmacauth_credentials:select_by_username(username)
if err then
return nil, err
end
return key
end
local function load_credential(username)
local credential, err
if username then
local credential_cache_key = kong.db.hmacauth_credentials:cache_key(username)
credential, err = kong.cache:get(credential_cache_key, nil,
load_credential_into_memory,
username)
end
if err then
kong.log.err(err)
return kong.response.exit(500, { message = "An unexpected error occurred" })
end
return credential
end
local function validate_clock_skew(date_header_name, allowed_clock_skew)
local date = kong.request.get_header(date_header_name)
if not date then
return false
end
local requestTime = parse_time(date)
if not requestTime then
return false
end
local skew = abs(time() - requestTime)
if skew > allowed_clock_skew then
return false
end
return true
end
local function validate_body()
local body, err = kong.request.get_raw_body()
if err then
kong.log.debug(err)
return false
end
local digest_received = kong.request.get_header(DIGEST)
if not digest_received then
-- if there is no digest and no body, it is ok
return body == ""
end
local digest = sha256:new()
digest:update(body or '')
local digest_created = "SHA-256=" .. encode_base64(digest:final())
return digest_created == digest_received
end
local function load_consumer_into_memory(consumer_id, anonymous)
local result, err = kong.db.consumers:select { id = consumer_id }
if not result then
if anonymous and not err then
err = 'anonymous consumer "' .. consumer_id .. '" not found'
end
return nil, err
end
return result
end
local function set_consumer(consumer, credential)
local set_header = kong.service.request.set_header
local clear_header = kong.service.request.clear_header
if consumer and consumer.id then
set_header(constants.HEADERS.CONSUMER_ID, consumer.id)
else
clear_header(constants.HEADERS.CONSUMER_ID)
end
if consumer and consumer.custom_id then
set_header(constants.HEADERS.CONSUMER_CUSTOM_ID, consumer.custom_id)
else
clear_header(constants.HEADERS.CONSUMER_CUSTOM_ID)
end
if consumer and consumer.username then
set_header(constants.HEADERS.CONSUMER_USERNAME, consumer.username)
else
clear_header(constants.HEADERS.CONSUMER_USERNAME)
end
kong.client.authenticate(consumer, credential)
if credential then
if credential.username then
set_header(constants.HEADERS.CREDENTIAL_USERNAME, credential.username)
else
clear_header(constants.HEADERS.CREDENTIAL_USERNAME)
end
clear_header(constants.HEADERS.ANONYMOUS)
else
clear_header(constants.HEADERS.CREDENTIAL_USERNAME)
set_header(constants.HEADERS.ANONYMOUS, true)
end
end
local function do_authentication(conf)
local authorization = kong.request.get_header(AUTHORIZATION)
local proxy_authorization = kong.request.get_header(PROXY_AUTHORIZATION)
-- If both headers are missing, return 401
if not (authorization or proxy_authorization) then
return false, { status = 401, message = "Unauthorized" }
end
-- validate clock skew
if not (validate_clock_skew(X_DATE, conf.clock_skew) or
validate_clock_skew(DATE, conf.clock_skew)) then
return false, {
status = 403,
message = "HMAC signature cannot be verified, a valid date or " ..
"x-date header is required for HMAC Authentication"
}
end
-- retrieve hmac parameter from Proxy-Authorization header
local hmac_params = retrieve_hmac_fields(proxy_authorization)
-- Try with the authorization header
if not hmac_params.username then
hmac_params = retrieve_hmac_fields(authorization)
if hmac_params and conf.hide_credentials then
kong.service.request.clear_header(AUTHORIZATION)
end
elseif conf.hide_credentials then
kong.service.request.clear_header(PROXY_AUTHORIZATION)
end
local ok, err = validate_params(hmac_params, conf)
if not ok then
kong.log.debug(err)
return false, { status = 403, message = SIGNATURE_NOT_VALID }
end
-- validate signature
local credential = load_credential(hmac_params.username)
if not credential then
kong.log.debug("failed to retrieve credential for ", hmac_params.username)
return false, { status = 403, message = SIGNATURE_NOT_VALID }
end
hmac_params.secret = credential.secret
if not validate_signature(hmac_params) then
return false, { status = 403, message = SIGNATURE_NOT_SAME }
end
-- If request body validation is enabled, then verify digest.
if conf.validate_request_body and not validate_body() then
kong.log.debug("digest validation failed")
return false, { status = 403, message = SIGNATURE_NOT_SAME }
end
-- Retrieve consumer
local consumer_cache_key, consumer
consumer_cache_key = kong.db.consumers:cache_key(credential.consumer.id)
consumer, err = kong.cache:get(consumer_cache_key, nil,
load_consumer_into_memory,
credential.consumer.id)
if err then
kong.log.err(err)
return kong.response.exit(500, { message = "An unexpected error occurred" })
end
set_consumer(consumer, credential)
return true
end
local _M = {}
function _M.execute(conf)
if conf.anonymous and kong.client.get_credential() then
-- we're already authenticated, and we're configured for using anonymous,
-- hence we're in a logical OR between auth methods and we're already done.
return
end
local ok, err = do_authentication(conf)
if not ok then
if conf.anonymous then
-- get anonymous user
local consumer_cache_key = kong.db.consumers:cache_key(conf.anonymous)
local consumer, err = kong.cache:get(consumer_cache_key, nil,
load_consumer_into_memory,
conf.anonymous, true)
if err then
kong.log.err(err)
return kong.response.exit(500, { message = "An unexpected error occurred" })
end
set_consumer(consumer, nil)
else
return kong.response.exit(err.status, { message = err.message }, err.headers)
end
end
end
return _M