1
0
mirror of https://github.com/docker/docker-credential-helpers.git synced 2026-06-16 01:12:47 +05:30

Merge pull request #321 from thaJeztah/fix_pass_errors

pass: return correct error, and ignore empty stores on list
This commit is contained in:
Sebastiaan van Stijn
2024-05-09 16:44:30 +02:00
committed by GitHub
2 changed files with 74 additions and 2 deletions
+2 -2
View File
@@ -158,7 +158,7 @@ func (p Pass) Get(serverURL string) (string, string, error) {
} }
if len(usernames) < 1 { if len(usernames) < 1 {
return "", "", fmt.Errorf("no usernames for %s", serverURL) return "", "", credentials.NewErrCredentialsNotFound()
} }
actual := strings.TrimSuffix(usernames[0].Name(), ".gpg") actual := strings.TrimSuffix(usernames[0].Name(), ".gpg")
@@ -191,7 +191,7 @@ func (p Pass) List() (map[string]string, error) {
} }
if len(usernames) < 1 { if len(usernames) < 1 {
return nil, fmt.Errorf("no usernames for %s", serverURL) continue
} }
resp[string(serverURL)] = strings.TrimSuffix(usernames[0].Name(), ".gpg") resp[string(serverURL)] = strings.TrimSuffix(usernames[0].Name(), ".gpg")
+72
View File
@@ -3,6 +3,9 @@
package pass package pass
import ( import (
"encoding/base64"
"os"
"path"
"strings" "strings"
"testing" "testing"
@@ -116,6 +119,75 @@ func TestPassHelperList(t *testing.T) {
} }
} }
// TestPassHelperWithEmptyServer verifies that empty directories (servers
// without credentials) are ignored, but still returns credentials for other
// servers.
func TestPassHelperWithEmptyServer(t *testing.T) {
helper := Pass{}
if err := helper.checkInitialized(); err != nil {
t.Error(err)
}
creds := []*credentials.Credentials{
{
ServerURL: "https://myreqistry.example.com:2375/v1",
Username: "foo",
Secret: "isthebestmeshuggahalbum",
},
{
ServerURL: "https://index.example.com/v1//access-token",
},
}
t.Cleanup(func() {
for _, cred := range creds {
_ = helper.Delete(cred.ServerURL)
}
})
for _, cred := range creds {
if cred.Username != "" {
if err := helper.Add(cred); err != nil {
t.Error(err)
}
} else {
// No credentials; create an empty directory for this server.
serverURL := base64.URLEncoding.EncodeToString([]byte(cred.ServerURL))
p := path.Join(getPassDir(), PASS_FOLDER, serverURL)
if err := os.Mkdir(p, 0o755); err != nil {
t.Error(err)
}
}
}
credsList, err := helper.List()
if err != nil {
t.Error(err)
}
if len(credsList) == 0 {
t.Error("expected credentials to be returned, but got none")
}
for _, cred := range creds {
if cred.Username != "" {
userName, secret, err := helper.Get(cred.ServerURL)
if err != nil {
t.Error(err)
}
if userName != cred.Username {
t.Errorf("expected username %q, actual: %q", cred.Username, userName)
}
if secret != cred.Secret {
t.Errorf("expected secret %q, actual: %q", cred.Secret, secret)
}
} else {
_, _, err := helper.Get(cred.ServerURL)
if !credentials.IsErrCredentialsNotFound(err) {
t.Errorf("expected credentials not found, actual: %v", err)
}
}
}
}
func TestMissingCred(t *testing.T) { func TestMissingCred(t *testing.T) {
helper := Pass{} helper := Pass{}
if _, _, err := helper.Get("garbage"); !credentials.IsErrCredentialsNotFound(err) { if _, _, err := helper.Get("garbage"); !credentials.IsErrCredentialsNotFound(err) {