Skip to content

Commit

Permalink
fix: cache behavior with TTL (#968)
Browse files Browse the repository at this point in the history
This test will fail since everytime Authenticate() succeeds the token
is cached, even if it was already cached. This behavior makes it
possible to keep a token in cache if it is authenticated in a period
less than the TTL.

Signed-off-by: flusflas <[email protected]>
  • Loading branch information
flusflas authored May 23, 2022
1 parent 7f370a1 commit c4836f5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
15 changes: 13 additions & 2 deletions pipeline/authn/authenticator_oauth2_introspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,11 @@ func (a *AuthenticatorOAuth2Introspection) Authenticate(r *http.Request, session
ss := a.c.ToScopeStrategy(cf.ScopeStrategy, "authenticators.oauth2_introspection.config.scope_strategy")

i := a.tokenFromCache(cf, token, ss)
inCache := i != nil

// If the token can not be found, and the scope strategy is nil, and the required scope list
// is not empty, then we can not use the cache.
if i == nil {
if !inCache {
body := url.Values{"token": {token}}
if ss == nil {
body.Add("scope", strings.Join(cf.Scopes, " "))
Expand Down Expand Up @@ -256,7 +257,9 @@ func (a *AuthenticatorOAuth2Introspection) Authenticate(r *http.Request, session
}
}

a.tokenToCache(cf, i, token, ss)
if !inCache {
a.tokenToCache(cf, i, token, ss)
}

if len(i.Extra) == 0 {
i.Extra = map[string]interface{}{}
Expand Down Expand Up @@ -352,6 +355,14 @@ func (a *AuthenticatorOAuth2Introspection) Config(config json.RawMessage) (*Auth
if err != nil {
return nil, nil, err
}

// clear cache if previous ttl was longer (or none)
if a.tokenCache != nil {
if a.cacheTTL == nil || (a.cacheTTL != nil && a.cacheTTL.Seconds() > cacheTTL.Seconds()) {
a.tokenCache.Clear()
}
}

a.cacheTTL = &cacheTTL
}

Expand Down
23 changes: 23 additions & 0 deletions pipeline/authn/authenticator_oauth2_introspection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,29 @@ func TestAuthenticatorOAuth2Introspection(t *testing.T) {
require.Error(t, a.Authenticate(r, new(AuthenticationSession), config, nil))
didNotUseCache.Wait()
})

t.Run("case=cache cleared after ttl", func(t *testing.T) {
//time.Sleep(time.Second)
config := setup(t, `{ "required_scope": ["scope-a"], "trusted_issuers": ["foo", "bar"], "target_audience": ["audience"], "cache": { "ttl": "100ms" } }`)

didNotUseCache.Add(1)
require.NoError(t, a.Authenticate(r, expected, config, nil))
didNotUseCache.Wait()

// wait cache to save value
time.Sleep(time.Millisecond * 10)

require.NoError(t, a.Authenticate(r, new(AuthenticationSession), config, nil))
time.Sleep(50 * time.Millisecond)

require.NoError(t, a.Authenticate(r, new(AuthenticationSession), config, nil))
time.Sleep(50 * time.Millisecond)

// cache should have been cleared
didNotUseCache.Add(1)
require.NoError(t, a.Authenticate(r, new(AuthenticationSession), config, nil))
didNotUseCache.Wait()
})
})
})

Expand Down

0 comments on commit c4836f5

Please sign in to comment.