Repository navigation
Authenticate with workload identity federation #330
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
+1,662
−20
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4b21524
Authenticate with workload identity federation
rgarcia 6d8b4e7
Tighten the workload identity token cache
rgarcia 61d3a4d
Send a request that must not be replayed once
rgarcia 19d9d8a
Share one credential order between commands and kernel auth
rgarcia b359474
Work out the token cache once and stop resending after a rejected token
rgarcia 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,98 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/kernel/cli/pkg/auth" | ||
| "github.com/pterm/pterm" | ||
| "github.com/zalando/go-keyring" | ||
| ) | ||
|
|
||
| func TestAuthReportsTheSourceCommandsUse(t *testing.T) { | ||
| for _, tc := range []struct { | ||
| name string | ||
| apiKey, rule, login bool | ||
| env map[string]string | ||
| want []string | ||
| notWant string | ||
| }{ | ||
| {name: "API key first", apiKey: true, rule: true, login: true, | ||
| want: []string{"Authentication method: API Key"}, notWant: "Workload identity"}, | ||
| {name: "workload identity before a saved login", rule: true, login: true, | ||
| want: []string{ | ||
| "Authentication method: Workload identity federation", | ||
| "Federation rule: wifr_status", | ||
| "Cached token: not in use; the next command exchanges a new one", | ||
| }, notWant: "OAuth"}, | ||
| {name: "workload identity with the cache off", rule: true, | ||
| env: map[string]string{auth.EnvFederationTokenCache: "off"}, | ||
| want: []string{"Cached token: not in use (turned off by KERNEL_FEDERATION_TOKEN_CACHE); every command exchanges a new one"}}, | ||
| {name: "workload identity without an identity token source", rule: true, login: true, | ||
| env: map[string]string{"ACTIONS_ID_TOKEN_REQUEST_URL": ""}, | ||
| want: []string{"Federation rule: wifr_status", "no identity token source could be configured"}, notWant: "OAuth"}, | ||
| {name: "saved login", login: true, | ||
| want: []string{"Authenticated with OAuth"}, notWant: "Workload identity"}, | ||
| {name: "nothing", want: []string{"not authenticated"}}, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| home := t.TempDir() | ||
| keyring.MockInit() | ||
| env := map[string]string{ | ||
| "HOME": home, "USERPROFILE": home, | ||
| "KERNEL_API_KEY": "", "KERNEL_FEDERATION_RULE_ID": "", | ||
| "KERNEL_IDENTITY_TOKEN_SOURCE": "", "KERNEL_IDENTITY_TOKEN_FILE": "", "KERNEL_IDENTITY_TOKEN": "", | ||
| auth.EnvFederationTokenCache: "", | ||
| "GITHUB_ACTIONS": "true", "RUNNER_TEMP": t.TempDir(), | ||
| "ACTIONS_ID_TOKEN_REQUEST_URL": "http://127.0.0.1:1/idtoken", "ACTIONS_ID_TOKEN_REQUEST_TOKEN": "request-credential", | ||
| } | ||
| if tc.apiKey { | ||
| env["KERNEL_API_KEY"] = "sk_test_0123456789" | ||
| } | ||
| if tc.rule { | ||
| env["KERNEL_FEDERATION_RULE_ID"] = "wifr_status" | ||
| } | ||
| for name, value := range tc.env { | ||
| env[name] = value | ||
| } | ||
| for name, value := range env { | ||
| t.Setenv(name, value) | ||
| } | ||
| if tc.login { | ||
| if err := auth.SaveTokens(&auth.TokenStorage{AccessToken: "login-token", ExpiresAt: time.Now().Add(48 * time.Hour)}); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
| previous := logger | ||
| logger = pterm.DefaultLogger.WithLevel(pterm.LogLevelWarn) | ||
| t.Cleanup(func() { logger = previous }) | ||
| output := capturePtermOutput(t) | ||
|
|
||
| if err := runAuth(authCmd, nil); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| for _, want := range tc.want { | ||
| if !strings.Contains(output.String(), want) { | ||
| t.Errorf("output does not contain %q:\n%s", want, output.String()) | ||
| } | ||
| } | ||
| if tc.notWant != "" && strings.Contains(output.String(), tc.notWant) { | ||
| t.Errorf("output contains %q:\n%s", tc.notWant, output.String()) | ||
| } | ||
| for _, secret := range []string{"login-token", "request-credential", "sk_test_0123456789"} { | ||
| if strings.Contains(output.String(), secret) { | ||
| t.Errorf("output contains the secret %q:\n%s", secret, output.String()) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestAuthReportsACachedWorkloadIdentityToken(t *testing.T) { | ||
| output := capturePtermOutput(t) | ||
| printFederationStatus(&auth.FederationStatus{RuleID: "wifr_status", CachedTokenExpiresAt: time.Now().Add(3 * time.Minute)}) | ||
| if !strings.Contains(output.String(), "Cached token: in use, expires in 3m0s") { | ||
| t.Fatalf("output = %s, want the cached token reported in use", output.String()) | ||
| } | ||
| } |
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
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.