Skip to content

Make api-key data more generic in order to attach more info to it #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/lua/api-gateway/validation/key/redisApiKeyValidator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,24 @@ local RedisHealthCheck = require "api-gateway.redis.redisHealthCheck"

local ApiKeyValidator = BaseValidator:new()

local super = {
instance = BaseValidator,
getKeyFromRedis = BaseValidator.getKeyFromRedis,
setKeyInRedis = BaseValidator.setKeyInRedis
}

local RESPONSES = {
MISSING_KEY = { error_code = "403000", message = "Api KEY is missing" },
INVALID_KEY = { error_code = "403003", message = "Api KEY is invalid" },
UNKNOWN_ERROR = { error_code = "503000", message = "Could not validate API KEY"}
}

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

local redis_host, redis_port = self:getRedisUpstream()
Expand Down Expand Up @@ -86,6 +96,21 @@ function ApiKeyValidator:getKeyFromRedis(hashed_key)
end
end

function ApiKeyValidator:getKeyFromRedis(hashed_key)
local redis_key = "cachedkey:" .. hashed_key;
--1. try to read the key in the new format
local redis_metadata = super.getKeyFromRedis(ApiKeyValidator, redis_key, "metadata")
if redis_metadata ~= nil then
ngx.log(ngx.DEBUG, "Found API KEY Metadata in Redis:", tostring(redis_metadata))
local metadata = assert( cjson.decode(redis_metadata), "Invalid metadata found in Redis:" .. tostring(redis_metadata) )
if metadata ~= nil then
return metadata
end
end
--2. the key in the new format doesn't exist, try the old format
return self:getLegacyKeyFromRedis(redis_key)
end


function ApiKeyValidator:validate_api_key()
local api_key = ngx.var.api_key
Expand Down
5 changes: 3 additions & 2 deletions src/lua/api-gateway/validation/validator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ function BaseValidator:getKeyFromRedis(key, hash_name)
if ok then
local redis_key, selecterror = redisread:hget(key, hash_name)
redisread:set_keepalive(30000, 100)
--ngx.log(ngx.WARN, "GOT REDIS RESPONSE:" .. type(redis_key));
if (type(redis_key) == 'string') then
return redis_key
end
Expand All @@ -122,7 +121,9 @@ function BaseValidator:setKeyInRedis(key, hash_name, keyexpires, value)
--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)
rediss:pexpireat(key, keyexpires)
if keyexpires ~= nil then
rediss:pexpireat(key, keyexpires)
end
local commit_res, commit_err = rediss:commit_pipeline()
rediss:set_keepalive(30000, 100)
--ngx.log(ngx.WARN, "SAVE RESULT:" .. cjson.encode(commit_res) )
Expand Down
Loading