mirror of
https://github.com/docker/docker-credential-helpers.git
synced 2026-06-13 16:01:28 +05:30
Add a Docker Credentials label support for windows
Signed-off-by: Nassim 'Nass' Eddequiouaq <eddequiouaq.nassim@gmail.com>
This commit is contained in:
@@ -3,6 +3,8 @@ package wincred
|
|||||||
import (
|
import (
|
||||||
winc "github.com/danieljoos/wincred"
|
winc "github.com/danieljoos/wincred"
|
||||||
"github.com/docker/docker-credential-helpers/credentials"
|
"github.com/docker/docker-credential-helpers/credentials"
|
||||||
|
"bytes"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Wincred handles secrets using the Windows credential service.
|
// Wincred handles secrets using the Windows credential service.
|
||||||
@@ -14,6 +16,8 @@ func (h Wincred) Add(creds *credentials.Credentials) error {
|
|||||||
g.UserName = creds.Username
|
g.UserName = creds.Username
|
||||||
g.CredentialBlob = []byte(creds.Secret)
|
g.CredentialBlob = []byte(creds.Secret)
|
||||||
g.Persist = winc.PersistLocalMachine
|
g.Persist = winc.PersistLocalMachine
|
||||||
|
g.Attributes = []winc.CredentialAttribute{winc.CredentialAttribute{"label", []byte(creds.Label)}}
|
||||||
|
|
||||||
return g.Write()
|
return g.Write()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,8 +42,8 @@ func (h Wincred) Get(serverURL string) (string, string, error) {
|
|||||||
return g.UserName, string(g.CredentialBlob), nil
|
return g.UserName, string(g.CredentialBlob), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// List returns the stored URLs and corresponding usernames.
|
// List returns the stored URLs and corresponding usernames for a given credentials label.
|
||||||
func (h Wincred) List() (map[string]string, error) {
|
func (h Wincred) List(credsLabel string) (map[string]string, error) {
|
||||||
creds, err := winc.List()
|
creds, err := winc.List()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -47,7 +51,16 @@ func (h Wincred) List() (map[string]string, error) {
|
|||||||
|
|
||||||
resp := make(map[string]string)
|
resp := make(map[string]string)
|
||||||
for i := range creds {
|
for i := range creds {
|
||||||
resp[creds[i].TargetName] = creds[i].UserName
|
attrs = creds[i].Attributes
|
||||||
|
for _, attr := range attrs {
|
||||||
|
if !strings.Compare(attr.Keyword, "label") &&
|
||||||
|
!bytes.Compare(attr.Value, []byte(credentials.CredsLabel)) {
|
||||||
|
|
||||||
|
resp[creds[i].TargetName] = creds[i].UserName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,11 +8,13 @@ import (
|
|||||||
|
|
||||||
func TestWinCredHelper(t *testing.T) {
|
func TestWinCredHelper(t *testing.T) {
|
||||||
creds := &credentials.Credentials{
|
creds := &credentials.Credentials{
|
||||||
|
Label: credentials.CredsLabel,
|
||||||
ServerURL: "https://foobar.docker.io:2376/v1",
|
ServerURL: "https://foobar.docker.io:2376/v1",
|
||||||
Username: "foobar",
|
Username: "foobar",
|
||||||
Secret: "foobarbaz",
|
Secret: "foobarbaz",
|
||||||
}
|
}
|
||||||
creds1 := &credentials.Credentials{
|
creds1 := &credentials.Credentials{
|
||||||
|
Label: credentials.CredsLabel,
|
||||||
ServerURL: "https://foobar.docker.io:2376/v2",
|
ServerURL: "https://foobar.docker.io:2376/v2",
|
||||||
Username: "foobarbaz",
|
Username: "foobarbaz",
|
||||||
Secret: "foobar",
|
Secret: "foobar",
|
||||||
@@ -36,14 +38,14 @@ func TestWinCredHelper(t *testing.T) {
|
|||||||
t.Fatalf("expected %s, got %s\n", "foobarbaz", secret)
|
t.Fatalf("expected %s, got %s\n", "foobarbaz", secret)
|
||||||
}
|
}
|
||||||
|
|
||||||
auths, err := helper.List()
|
auths, err := helper.List(credentials.CredsLabel)
|
||||||
if err != nil || len(auths) == 0 {
|
if err != nil || len(auths) == 0 {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
helper.Add(creds1)
|
helper.Add(creds1)
|
||||||
defer helper.Delete(creds1.ServerURL)
|
defer helper.Delete(creds1.ServerURL)
|
||||||
newauths, err := helper.List()
|
newauths, err := helper.List(credentials.CredsLabel)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user