-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathvalidator.lua
More file actions
331 lines (286 loc) · 11.8 KB
/
validator.lua
File metadata and controls
331 lines (286 loc) · 11.8 KB
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
-- Copyright (c) 2015 Adobe Systems Incorporated. All rights reserved.
--
-- Permission is hereby granted, free of charge, to any person obtaining a
-- copy of this software and associated documentation files (the "Software"),
-- to deal in the Software without restriction, including without limitation
-- the rights to use, copy, modify, merge, publish, distribute, sublicense,
-- and/or sell copies of the Software, and to permit persons to whom the
-- Software is furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
-- DEALINGS IN THE SOFTWARE.
--
-- Base class for Gateway validators
--
-- User: ddascal
-- Date: 12/03/13
-- Time: 18:01
--
-- Dependencies:
-- 1. api-gateway-redis upstream needs to be set
-- 2. api-gateway-redis-replica needs to be set
--
local base = require "api-gateway.validation.base"
local RedisHealthCheck = require "api-gateway.redis.redisHealthCheck"
local cjson = require "cjson"
local debug_mode = ngx.config.debug
local RedisConnectionProvider = require "api-gateway.redis.redisConnectionProvider"
-- class to be used as a base class for all api-gateway validators --
local BaseValidator = {}
local redisHealthCheck = RedisHealthCheck:new({
shared_dict = "cachedkeys"
})
local redisConnectionProvider = RedisConnectionProvider:new()
function BaseValidator:new(o)
local o = o or {}
self.redis_RO_upstream = self.redis_RO_upstream or "api-gateway-redis-replica"
self.redis_RW_upstream = self.redis_RW_upstream or "api-gateway-redis"
self.redis_pass_env = self.redis_pass_env or "REDIS_PASS_API_KEY"
self.log_identifier = self.log_identifier or nil
setmetatable(o, self)
self.__index = self
return o
end
function BaseValidator:getVersion()
return base.version;
end
function BaseValidator:debug(...)
-- print("debug mode: ", debug_mode)
if debug_mode then
ngx.log(ngx.DEBUG, "validator: ", ...)
end
end
function BaseValidator:getKeyFromLocalCache(key, dict_name)
local localCachedKeys = ngx.shared[dict_name];
if (nil ~= localCachedKeys) then
return localCachedKeys:get(key);
else
ngx.log(ngx.ERR, "Dictionary " .. dict_name .. " does not exist")
end
end
function BaseValidator:setKeyInLocalCache(key, string_value, exptime, dict_name)
local localCachedKeys = ngx.shared[dict_name];
if (nil ~= localCachedKeys) then
return localCachedKeys:safe_set(key, string_value, exptime);
else
ngx.log(ngx.ERR, "Dictionary " .. dict_name .. " does not exist")
end
end
function BaseValidator:deleteKeyInLocalCache(key, dict_name)
local localCachedKeys = ngx.shared[dict_name]
if (nil ~= localCachedKeys) then
ngx.log(ngx.DEBUG, "Deleting entry with key " .. key .. " from local cache [" .. dict_name .. "]")
return localCachedKeys:delete(key)
else
ngx.log(ngx.ERR, "Dictionary " .. dict_name .. " does not exist")
end
end
-- retrieves a saved information from the Redis cache --
-- the method uses GET redis command --
-- it returns the value of the key, when found in the cache, nil otherwise --
-- for backward compatibility this method accepts a second argument, in which case it will perform a HGET instead.
function BaseValidator:getKeyFromRedis(key, hash_name)
if hash_name ~= nil then
return self:getHashValueFromRedis(key, hash_name)
end
local connection_options = {
upstream = self.redis_RO_upstream,
password = os.getenv(self.redis_pass_env)
}
local ok, redisread = redisConnectionProvider:getConnection(connection_options);
if ok then
local result, err = redisread:get(key)
redisConnectionProvider:closeConnection(redisread)
if (not result and err ~= nil) then
ngx.log(ngx.WARN, "Failed to read key " .. tostring(key) .. ". Error:", err)
return nil
else
if (type(result) == 'string') then
return result
elseif (result == ngx.null) then
ngx.log(ngx.WARN, "The value for the key " .. tostring(key) .. " is empty")
else
ngx.log(ngx.WARN, "type of result is not correct " .. tostring(type(result)))
end
end
end
return nil;
end
-- retrieves a saved information from the Redis cache --
-- the method uses HGET redis command --
-- it returns the value of the key, when found in the cache, nil otherwise --
function BaseValidator:getHashValueFromRedis(key, hash_field)
local connection_options = {
upstream = self.redis_RO_upstream,
password = os.getenv(self.redis_pass_env)
}
local ok, redisread = redisConnectionProvider:getConnection(connection_options);
if ok then
local redis_key, selecterror = redisread:hget(key, hash_field)
redisConnectionProvider:closeConnection(redisread)
if (type(redis_key) == 'string') then
return redis_key
elseif selecterror then
ngx.log(ngx.ERR, "failed to get key from redis ", tostring(key), " error: ", selecterror)
end
end
return nil;
end
-- is wrapper over redis exists but returns boolean instead
function BaseValidator:exists(key)
local connection_options = {
upstream = self.redis_RO_upstream,
password = os.getenv(self.redis_pass_env)
}
local ok, redisread = redisConnectionProvider:getConnection(connection_options);
if ok then
local redis_key, selecterror = redisread:exists(key)
redisConnectionProvider:closeConnection(redisread)
if selecterror or redis_key ~= 1 then
ngx.log(ngx.WARN, "Failed to read key " .. key .. " from Redis cache ", selecterror)
return false
end
return true;
end
return false
end
-- saves a value into the redis cache. --
-- the method uses HSET redis command --
-- it retuns true if the information is saved in the cache, false otherwise --
function BaseValidator:setKeyInRedis(key, hash_name, keyexpires, value)
ngx.log(ngx.DEBUG, "Storing in Redis the key [", tostring(key), "], expireat=", tostring(keyexpires), ", value=", tostring(value))
local connection_options = {
upstream = self.redis_RW_upstream,
password = os.getenv(self.redis_pass_env)
}
local ok, rediss = redisConnectionProvider:getConnection(connection_options);
if ok then
--ngx.log(ngx.DEBUG, "WRITING IN REDIS JSON OBJ key=" .. key .. "=" .. value .. ",expiring in:" .. (keyexpires - (os.time() * 1000)) )
rediss:init_pipeline()
rediss:hset(key, hash_name, value)
if keyexpires ~= nil then
rediss:pexpireat(key, keyexpires)
end
local _, commit_err = rediss:commit_pipeline()
redisConnectionProvider:closeConnection(rediss)
--ngx.log(ngx.WARN, "SAVE RESULT:" .. cjson.encode(commit_res) )
if (commit_err == nil) then
return true
else
ngx.log(ngx.WARN, "Failed to write the key [", key, "] in Redis. Error:", commit_err)
end
end
return false;
end
function BaseValidator:deleteKeyFromRedis(key)
ngx.log(ngx.DEBUG, "Deleting key from Redis: " .. key)
local connection_options = {
upstream = self.redis_RW_upstream,
password = os.getenv(self.redis_pass_env)
}
local ok, redis = redisConnectionProvider:getConnection(connection_options);
if ok then
local redisResponse, err = redis:del(key)
if err then
ngx.log(ngx.ERR, "Error while deleting key from redis: ", err)
return nil
end
return redisResponse
end
end
-- it accepts a table or a string and saves the properties into the current request context --
function BaseValidator:setContextProperties(cached_token)
local jsonCacheObj = cached_token
if (type(cached_token) == 'string') then
jsonCacheObj = cjson.decode(cached_token)
end
for k, v in pairs(jsonCacheObj) do
ngx.ctx[k] = v
self:debug("Setting ngx.ctx." .. tostring(k) .. "=" .. tostring(v))
end
end
-- TTL using LuaResty Redis
function BaseValidator:executeTtl(key)
local connection_options = {
upstream = self.redis_RO_upstream,
password = os.getenv(self.redis_pass_env)
}
local ok, redis = redisConnectionProvider:getConnection(connection_options);
if ok then
ngx.log(ngx.DEBUG, "Executing TTL for key:" .. key)
local ttl, err = redis:ttl(key)
if not ttl then
ngx.log(ngx.ERR, "Could not execute TTL for key: " .. key .. ". Error: " .. err)
else
ngx.log(ngx.DEBUG, "TTL response: " .. ttl)
return ttl
end
end
end
-- converts a response status to a valid HTTP status code
function BaseValidator:convertToValidHttpStatusCode(response_status)
response_status = tonumber(response_status)
if response_status == nil then
return 500
end
if (response_status >= 100 and response_status <= 599) then
return response_status
end
local http_code_str = string.sub(tostring(response_status), 1, 3)
local http_code_number = tonumber(http_code_str)
if http_code_number ~= nil and http_code_number >= 100 and http_code_number <= 599 then
return http_code_number
end
ngx.log(ngx.DEBUG, "Status code: ", tostring(response_status), " is not in a valid HTTP Status Code format")
return 500
end
-- generic exit function for a validator --
function BaseValidator:exitFn(status, resp_body)
local responseTime = ngx.now() - ngx.req.start_time()
ngx.header["Response-Time"] = responseTime
if(self.log_identifier) then
if(ngx.var[self.log_identifier]) then
ngx.var[self.log_identifier] = string.format("%.3f", responseTime)
else
ngx.log(ngx.WARN, "ngx variable ", self.log_identifier , " is not declared in ngx conf")
end
end
ngx.status = self:convertToValidHttpStatusCode(status)
if (ngx.null ~= resp_body) then
ngx.header["Content-Type"] = "application/json"
ngx.say(resp_body)
end
return ngx.OK
end
function BaseValidator:overrideErrorResponses(custom_error_responses)
--- handle the case when custom_error_responses is passed as string
if type(custom_error_responses) == "string" then
custom_error_responses = cjson.decode(custom_error_responses)
end
if custom_error_responses ~= nil and type(custom_error_responses) == "table" then
local existing_custom_error_responses = ngx.var.validator_custom_error_responses
if existing_custom_error_responses ~= nil and existing_custom_error_responses ~= "" then
ngx.log(ngx.DEBUG, "ngx.var.validator_custom_error_responses already exist. Going to merge...")
existing_custom_error_responses = cjson.decode(existing_custom_error_responses)
for k, v in pairs(custom_error_responses) do
if (existing_custom_error_responses[k] == nil) then
existing_custom_error_responses[k] = v
end
end
ngx.var.validator_custom_error_responses = cjson.encode(existing_custom_error_responses)
else
ngx.var.validator_custom_error_responses = cjson.encode(custom_error_responses)
end
else
ngx.log(ngx.DEBUG, "No custom error responses defined for validator")
end
end
return BaseValidator