forked from alibaba/open-code-review
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(config): api_key_cmd / auth_token_cmd — resolve LLM credential from a command (#236) #13
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
chethanuk
wants to merge
3
commits into
main
Choose a base branch
from
feat/api-key-cmd
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
3 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
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,50 @@ | ||
| package llm | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
| "time" | ||
| ) | ||
|
|
||
| // keyCmdTimeout bounds how long an api_key_cmd / auth_token_cmd may run. | ||
| // It is a package var (not const) so tests can shrink it. | ||
| var keyCmdTimeout = 60 * time.Second | ||
|
|
||
| // resolveKeyCmd runs a credential-fetching shell command and returns its | ||
| // trimmed, single-line stdout. label names the source (e.g. | ||
| // `api_key_cmd for provider "x"`) and is used in error messages. | ||
| // | ||
| // The child's stderr is wired to the process stderr so interactive prompts | ||
| // (pinentry, 1Password, `op`) stay visible. Any failure is a hard error, never | ||
| // a silent fallback. The resolved credential is used in memory only and is | ||
| // never written to config or logged. | ||
| func resolveKeyCmd(cmd, label string) (string, error) { | ||
| ctx, cancel := context.WithTimeout(context.Background(), keyCmdTimeout) | ||
| defer cancel() | ||
|
|
||
| c := newKeyCmd(ctx, cmd) | ||
| c.Stderr = os.Stderr | ||
|
|
||
| out, err := c.Output() | ||
| if ctx.Err() == context.DeadlineExceeded { | ||
| return "", fmt.Errorf("%s timed out after %s", label, keyCmdTimeout) | ||
| } | ||
| if err != nil { | ||
| // Covers non-zero exit and command-not-found (the shell exits non-zero | ||
| // and prints its not-found message on the child's stderr). | ||
| return "", fmt.Errorf("%s failed: %w", label, err) | ||
| } | ||
|
|
||
| // Trim a trailing line break; multi-line output past that is ambiguous and refused. | ||
| trimmed := strings.TrimRight(string(out), "\r\n") | ||
| if strings.Contains(trimmed, "\n") { | ||
| return "", fmt.Errorf("%s produced multi-line output; expected a single credential", label) | ||
| } | ||
| key := strings.TrimSpace(trimmed) | ||
| if key == "" { | ||
| return "", fmt.Errorf("%s produced empty output", label) | ||
| } | ||
| return key, nil | ||
| } |
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,73 @@ | ||
| //go:build !windows | ||
|
|
||
| package llm | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestResolveKeyCmd(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| cmd string | ||
| want string | ||
| wantErr string // substring the error must contain; "" means success | ||
| }{ | ||
| {name: "success", cmd: "printf 'sk-test\\n'", want: "sk-test"}, | ||
| {name: "trailing whitespace trimmed", cmd: "printf ' sk-test \\n'", want: "sk-test"}, | ||
| {name: "no trailing newline", cmd: "printf 'sk-test'", want: "sk-test"}, | ||
| {name: "crlf line ending trimmed", cmd: "printf 'sk-crlf\\r\\n'", want: "sk-crlf"}, | ||
| {name: "non-zero exit", cmd: "exit 3", wantErr: "failed: exit status 3"}, | ||
| {name: "false", cmd: "false", wantErr: "failed:"}, | ||
| {name: "empty output", cmd: "true", wantErr: "produced empty output"}, | ||
| {name: "empty printf", cmd: "printf ''", wantErr: "produced empty output"}, | ||
| {name: "whitespace-only output", cmd: "printf ' \\n'", wantErr: "produced empty output"}, | ||
| {name: "multi-line output", cmd: "printf 'a\\nb\\n'", wantErr: "produced multi-line output"}, | ||
| {name: "command not found", cmd: "this-cmd-does-not-exist-xyz", wantErr: "failed:"}, | ||
| } | ||
| for _, tt := range tests { | ||
| tt := tt | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| got, err := resolveKeyCmd(tt.cmd, "api_key_cmd for provider \"x\"") | ||
| if tt.wantErr != "" { | ||
| if err == nil { | ||
| t.Fatalf("expected error containing %q, got nil (output %q)", tt.wantErr, got) | ||
| } | ||
| if !strings.Contains(err.Error(), tt.wantErr) { | ||
| t.Fatalf("error %q does not contain %q", err.Error(), tt.wantErr) | ||
| } | ||
| return | ||
| } | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if got != tt.want { | ||
| t.Fatalf("got %q, want %q", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestResolveKeyCmd_Timeout(t *testing.T) { | ||
| orig := keyCmdTimeout | ||
| keyCmdTimeout = 50 * time.Millisecond | ||
| t.Cleanup(func() { keyCmdTimeout = orig }) | ||
|
|
||
| _, err := resolveKeyCmd("sleep 5", "api_key_cmd for provider \"x\"") | ||
| if err == nil { | ||
| t.Fatal("expected timeout error, got nil") | ||
| } | ||
| if !strings.Contains(err.Error(), "timed out after") { | ||
| t.Fatalf("error %q does not mention timeout", err.Error()) | ||
| } | ||
| } | ||
|
|
||
| func TestResolveKeyCmd_LabelInError(t *testing.T) { | ||
| _, err := resolveKeyCmd("false", `auth_token_cmd for llm config`) | ||
| if err == nil || !strings.HasPrefix(err.Error(), "auth_token_cmd for llm config") { | ||
| t.Fatalf("expected label prefix in error, got %v", err) | ||
| } | ||
| } |
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,14 @@ | ||
| //go:build !windows | ||
|
|
||
| package llm | ||
|
|
||
| import ( | ||
| "context" | ||
| "os/exec" | ||
| ) | ||
|
|
||
| // newKeyCmd builds the OS-specific shell invocation (sh -c on Unix) that runs a | ||
| // credential command under ctx, so its timeout and cancellation are honored. | ||
| func newKeyCmd(ctx context.Context, cmd string) *exec.Cmd { | ||
| return exec.CommandContext(ctx, "sh", "-c", cmd) | ||
| } | ||
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,14 @@ | ||
| //go:build windows | ||
|
|
||
| package llm | ||
|
|
||
| import ( | ||
| "context" | ||
| "os/exec" | ||
| ) | ||
|
|
||
| // newKeyCmd builds the OS-specific shell invocation (cmd /C on Windows) that runs a | ||
| // credential command under ctx, so its timeout and cancellation are honored. | ||
| func newKeyCmd(ctx context.Context, cmd string) *exec.Cmd { | ||
| return exec.CommandContext(ctx, "cmd", "/C", cmd) | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: chethanuk/open-code-review
Length of output: 595
🏁 Script executed:
Repository: chethanuk/open-code-review
Length of output: 4150
Kill the credential command’s process group on Unix
exec.CommandContext(ctx, "sh", "-c", cmd)only stops the shell; the credential command can keep running after cancellation. SetSysProcAttr.Setpgidhere and terminate the whole group on cancel.🤖 Prompt for AI Agents