Repository navigation
fix: reuse CF login across reconciles to avoid UAA lockout #347
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
Open
dwlou
wants to merge
6
commits into
main
Choose a base branch
from
fix/cf-login-reuse
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ea50104
fix: reuse CF login across reconciles to avoid UAA lockout
dwlou 4d30062
refactor: cache CF token source instead of config to fix re-auth race
dwlou 2b6b84b
fix: thread context through CF auth bootstrap and satisfy linters
dwlou ca69233
build: update nix vendorHash for updated dependencies
dwlou d027417
fix: coalesce concurrent re-bootstrap after shared token dies
dwlou 0344d13
Merge branch 'main' into fix/cf-login-reuse
SatabdiG File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| /* | ||
| Copyright 2023 SAP SE | ||
| */ | ||
|
|
||
| package clients | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/tls" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "strings" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/cloudfoundry/go-cfclient/v3/config" | ||
| "golang.org/x/oauth2" | ||
| "golang.org/x/sync/singleflight" | ||
| ) | ||
|
|
||
| // cfAuthCache reuses one CF UAA login per credential across all reconciles. | ||
| // | ||
| // GetCredentialConfig runs on every Connect (i.e. every reconcile) for every CF | ||
| // resource, and config.New(UserPassword(...)) performs an eager password-grant | ||
| // login against CF UAA. Reference resolvers (space/org/domain ResolveByName) build | ||
| // a client again within the same reconcile, so a single reconcile can trigger | ||
| // several logins. Concurrent per-identity password logins are the documented | ||
| // lockout trigger for the shared technical user. | ||
| // | ||
| // We cache the *login* — a goroutine-safe oauth2.TokenSource — not the | ||
| // *config.Config. A config carries a mutable, shared transport: on a 401 | ||
| // go-cfclient's retryableAuthTransport re-runs the password grant and reassigns | ||
| // oauth2.Transport.Source without synchronization, so sharing one config across | ||
| // controllers races and can stampede logins. Instead every reconcile builds its | ||
| // own short-lived config from the shared token source, so each has its own | ||
| // transport, and token expiry refreshes via the refresh_token grant — never the | ||
| // password grant. | ||
| // | ||
| // The key intentionally omits Passcode: only UserPassword is used, and a passcode | ||
| // is a one-time code that must not be cached. If passcode auth is ever wired into | ||
| // GetCredentialConfig it must bypass this cache. | ||
|
|
||
| // cfAuthEntry is the cached, reusable login for one credential. | ||
| type cfAuthEntry struct { | ||
| src oauth2.TokenSource // oauth2.ReuseTokenSource: goroutine-safe, refreshes via refresh_token | ||
| loginURL string // discovered CF login endpoint | ||
| uaaURL string // discovered CF UAA endpoint | ||
| } | ||
|
|
||
| var ( | ||
| cfAuthCache sync.Map // map[string]*cfAuthEntry | ||
| cfAuthSF singleflight.Group | ||
| ) | ||
|
|
||
| // bootstrapTimeout bounds the one-time discovery+login per credential. | ||
| const bootstrapTimeout = 30 * time.Second | ||
|
|
||
| // cfAuthKey identifies a credential bundle. url+email+password only (see doc above). | ||
| func cfAuthKey(url, email, password string) string { | ||
| return strings.Join([]string{url, email, password}, "\x00") | ||
| } | ||
|
|
||
| // cachedCFConfig returns a go-cfclient config for the credential, reusing a single | ||
| // UAA login across reconciles. Each call returns a fresh *config.Config seeded with | ||
| // the current token (grant type refresh_token, so config.New performs no login) and | ||
| // the pre-discovered auth URLs (so config.New performs no discovery GET). The build | ||
| // is therefore fully local. | ||
| func cachedCFConfig(ctx context.Context, url, email, password string) (*config.Config, error) { | ||
| key := cfAuthKey(url, email, password) | ||
|
|
||
| entry, err := getOrBootstrap(ctx, key, url, email, password) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| tok, err := entry.src.Token() | ||
| if err != nil { | ||
| // The refresh token is dead/revoked (or bootstrap produced a token that | ||
| // can no longer refresh). Drop this wedged entry and bootstrap once more | ||
| // with a fresh password login, then retry a single time. | ||
| // | ||
| // CompareAndDelete (not Delete) so we only evict the exact bad entry we | ||
| // observed: when N reconciles share a dying token source they all fail | ||
| // here at once, and an unconditional Delete would let a late caller wipe | ||
| // the fresh entry an earlier one just re-bootstrapped. We also must not | ||
| // Forget the singleflight key — leaving it lets the concurrent | ||
| // re-bootstraps coalesce into a single password login instead of each | ||
| // starting its own. | ||
| cfAuthCache.CompareAndDelete(key, entry) | ||
| entry, err = getOrBootstrap(ctx, key, url, email, password) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| tok, err = entry.src.Token() | ||
| if err != nil { | ||
| // Still unusable: drop this entry (again only if it's still the one | ||
| // we saw) so the next reconcile starts clean rather than reusing a | ||
| // known-bad login. | ||
| cfAuthCache.CompareAndDelete(key, entry) | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| return config.New(url, | ||
| config.Token(tok.AccessToken, tok.RefreshToken), | ||
| config.AuthTokenURL(entry.loginURL, entry.uaaURL), | ||
| config.SkipTLSValidation(), | ||
| ) | ||
| } | ||
|
|
||
| // getOrBootstrap returns the cached entry for key, creating it (once, coalesced | ||
| // across concurrent callers) via a single password login if absent. | ||
| func getOrBootstrap(ctx context.Context, key, url, email, password string) (*cfAuthEntry, error) { | ||
| if e, ok := cfAuthCache.Load(key); ok { | ||
| return e.(*cfAuthEntry), nil | ||
| } | ||
|
|
||
| v, err, _ := cfAuthSF.Do(key, func() (interface{}, error) { | ||
| // Another caller may have populated the cache while we queued. | ||
| if e, ok := cfAuthCache.Load(key); ok { | ||
| return e.(*cfAuthEntry), nil | ||
| } | ||
| entry, err := bootstrapCFAuth(ctx, url, email, password) | ||
| if err != nil { | ||
| // Do not cache a failed login; the next reconcile retries | ||
| // (controller-runtime already backs off failing reconciles). | ||
| return nil, err | ||
| } | ||
| cfAuthCache.Store(key, entry) | ||
| return entry, nil | ||
| }) | ||
| if err != nil { | ||
|
SatabdiG marked this conversation as resolved.
|
||
| return nil, err | ||
| } | ||
| return v.(*cfAuthEntry), nil | ||
| } | ||
|
|
||
| // bootstrapCFAuth performs the one login per credential: discover the CF auth | ||
| // endpoints, then obtain a refreshing token source via the password grant. | ||
| func bootstrapCFAuth(ctx context.Context, url, email, password string) (*cfAuthEntry, error) { | ||
| // The bootstrap is shared across reconciles (singleflight) and the resulting | ||
| // token source lives on to refresh long after this call returns, so it must | ||
| // not be tied to the triggering reconcile's cancellation. WithoutCancel keeps | ||
| // the request values but drops cancellation/deadline. | ||
| ctx = context.WithoutCancel(ctx) | ||
|
|
||
| httpClient := &http.Client{ | ||
| Timeout: bootstrapTimeout, | ||
| Transport: &http.Transport{ | ||
| TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, //nolint:gosec // mirrors config.SkipTLSValidation(); making TLS verification configurable is tracked in #348 | ||
| }, | ||
| } | ||
|
|
||
| loginURL, uaaURL, err := discoverCFAuthEndpoints(ctx, httpClient, url) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| oauthCfg := &oauth2.Config{ | ||
| ClientID: "cf", | ||
| Endpoint: oauth2.Endpoint{ | ||
| AuthURL: loginURL + "/oauth/auth", | ||
| TokenURL: uaaURL + "/oauth/token", | ||
| AuthStyle: oauth2.AuthStyleInHeader, | ||
| }, | ||
| } | ||
|
|
||
| // oauth2's ReuseTokenSource captures this context for every future refresh. | ||
| authCtx := context.WithValue(ctx, oauth2.HTTPClient, httpClient) | ||
|
|
||
| tok, err := oauthCfg.PasswordCredentialsToken(authCtx, email, password) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("cloudfoundry UAA login failed: %w", err) | ||
| } | ||
|
|
||
| return &cfAuthEntry{ | ||
| src: oauthCfg.TokenSource(authCtx, tok), | ||
| loginURL: loginURL, | ||
| uaaURL: uaaURL, | ||
| }, nil | ||
| } | ||
|
|
||
| // discoverCFAuthEndpoints reads the CF API root and returns the login and UAA | ||
| // endpoints — the same discovery go-cfclient performs internally. | ||
| func discoverCFAuthEndpoints(ctx context.Context, httpClient *http.Client, url string) (loginURL, uaaURL string, err error) { | ||
| root := strings.TrimRight(url, "/") + "/" | ||
| req, err := http.NewRequestWithContext(ctx, http.MethodGet, root, nil) | ||
| if err != nil { | ||
| return "", "", err | ||
| } | ||
| resp, err := httpClient.Do(req) | ||
| if err != nil { | ||
| return "", "", fmt.Errorf("error while discovering CF auth endpoints: %w", err) | ||
| } | ||
| defer resp.Body.Close() //nolint:errcheck | ||
|
|
||
| var body struct { | ||
| Links struct { | ||
| Login struct { | ||
| Href string `json:"href"` | ||
| } `json:"login"` | ||
| Uaa struct { | ||
| Href string `json:"href"` | ||
| } `json:"uaa"` | ||
| } `json:"links"` | ||
| } | ||
| if err := json.NewDecoder(resp.Body).Decode(&body); err != nil { | ||
| return "", "", fmt.Errorf("error decoding CF API root: %w", err) | ||
| } | ||
| if body.Links.Login.Href == "" || body.Links.Uaa.Href == "" { | ||
| return "", "", fmt.Errorf("CF API root did not advertise login/uaa endpoints") | ||
| } | ||
| return strings.TrimRight(body.Links.Login.Href, "/"), strings.TrimRight(body.Links.Uaa.Href, "/"), nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.