-
Notifications
You must be signed in to change notification settings - Fork 4
Add desktop OS secret plugins: keychain, secretservice, wincred #627
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
12 commits
Select commit
Hold shift + click to select a range
240e7d9
Add keychain, secretservice, and wincred secret plugins
winhowes 64e3c21
Fix wincred UTF-16 decoding for non-ASCII secrets
winhowes 677b541
Harden wincred decoding heuristics and edge-case tests
winhowes 6945605
Increase secret plugin test coverage to 100 percent
winhowes 8d31849
Preserve secret bytes and harden wincred generic blob decoding
winhowes e3c1d5e
Make go test -cover reach 100 for new secret plugins
winhowes b497e15
Strip UTF-16 BOM when decoding wincred blobs
winhowes 8516652
Tighten wincred UTF-16 detection for generic blobs
winhowes 302b40b
Replace wincred auto-heuristics with explicit decode modes
winhowes 13c2db7
Add non-terminated UTF-16LE wincred decode regression test
winhowes 6b9b77c
Validate keychain account suffix and stub wincred loader in tests
winhowes 0318d5a
Skip unsupported-path wincred test on Windows
winhowes 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package plugins | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "os/exec" | ||
| "strings" | ||
|
|
||
| "github.com/winhowes/AuthTranslator/app/secrets" | ||
| ) | ||
|
|
||
| // keychainPlugin loads secrets from the macOS Keychain via the security CLI. | ||
| // | ||
| // Expected id formats: | ||
| // - "service" | ||
| // - "service#account" | ||
| type keychainPlugin struct{} | ||
|
|
||
| var execSecurityCommand = func(ctx context.Context, args ...string) ([]byte, error) { | ||
| cmd := exec.CommandContext(ctx, "security", args...) | ||
| return cmd.Output() | ||
| } | ||
|
|
||
| func (keychainPlugin) Prefix() string { return "keychain" } | ||
|
|
||
| func (keychainPlugin) Load(ctx context.Context, id string) (string, error) { | ||
| service, account, err := parseKeychainID(id) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| args := []string{"find-generic-password", "-w", "-s", service} | ||
| if account != "" { | ||
| args = append(args, "-a", account) | ||
| } | ||
|
|
||
| out, err := execSecurityCommand(ctx, args...) | ||
| if err != nil { | ||
| var ee *exec.ExitError | ||
| if errors.As(err, &ee) { | ||
| stderr := strings.TrimSpace(string(ee.Stderr)) | ||
| if stderr != "" { | ||
| return "", fmt.Errorf("keychain lookup failed: %s", stderr) | ||
| } | ||
| } | ||
| return "", fmt.Errorf("keychain lookup failed: %w", err) | ||
| } | ||
|
|
||
| return string(out), nil | ||
| } | ||
|
|
||
| func parseKeychainID(id string) (service, account string, err error) { | ||
| parts := strings.SplitN(id, "#", 2) | ||
| service = strings.TrimSpace(parts[0]) | ||
| if service == "" { | ||
| return "", "", fmt.Errorf("keychain service is required") | ||
| } | ||
| if len(parts) == 2 { | ||
| account = strings.TrimSpace(parts[1]) | ||
| if account == "" { | ||
| return "", "", fmt.Errorf("keychain account is required when using service#account format") | ||
| } | ||
| } | ||
| return service, account, nil | ||
| } | ||
|
|
||
| func init() { secrets.Register(keychainPlugin{}) } | ||
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,163 @@ | ||
| package plugins | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "os/exec" | ||
| "reflect" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestKeychainPluginLoad(t *testing.T) { | ||
| old := execSecurityCommand | ||
| t.Cleanup(func() { execSecurityCommand = old }) | ||
|
|
||
| var gotArgs []string | ||
| execSecurityCommand = func(ctx context.Context, args ...string) ([]byte, error) { | ||
| gotArgs = append([]string{}, args...) | ||
| return []byte("super-secret\n"), nil | ||
| } | ||
|
|
||
| p := keychainPlugin{} | ||
| got, err := p.Load(context.Background(), "slack#bot") | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if got != "super-secret\n" { | ||
| t.Fatalf("expected exact secret bytes, got %q", got) | ||
| } | ||
|
|
||
| wantArgs := []string{"find-generic-password", "-w", "-s", "slack", "-a", "bot"} | ||
| if !reflect.DeepEqual(gotArgs, wantArgs) { | ||
| t.Fatalf("args = %v, want %v", gotArgs, wantArgs) | ||
| } | ||
| } | ||
|
|
||
| func TestKeychainPluginLoadPreservesWhitespace(t *testing.T) { | ||
| old := execSecurityCommand | ||
| t.Cleanup(func() { execSecurityCommand = old }) | ||
|
|
||
| execSecurityCommand = func(ctx context.Context, args ...string) ([]byte, error) { | ||
| return []byte(" secret with spaces \n"), nil | ||
| } | ||
|
|
||
| p := keychainPlugin{} | ||
| got, err := p.Load(context.Background(), "svc") | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if got != " secret with spaces \n" { | ||
| t.Fatalf("expected exact secret bytes, got %q", got) | ||
| } | ||
| } | ||
|
|
||
| func TestKeychainPluginLoadServiceOnly(t *testing.T) { | ||
| old := execSecurityCommand | ||
| t.Cleanup(func() { execSecurityCommand = old }) | ||
|
|
||
| var gotArgs []string | ||
| execSecurityCommand = func(ctx context.Context, args ...string) ([]byte, error) { | ||
| gotArgs = append([]string{}, args...) | ||
| return []byte("token"), nil | ||
| } | ||
|
|
||
| p := keychainPlugin{} | ||
| if _, err := p.Load(context.Background(), "slack"); err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
|
|
||
| wantArgs := []string{"find-generic-password", "-w", "-s", "slack"} | ||
| if !reflect.DeepEqual(gotArgs, wantArgs) { | ||
| t.Fatalf("args = %v, want %v", gotArgs, wantArgs) | ||
| } | ||
| } | ||
|
|
||
| func TestKeychainPluginLoadMissingService(t *testing.T) { | ||
| p := keychainPlugin{} | ||
| if _, err := p.Load(context.Background(), " "); err == nil { | ||
| t.Fatal("expected validation error") | ||
| } | ||
| } | ||
|
|
||
| func TestKeychainPluginLoadExitError(t *testing.T) { | ||
| old := execSecurityCommand | ||
| t.Cleanup(func() { execSecurityCommand = old }) | ||
|
|
||
| execSecurityCommand = func(ctx context.Context, args ...string) ([]byte, error) { | ||
| return nil, &exec.ExitError{Stderr: []byte("item not found")} | ||
| } | ||
|
|
||
| p := keychainPlugin{} | ||
| if _, err := p.Load(context.Background(), "missing"); err == nil { | ||
| t.Fatal("expected lookup error") | ||
| } | ||
| } | ||
|
|
||
| func TestKeychainPluginLoadExitErrorNoStderr(t *testing.T) { | ||
| old := execSecurityCommand | ||
| t.Cleanup(func() { execSecurityCommand = old }) | ||
|
|
||
| execSecurityCommand = func(ctx context.Context, args ...string) ([]byte, error) { | ||
| return nil, &exec.ExitError{} | ||
| } | ||
|
|
||
| p := keychainPlugin{} | ||
| if _, err := p.Load(context.Background(), "missing"); err == nil { | ||
| t.Fatal("expected lookup error") | ||
| } | ||
| } | ||
|
|
||
| func TestKeychainPluginLoadCommandError(t *testing.T) { | ||
| old := execSecurityCommand | ||
| t.Cleanup(func() { execSecurityCommand = old }) | ||
|
|
||
| execSecurityCommand = func(ctx context.Context, args ...string) ([]byte, error) { | ||
| return nil, errors.New("command missing") | ||
| } | ||
|
|
||
| p := keychainPlugin{} | ||
| if _, err := p.Load(context.Background(), "service"); err == nil { | ||
| t.Fatal("expected lookup error") | ||
| } | ||
| } | ||
|
|
||
| func TestExecSecurityCommandDefault(t *testing.T) { | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| cancel() | ||
|
|
||
| if _, err := execSecurityCommand(ctx, "find-generic-password", "-w", "-s", "unused"); err == nil { | ||
| t.Fatal("expected error from canceled context") | ||
| } | ||
| } | ||
|
|
||
| func TestParseKeychainID(t *testing.T) { | ||
| service, account, err := parseKeychainID("svc#acc") | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if service != "svc" || account != "acc" { | ||
| t.Fatalf("unexpected parse result: %q %q", service, account) | ||
| } | ||
|
|
||
| service, account, err = parseKeychainID("svc-only") | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if service != "svc-only" || account != "" { | ||
| t.Fatalf("unexpected parse result: %q %q", service, account) | ||
| } | ||
|
|
||
| service, account, err = parseKeychainID(" svc # acc ") | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if service != "svc" || account != "acc" { | ||
| t.Fatalf("unexpected trimmed parse result: %q %q", service, account) | ||
| } | ||
| } | ||
|
|
||
| func TestParseKeychainIDMissingAccount(t *testing.T) { | ||
| if _, _, err := parseKeychainID("svc#"); err == nil { | ||
| t.Fatal("expected error for empty account") | ||
| } | ||
| } |
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,63 @@ | ||
| package plugins | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os/exec" | ||
| "strings" | ||
|
|
||
| "github.com/winhowes/AuthTranslator/app/secrets" | ||
| ) | ||
|
|
||
| // secretServicePlugin reads secrets from Linux Secret Service using secret-tool. | ||
| // id must be comma-separated key/value pairs, e.g. "service=slack,user=bot". | ||
| type secretServicePlugin struct{} | ||
|
|
||
| var execSecretTool = func(ctx context.Context, args ...string) ([]byte, error) { | ||
| cmd := exec.CommandContext(ctx, "secret-tool", args...) | ||
| return cmd.Output() | ||
| } | ||
|
|
||
| func (secretServicePlugin) Prefix() string { return "secretservice" } | ||
|
|
||
| func (secretServicePlugin) Load(ctx context.Context, id string) (string, error) { | ||
| attrs, err := parseSecretServiceAttrs(id) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| args := []string{"lookup"} | ||
| for _, attr := range attrs { | ||
| args = append(args, attr[0], attr[1]) | ||
| } | ||
|
|
||
| out, err := execSecretTool(ctx, args...) | ||
| if err != nil { | ||
| return "", fmt.Errorf("secretservice lookup failed: %w", err) | ||
| } | ||
| return string(out), nil | ||
| } | ||
|
|
||
| func parseSecretServiceAttrs(id string) ([][2]string, error) { | ||
| id = strings.TrimSpace(id) | ||
| if id == "" { | ||
| return nil, fmt.Errorf("secretservice attributes are required") | ||
| } | ||
| parts := strings.Split(id, ",") | ||
| attrs := make([][2]string, 0, len(parts)) | ||
| for _, part := range parts { | ||
| kv := strings.SplitN(strings.TrimSpace(part), "=", 2) | ||
| if len(kv) != 2 { | ||
| return nil, fmt.Errorf("invalid secretservice attribute %q", part) | ||
| } | ||
| k := strings.TrimSpace(kv[0]) | ||
| v := strings.TrimSpace(kv[1]) | ||
| if k == "" || v == "" { | ||
| return nil, fmt.Errorf("invalid secretservice attribute %q", part) | ||
| } | ||
| attrs = append(attrs, [2]string{k, v}) | ||
| } | ||
| return attrs, nil | ||
| } | ||
|
|
||
| func init() { secrets.Register(secretServicePlugin{}) } |
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.