-
Notifications
You must be signed in to change notification settings - Fork 415
refactor(authn): make oauth2 authenticators stateless after initialization #1282
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
898c987
6349c84
0570aca
b78b59d
422740b
edba022
a3b8bc9
444b932
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| //go:build race | ||
| // +build race | ||
|
|
||
| // This test is a minimal concurrent harness to reproduce the data races | ||
| // in AuthenticatorOAuth2Introspection.Config() vs TokenToCache/TokenFromCache. | ||
| // Run with: go test -race -v ./pipeline/authn -run TestConfigDataRace | ||
| package authn_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/ory/x/configx" | ||
|
|
||
| . "github.com/ory/oathkeeper/pipeline/authn" | ||
| "github.com/ory/oathkeeper/internal" | ||
| ) | ||
|
|
||
| // TestConfigDataRace launches concurrent writers invoking Config() with varying | ||
| // cache TTLs while readers concurrently hit TokenToCache/TokenFromCache. | ||
| // The Go race detector should report races on a.cacheTTL and a.TokenCache. | ||
| func TestConfigDataRace(t *testing.T) { | ||
| reg := internal.NewRegistry(t, configx.SkipValidation()) | ||
| aa, err := reg.PipelineAuthenticator("oauth2_introspection") | ||
| require.NoError(t, err) | ||
| a, ok := aa.(*AuthenticatorOAuth2Introspection) | ||
| require.Truef(t, ok, "got type %T", aa) | ||
|
|
||
| base := `{"introspection_url":"http://127.0.0.1/oauth2/introspect","cache":{"enabled":true,"max_cost":1000}}` | ||
| ttls := []string{"25ms", "50ms", "75ms"} | ||
|
|
||
| var wg sync.WaitGroup | ||
|
|
||
| // Writers mutate TTL and (re)initialize cache concurrently. | ||
| for i := 0; i < 8; i++ { | ||
| wg.Add(1) | ||
| go func(id int) { | ||
| defer wg.Done() | ||
| for j := 0; j < 2000; j++ { | ||
| cfg := fmt.Sprintf(`{"introspection_url":"http://127.0.0.1/oauth2/introspect","cache":{"enabled":true,"max_cost":1000,"ttl":"%s"}}`, ttls[j%len(ttls)]) | ||
| _, _, _ = a.Config([]byte(cfg)) | ||
| } | ||
| }(i) | ||
| } | ||
|
|
||
| // Readers exercise cache paths concurrently with Config() calls. | ||
| for i := 0; i < 8; i++ { | ||
| wg.Add(1) | ||
| go func(id int) { | ||
| defer wg.Done() | ||
| res := &AuthenticatorOAuth2IntrospectionResult{Active: true} | ||
| for j := 0; j < 2000; j++ { | ||
| c, _, _ := a.Config([]byte(base)) | ||
| a.TokenToCache(c, res, "tok", nil) | ||
| _ = a.TokenFromCache(c, "tok", nil) | ||
| } | ||
| }(i) | ||
| } | ||
|
Comment on lines
+64
to
+78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🔴 Critical | 🏗️ Heavy lift Test would fail under The fix in The fix is incomplete: 🔒 Proposed fix: add RLock to TokenToCache and TokenFromCacheIn func (a *AuthenticatorOAuth2Introspection) TokenFromCache(config *AuthenticatorOAuth2IntrospectionConfiguration, token string, ss fosite.ScopeStrategy) *AuthenticatorOAuth2IntrospectionResult {
if !config.Cache.Enabled {
return nil
}
if ss == nil && len(config.Scopes) > 0 {
return nil
}
+ a.mu.RLock()
+ defer a.mu.RUnlock()
+
key := TokenCacheKey(token, config.IntrospectionURL)
i, found := a.TokenCache.Get(key) func (a *AuthenticatorOAuth2Introspection) TokenToCache(config *AuthenticatorOAuth2IntrospectionConfiguration, i *AuthenticatorOAuth2IntrospectionResult, token string, ss fosite.ScopeStrategy) {
if !config.Cache.Enabled {
return
}
if ss == nil && len(config.Scopes) > 0 {
return
}
+ a.mu.RLock()
+ defer a.mu.RUnlock()
+
key := TokenCacheKey(token, config.IntrospectionURL)
v, err := json.Marshal(i)
if err != nil {
return
}
if a.cacheTTL != nil {
a.TokenCache.SetWithTTL(key, v, 1, *a.cacheTTL)
} else {
a.TokenCache.Set(key, v, 1)
}
}🤖 Prompt for AI Agents |
||
|
|
||
| wg.Wait() | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.