-
Notifications
You must be signed in to change notification settings - Fork 174
ROX-20100: Add exchange token API #8176
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
becd7d6
rox-20100: exchange m2m token
dhaus67 37c9b0e
Fix tests.
dhaus67 7be0307
Address first review comments.
dhaus67 e321f71
Address review comments.
dhaus67 e1abb6f
Improve claims for non-github ID tokens.
dhaus67 f46bc37
Address review comments.
dhaus67 6c167b7
Address review comments.
dhaus67 0247e0a
Address review comments.
dhaus67 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
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,132 @@ | ||
| package m2m | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/coreos/go-oidc/v3/oidc" | ||
| "github.com/pkg/errors" | ||
| "github.com/stackrox/rox/generated/storage" | ||
| "github.com/stackrox/rox/pkg/auth/tokens" | ||
| "github.com/stackrox/rox/pkg/stringutils" | ||
| "github.com/stackrox/rox/pkg/utils" | ||
| ) | ||
|
|
||
| var ( | ||
| _ claimExtractor = (*githubClaimExtractor)(nil) | ||
| _ claimExtractor = (*genericClaimExtractor)(nil) | ||
| ) | ||
|
|
||
| type claimExtractor interface { | ||
| ExtractRoxClaims(idToken *oidc.IDToken) (tokens.RoxClaims, error) | ||
| } | ||
|
|
||
| func newClaimExtractorFromConfig(config *storage.AuthMachineToMachineConfig) claimExtractor { | ||
| if config.GetType() == storage.AuthMachineToMachineConfig_GENERIC { | ||
| return &genericClaimExtractor{configID: config.GetId()} | ||
| } | ||
|
|
||
| return &githubClaimExtractor{configID: config.GetId()} | ||
| } | ||
|
|
||
| type genericClaimExtractor struct { | ||
| configID string | ||
| } | ||
|
|
||
| func (g *genericClaimExtractor) ExtractRoxClaims(idToken *oidc.IDToken) (tokens.RoxClaims, error) { | ||
| var unstructured map[string]interface{} | ||
| if err := idToken.Claims(&unstructured); err != nil { | ||
| return tokens.RoxClaims{}, errors.Wrap(err, "extracting claims") | ||
| } | ||
|
|
||
| return createRoxClaimsFromGenericClaims(idToken.Subject, unstructured), nil | ||
| } | ||
|
|
||
| func createRoxClaimsFromGenericClaims(subject string, unstructured map[string]interface{}) tokens.RoxClaims { | ||
| stringClaims := mapToStringClaims(unstructured) | ||
|
|
||
| friendlyName := getFriendlyName(stringClaims) | ||
|
|
||
| userID := utils.IfThenElse(friendlyName == "", subject, | ||
| fmt.Sprintf("%s|%s", subject, friendlyName)) | ||
|
|
||
| userClaims := &tokens.ExternalUserClaim{ | ||
| UserID: userID, | ||
| FullName: stringutils.FirstNonEmpty(friendlyName, userID), | ||
| Attributes: mapToStringClaims(unstructured), | ||
| } | ||
|
|
||
| return tokens.RoxClaims{ | ||
| ExternalUser: userClaims, | ||
| Name: userID, | ||
| } | ||
| } | ||
|
|
||
| func getFriendlyName(claims map[string][]string) string { | ||
| // These are some sample claims that typically have the user's name or email. | ||
| userNameClaims := []string{ | ||
| "email", | ||
| "preferred_username", | ||
| "full_name", | ||
| } | ||
|
|
||
| for _, userNameClaim := range userNameClaims { | ||
| if value, ok := claims[userNameClaim]; ok && len(value) == 1 { | ||
| return value[0] | ||
| } | ||
| } | ||
|
|
||
| return "" | ||
| } | ||
|
|
||
| type githubClaimExtractor struct { | ||
| configID string | ||
| } | ||
|
|
||
| // Claims of the ID token issued for github actions. | ||
| // See: https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/about-security-hardening-with-openid-connect#understanding-the-oidc-token | ||
| type githubActionClaims struct { | ||
| Actor string `json:"actor"` | ||
| ActorID string `json:"actor_id"` | ||
| Environment string `json:"environment"` | ||
| EventName string `json:"event_name"` | ||
| GitRef string `json:"ref"` | ||
| Repository string `json:"repository"` | ||
| RepositoryOwner string `json:"repository_owner"` | ||
| } | ||
|
|
||
| func (g *githubClaimExtractor) ExtractRoxClaims(idToken *oidc.IDToken) (tokens.RoxClaims, error) { | ||
| // OIDC tokens issued for GitHub Actions have special claims, we'll reuse them. | ||
| var claims githubActionClaims | ||
| if err := idToken.Claims(&claims); err != nil { | ||
| return tokens.RoxClaims{}, errors.Wrap(err, "extracting GitHub Actions claims") | ||
| } | ||
|
|
||
| return createRoxClaimsFromGitHubClaims(idToken.Subject, idToken.Audience, claims), nil | ||
| } | ||
|
|
||
| func createRoxClaimsFromGitHubClaims(subject string, audiences []string, claims githubActionClaims) tokens.RoxClaims { | ||
| // This is in-line with the user ID we use for other auth providers, where a mix of username + ID wil be used. | ||
| // In general, "|" is used as a separator for auth attributes. | ||
| actorWithID := fmt.Sprintf("%s|%s", claims.ActorID, claims.Actor) | ||
|
|
||
| userClaims := &tokens.ExternalUserClaim{ | ||
| UserID: actorWithID, | ||
| FullName: claims.Actor, | ||
| Attributes: map[string][]string{ | ||
| "actor": {claims.Actor}, | ||
| "actor_id": {claims.ActorID}, | ||
| "repository": {claims.Repository}, | ||
| "repository_owner": {claims.RepositoryOwner}, | ||
| "environment": {claims.Environment}, | ||
| "event_name": {claims.EventName}, | ||
| "ref": {claims.GitRef}, | ||
| "sub": {subject}, | ||
| "aud": audiences, | ||
| }, | ||
| } | ||
|
|
||
| return tokens.RoxClaims{ | ||
| ExternalUser: userClaims, | ||
| Name: actorWithID, | ||
| } | ||
| } |
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.