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

revert: init introspect http client once #722

Merged
merged 1 commit into from
May 8, 2021
Merged
Show file tree
Hide file tree
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
69 changes: 34 additions & 35 deletions pipeline/authn/authenticator_oauth2_introspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ type AuthenticatorOAuth2Introspection struct {
}

func NewAuthenticatorOAuth2Introspection(c configuration.Provider, logger *logrusx.Logger) *AuthenticatorOAuth2Introspection {
return &AuthenticatorOAuth2Introspection{c: c, logger: logger}
var rt http.RoundTripper
return &AuthenticatorOAuth2Introspection{c: c, client: httpx.NewResilientClientLatencyToleranceSmall(rt), logger: logger}
}

func (a *AuthenticatorOAuth2Introspection) GetID() string {
Expand Down Expand Up @@ -261,49 +262,47 @@ func (a *AuthenticatorOAuth2Introspection) Config(config json.RawMessage) (*Auth
return nil, NewErrAuthenticatorMisconfigured(a, err)
}

if a.client == nil {
a.logger.Debug("Initializing http client")
var rt http.RoundTripper
if c.PreAuth != nil && c.PreAuth.Enabled {
var ep url.Values
var rt http.RoundTripper

if c.PreAuth.Audience != "" {
ep = url.Values{"audience": {c.PreAuth.Audience}}
}
if c.PreAuth != nil && c.PreAuth.Enabled {
var ep url.Values

rt = (&clientcredentials.Config{
ClientID: c.PreAuth.ClientID,
ClientSecret: c.PreAuth.ClientSecret,
Scopes: c.PreAuth.Scope,
EndpointParams: ep,
TokenURL: c.PreAuth.TokenURL,
}).Client(context.Background()).Transport
if c.PreAuth.Audience != "" {
ep = url.Values{"audience": {c.PreAuth.Audience}}
}

if c.Retry == nil {
c.Retry = &AuthenticatorOAuth2IntrospectionRetryConfiguration{Timeout: "500ms", MaxWait: "1s"}
} else {
if c.Retry.Timeout == "" {
c.Retry.Timeout = "500ms"
}
if c.Retry.MaxWait == "" {
c.Retry.MaxWait = "1s"
}
}
duration, err := time.ParseDuration(c.Retry.Timeout)
if err != nil {
return nil, err
}
timeout := time.Millisecond * duration
rt = (&clientcredentials.Config{
ClientID: c.PreAuth.ClientID,
ClientSecret: c.PreAuth.ClientSecret,
Scopes: c.PreAuth.Scope,
EndpointParams: ep,
TokenURL: c.PreAuth.TokenURL,
}).Client(context.Background()).Transport
}

maxWait, err := time.ParseDuration(c.Retry.MaxWait)
if err != nil {
return nil, err
if c.Retry == nil {
c.Retry = &AuthenticatorOAuth2IntrospectionRetryConfiguration{Timeout: "500ms", MaxWait: "1s"}
} else {
if c.Retry.Timeout == "" {
c.Retry.Timeout = "500ms"
}
if c.Retry.MaxWait == "" {
c.Retry.MaxWait = "1s"
}
}
duration, err := time.ParseDuration(c.Retry.Timeout)
if err != nil {
return nil, err
}
timeout := time.Millisecond * duration

a.client = httpx.NewResilientClientLatencyToleranceConfigurable(rt, timeout, maxWait)
maxWait, err := time.ParseDuration(c.Retry.MaxWait)
if err != nil {
return nil, err
}

a.client = httpx.NewResilientClientLatencyToleranceConfigurable(rt, timeout, maxWait)

if c.Cache.TTL != "" {
cacheTTL, err := time.ParseDuration(c.Cache.TTL)
if err != nil {
Expand Down
6 changes: 1 addition & 5 deletions pipeline/authn/authenticator_oauth2_introspection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,12 +555,8 @@ func TestAuthenticatorOAuth2Introspection(t *testing.T) {
tc.config, _ = sjson.SetBytes(tc.config, "introspection_url", ts.URL+"/oauth2/introspect")
tc.config, _ = sjson.SetBytes(tc.config, "pre_authorization.token_url", ts.URL+"/oauth2/token")

//reinitialize authenticator so client will be reinitialized in authenticator
reg := internal.NewRegistry(conf)
a, err := reg.PipelineAuthenticator("oauth2_introspection")

sess := new(AuthenticationSession)
err = a.Authenticate(tc.r, sess, tc.config, nil)
err := a.Authenticate(tc.r, sess, tc.config, nil)
if tc.expectErr {
require.Error(t, err)
if tc.expectExactErr != nil {
Expand Down