Repository navigation
PLAT-1314/fix: preserve keychain lookup and deletion errors #10
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
2 commits
Select commit
Hold shift + click to select a range
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,129 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "net" | ||
| "os/exec" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/zalando/go-keyring" | ||
| ) | ||
|
|
||
| func TestResolvePreservesKeychainFailure(t *testing.T) { | ||
| for _, message := range []string{ | ||
| "could not connect to secret-service", | ||
| "org.freedesktop.secrets returned an error", | ||
| "DBUS_SESSION_BUS_ADDRESS not set", | ||
| "User interaction is not allowed", | ||
| } { | ||
| t.Run(message, func(t *testing.T) { | ||
| withTempHome(t) | ||
| t.Setenv("TWO_API_KEY", "") | ||
| failure := errors.New(message) | ||
| keyring.MockInitWithError(failure) | ||
| t.Cleanup(keyring.MockInit) | ||
| _, err := Resolve("", "sandbox", "") | ||
| if !errors.Is(err, failure) { | ||
| t.Fatalf("lost underlying keychain failure: %v", err) | ||
| } | ||
| if !strings.Contains(err.Error(), "sandbox") { | ||
| t.Fatalf("error omitted the affected context: %v", err) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestKeychainOperationsExplainMissingBackend(t *testing.T) { | ||
| for name, operation := range map[string]func() error{ | ||
| "read": func() error { _, _, err := resolveKey("", "sandbox"); return err }, | ||
| "write": func() error { return StoreKey("sandbox", "test-key") }, | ||
| "delete": func() error { return DeleteKey("sandbox") }, | ||
| } { | ||
| t.Run(name, func(t *testing.T) { | ||
| t.Setenv("TWO_API_KEY", "") | ||
| keyring.MockInitWithError(errors.New("DBUS_SESSION_BUS_ADDRESS not set")) | ||
| t.Cleanup(keyring.MockInit) | ||
| err := operation() | ||
| if err == nil || !strings.Contains(err.Error(), "libsecret") || !strings.Contains(err.Error(), "TWO_API_KEY") { | ||
| t.Fatalf("expected backend guidance, got %v", err) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestExplicitKeysBypassUnavailableKeychain(t *testing.T) { | ||
| for _, useFlag := range []bool{false, true} { | ||
| withTempHome(t) | ||
| t.Setenv("TWO_API_KEY", "env-key") | ||
| keyring.MockInitWithError(errors.New("DBUS_SESSION_BUS_ADDRESS not set")) | ||
| t.Cleanup(keyring.MockInit) | ||
| flag, want, source := "", "env-key", "$TWO_API_KEY" | ||
| if useFlag { | ||
| flag, want, source = "flag-key", "flag-key", "--api-key flag" | ||
| } | ||
| resolved, err := Resolve(flag, "sandbox", "") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if resolved.APIKey != want || resolved.Source != source { | ||
| t.Fatalf("wrong explicit key/source: %+v", resolved) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestMissingKeyStillReportsNoAPIKey(t *testing.T) { | ||
| withTempHome(t) | ||
| t.Setenv("TWO_API_KEY", "") | ||
| keyring.MockInitWithError(keyring.ErrNotFound) | ||
| t.Cleanup(keyring.MockInit) | ||
| _, err := Resolve("", "sandbox", "") | ||
| if !errors.Is(err, ErrNoAPIKey) { | ||
| t.Fatalf("missing key should retain login guidance: %v", err) | ||
| } | ||
| if err := DeleteKey("sandbox"); err != nil { | ||
| t.Fatalf("deleting an absent key should remain a no-op: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestKeychainOperationsExplainDBusConnectionFailures(t *testing.T) { | ||
| failures := []error{ | ||
| &exec.Error{Name: "dbus-launch", Err: exec.ErrNotFound}, | ||
| &net.OpError{Op: "dial", Net: "unix", Err: errors.New("connect: no such file or directory")}, | ||
| &net.OpError{Op: "dial", Net: "tcp", Err: errors.New("connect: connection refused")}, | ||
| } | ||
| for _, failure := range failures { | ||
| t.Run(failure.Error(), func(t *testing.T) { | ||
| t.Setenv("TWO_API_KEY", "") | ||
| keyring.MockInitWithError(fmt.Errorf("backend: %w", failure)) | ||
| t.Cleanup(keyring.MockInit) | ||
| for name, operation := range map[string]func() error{ | ||
| "read": func() error { _, _, err := resolveKey("", "sandbox"); return err }, | ||
| "write": func() error { return StoreKey("sandbox", "test-key") }, | ||
| "delete": func() error { return DeleteKey("sandbox") }, | ||
| } { | ||
| t.Run(name, func(t *testing.T) { | ||
| err := operation() | ||
| if !errors.Is(err, failure) { | ||
| t.Fatalf("lost backend cause: %v", err) | ||
| } | ||
| if !strings.Contains(err.Error(), "libsecret") || !strings.Contains(err.Error(), "TWO_API_KEY") { | ||
| t.Fatalf("missing backend guidance: %v", err) | ||
| } | ||
| }) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestUnrelatedKeyringErrorsRemainUnchanged(t *testing.T) { | ||
| for _, failure := range []error{ | ||
| errors.New("User interaction is not allowed"), | ||
| &net.OpError{Op: "read", Net: "unix", Err: errors.New("permission denied")}, | ||
| } { | ||
| if got := wrapKeyringErr(failure); got != failure { | ||
| t.Fatalf("unrelated error was reclassified: %v", got) | ||
| } | ||
| } | ||
| } |
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.