Skip to content

Commit e1b69ac

Browse files
committed
fix: credential overrides for credential aliases
Signed-off-by: Grant Linville <[email protected]>
1 parent 02a3cee commit e1b69ac

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

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

Lines changed: 7 additions & 0 deletions
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 credential overrides, 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

pkg/runner/credentials.go

Lines changed: 7 additions & 7 deletions
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

Lines changed: 14 additions & 13 deletions
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)