Skip to content
This repository has been archived by the owner on Apr 7, 2024. It is now read-only.

Commit

Permalink
fix: added back handling no credentials error (#74)
Browse files Browse the repository at this point in the history
Fixing the error of PR #68

---------

Signed-off-by: wangxiaoxuan273 <wangxiaoxuan119@gmail.com>
  • Loading branch information
wangxiaoxuan273 authored Jun 8, 2023
1 parent ab5d0bd commit 323e44c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
9 changes: 7 additions & 2 deletions native_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ import (
)

const (
remoteCredentialsPrefix = "docker-credential-"
emptyUsername = "<token>"
remoteCredentialsPrefix = "docker-credential-"
emptyUsername = "<token>"
errCredentialsNotFoundMessage = "credentials not found in native keychain"
)

// dockerCredentials mimics how docker credential helper binaries store
Expand Down Expand Up @@ -67,6 +68,10 @@ func (ns *nativeStore) Get(ctx context.Context, serverAddress string) (auth.Cred
var cred auth.Credential
out, err := ns.exec.Execute(ctx, strings.NewReader(serverAddress), "get")
if err != nil {
if err.Error() == errCredentialsNotFoundMessage {
// do not return an error if the credentials are not in the keychain.
return auth.EmptyCredential, nil
}
return auth.EmptyCredential, err
}
var dockerCred dockerCredentials
Expand Down
35 changes: 22 additions & 13 deletions native_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,29 @@ import (
)

const (
basicAuthHost = "localhost:2333"
bearerAuthHost = "localhost:6666"
exeErrorHost = "localhost:500/exeError"
jsonErrorHost = "localhost:500/jsonError"
testUsername = "test_username"
testPassword = "test_password"
testRefreshToken = "test_token"
basicAuthHost = "localhost:2333"
bearerAuthHost = "localhost:6666"
exeErrorHost = "localhost:500/exeError"
jsonErrorHost = "localhost:500/jsonError"
noCredentialsHost = "localhost:404"
testUsername = "test_username"
testPassword = "test_password"
testRefreshToken = "test_token"
)

var (
errCommandExited = fmt.Errorf("exited with error")
exeErr = fmt.Errorf("Execute failed")
errCommandExited = fmt.Errorf("exited with error")
errExecute = fmt.Errorf("Execute failed")
errCredentialsNotFound = fmt.Errorf(errCredentialsNotFoundMessage)
)

// testExecuter implements the Executer interface for testing purpose.
// It simulates interactions between the docker client and a remote
// credentials helper.
type testExecuter struct{}

// Execute returns responses from the remote credentials helper.
// It mocks those responses based in the input in the mock.
// Execute mocks the behavior of a credential helper binary. It returns responses
// and errors based on the input.
func (e *testExecuter) Execute(ctx context.Context, input io.Reader, action string) ([]byte, error) {
in, err := io.ReadAll(input)
if err != nil {
Expand All @@ -62,9 +64,11 @@ func (e *testExecuter) Execute(ctx context.Context, input io.Reader, action stri
case bearerAuthHost:
return []byte(`{"Username": "<token>", "Secret": "test_token"}`), nil
case exeErrorHost:
return []byte("Execute failed"), exeErr
return []byte("Execute failed"), errExecute
case jsonErrorHost:
return []byte("json.Unmarshal failed"), nil
case noCredentialsHost:
return []byte("credentials not found"), errCredentialsNotFound
default:
return []byte("program failed"), errCommandExited
}
Expand Down Expand Up @@ -158,12 +162,17 @@ func TestNativeStore_errorHandling(t *testing.T) {
}
// Get Error: Execute error
_, err := ns.Get(context.Background(), exeErrorHost)
if err != exeErr {
if err != errExecute {
t.Fatalf("got error: %v, should get exeErr", err)
}
// Get Error: json.Unmarshal
_, err = ns.Get(context.Background(), jsonErrorHost)
if err == nil {
t.Fatalf("should get error from json.Unmarshal")
}
// Get: Should not return error when credentials are not found
_, err = ns.Get(context.Background(), noCredentialsHost)
if err != nil {
t.Fatalf("should not get error when no credentials are found")
}
}

0 comments on commit 323e44c

Please sign in to comment.