@@ -41,14 +41,24 @@ local RedisHealthCheck = require "api-gateway.redis.redisHealthCheck"
4141
4242local ApiKeyValidator = BaseValidator :new ()
4343
44+ local super = {
45+ instance = BaseValidator ,
46+ getKeyFromRedis = BaseValidator .getKeyFromRedis ,
47+ setKeyInRedis = BaseValidator .setKeyInRedis
48+ }
49+
4450local 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
8797end
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
90115function ApiKeyValidator :validate_api_key ()
91116 local api_key = ngx .var .api_key
0 commit comments