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

Prevent goroutine leak in oidc client #11974

Merged
merged 7 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions lib/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3664,6 +3664,7 @@ const (
type oidcClient struct {
client *oidc.Client
config oidc.ClientConfig
cancel context.CancelFunc
}

// samlProvider is internal structure that stores SAML client and its config
Expand Down
22 changes: 17 additions & 5 deletions lib/auth/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func (a *Server) getOIDCClient(conn types.OIDCConnector) (*oidc.Client, error) {
return clientPack.client, nil
}

clientPack.cancel()
delete(a.oidcClients, conn.GetName())
return nil, trace.NotFound("connector %v has updated the configuration and is invalidated", conn.GetName())

Expand All @@ -79,26 +80,37 @@ func (a *Server) createOIDCClient(conn types.OIDCConnector) (*oidc.Client, error
return nil, trace.Wrap(err)
}

doneSyncing := make(chan struct{})
// SyncProviderConfig doesn't take a context for cancellation, instead it
// returns a channel that has to be closed to stop the sync. So what we
// can do until it is changed to take a context, is to wait for the syncContext
codingllama marked this conversation as resolved.
Show resolved Hide resolved
// we create below to be done and then close the channel. This allows the sync
// to be stopped in the event that the oidcClient is removed, or if the Server
// is Closed.
firstSync := make(chan struct{})
syncCtx, syncCancel := context.WithCancel(a.closeCtx)
go func() {
defer close(doneSyncing)
client.SyncProviderConfig(conn.GetIssuerURL())
stop := client.SyncProviderConfig(conn.GetIssuerURL())
close(firstSync)
<-syncCtx.Done()
close(stop)
}()

select {
case <-doneSyncing:
case <-firstSync:
case <-time.After(defaults.WebHeadersTimeout):
syncCancel()
return nil, trace.ConnectionProblem(nil,
"timed out syncing oidc connector %v, ensure URL %q is valid and accessible and check configuration",
conn.GetName(), conn.GetIssuerURL())
case <-a.closeCtx.Done():
syncCancel()
return nil, trace.ConnectionProblem(nil, "auth server is shutting down")
}

a.lock.Lock()
defer a.lock.Unlock()

a.oidcClients[conn.GetName()] = &oidcClient{client: client, config: config}
a.oidcClients[conn.GetName()] = &oidcClient{client: client, config: config, cancel: syncCancel}

return client, nil
}
Expand Down