-
Notifications
You must be signed in to change notification settings - Fork 25
Add stackit-cli compatible authentication to the SDK #3865
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
franklouwers
wants to merge
6
commits into
stackitcloud:main
Choose a base branch
from
franklouwers:feature/cli-provider-auth
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
4db745b
feat(core): add CLI provider authentication support
franklouwers db05cee
feat(core): add SDK config integration for CLI provider auth
franklouwers cab4194
test(core): add comprehensive tests for CLI provider auth
franklouwers 25b6b99
fix(core): update CLI auth tests to match implementation
franklouwers c3153a1
docs: add changelog entries for CLI provider authentication
franklouwers 7ceaa43
docs: add CLI provider authentication example
franklouwers 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,168 @@ | ||||||||||
| package cliauth | ||||||||||
|
|
||||||||||
| import ( | ||||||||||
| "fmt" | ||||||||||
| "os" | ||||||||||
| "time" | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| var ( | ||||||||||
| // Start refresh attempts this duration before token expiration | ||||||||||
| defaultTimeStartBeforeTokenExpiration = 5 * time.Minute | ||||||||||
| // Check context cancellation this frequently while waiting | ||||||||||
| defaultTimeBetweenContextCheck = time.Second | ||||||||||
| // Retry interval on refresh failures | ||||||||||
| defaultTimeBetweenTries = 2 * time.Minute | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| // continuousRefreshToken continuously refreshes the CLI provider token in the background. | ||||||||||
| // It monitors token expiration and automatically refreshes before the token expires. | ||||||||||
| // | ||||||||||
| // The goroutine terminates when: | ||||||||||
| // - The context is canceled | ||||||||||
| // - A non-retryable error occurs | ||||||||||
| // | ||||||||||
| // To terminate this routine, cancel the context in flow.refreshContext. | ||||||||||
| func continuousRefreshToken(flow *CLIProviderFlow) { | ||||||||||
| refresher := &continuousTokenRefresher{ | ||||||||||
| flow: flow, | ||||||||||
| timeStartBeforeTokenExpiration: defaultTimeStartBeforeTokenExpiration, | ||||||||||
| timeBetweenContextCheck: defaultTimeBetweenContextCheck, | ||||||||||
| timeBetweenTries: defaultTimeBetweenTries, | ||||||||||
| } | ||||||||||
| err := refresher.continuousRefreshToken() | ||||||||||
| fmt.Fprintf(os.Stderr, "CLI provider token refreshing terminated: %v\n", err) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| type continuousTokenRefresher struct { | ||||||||||
| flow *CLIProviderFlow | ||||||||||
| timeStartBeforeTokenExpiration time.Duration | ||||||||||
| timeBetweenContextCheck time.Duration | ||||||||||
| timeBetweenTries time.Duration | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // continuousRefreshToken runs the main background refresh loop. | ||||||||||
| // It waits until the token is close to expiring, then refreshes it. | ||||||||||
| // Always returns with a non-nil error (indicating why it terminated). | ||||||||||
| func (r *continuousTokenRefresher) continuousRefreshToken() error { | ||||||||||
| // Compute initial refresh timestamp | ||||||||||
| startRefreshTimestamp := r.getNextRefreshTimestamp() | ||||||||||
|
|
||||||||||
| for { | ||||||||||
| // Wait until it's time to refresh (or context is canceled) | ||||||||||
| err := r.waitUntilTimestamp(startRefreshTimestamp) | ||||||||||
| if err != nil { | ||||||||||
| return err | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Check if context was canceled | ||||||||||
| err = r.flow.refreshContext.Err() | ||||||||||
| if err != nil { | ||||||||||
| return fmt.Errorf("context canceled: %w", err) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Attempt to refresh the token | ||||||||||
| ok, err := r.refreshToken() | ||||||||||
| if err != nil { | ||||||||||
| return fmt.Errorf("refresh token: %w", err) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if !ok { | ||||||||||
| // Refresh failed (but is retryable), try again later | ||||||||||
| startRefreshTimestamp = time.Now().Add(r.timeBetweenTries) | ||||||||||
| continue | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Refresh succeeded, compute next refresh time | ||||||||||
| startRefreshTimestamp = r.getNextRefreshTimestamp() | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // getNextRefreshTimestamp calculates when the next token refresh should start. | ||||||||||
| // Returns now if token is already expired, otherwise returns expiry time minus safety margin. | ||||||||||
| func (r *continuousTokenRefresher) getNextRefreshTimestamp() time.Time { | ||||||||||
| r.flow.tokenMutex.RLock() | ||||||||||
| expiresAt := r.flow.creds.SessionExpiresAt | ||||||||||
| r.flow.tokenMutex.RUnlock() | ||||||||||
|
|
||||||||||
| // If no expiry time set, check again in 5 minutes | ||||||||||
| if expiresAt.IsZero() { | ||||||||||
| return time.Now().Add(5 * time.Minute) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // If already expired, refresh immediately | ||||||||||
| if time.Now().After(expiresAt) { | ||||||||||
| return time.Now() | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Schedule refresh before expiration (with safety margin) | ||||||||||
| return expiresAt.Add(-r.timeStartBeforeTokenExpiration) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // waitUntilTimestamp blocks until the target timestamp is reached or context is canceled. | ||||||||||
| // Periodically checks if the context has been canceled. | ||||||||||
| func (r *continuousTokenRefresher) waitUntilTimestamp(timestamp time.Time) error { | ||||||||||
| for time.Now().Before(timestamp) { | ||||||||||
| // Check if context was canceled | ||||||||||
| err := r.flow.refreshContext.Err() | ||||||||||
| if err != nil { | ||||||||||
| return fmt.Errorf("context canceled during wait: %w", err) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Sleep briefly before checking again | ||||||||||
| time.Sleep(r.timeBetweenContextCheck) | ||||||||||
| } | ||||||||||
| return nil | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // refreshToken attempts to refresh the access token. | ||||||||||
| // Returns: | ||||||||||
| // - (true, nil) if refresh succeeded | ||||||||||
| // - (false, nil) if refresh failed but should be retried (e.g., network error) | ||||||||||
| // - (false, err) if refresh failed and should not be retried (e.g., invalid refresh token) | ||||||||||
| func (r *continuousTokenRefresher) refreshToken() (bool, error) { | ||||||||||
| // Acquire write lock for refresh | ||||||||||
| r.flow.tokenMutex.Lock() | ||||||||||
| defer r.flow.tokenMutex.Unlock() | ||||||||||
|
|
||||||||||
| // Double-check if refresh is still needed (another goroutine might have refreshed) | ||||||||||
| if !IsTokenExpired(r.flow.creds) { | ||||||||||
| return true, nil | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Attempt refresh | ||||||||||
| err := RefreshTokenWithClient(r.flow.creds, r.flow.httpClient) | ||||||||||
| if err == nil { | ||||||||||
| return true, nil | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Check if error is retryable | ||||||||||
| // Network errors, 5xx errors are retryable | ||||||||||
| // 4xx errors (invalid refresh token) are not retryable | ||||||||||
| errStr := err.Error() | ||||||||||
|
|
||||||||||
| // Non-retryable errors (invalid refresh token, auth errors) | ||||||||||
| if contains(errStr, "status 400") || contains(errStr, "status 401") || | ||||||||||
| contains(errStr, "status 403") || contains(errStr, "refresh token is empty") { | ||||||||||
| return false, fmt.Errorf("token refresh failed (non-retryable): %w", err) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Retryable errors (network issues, 5xx errors) | ||||||||||
| return false, nil | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // contains checks if a string contains a substring | ||||||||||
| func contains(s, substr string) bool { | ||||||||||
| return len(s) >= len(substr) && (s == substr || len(s) > len(substr) && | ||||||||||
| (s[:len(substr)] == substr || s[len(s)-len(substr):] == substr || | ||||||||||
| containsMiddle(s, substr))) | ||||||||||
|
Comment on lines
+156
to
+158
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| } | ||||||||||
|
|
||||||||||
| func containsMiddle(s, substr string) bool { | ||||||||||
| for i := 0; i <= len(s)-len(substr); i++ { | ||||||||||
| if s[i:i+len(substr)] == substr { | ||||||||||
| return true | ||||||||||
| } | ||||||||||
| } | ||||||||||
| return false | ||||||||||
| } | ||||||||||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this one matters, because the default timeBetweenContextCheck is just a second.