Skip to content

Commit

Permalink
fix: credentials: check for not found errors properly
Browse files Browse the repository at this point in the history
Signed-off-by: Grant Linville <[email protected]>
  • Loading branch information
g-linville committed Nov 6, 2024
1 parent 2e26a10 commit 9a712eb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
12 changes: 12 additions & 0 deletions pkg/credentials/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package credentials

import (
"strings"
)

func IsCredentialsNotFoundError(err error) bool {
if err == nil {
return false
}
return strings.Contains(err.Error(), "credentials not found in native keychain")
}
3 changes: 1 addition & 2 deletions pkg/credentials/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/docker/cli/cli/config/credentials"
"github.com/docker/cli/cli/config/types"
"github.com/docker/docker-credential-helpers/client"
credentials2 "github.com/docker/docker-credential-helpers/credentials"
"github.com/gptscript-ai/gptscript/pkg/config"
"golang.org/x/exp/maps"
)
Expand Down Expand Up @@ -50,7 +49,7 @@ func (s Store) Get(_ context.Context, toolName string) (*Credential, bool, error
for _, c := range s.credCtxs {
auth, err := store.Get(toolNameWithCtx(toolName, c))
if err != nil {
if credentials2.IsErrCredentialsNotFound(err) {
if IsCredentialsNotFoundError(err) {
continue
}
return nil, false, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/credentials/toolstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (h *toolCredentialStore) Erase(serverAddress string) error {

func (h *toolCredentialStore) Get(serverAddress string) (types.AuthConfig, error) {
creds, err := client.Get(h.program, serverAddress)
if credentials2.IsErrCredentialsNotFound(err) {
if IsCredentialsNotFoundError(err) {
return h.file.Get(serverAddress)
} else if err != nil {
return types.AuthConfig{}, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ func (r *Runner) handleCredentials(callCtx engine.Context, monitor Monitor, env
} else if credentialAlias != "" {
c, exists, err = r.credStore.Get(callCtx.Ctx, credentialAlias)
if err != nil {
return nil, fmt.Errorf("failed to get credentials for tool %s: %w", credentialAlias, err)
return nil, fmt.Errorf("failed to get credential %s: %w", credentialAlias, err)
}
}

Expand Down

0 comments on commit 9a712eb

Please sign in to comment.