@@ -41,14 +41,24 @@ local RedisHealthCheck = require "api-gateway.redis.redisHealthCheck"
41
41
42
42
local ApiKeyValidator = BaseValidator :new ()
43
43
44
+ local super = {
45
+ instance = BaseValidator ,
46
+ getKeyFromRedis = BaseValidator .getKeyFromRedis ,
47
+ setKeyInRedis = BaseValidator .setKeyInRedis
48
+ }
49
+
44
50
local RESPONSES = {
45
51
MISSING_KEY = { error_code = " 403000" , message = " Api KEY is missing" },
46
52
INVALID_KEY = { error_code = " 403003" , message = " Api KEY is invalid" },
47
53
UNKNOWN_ERROR = { error_code = " 503000" , message = " Could not validate API KEY" }
48
54
}
49
55
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" )
52
62
local red = redis :new ();
53
63
54
64
local redis_host , redis_port = self :getRedisUpstream ()
@@ -86,6 +96,21 @@ function ApiKeyValidator:getKeyFromRedis(hashed_key)
86
96
end
87
97
end
88
98
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
+
89
114
90
115
function ApiKeyValidator :validate_api_key ()
91
116
local api_key = ngx .var .api_key
0 commit comments