-
Notifications
You must be signed in to change notification settings - Fork 24
feat: Support workload identity federation flow #4074
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
JorTurFer
wants to merge
14
commits into
stackitcloud:main
Choose a base branch
from
JorTurFer:wif
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
14 commits
Select commit
Hold shift + click to select a range
3c1650b
feat: Remove refresh_token grant type
JorTurFer a552c25
Update changelogs
JorTurFer e561ef1
update exp time of assertion as access token CAN'T be longer that it
JorTurFer fa5f8ef
Update changelogs
JorTurFer ebb1d5a
feat: Support Workload Identity Federation flow
JorTurFer c13c7ad
update changelog
JorTurFer a4ad581
apply feedback
JorTurFer c00e7f0
apply feedback
JorTurFer 5af53b2
apply feedback
JorTurFer e13f147
apply feedback
JorTurFer 84c2f5b
remove docs from PR
JorTurFer 125637a
remove docs from PR
JorTurFer cd47f0e
remove docs from PR
JorTurFer 39961b0
remove docs from PR
JorTurFer 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -234,4 +234,4 @@ See the [release documentation](./RELEASE.md) for further information. | |
|
|
||
| ## License | ||
|
|
||
| Apache 2.0 | ||
| Apache 2.0 | ||
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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| v0.20.1 | ||
| v0.21.0 |
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,84 @@ | ||
| package clients | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "github.com/golang-jwt/jwt/v5" | ||
| "github.com/stackitcloud/stackit-sdk-go/core/oapierror" | ||
| ) | ||
|
|
||
| const ( | ||
| defaultTokenExpirationLeeway = time.Second * 5 | ||
| ) | ||
|
|
||
| type AuthFlow interface { | ||
| RoundTrip(req *http.Request) (*http.Response, error) | ||
| GetAccessToken() (string, error) | ||
| GetBackgroundTokenRefreshContext() context.Context | ||
| } | ||
|
|
||
| // TokenResponseBody is the API response | ||
| // when requesting a new token | ||
| type TokenResponseBody struct { | ||
| AccessToken string `json:"access_token"` | ||
| ExpiresIn int `json:"expires_in"` | ||
| Scope string `json:"scope"` | ||
| TokenType string `json:"token_type"` | ||
| } | ||
|
|
||
| func parseTokenResponse(res *http.Response) (*TokenResponseBody, error) { | ||
| if res == nil { | ||
| return nil, fmt.Errorf("received bad response from API") | ||
| } | ||
| if res.StatusCode != http.StatusOK { | ||
| body, err := io.ReadAll(res.Body) | ||
| if err != nil { | ||
| // Fail silently, omit body from error | ||
| // We're trying to show error details, so it's unnecessary to fail because of this err | ||
| body = []byte{} | ||
| } | ||
| return nil, &oapierror.GenericOpenAPIError{ | ||
| StatusCode: res.StatusCode, | ||
| Body: body, | ||
| } | ||
| } | ||
| body, err := io.ReadAll(res.Body) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| token := &TokenResponseBody{} | ||
| err = json.Unmarshal(body, token) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("unmarshal token response: %w", err) | ||
| } | ||
| return token, nil | ||
| } | ||
|
|
||
| func tokenExpired(token string, tokenExpirationLeeway time.Duration) (bool, error) { | ||
| if token == "" { | ||
| return true, nil | ||
| } | ||
|
|
||
| // We can safely use ParseUnverified because we are not authenticating the user at this point. | ||
| // We're just checking the expiration time | ||
| tokenParsed, _, err := jwt.NewParser().ParseUnverified(token, &jwt.RegisteredClaims{}) | ||
| if err != nil { | ||
| return false, fmt.Errorf("parse token: %w", err) | ||
| } | ||
|
|
||
| expirationTimestampNumeric, err := tokenParsed.Claims.GetExpirationTime() | ||
| if err != nil { | ||
| return false, fmt.Errorf("get expiration timestamp: %w", err) | ||
| } | ||
|
|
||
| // Pretend to be `tokenExpirationLeeway` into the future to avoid token expiring | ||
| // between retrieving the token and upstream systems validating it. | ||
| now := time.Now().Add(tokenExpirationLeeway) | ||
| return now.After(expirationTimestampNumeric.Time), nil | ||
| } |
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.