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

[branch/v7] Prevent goroutine leak in oidc client (#11974) #12076

Merged
merged 2 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -2847,6 +2847,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
21 changes: 16 additions & 5 deletions lib/auth/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,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 @@ -81,26 +82,36 @@ 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. To ensure that
// the sync is eventually stopped we create a child context of the server context, which
// is cancelled either on deletion of the connector or shutdown of the server.
// This will cause syncCtx.Done() to unblock, at which point we can close the stop channel.
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