Skip to content

Commit 612fedf

Browse files
committed
Merge pull request #3 from adobe-apiplatform/issue-2
Make api-key data more generic in order to attach more info to it
2 parents 380c5c2 + b1ca85e commit 612fedf

File tree

7 files changed

+621
-25
lines changed

7 files changed

+621
-25
lines changed

src/lua/api-gateway/validation/key/redisApiKeyValidator.lua

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,24 @@ local RedisHealthCheck = require "api-gateway.redis.redisHealthCheck"
4141

4242
local ApiKeyValidator = BaseValidator:new()
4343

44+
local super = {
45+
instance = BaseValidator,
46+
getKeyFromRedis = BaseValidator.getKeyFromRedis,
47+
setKeyInRedis = BaseValidator.setKeyInRedis
48+
}
49+
4450
local RESPONSES = {
4551
MISSING_KEY = { error_code = "403000", message = "Api KEY is missing" },
4652
INVALID_KEY = { error_code = "403003", message = "Api KEY is invalid" },
4753
UNKNOWN_ERROR = { error_code = "503000", message = "Could not validate API KEY"}
4854
}
4955

50-
function ApiKeyValidator:getKeyFromRedis(hashed_key)
51-
local redis_key = "cachedkey:" .. hashed_key;
56+
--- @Deprecated
57+
-- Returns a set of fields associated to the api-key from Redis, if the key exists
58+
-- @param hashed_key
59+
--
60+
function ApiKeyValidator:getLegacyKeyFromRedis(redis_key)
61+
ngx.log(ngx.DEBUG, "Looking for a legacy api-key in Redis")
5262
local red = redis:new();
5363

5464
local redis_host, redis_port = self:getRedisUpstream()
@@ -86,6 +96,21 @@ function ApiKeyValidator:getKeyFromRedis(hashed_key)
8696
end
8797
end
8898

99+
function ApiKeyValidator:getKeyFromRedis(hashed_key)
100+
local redis_key = "cachedkey:" .. hashed_key;
101+
--1. try to read the key in the new format
102+
local redis_metadata = super.getKeyFromRedis(ApiKeyValidator, redis_key, "metadata")
103+
if redis_metadata ~= nil then
104+
ngx.log(ngx.DEBUG, "Found API KEY Metadata in Redis:", tostring(redis_metadata))
105+
local metadata = assert( cjson.decode(redis_metadata), "Invalid metadata found in Redis:" .. tostring(redis_metadata) )
106+
if metadata ~= nil then
107+
return metadata
108+
end
109+
end
110+
--2. the key in the new format doesn't exist, try the old format
111+
return self:getLegacyKeyFromRedis(redis_key)
112+
end
113+
89114

90115
function ApiKeyValidator:validate_api_key()
91116
local api_key = ngx.var.api_key

src/lua/api-gateway/validation/validator.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ function BaseValidator:getKeyFromRedis(key, hash_name)
100100
if ok then
101101
local redis_key, selecterror = redisread:hget(key, hash_name)
102102
redisread:set_keepalive(30000, 100)
103-
--ngx.log(ngx.WARN, "GOT REDIS RESPONSE:" .. type(redis_key));
104103
if (type(redis_key) == 'string') then
105104
return redis_key
106105
end
@@ -122,7 +121,9 @@ function BaseValidator:setKeyInRedis(key, hash_name, keyexpires, value)
122121
--ngx.log(ngx.DEBUG, "WRITING IN REDIS JSON OBJ key=" .. key .. "=" .. value .. ",expiring in:" .. (keyexpires - (os.time() * 1000)) )
123122
rediss:init_pipeline()
124123
rediss:hset(key, hash_name, value)
125-
rediss:pexpireat(key, keyexpires)
124+
if keyexpires ~= nil then
125+
rediss:pexpireat(key, keyexpires)
126+
end
126127
local commit_res, commit_err = rediss:commit_pipeline()
127128
rediss:set_keepalive(30000, 100)
128129
--ngx.log(ngx.WARN, "SAVE RESULT:" .. cjson.encode(commit_res) )

0 commit comments

Comments
 (0)