Skip to content

Commit 64d0748

Browse files
authored
fix: credential overrides for credential aliases (#511)
Signed-off-by: Grant Linville <[email protected]>
1 parent 02a3cee commit 64d0748

File tree

7 files changed

+57
-23
lines changed

7 files changed

+57
-23
lines changed

docs/docs/03-tools/04-credential-tools.md

+7
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ need to be aware of which environment variables the credential tool sets. You ca
159159

160160
### Format
161161

162+
:::info
163+
In the examples that follow, `toolA`, `toolB`, etc. are the names of credentials.
164+
By default, a credential has the same name as the tool that created it.
165+
This can be overridden with a credential alias, i.e. `credential: my-cred-tool.gpt as myAlias`.
166+
If the credential has an alias, use it instead of the tool name when you specify an override.
167+
:::
168+
162169
The `--credential-override` argument must be formatted in one of the following three ways:
163170

164171
#### 1. Key-Value Pairs

docs/docs/04-command-line-reference/gptscript.md

-2
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,13 @@ gptscript [flags] PROGRAM_FILE [INPUT...]
3333
-f, --input string Read input from a file ("-" for stdin) ($GPTSCRIPT_INPUT)
3434
--list-models List the models available and exit ($GPTSCRIPT_LIST_MODELS)
3535
--list-tools List built-in tools and exit ($GPTSCRIPT_LIST_TOOLS)
36-
--listen-address string Server listen address ($GPTSCRIPT_LISTEN_ADDRESS) (default "127.0.0.1:0")
3736
--no-trunc Do not truncate long log messages ($GPTSCRIPT_NO_TRUNC)
3837
--openai-api-key string OpenAI API KEY ($OPENAI_API_KEY)
3938
--openai-base-url string OpenAI base URL ($OPENAI_BASE_URL)
4039
--openai-org-id string OpenAI organization ID ($OPENAI_ORG_ID)
4140
-o, --output string Save output to a file, or - for stdout ($GPTSCRIPT_OUTPUT)
4241
-q, --quiet No output logging (set --quiet=false to force on even when there is no TTY) ($GPTSCRIPT_QUIET)
4342
--save-chat-state-file string A file to save the chat state to so that a conversation can be resumed with --chat-state ($GPTSCRIPT_SAVE_CHAT_STATE_FILE)
44-
--server Start server ($GPTSCRIPT_SERVER)
4543
--sub-tool string Use tool of this name, not the first tool in file ($GPTSCRIPT_SUB_TOOL)
4644
--ui Launch the UI ($GPTSCRIPT_UI)
4745
--workspace string Directory to use for the workspace, if specified it will not be deleted on exit ($GPTSCRIPT_WORKSPACE)

docs/docs/04-command-line-reference/gptscript_credential.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ gptscript credential [flags]
2727

2828
* [gptscript](gptscript.md) -
2929
* [gptscript credential delete](gptscript_credential_delete.md) - Delete a stored credential
30+
* [gptscript credential show](gptscript_credential_show.md) - Show the secret value of a stored credential
3031

docs/docs/04-command-line-reference/gptscript_credential_delete.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ title: "gptscript credential delete"
66
Delete a stored credential
77

88
```
9-
gptscript credential delete <tool name> [flags]
9+
gptscript credential delete <credential name> [flags]
1010
```
1111

1212
### Options
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
title: "gptscript credential show"
3+
---
4+
## gptscript credential show
5+
6+
Show the secret value of a stored credential
7+
8+
```
9+
gptscript credential show <credential name> [flags]
10+
```
11+
12+
### Options
13+
14+
```
15+
-h, --help help for show
16+
```
17+
18+
### Options inherited from parent commands
19+
20+
```
21+
--credential-context string Context name in which to store credentials ($GPTSCRIPT_CREDENTIAL_CONTEXT) (default "default")
22+
```
23+
24+
### SEE ALSO
25+
26+
* [gptscript credential](gptscript_credential.md) - List stored credentials
27+

pkg/runner/credentials.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ import (
88

99
// parseCredentialOverrides parses a string of credential overrides that the user provided as a command line arg.
1010
// The format of credential overrides can be one of three things:
11-
// tool1:ENV1,ENV2;tool2:ENV1,ENV2 (direct mapping of environment variables)
12-
// tool1:ENV1=VALUE1,ENV2=VALUE2;tool2:ENV1=VALUE1,ENV2=VALUE2 (key-value pairs)
13-
// tool1:ENV1->OTHER_ENV1,ENV2->OTHER_ENV2;tool2:ENV1->OTHER_ENV1,ENV2->OTHER_ENV2 (mapping to other environment variables)
11+
// cred1:ENV1,ENV2;cred2:ENV1,ENV2 (direct mapping of environment variables)
12+
// cred1:ENV1=VALUE1,ENV2=VALUE2;cred2:ENV1=VALUE1,ENV2=VALUE2 (key-value pairs)
13+
// cred1:ENV1->OTHER_ENV1,ENV2->OTHER_ENV2;cred2:ENV1->OTHER_ENV1,ENV2->OTHER_ENV2 (mapping to other environment variables)
1414
//
1515
// This function turns it into a map[string]map[string]string like this:
1616
//
1717
// {
18-
// "tool1": {
18+
// "cred1": {
1919
// "ENV1": "VALUE1",
2020
// "ENV2": "VALUE2",
2121
// },
22-
// "tool2": {
22+
// "cred2": {
2323
// "ENV1": "VALUE1",
2424
// "ENV2": "VALUE2",
2525
// },
@@ -28,7 +28,7 @@ func parseCredentialOverrides(override string) (map[string]map[string]string, er
2828
credentialOverrides := make(map[string]map[string]string)
2929

3030
for _, o := range strings.Split(override, ";") {
31-
toolName, envs, found := strings.Cut(o, ":")
31+
credName, envs, found := strings.Cut(o, ":")
3232
if !found {
3333
return nil, fmt.Errorf("invalid credential override: %s", o)
3434
}
@@ -48,7 +48,7 @@ func parseCredentialOverrides(override string) (map[string]map[string]string, er
4848
}
4949
envMap[key] = value
5050
}
51-
credentialOverrides[toolName] = envMap
51+
credentialOverrides[credName] = envMap
5252
}
5353

5454
return credentialOverrides, nil

pkg/runner/runner.go

+14-13
Original file line numberDiff line numberDiff line change
@@ -813,19 +813,24 @@ func (r *Runner) handleCredentials(callCtx engine.Context, monitor Monitor, env
813813
}
814814

815815
for _, credToolName := range callCtx.Tool.Credentials {
816+
toolName, credentialAlias, args, err := types.ParseCredentialArgs(credToolName, callCtx.Input)
817+
if err != nil {
818+
return nil, fmt.Errorf("failed to parse credential tool %q: %w", credToolName, err)
819+
}
820+
821+
credName := toolName
822+
if credentialAlias != "" {
823+
credName = credentialAlias
824+
}
825+
816826
// Check whether the credential was overridden before we attempt to find it in the store or run the tool.
817-
if override, exists := credOverrides[credToolName]; exists {
827+
if override, exists := credOverrides[credName]; exists {
818828
for k, v := range override {
819829
env = append(env, fmt.Sprintf("%s=%s", k, v))
820830
}
821831
continue
822832
}
823833

824-
toolName, credentialAlias, args, err := types.ParseCredentialArgs(credToolName, callCtx.Input)
825-
if err != nil {
826-
return nil, fmt.Errorf("failed to parse credential tool %q: %w", credToolName, err)
827-
}
828-
829834
var (
830835
cred *credentials.Credential
831836
exists bool
@@ -884,13 +889,9 @@ func (r *Runner) handleCredentials(callCtx engine.Context, monitor Monitor, env
884889
}
885890

886891
cred = &credentials.Credential{
887-
Type: credentials.CredentialTypeTool,
888-
Env: envMap.Env,
889-
}
890-
if credentialAlias != "" {
891-
cred.ToolName = credentialAlias
892-
} else {
893-
cred.ToolName = toolName
892+
Type: credentials.CredentialTypeTool,
893+
Env: envMap.Env,
894+
ToolName: credName,
894895
}
895896

896897
isEmpty := true

0 commit comments

Comments
 (0)