Skip to content
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

feat: Cache OAuth2 introspection client #482

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions pipeline/authn/authenticator_oauth2_introspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/url"
"strings"
"sync"
"time"

"github.com/dgraph-io/ristretto"
Expand Down Expand Up @@ -53,13 +54,41 @@ type cacheConfig struct {
TTL string `json:"ttl"`
}

type PreAuthCache struct {
sync.RWMutex

client *http.Client
config *clientcredentials.Config
}

func (c *PreAuthCache) Client(ctx context.Context, preAuthConfig *AuthenticatorOAuth2IntrospectionPreAuthConfiguration) *http.Client {
c.Lock()
defer c.Unlock()

config := clientcredentials.Config{
ClientID: preAuthConfig.ClientID,
ClientSecret: preAuthConfig.ClientSecret,
Scopes: preAuthConfig.Scope,
TokenURL: preAuthConfig.TokenURL,
}

if c.config == &config {
return c.client
}

client := config.Client(ctx)
c.client = client
return client
}

type AuthenticatorOAuth2Introspection struct {
c configuration.Provider

client *http.Client

tokenCache *ristretto.Cache
cacheTTL *time.Duration
preAuthCache *PreAuthCache
tokenCache *ristretto.Cache
cacheTTL *time.Duration
}

func NewAuthenticatorOAuth2Introspection(c configuration.Provider) *AuthenticatorOAuth2Introspection {
Expand Down Expand Up @@ -231,12 +260,7 @@ func (a *AuthenticatorOAuth2Introspection) Config(config json.RawMessage) (*Auth
var rt http.RoundTripper

if c.PreAuth != nil && c.PreAuth.Enabled {
rt = (&clientcredentials.Config{
ClientID: c.PreAuth.ClientID,
ClientSecret: c.PreAuth.ClientSecret,
Scopes: c.PreAuth.Scope,
TokenURL: c.PreAuth.TokenURL,
}).Client(context.Background()).Transport
rt = a.preAuthCache.Client(context.Background(), c.PreAuth).Transport
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c.PreAuth is a parameter which may change per route, but the authenticator is instantiated only once with one global cache, which will cause errors when more than one PreAuth are configured. Instead, this should probably be cached using the c.PreAuth contents as the key (e.g. crc32ing a string representation of c.PreAuth) .

Another issue is that the cache is never invalidated, even if requests start failing. This is problematic when, for example, the configuration is changed and subsequently reloaded.

}

if c.Retry == nil {
Expand Down