-
Notifications
You must be signed in to change notification settings - Fork 170
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
Add caching to the token introspection policy #656
Merged
+259
−5
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2db3731
policy/token_introspection: add cache for tokens
davidor c31001d
spec/policy/token_introspection: test cache for tokens
davidor 3fbb880
policy/token_introspection: add config params for tokens cache
davidor c716e9f
policy/token_introspection: use tokens cache
davidor 077e274
spec/policy/token_introspection: require policy once to simplify code
davidor 15c22ab
spec/policy/token_introspection: add tests for tokens caching
davidor 4551143
CHANGELOG: add caching for token introspection policy
davidor 393fc62
policy/token_introspection: add min/max for tokens and tlls in json s…
davidor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
gateway/src/apicast/policy/token_introspection/tokens_cache.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- TokensCache | ||
-- Tokens cache for the token introspection policy. | ||
|
||
local setmetatable = setmetatable | ||
local tonumber = tonumber | ||
|
||
local lrucache = require('resty.lrucache') | ||
|
||
local _M = { } | ||
|
||
local mt = { __index = _M } | ||
|
||
local function ttl_from_introspection(introspection_info) | ||
local token_exp = introspection_info.exp | ||
return token_exp and (tonumber(token_exp) - ngx.time()) | ||
end | ||
|
||
function _M.new(max_ttl, max_cached_tokens) | ||
local self = setmetatable({}, mt) | ||
self.max_ttl = max_ttl | ||
self.storage = lrucache.new(max_cached_tokens or 10000) | ||
return self | ||
end | ||
|
||
function _M:get(token) | ||
return self.storage:get(token) | ||
end | ||
|
||
function _M:set(token, introspection_info) | ||
local ttl = ttl_from_introspection(introspection_info) | ||
|
||
if self.max_ttl and (not ttl or self.max_ttl < ttl) then | ||
ttl = self.max_ttl | ||
end | ||
|
||
-- If the config does not contain a max ttl and the token instrospection did | ||
-- not return one, we cannot cache the token. | ||
if ttl and ttl > 0 then | ||
self.storage:set(token, introspection_info, ttl) | ||
end | ||
end | ||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
local lrucache = require('resty.lrucache') | ||
|
||
local TokenCache = require('apicast.policy.token_introspection.tokens_cache') | ||
|
||
local function assert_cache_spy_called_with_ttl(cache_spy, ttl) | ||
-- We only care about the 4th arg (ttl) of the first set() call | ||
assert.equals(ttl, cache_spy.calls[1].vals[4]) | ||
end | ||
|
||
describe('token_cache', function() | ||
local current_time = 1521054560 | ||
local test_token = 'a_token' | ||
|
||
local max_ttl = 10 | ||
local ttl_longer_than_max = max_ttl + 1 | ||
local ttl_shorter_than_max = max_ttl - 1 | ||
|
||
-- Storage to inject in the cache so we can spy on it | ||
local cache_storage | ||
local cache_storage_spy | ||
|
||
before_each(function() | ||
stub(ngx, 'time').returns(current_time) | ||
|
||
cache_storage = lrucache.new(10) | ||
cache_storage_spy = spy.on(cache_storage, 'set') | ||
end) | ||
|
||
describe('when max TTL is set to 0', function() | ||
it('does not cache anything', function() | ||
local cache = TokenCache.new(0) | ||
cache:set(test_token, { exp = current_time + 60 }) | ||
assert.is_falsy(cache:get(test_token)) | ||
end) | ||
end) | ||
|
||
describe('when the token info contains an "exp" field', function() | ||
describe('and max TTL > "exp" TTL ', function() | ||
it('caches the token with the TTL from "exp"', function() | ||
local cache = TokenCache.new(max_ttl) | ||
cache.storage = cache_storage | ||
local introspection_info = { | ||
active = true, | ||
exp = current_time + ttl_shorter_than_max | ||
} | ||
|
||
cache:set(test_token, introspection_info) | ||
|
||
assert.same(introspection_info, cache:get(test_token)) | ||
assert_cache_spy_called_with_ttl( | ||
cache_storage_spy, ttl_shorter_than_max) | ||
end) | ||
end) | ||
|
||
describe('and max TTL < "exp" TTL', function() | ||
it('caches the token with the max TTL', function() | ||
local cache = TokenCache.new(max_ttl) | ||
cache.storage = cache_storage | ||
local introspection_info = { | ||
active = true, | ||
exp = current_time + ttl_longer_than_max | ||
} | ||
|
||
cache:set(test_token, introspection_info) | ||
|
||
assert.same(introspection_info, cache:get(test_token)) | ||
assert_cache_spy_called_with_ttl(cache_storage_spy, max_ttl) | ||
end) | ||
end) | ||
|
||
describe('and max TTL is nil', function() | ||
it('caches the token with the TTL from "exp"', function() | ||
local cache = TokenCache.new() | ||
cache.storage = cache_storage | ||
local introspection_info = { | ||
active = true, | ||
exp = current_time + ttl_longer_than_max | ||
} | ||
|
||
cache:set(test_token, introspection_info) | ||
|
||
assert.same(introspection_info, cache:get(test_token)) | ||
assert_cache_spy_called_with_ttl( | ||
cache_storage_spy, ttl_longer_than_max) | ||
end) | ||
end) | ||
end) | ||
|
||
describe('when the token info does not contain an "exp" field', function() | ||
describe('and there is a max TTL configured', function() | ||
it('caches the token with the max TTL', function() | ||
local cache = TokenCache.new(max_ttl) | ||
cache.storage = cache_storage | ||
local introspection_info = { active = true, } | ||
|
||
cache:set(test_token, introspection_info) | ||
|
||
assert.same(introspection_info, cache:get(test_token)) | ||
assert_cache_spy_called_with_ttl(cache_storage_spy, max_ttl) | ||
|
||
end) | ||
end) | ||
|
||
describe('and there is not a max TTL configured', function() | ||
it('does not cache the token', function() | ||
local cache = TokenCache.new() | ||
cache.storage = cache_storage | ||
local introspection_info = { active = true, } | ||
|
||
cache:set(test_token, introspection_info) | ||
|
||
assert.is_falsy(cache:get(test_token)) | ||
end) | ||
end) | ||
end) | ||
end) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably should have "default" 0 ?
edit: maybe not needed when it is not required property
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. It already defaults to 0 in the code but should be here too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@davidor probably not needed. Only if we want to populate it in the UI.