mirror of
https://github.com/docker/docker-credential-helpers.git
synced 2026-06-28 07:11:36 +05:30
Compare commits
43 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0e7779e5a1 | |||
| fb9549d396 | |||
| ed11c58ebf | |||
| 79ab7059b0 | |||
| 18d35e4984 | |||
| 71779cf7f5 | |||
| 51c78cdc14 | |||
| 120e37f15d | |||
| 4962f775bf | |||
| ce617b3357 | |||
| f3071aff0a | |||
| 1515d4547e | |||
| f67589c36e | |||
| 479de2a4f5 | |||
| cdba2ced06 | |||
| 7f0538cd5e | |||
| 14381bf0d6 | |||
| 7133af577e | |||
| 2f2e85cfb9 | |||
| 47566329ff | |||
| b9d19b479a | |||
| e522e56699 | |||
| 8cb3338668 | |||
| cd76e4253f | |||
| 021d7d6a19 | |||
| 2a8670e0da | |||
| c5fbd3a5ad | |||
| c6cf8aa13b | |||
| cfe7556d6d | |||
| 23a1f310a5 | |||
| f7f2744e6d | |||
| 406812bf8e | |||
| 595b7f2531 | |||
| ad4463616e | |||
| 365da011fb | |||
| 1057cf7f86 | |||
| 40d06d0090 | |||
| de50f50ab0 | |||
| 3c3e1d3af1 | |||
| 19ec1c3164 | |||
| 2a3f7a4468 | |||
| b7c53e02cd | |||
| 94963d0da8 |
@@ -1 +1,2 @@
|
|||||||
bin
|
bin
|
||||||
|
release
|
||||||
|
|||||||
@@ -25,6 +25,7 @@
|
|||||||
"lk4d4",
|
"lk4d4",
|
||||||
"mavenugo",
|
"mavenugo",
|
||||||
"mhbauer",
|
"mhbauer",
|
||||||
|
"n4ss",
|
||||||
"runcom",
|
"runcom",
|
||||||
"stevvooe",
|
"stevvooe",
|
||||||
"thajeztah",
|
"thajeztah",
|
||||||
@@ -114,6 +115,11 @@
|
|||||||
Email = "mbauer@us.ibm.com"
|
Email = "mbauer@us.ibm.com"
|
||||||
GitHub = "mhbauer"
|
GitHub = "mhbauer"
|
||||||
|
|
||||||
|
[people.n4ss]
|
||||||
|
Name = "Nassim Eddequiouaq"
|
||||||
|
Email = "eddequiouaq.nassim@gmail.com"
|
||||||
|
GitHub = "n4ss"
|
||||||
|
|
||||||
[people.runcom]
|
[people.runcom]
|
||||||
Name = "Antonio Murdaca"
|
Name = "Antonio Murdaca"
|
||||||
Email = "runcom@redhat.com"
|
Email = "runcom@redhat.com"
|
||||||
|
|||||||
@@ -1,23 +1,37 @@
|
|||||||
.PHONY: all deps osxkeychain secretservice test validate wincred
|
.PHONY: all deps osxkeychain secretservice test validate wincred
|
||||||
|
|
||||||
TRAVIS_OS_NAME ?= linux
|
TRAVIS_OS_NAME ?= linux
|
||||||
|
VERSION = 0.5.1
|
||||||
|
|
||||||
all: test
|
all: test
|
||||||
|
|
||||||
deps:
|
deps:
|
||||||
go get github.com/golang/lint/golint
|
go get github.com/golang/lint/golint
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf bin
|
||||||
|
rm -rf release
|
||||||
|
|
||||||
osxkeychain:
|
osxkeychain:
|
||||||
mkdir -p bin
|
mkdir bin
|
||||||
go build -o bin/docker-credential-osxkeychain osxkeychain/cmd/main_darwin.go
|
go build -ldflags -s -o bin/docker-credential-osxkeychain osxkeychain/cmd/main_darwin.go
|
||||||
|
|
||||||
|
codesign: osxkeychain
|
||||||
|
$(eval SIGNINGHASH = $(shell security find-identity -v -p codesigning | grep "Developer ID Application: Docker Inc" | cut -d ' ' -f 4))
|
||||||
|
xcrun -log codesign -s $(SIGNINGHASH) --force --verbose bin/docker-credential-osxkeychain
|
||||||
|
xcrun codesign --verify --deep --strict --verbose=2 --display bin/docker-credential-osxkeychain
|
||||||
|
|
||||||
|
osxrelease: clean test codesign
|
||||||
|
mkdir -p release
|
||||||
|
cd bin && tar cvfz ../release/docker-credential-osxkeychain-v$(VERSION)-amd64.tar.gz docker-credential-osxkeychain
|
||||||
|
|
||||||
secretservice:
|
secretservice:
|
||||||
mkdir -p bin
|
mkdir bin
|
||||||
go build -o bin/docker-credential-secretservice secretservice/cmd/main_linux.go
|
go build -o bin/docker-credential-secretservice secretservice/cmd/main_linux.go
|
||||||
|
|
||||||
wincred:
|
wincred:
|
||||||
mkdir -p bin
|
mkdir bin
|
||||||
go build -o bin/docker-credential-wincred wincred/cmd/main_windows.go
|
go build -o bin/docker-credential-wincred.exe wincred/cmd/main_windows.go
|
||||||
|
|
||||||
test:
|
test:
|
||||||
# tests all packages except vendor
|
# tests all packages except vendor
|
||||||
|
|||||||
+36
-2
@@ -9,12 +9,27 @@ import (
|
|||||||
"github.com/docker/docker-credential-helpers/credentials"
|
"github.com/docker/docker-credential-helpers/credentials"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// isValidCredsMessage checks if 'msg' contains invalid credentials error message.
|
||||||
|
// It returns whether the logs are free of invalid credentials errors and the error if it isn't.
|
||||||
|
// error values can be errCredentialsMissingServerURL or errCredentialsMissingUsername.
|
||||||
|
func isValidCredsMessage(msg string) error {
|
||||||
|
if credentials.IsCredentialsMissingServerURLMessage(msg) {
|
||||||
|
return credentials.NewErrCredentialsMissingServerURL()
|
||||||
|
}
|
||||||
|
|
||||||
|
if credentials.IsCredentialsMissingUsernameMessage(msg) {
|
||||||
|
return credentials.NewErrCredentialsMissingUsername()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Store uses an external program to save credentials.
|
// Store uses an external program to save credentials.
|
||||||
func Store(program ProgramFunc, credentials *credentials.Credentials) error {
|
func Store(program ProgramFunc, creds *credentials.Credentials) error {
|
||||||
cmd := program("store")
|
cmd := program("store")
|
||||||
|
|
||||||
buffer := new(bytes.Buffer)
|
buffer := new(bytes.Buffer)
|
||||||
if err := json.NewEncoder(buffer).Encode(credentials); err != nil {
|
if err := json.NewEncoder(buffer).Encode(creds); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
cmd.Input(buffer)
|
cmd.Input(buffer)
|
||||||
@@ -22,6 +37,11 @@ func Store(program ProgramFunc, credentials *credentials.Credentials) error {
|
|||||||
out, err := cmd.Output()
|
out, err := cmd.Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t := strings.TrimSpace(string(out))
|
t := strings.TrimSpace(string(out))
|
||||||
|
|
||||||
|
if isValidErr := isValidCredsMessage(t); isValidErr != nil {
|
||||||
|
err = isValidErr
|
||||||
|
}
|
||||||
|
|
||||||
return fmt.Errorf("error storing credentials - err: %v, out: `%s`", err, t)
|
return fmt.Errorf("error storing credentials - err: %v, out: `%s`", err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,6 +61,10 @@ func Get(program ProgramFunc, serverURL string) (*credentials.Credentials, error
|
|||||||
return nil, credentials.NewErrCredentialsNotFound()
|
return nil, credentials.NewErrCredentialsNotFound()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if isValidErr := isValidCredsMessage(t); isValidErr != nil {
|
||||||
|
err = isValidErr
|
||||||
|
}
|
||||||
|
|
||||||
return nil, fmt.Errorf("error getting credentials - err: %v, out: `%s`", err, t)
|
return nil, fmt.Errorf("error getting credentials - err: %v, out: `%s`", err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,6 +86,11 @@ func Erase(program ProgramFunc, serverURL string) error {
|
|||||||
out, err := cmd.Output()
|
out, err := cmd.Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t := strings.TrimSpace(string(out))
|
t := strings.TrimSpace(string(out))
|
||||||
|
|
||||||
|
if isValidErr := isValidCredsMessage(t); isValidErr != nil {
|
||||||
|
err = isValidErr
|
||||||
|
}
|
||||||
|
|
||||||
return fmt.Errorf("error erasing credentials - err: %v, out: `%s`", err, t)
|
return fmt.Errorf("error erasing credentials - err: %v, out: `%s`", err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,6 +104,11 @@ func List(program ProgramFunc) (map[string]string, error) {
|
|||||||
out, err := cmd.Output()
|
out, err := cmd.Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t := strings.TrimSpace(string(out))
|
t := strings.TrimSpace(string(out))
|
||||||
|
|
||||||
|
if isValidErr := isValidCredsMessage(t); isValidErr != nil {
|
||||||
|
err = isValidErr
|
||||||
|
}
|
||||||
|
|
||||||
return nil, fmt.Errorf("error listing credentials - err: %v, out: `%s`", err, t)
|
return nil, fmt.Errorf("error listing credentials - err: %v, out: `%s`", err, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -56,6 +56,8 @@ func (m *mockProgram) Output() ([]byte, error) {
|
|||||||
return []byte(credentials.NewErrCredentialsNotFound().Error()), errProgramExited
|
return []byte(credentials.NewErrCredentialsNotFound().Error()), errProgramExited
|
||||||
case invalidServerAddress:
|
case invalidServerAddress:
|
||||||
return []byte("program failed"), errProgramExited
|
return []byte("program failed"), errProgramExited
|
||||||
|
case "":
|
||||||
|
return []byte(credentials.NewErrCredentialsMissingServerURL().Error()), errProgramExited
|
||||||
}
|
}
|
||||||
case "store":
|
case "store":
|
||||||
var c credentials.Credentials
|
var c credentials.Credentials
|
||||||
@@ -158,12 +160,16 @@ func TestGet(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
missingServerURLErr := credentials.NewErrCredentialsMissingServerURL()
|
||||||
|
|
||||||
invalid := []struct {
|
invalid := []struct {
|
||||||
serverURL string
|
serverURL string
|
||||||
err string
|
err string
|
||||||
}{
|
}{
|
||||||
{missingCredsAddress, credentials.NewErrCredentialsNotFound().Error()},
|
{missingCredsAddress, credentials.NewErrCredentialsNotFound().Error()},
|
||||||
{invalidServerAddress, "error getting credentials - err: exited 1, out: `program failed`"},
|
{invalidServerAddress, "error getting credentials - err: exited 1, out: `program failed`"},
|
||||||
|
{"", fmt.Sprintf("error getting credentials - err: %s, out: `%s`",
|
||||||
|
missingServerURLErr.Error(), missingServerURLErr.Error())},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range invalid {
|
for _, v := range invalid {
|
||||||
|
|||||||
+8
-1
@@ -2,6 +2,7 @@ package client
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -17,10 +18,16 @@ type ProgramFunc func(args ...string) Program
|
|||||||
// NewShellProgramFunc creates programs that are executed in a Shell.
|
// NewShellProgramFunc creates programs that are executed in a Shell.
|
||||||
func NewShellProgramFunc(name string) ProgramFunc {
|
func NewShellProgramFunc(name string) ProgramFunc {
|
||||||
return func(args ...string) Program {
|
return func(args ...string) Program {
|
||||||
return &Shell{cmd: exec.Command(name, args...)}
|
return &Shell{cmd: newCmdRedirectErr(name, args)}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newCmdRedirectErr(name string, args []string) *exec.Cmd {
|
||||||
|
newCmd := exec.Command(name, args...)
|
||||||
|
newCmd.Stderr = os.Stderr
|
||||||
|
return newCmd
|
||||||
|
}
|
||||||
|
|
||||||
// Shell invokes shell commands to talk with a remote credentials helper.
|
// Shell invokes shell commands to talk with a remote credentials helper.
|
||||||
type Shell struct {
|
type Shell struct {
|
||||||
cmd *exec.Cmd
|
cmd *exec.Cmd
|
||||||
|
|||||||
@@ -17,6 +17,31 @@ type Credentials struct {
|
|||||||
Secret string
|
Secret string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isValid checks the integrity of Credentials object such that no credentials lack
|
||||||
|
// a server URL or a username.
|
||||||
|
// It returns whether the credentials are valid and the error if it isn't.
|
||||||
|
// error values can be errCredentialsMissingServerURL or errCredentialsMissingUsername
|
||||||
|
func (c *Credentials) isValid() (bool, error) {
|
||||||
|
if len(c.ServerURL) == 0 {
|
||||||
|
return false, NewErrCredentialsMissingServerURL()
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(c.Username) == 0 {
|
||||||
|
return false, NewErrCredentialsMissingUsername()
|
||||||
|
}
|
||||||
|
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Docker credentials should be labeled as such in credentials stores that allow labelling.
|
||||||
|
// That label allows to filter out non-Docker credentials too at lookup/search in macOS keychain,
|
||||||
|
// Windows credentials manager and Linux libsecret. Default value is "Docker Credentials"
|
||||||
|
var CredsLabel = "Docker Credentials"
|
||||||
|
|
||||||
|
func SetCredsLabel(label string) {
|
||||||
|
CredsLabel = label
|
||||||
|
}
|
||||||
|
|
||||||
// Serve initializes the credentials helper and parses the action argument.
|
// Serve initializes the credentials helper and parses the action argument.
|
||||||
// This function is designed to be called from a command line interface.
|
// This function is designed to be called from a command line interface.
|
||||||
// It uses os.Args[1] as the key for the action.
|
// It uses os.Args[1] as the key for the action.
|
||||||
@@ -72,6 +97,10 @@ func Store(helper Helper, reader io.Reader) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ok, err := creds.isValid(); !ok {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return helper.Add(&creds)
|
return helper.Add(&creds)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,6 +120,9 @@ func Get(helper Helper, reader io.Reader, writer io.Writer) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
serverURL := strings.TrimSpace(buffer.String())
|
serverURL := strings.TrimSpace(buffer.String())
|
||||||
|
if len(serverURL) == 0 {
|
||||||
|
return NewErrCredentialsMissingServerURL()
|
||||||
|
}
|
||||||
|
|
||||||
username, secret, err := helper.Get(serverURL)
|
username, secret, err := helper.Get(serverURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -98,6 +130,7 @@ func Get(helper Helper, reader io.Reader, writer io.Writer) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
resp := Credentials{
|
resp := Credentials{
|
||||||
|
ServerURL: serverURL,
|
||||||
Username: username,
|
Username: username,
|
||||||
Secret: secret,
|
Secret: secret,
|
||||||
}
|
}
|
||||||
@@ -126,6 +159,9 @@ func Erase(helper Helper, reader io.Reader) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
serverURL := strings.TrimSpace(buffer.String())
|
serverURL := strings.TrimSpace(buffer.String())
|
||||||
|
if len(serverURL) == 0 {
|
||||||
|
return NewErrCredentialsMissingServerURL()
|
||||||
|
}
|
||||||
|
|
||||||
return helper.Delete(serverURL)
|
return helper.Delete(serverURL)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -73,6 +73,46 @@ func TestStore(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStoreMissingServerURL(t *testing.T) {
|
||||||
|
creds := &Credentials{
|
||||||
|
ServerURL: "",
|
||||||
|
Username: "foo",
|
||||||
|
Secret: "bar",
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err := json.Marshal(creds)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
in := bytes.NewReader(b)
|
||||||
|
|
||||||
|
h := newMemoryStore()
|
||||||
|
|
||||||
|
if err := Store(h, in); IsCredentialsMissingServerURL(err) == false {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStoreMissingUsername(t *testing.T) {
|
||||||
|
creds := &Credentials{
|
||||||
|
ServerURL: "https://index.docker.io/v1/",
|
||||||
|
Username: "",
|
||||||
|
Secret: "bar",
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err := json.Marshal(creds)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
in := bytes.NewReader(b)
|
||||||
|
|
||||||
|
h := newMemoryStore()
|
||||||
|
|
||||||
|
if err := Store(h, in); IsCredentialsMissingUsername(err) == false {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestGet(t *testing.T) {
|
func TestGet(t *testing.T) {
|
||||||
serverURL := "https://index.docker.io/v1/"
|
serverURL := "https://index.docker.io/v1/"
|
||||||
creds := &Credentials{
|
creds := &Credentials{
|
||||||
@@ -115,6 +155,32 @@ func TestGet(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetMissingServerURL(t *testing.T) {
|
||||||
|
serverURL := "https://index.docker.io/v1/"
|
||||||
|
creds := &Credentials{
|
||||||
|
ServerURL: serverURL,
|
||||||
|
Username: "foo",
|
||||||
|
Secret: "bar",
|
||||||
|
}
|
||||||
|
b, err := json.Marshal(creds)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
in := bytes.NewReader(b)
|
||||||
|
|
||||||
|
h := newMemoryStore()
|
||||||
|
if err := Store(h, in); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := strings.NewReader("")
|
||||||
|
w := new(bytes.Buffer)
|
||||||
|
|
||||||
|
if err := Get(h, buf, w); IsCredentialsMissingServerURL(err) == false {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestErase(t *testing.T) {
|
func TestErase(t *testing.T) {
|
||||||
serverURL := "https://index.docker.io/v1/"
|
serverURL := "https://index.docker.io/v1/"
|
||||||
creds := &Credentials{
|
creds := &Credentials{
|
||||||
@@ -144,6 +210,30 @@ func TestErase(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEraseMissingServerURL(t *testing.T) {
|
||||||
|
serverURL := "https://index.docker.io/v1/"
|
||||||
|
creds := &Credentials{
|
||||||
|
ServerURL: serverURL,
|
||||||
|
Username: "foo",
|
||||||
|
Secret: "bar",
|
||||||
|
}
|
||||||
|
b, err := json.Marshal(creds)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
in := bytes.NewReader(b)
|
||||||
|
|
||||||
|
h := newMemoryStore()
|
||||||
|
if err := Store(h, in); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := strings.NewReader("")
|
||||||
|
if err := Erase(h, buf); IsCredentialsMissingServerURL(err) == false {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestList(t *testing.T) {
|
func TestList(t *testing.T) {
|
||||||
//This tests that there is proper input an output into the byte stream
|
//This tests that there is proper input an output into the byte stream
|
||||||
//Individual stores are very OS specific and have been tested in osxkeychain and secretservice respectively
|
//Individual stores are very OS specific and have been tested in osxkeychain and secretservice respectively
|
||||||
|
|||||||
+71
-3
@@ -1,8 +1,15 @@
|
|||||||
package credentials
|
package credentials
|
||||||
|
|
||||||
// ErrCredentialsNotFound standarizes the not found error, so every helper returns
|
const (
|
||||||
// the same message and docker can handle it properly.
|
// ErrCredentialsNotFound standardizes the not found error, so every helper returns
|
||||||
const errCredentialsNotFoundMessage = "credentials not found in native keychain"
|
// the same message and docker can handle it properly.
|
||||||
|
errCredentialsNotFoundMessage = "credentials not found in native keychain"
|
||||||
|
|
||||||
|
// ErrCredentialsMissingServerURL and ErrCredentialsMissingUsername standardize
|
||||||
|
// invalid credentials or credentials management operations
|
||||||
|
errCredentialsMissingServerURLMessage = "no credentials server URL"
|
||||||
|
errCredentialsMissingUsernameMessage = "no credentials username"
|
||||||
|
)
|
||||||
|
|
||||||
// errCredentialsNotFound represents an error
|
// errCredentialsNotFound represents an error
|
||||||
// raised when credentials are not in the store.
|
// raised when credentials are not in the store.
|
||||||
@@ -35,3 +42,64 @@ func IsErrCredentialsNotFound(err error) bool {
|
|||||||
func IsErrCredentialsNotFoundMessage(err string) bool {
|
func IsErrCredentialsNotFoundMessage(err string) bool {
|
||||||
return err == errCredentialsNotFoundMessage
|
return err == errCredentialsNotFoundMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// errCredentialsMissingServerURL represents an error raised
|
||||||
|
// when the credentials object has no server URL or when no
|
||||||
|
// server URL is provided to a credentials operation requiring
|
||||||
|
// one.
|
||||||
|
type errCredentialsMissingServerURL struct{}
|
||||||
|
|
||||||
|
func (errCredentialsMissingServerURL) Error() string {
|
||||||
|
return errCredentialsMissingServerURLMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// errCredentialsMissingUsername represents an error raised
|
||||||
|
// when the credentials object has no username or when no
|
||||||
|
// username is provided to a credentials operation requiring
|
||||||
|
// one.
|
||||||
|
type errCredentialsMissingUsername struct{}
|
||||||
|
|
||||||
|
func (errCredentialsMissingUsername) Error() string {
|
||||||
|
return errCredentialsMissingUsernameMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// NewErrCredentialsMissingServerURL creates a new error for
|
||||||
|
// errCredentialsMissingServerURL.
|
||||||
|
func NewErrCredentialsMissingServerURL() error {
|
||||||
|
return errCredentialsMissingServerURL{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewErrCredentialsMissingUsername creates a new error for
|
||||||
|
// errCredentialsMissingUsername.
|
||||||
|
func NewErrCredentialsMissingUsername() error {
|
||||||
|
return errCredentialsMissingUsername{}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// IsCredentialsMissingServerURL returns true if the error
|
||||||
|
// was an errCredentialsMissingServerURL.
|
||||||
|
func IsCredentialsMissingServerURL(err error) bool {
|
||||||
|
_, ok := err.(errCredentialsMissingServerURL)
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsCredentialsMissingServerURLMessage checks for an
|
||||||
|
// errCredentialsMissingServerURL in the error message.
|
||||||
|
func IsCredentialsMissingServerURLMessage(err string) bool {
|
||||||
|
return err == errCredentialsMissingServerURLMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsCredentialsMissingUsername returns true if the error
|
||||||
|
// was an errCredentialsMissingUsername.
|
||||||
|
func IsCredentialsMissingUsername(err error) bool {
|
||||||
|
_, ok := err.(errCredentialsMissingUsername)
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsCredentialsMissingUsernameMessage checks for an
|
||||||
|
// errCredentialsMissingUsername in the error message.
|
||||||
|
func IsCredentialsMissingUsernameMessage(err string) bool {
|
||||||
|
return err == errCredentialsMissingUsernameMessage
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include "osxkeychain_darwin.h"
|
#include "osxkeychain_darwin.h"
|
||||||
#include <CoreFoundation/CoreFoundation.h>
|
#include <CoreFoundation/CoreFoundation.h>
|
||||||
|
#include <Foundation/NSValue.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@@ -13,7 +14,9 @@ char *get_error(OSStatus status) {
|
|||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *keychain_add(struct Server *server, char *username, char *secret) {
|
char *keychain_add(struct Server *server, char *label, char *username, char *secret) {
|
||||||
|
SecKeychainItemRef item;
|
||||||
|
|
||||||
OSStatus status = SecKeychainAddInternetPassword(
|
OSStatus status = SecKeychainAddInternetPassword(
|
||||||
NULL,
|
NULL,
|
||||||
strlen(server->host), server->host,
|
strlen(server->host), server->host,
|
||||||
@@ -24,11 +27,27 @@ char *keychain_add(struct Server *server, char *username, char *secret) {
|
|||||||
server->proto,
|
server->proto,
|
||||||
kSecAuthenticationTypeDefault,
|
kSecAuthenticationTypeDefault,
|
||||||
strlen(secret), secret,
|
strlen(secret), secret,
|
||||||
NULL
|
&item
|
||||||
);
|
);
|
||||||
|
|
||||||
if (status) {
|
if (status) {
|
||||||
return get_error(status);
|
return get_error(status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SecKeychainAttribute attribute;
|
||||||
|
SecKeychainAttributeList attrs;
|
||||||
|
attribute.tag = kSecLabelItemAttr;
|
||||||
|
attribute.data = label;
|
||||||
|
attribute.length = strlen(label);
|
||||||
|
attrs.count = 1;
|
||||||
|
attrs.attr = &attribute;
|
||||||
|
|
||||||
|
status = SecKeychainItemModifyContent(item, &attrs, 0, NULL);
|
||||||
|
|
||||||
|
if (status) {
|
||||||
|
return get_error(status);
|
||||||
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,44 +134,42 @@ char * CFStringToCharArr(CFStringRef aString) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *keychain_list(char *** paths, char *** accts, unsigned int *list_l) {
|
char *keychain_list(char *credsLabel, char *** paths, char *** accts, unsigned int *list_l) {
|
||||||
|
CFStringRef credsLabelCF = CFStringCreateWithCString(NULL, credsLabel, kCFStringEncodingUTF8);
|
||||||
CFMutableDictionaryRef query = CFDictionaryCreateMutable (NULL, 1, NULL, NULL);
|
CFMutableDictionaryRef query = CFDictionaryCreateMutable (NULL, 1, NULL, NULL);
|
||||||
CFDictionaryAddValue(query, kSecClass, kSecClassInternetPassword);
|
CFDictionaryAddValue(query, kSecClass, kSecClassInternetPassword);
|
||||||
CFDictionaryAddValue(query, kSecReturnAttributes, kCFBooleanTrue);
|
CFDictionaryAddValue(query, kSecReturnAttributes, kCFBooleanTrue);
|
||||||
CFDictionaryAddValue(query, kSecMatchLimit, kSecMatchLimitAll);
|
CFDictionaryAddValue(query, kSecMatchLimit, kSecMatchLimitAll);
|
||||||
|
CFDictionaryAddValue(query, kSecAttrLabel, credsLabelCF);
|
||||||
//Use this query dictionary
|
//Use this query dictionary
|
||||||
CFTypeRef result= NULL;
|
CFTypeRef result= NULL;
|
||||||
OSStatus status = SecItemCopyMatching(
|
OSStatus status = SecItemCopyMatching(
|
||||||
query,
|
query,
|
||||||
&result);
|
&result);
|
||||||
|
|
||||||
|
CFRelease(credsLabelCF);
|
||||||
|
|
||||||
//Ran a search and store the results in result
|
//Ran a search and store the results in result
|
||||||
if (status) {
|
if (status) {
|
||||||
return get_error(status);
|
return get_error(status);
|
||||||
}
|
}
|
||||||
int numKeys = CFArrayGetCount(result);
|
CFIndex numKeys = CFArrayGetCount(result);
|
||||||
*paths = (char **) malloc((int)sizeof(char *)*numKeys);
|
*paths = (char **) malloc((int)sizeof(char *)*numKeys);
|
||||||
*accts = (char **) malloc((int)sizeof(char *)*numKeys);
|
*accts = (char **) malloc((int)sizeof(char *)*numKeys);
|
||||||
//result is of type CFArray
|
//result is of type CFArray
|
||||||
for(int i=0; i<numKeys; i++) {
|
for(CFIndex i=0; i<numKeys; i++) {
|
||||||
CFDictionaryRef currKey = CFArrayGetValueAtIndex(result,i);
|
CFDictionaryRef currKey = CFArrayGetValueAtIndex(result,i);
|
||||||
if (CFDictionaryContainsKey(currKey, CFSTR("path"))) {
|
|
||||||
//Even if a key is stored without an account, Apple defaults it to null so these arrays will be of the same length
|
CFStringRef protocolTmp = CFDictionaryGetValue(currKey, CFSTR("ptcl"));
|
||||||
CFStringRef pathTmp = CFDictionaryGetValue(currKey, CFSTR("path"));
|
if (protocolTmp != NULL) {
|
||||||
CFStringRef acctTmp = CFDictionaryGetValue(currKey, CFSTR("acct"));
|
CFStringRef protocolStr = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@"), protocolTmp);
|
||||||
if (acctTmp == NULL) {
|
if (CFStringCompare(protocolStr, CFSTR("htps"), 0) == kCFCompareEqualTo) {
|
||||||
acctTmp = CFSTR("account not defined");
|
protocolTmp = CFSTR("https://");
|
||||||
}
|
}
|
||||||
char * path = (char *) malloc(CFStringGetLength(pathTmp)+1);
|
else {
|
||||||
path = CFStringToCharArr(pathTmp);
|
protocolTmp = CFSTR("http://");
|
||||||
path[strlen(path)] = '\0';
|
}
|
||||||
char * acct = (char *) malloc(CFStringGetLength(acctTmp)+1);
|
CFRelease(protocolStr);
|
||||||
acct = CFStringToCharArr(acctTmp);
|
|
||||||
acct[strlen(acct)] = '\0';
|
|
||||||
//We now have all we need, username and servername. Now export this to .go
|
|
||||||
(*paths)[i] = (char *) malloc(sizeof(char)*(strlen(path)+1));
|
|
||||||
memcpy((*paths)[i], path, sizeof(char)*(strlen(path)+1));
|
|
||||||
(*accts)[i] = (char *) malloc(sizeof(char)*(strlen(acct)+1));
|
|
||||||
memcpy((*accts)[i], acct, sizeof(char)*(strlen(acct)+1));
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
char * path = "0";
|
char * path = "0";
|
||||||
@@ -161,9 +178,45 @@ char *keychain_list(char *** paths, char *** accts, unsigned int *list_l) {
|
|||||||
memcpy((*paths)[i], path, sizeof(char)*(strlen(path)));
|
memcpy((*paths)[i], path, sizeof(char)*(strlen(path)));
|
||||||
(*accts)[i] = (char *) malloc(sizeof(char)*(strlen(acct)));
|
(*accts)[i] = (char *) malloc(sizeof(char)*(strlen(acct)));
|
||||||
memcpy((*accts)[i], acct, sizeof(char)*(strlen(acct)));
|
memcpy((*accts)[i], acct, sizeof(char)*(strlen(acct)));
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CFMutableStringRef str = CFStringCreateMutableCopy(NULL, 0, protocolTmp);
|
||||||
|
CFStringRef serverTmp = CFDictionaryGetValue(currKey, CFSTR("srvr"));
|
||||||
|
if (serverTmp != NULL) {
|
||||||
|
CFStringAppend(str, serverTmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
CFStringRef pathTmp = CFDictionaryGetValue(currKey, CFSTR("path"));
|
||||||
|
if (pathTmp != NULL) {
|
||||||
|
CFStringAppend(str, pathTmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
const NSNumber * portTmp = CFDictionaryGetValue(currKey, CFSTR("port"));
|
||||||
|
if (portTmp != NULL && portTmp.integerValue != 0) {
|
||||||
|
CFStringRef portStr = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@"), portTmp);
|
||||||
|
CFStringAppend(str, CFSTR(":"));
|
||||||
|
CFStringAppend(str, portStr);
|
||||||
|
CFRelease(portStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
CFStringRef acctTmp = CFDictionaryGetValue(currKey, CFSTR("acct"));
|
||||||
|
if (acctTmp == NULL) {
|
||||||
|
acctTmp = CFSTR("account not defined");
|
||||||
|
}
|
||||||
|
|
||||||
|
char * path = CFStringToCharArr(str);
|
||||||
|
char * acct = CFStringToCharArr(acctTmp);
|
||||||
|
|
||||||
|
//We now have all we need, username and servername. Now export this to .go
|
||||||
|
(*paths)[i] = (char *) malloc(sizeof(char)*(strlen(path)+1));
|
||||||
|
memcpy((*paths)[i], path, sizeof(char)*(strlen(path)+1));
|
||||||
|
(*accts)[i] = (char *) malloc(sizeof(char)*(strlen(acct)+1));
|
||||||
|
memcpy((*accts)[i], acct, sizeof(char)*(strlen(acct)+1));
|
||||||
|
|
||||||
|
CFRelease(str);
|
||||||
}
|
}
|
||||||
*list_l = numKeys;
|
*list_l = (int)numKeys;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package osxkeychain
|
package osxkeychain
|
||||||
|
|
||||||
/*
|
/*
|
||||||
#cgo CFLAGS: -x objective-c
|
#cgo CFLAGS: -x objective-c -mmacosx-version-min=10.10
|
||||||
#cgo LDFLAGS: -framework Security -framework Foundation
|
#cgo LDFLAGS: -framework Security -framework Foundation -mmacosx-version-min=10.10
|
||||||
|
|
||||||
#include "osxkeychain_darwin.h"
|
#include "osxkeychain_darwin.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -35,12 +35,14 @@ func (h Osxkeychain) Add(creds *credentials.Credentials) error {
|
|||||||
}
|
}
|
||||||
defer freeServer(s)
|
defer freeServer(s)
|
||||||
|
|
||||||
|
label := C.CString(credentials.CredsLabel)
|
||||||
|
defer C.free(unsafe.Pointer(label))
|
||||||
username := C.CString(creds.Username)
|
username := C.CString(creds.Username)
|
||||||
defer C.free(unsafe.Pointer(username))
|
defer C.free(unsafe.Pointer(username))
|
||||||
secret := C.CString(creds.Secret)
|
secret := C.CString(creds.Secret)
|
||||||
defer C.free(unsafe.Pointer(secret))
|
defer C.free(unsafe.Pointer(secret))
|
||||||
|
|
||||||
errMsg := C.keychain_add(s, username, secret)
|
errMsg := C.keychain_add(s, label, username, secret)
|
||||||
if errMsg != nil {
|
if errMsg != nil {
|
||||||
defer C.free(unsafe.Pointer(errMsg))
|
defer C.free(unsafe.Pointer(errMsg))
|
||||||
return errors.New(C.GoString(errMsg))
|
return errors.New(C.GoString(errMsg))
|
||||||
@@ -99,12 +101,15 @@ func (h Osxkeychain) Get(serverURL string) (string, string, error) {
|
|||||||
|
|
||||||
// List returns the stored URLs and corresponding usernames.
|
// List returns the stored URLs and corresponding usernames.
|
||||||
func (h Osxkeychain) List() (map[string]string, error) {
|
func (h Osxkeychain) List() (map[string]string, error) {
|
||||||
|
credsLabelC := C.CString(credentials.CredsLabel)
|
||||||
|
defer C.free(unsafe.Pointer(credsLabelC))
|
||||||
|
|
||||||
var pathsC **C.char
|
var pathsC **C.char
|
||||||
defer C.free(unsafe.Pointer(pathsC))
|
defer C.free(unsafe.Pointer(pathsC))
|
||||||
var acctsC **C.char
|
var acctsC **C.char
|
||||||
defer C.free(unsafe.Pointer(acctsC))
|
defer C.free(unsafe.Pointer(acctsC))
|
||||||
var listLenC C.uint
|
var listLenC C.uint
|
||||||
errMsg := C.keychain_list(&pathsC, &acctsC, &listLenC)
|
errMsg := C.keychain_list(credsLabelC, &pathsC, &acctsC, &listLenC)
|
||||||
if errMsg != nil {
|
if errMsg != nil {
|
||||||
defer C.free(unsafe.Pointer(errMsg))
|
defer C.free(unsafe.Pointer(errMsg))
|
||||||
goMsg := C.GoString(errMsg)
|
goMsg := C.GoString(errMsg)
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ struct Server {
|
|||||||
unsigned int port;
|
unsigned int port;
|
||||||
};
|
};
|
||||||
|
|
||||||
char *keychain_add(struct Server *server, char *username, char *secret);
|
char *keychain_add(struct Server *server, char *label, char *username, char *secret);
|
||||||
char *keychain_get(struct Server *server, unsigned int *username_l, char **username, unsigned int *secret_l, char **secret);
|
char *keychain_get(struct Server *server, unsigned int *username_l, char **username, unsigned int *secret_l, char **secret);
|
||||||
char *keychain_delete(struct Server *server);
|
char *keychain_delete(struct Server *server);
|
||||||
char *keychain_list(char *** data, char *** accts, unsigned int *list_l);
|
char *keychain_list(char *credsLabel, char *** data, char *** accts, unsigned int *list_l);
|
||||||
void freeListData(char *** data, unsigned int length);
|
void freeListData(char *** data, unsigned int length);
|
||||||
@@ -7,6 +7,7 @@ const SecretSchema *docker_get_schema(void)
|
|||||||
static const SecretSchema docker_schema = {
|
static const SecretSchema docker_schema = {
|
||||||
"io.docker.Credentials", SECRET_SCHEMA_NONE,
|
"io.docker.Credentials", SECRET_SCHEMA_NONE,
|
||||||
{
|
{
|
||||||
|
{ "label", SECRET_SCHEMA_ATTRIBUTE_STRING },
|
||||||
{ "server", SECRET_SCHEMA_ATTRIBUTE_STRING },
|
{ "server", SECRET_SCHEMA_ATTRIBUTE_STRING },
|
||||||
{ "username", SECRET_SCHEMA_ATTRIBUTE_STRING },
|
{ "username", SECRET_SCHEMA_ATTRIBUTE_STRING },
|
||||||
{ "docker_cli", SECRET_SCHEMA_ATTRIBUTE_STRING },
|
{ "docker_cli", SECRET_SCHEMA_ATTRIBUTE_STRING },
|
||||||
@@ -16,11 +17,12 @@ const SecretSchema *docker_get_schema(void)
|
|||||||
return &docker_schema;
|
return &docker_schema;
|
||||||
}
|
}
|
||||||
|
|
||||||
GError *add(char *server, char *username, char *secret) {
|
GError *add(char *label, char *server, char *username, char *secret) {
|
||||||
GError *err = NULL;
|
GError *err = NULL;
|
||||||
|
|
||||||
secret_password_store_sync (DOCKER_SCHEMA, SECRET_COLLECTION_DEFAULT,
|
secret_password_store_sync (DOCKER_SCHEMA, SECRET_COLLECTION_DEFAULT,
|
||||||
server, secret, NULL, &err,
|
server, secret, NULL, &err,
|
||||||
|
"label", label,
|
||||||
"server", server,
|
"server", server,
|
||||||
"username", username,
|
"username", username,
|
||||||
"docker_cli", "1",
|
"docker_cli", "1",
|
||||||
@@ -40,7 +42,7 @@ GError *delete(char *server) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *get_username(SecretItem *item) {
|
char *get_attribute(const char *attribute, SecretItem *item) {
|
||||||
GHashTable *attributes;
|
GHashTable *attributes;
|
||||||
GHashTableIter iter;
|
GHashTableIter iter;
|
||||||
gchar *value, *key;
|
gchar *value, *key;
|
||||||
@@ -48,7 +50,7 @@ char *get_username(SecretItem *item) {
|
|||||||
attributes = secret_item_get_attributes(item);
|
attributes = secret_item_get_attributes(item);
|
||||||
g_hash_table_iter_init(&iter, attributes);
|
g_hash_table_iter_init(&iter, attributes);
|
||||||
while (g_hash_table_iter_next(&iter, (void **)&key, (void **)&value)) {
|
while (g_hash_table_iter_next(&iter, (void **)&key, (void **)&value)) {
|
||||||
if (strncmp(key, "username", strlen(key)) == 0)
|
if (strncmp(key, attribute, strlen(key)) == 0)
|
||||||
return (char *)value;
|
return (char *)value;
|
||||||
}
|
}
|
||||||
g_hash_table_unref(attributes);
|
g_hash_table_unref(attributes);
|
||||||
@@ -71,7 +73,7 @@ GError *get(char *server, char **username, char **secret) {
|
|||||||
|
|
||||||
service = secret_service_get_sync(SECRET_SERVICE_NONE, NULL, &err);
|
service = secret_service_get_sync(SECRET_SERVICE_NONE, NULL, &err);
|
||||||
if (err == NULL) {
|
if (err == NULL) {
|
||||||
items = secret_service_search_sync(service, NULL, attributes, flags, NULL, &err);
|
items = secret_service_search_sync(service, DOCKER_SCHEMA, attributes, flags, NULL, &err);
|
||||||
if (err == NULL) {
|
if (err == NULL) {
|
||||||
for (l = items; l != NULL; l = g_list_next(l)) {
|
for (l = items; l != NULL; l = g_list_next(l)) {
|
||||||
value = secret_item_get_schema_name(l->data);
|
value = secret_item_get_schema_name(l->data);
|
||||||
@@ -85,7 +87,7 @@ GError *get(char *server, char **username, char **secret) {
|
|||||||
*secret = strdup(secret_value_get(secretValue, &length));
|
*secret = strdup(secret_value_get(secretValue, &length));
|
||||||
secret_value_unref(secretValue);
|
secret_value_unref(secretValue);
|
||||||
}
|
}
|
||||||
*username = get_username(l->data);
|
*username = get_attribute("username", l->data);
|
||||||
}
|
}
|
||||||
g_list_free_full(items, g_object_unref);
|
g_list_free_full(items, g_object_unref);
|
||||||
}
|
}
|
||||||
@@ -98,22 +100,30 @@ GError *get(char *server, char **username, char **secret) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
GError *list(char *** paths, char *** accts, unsigned int *list_l) {
|
GError *list(char *ref_label, char *** paths, char *** accts, unsigned int *list_l) {
|
||||||
GList *items;
|
GList *items;
|
||||||
GError *err = NULL;
|
GError *err = NULL;
|
||||||
SecretService *service;
|
SecretService *service;
|
||||||
SecretSearchFlags flags = SECRET_SEARCH_LOAD_SECRETS | SECRET_SEARCH_ALL | SECRET_SEARCH_UNLOCK;
|
SecretSearchFlags flags = SECRET_SEARCH_LOAD_SECRETS | SECRET_SEARCH_ALL | SECRET_SEARCH_UNLOCK;
|
||||||
GHashTable *attributes;
|
GHashTable *attributes = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
|
||||||
g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
|
|
||||||
attributes = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
|
// List credentials with the right label only
|
||||||
|
g_hash_table_insert(attributes, g_strdup("label"), g_strdup(ref_label));
|
||||||
|
|
||||||
service = secret_service_get_sync(SECRET_SERVICE_NONE, NULL, &err);
|
service = secret_service_get_sync(SECRET_SERVICE_NONE, NULL, &err);
|
||||||
|
if (err != NULL) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
items = secret_service_search_sync(service, NULL, attributes, flags, NULL, &err);
|
items = secret_service_search_sync(service, NULL, attributes, flags, NULL, &err);
|
||||||
int numKeys = g_list_length(items);
|
int numKeys = g_list_length(items);
|
||||||
if (err != NULL) {
|
if (err != NULL) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
*paths = (char **) malloc((int)sizeof(char *)*numKeys);
|
|
||||||
*accts = (char **) malloc((int)sizeof(char *)*numKeys);
|
char **tmp_paths = (char **) calloc(1,(int)sizeof(char *)*numKeys);
|
||||||
|
char **tmp_accts = (char **) calloc(1,(int)sizeof(char *)*numKeys);
|
||||||
|
|
||||||
// items now contains our keys from the gnome keyring
|
// items now contains our keys from the gnome keyring
|
||||||
// we will now put it in our two lists to return it to go
|
// we will now put it in our two lists to return it to go
|
||||||
GList *current;
|
GList *current;
|
||||||
@@ -121,21 +131,25 @@ GError *list(char *** paths, char *** accts, unsigned int *list_l) {
|
|||||||
for(current = items; current!=NULL; current = current->next) {
|
for(current = items; current!=NULL; current = current->next) {
|
||||||
char *pathTmp = secret_item_get_label(current->data);
|
char *pathTmp = secret_item_get_label(current->data);
|
||||||
// you cannot have a key without a label in the gnome keyring
|
// you cannot have a key without a label in the gnome keyring
|
||||||
char *acctTmp = get_username(current->data);
|
char *acctTmp = get_attribute("username",current->data);
|
||||||
if (acctTmp==NULL) {
|
if (acctTmp==NULL) {
|
||||||
acctTmp = "account not defined";
|
acctTmp = "account not defined";
|
||||||
}
|
}
|
||||||
char *path = (char *) malloc(strlen(pathTmp));
|
|
||||||
char *acct = (char *) malloc(strlen(acctTmp));
|
tmp_paths[listNumber] = (char *) calloc(1, sizeof(char)*(strlen(pathTmp)+1));
|
||||||
path = pathTmp;
|
tmp_accts[listNumber] = (char *) calloc(1, sizeof(char)*(strlen(acctTmp)+1));
|
||||||
acct = acctTmp;
|
|
||||||
(*paths)[listNumber] = (char *) malloc(sizeof(char)*(strlen(path)));
|
memcpy(tmp_paths[listNumber], pathTmp, sizeof(char)*(strlen(pathTmp)+1));
|
||||||
memcpy((*paths)[listNumber], path, sizeof(char)*(strlen(path)));
|
memcpy(tmp_accts[listNumber], acctTmp, sizeof(char)*(strlen(acctTmp)+1));
|
||||||
(*accts)[listNumber] = (char *) malloc(sizeof(char)*(strlen(acct)));
|
|
||||||
memcpy((*accts)[listNumber], acct, sizeof(char)*(strlen(acct)));
|
|
||||||
listNumber = listNumber + 1;
|
listNumber = listNumber + 1;
|
||||||
}
|
}
|
||||||
*list_l = numKeys;
|
|
||||||
|
*paths = (char **) realloc(tmp_paths, (int)sizeof(char *)*listNumber);
|
||||||
|
*accts = (char **) realloc(tmp_accts, (int)sizeof(char *)*listNumber);
|
||||||
|
|
||||||
|
*list_l = listNumber;
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ func (h Secretservice) Add(creds *credentials.Credentials) error {
|
|||||||
if creds == nil {
|
if creds == nil {
|
||||||
return errors.New("missing credentials")
|
return errors.New("missing credentials")
|
||||||
}
|
}
|
||||||
|
credsLabel := C.CString(credentials.CredsLabel)
|
||||||
|
defer C.free(unsafe.Pointer(credsLabel))
|
||||||
server := C.CString(creds.ServerURL)
|
server := C.CString(creds.ServerURL)
|
||||||
defer C.free(unsafe.Pointer(server))
|
defer C.free(unsafe.Pointer(server))
|
||||||
username := C.CString(creds.Username)
|
username := C.CString(creds.Username)
|
||||||
@@ -29,7 +31,7 @@ func (h Secretservice) Add(creds *credentials.Credentials) error {
|
|||||||
secret := C.CString(creds.Secret)
|
secret := C.CString(creds.Secret)
|
||||||
defer C.free(unsafe.Pointer(secret))
|
defer C.free(unsafe.Pointer(secret))
|
||||||
|
|
||||||
if err := C.add(server, username, secret); err != nil {
|
if err := C.add(credsLabel, server, username, secret); err != nil {
|
||||||
defer C.g_error_free(err)
|
defer C.g_error_free(err)
|
||||||
errMsg := (*C.char)(unsafe.Pointer(err.message))
|
errMsg := (*C.char)(unsafe.Pointer(err.message))
|
||||||
return errors.New(C.GoString(errMsg))
|
return errors.New(C.GoString(errMsg))
|
||||||
@@ -79,14 +81,17 @@ func (h Secretservice) Get(serverURL string) (string, string, error) {
|
|||||||
return user, pass, nil
|
return user, pass, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// List returns the stored URLs and corresponding usernames.
|
// List returns the stored URLs and corresponding usernames for a given credentials label
|
||||||
func (h Secretservice) List() (map[string]string, error) {
|
func (h Secretservice) List() (map[string]string, error) {
|
||||||
|
credsLabelC := C.CString(credentials.CredsLabel)
|
||||||
|
defer C.free(unsafe.Pointer(credsLabelC))
|
||||||
|
|
||||||
var pathsC **C.char
|
var pathsC **C.char
|
||||||
defer C.free(unsafe.Pointer(pathsC))
|
defer C.free(unsafe.Pointer(pathsC))
|
||||||
var acctsC **C.char
|
var acctsC **C.char
|
||||||
defer C.free(unsafe.Pointer(acctsC))
|
defer C.free(unsafe.Pointer(acctsC))
|
||||||
var listLenC C.uint
|
var listLenC C.uint
|
||||||
err := C.list(&pathsC, &acctsC, &listLenC)
|
err := C.list(credsLabelC, &pathsC, &acctsC, &listLenC)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
defer C.free(unsafe.Pointer(err))
|
defer C.free(unsafe.Pointer(err))
|
||||||
return nil, errors.New("Error from list function in secretservice_linux.c likely due to error in secretservice library")
|
return nil, errors.New("Error from list function in secretservice_linux.c likely due to error in secretservice library")
|
||||||
@@ -94,10 +99,14 @@ func (h Secretservice) List() (map[string]string, error) {
|
|||||||
defer C.freeListData(&pathsC, listLenC)
|
defer C.freeListData(&pathsC, listLenC)
|
||||||
defer C.freeListData(&acctsC, listLenC)
|
defer C.freeListData(&acctsC, listLenC)
|
||||||
|
|
||||||
|
resp := make(map[string]string)
|
||||||
|
|
||||||
listLen := int(listLenC)
|
listLen := int(listLenC)
|
||||||
|
if listLen == 0 {
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
pathTmp := (*[1 << 30]*C.char)(unsafe.Pointer(pathsC))[:listLen:listLen]
|
pathTmp := (*[1 << 30]*C.char)(unsafe.Pointer(pathsC))[:listLen:listLen]
|
||||||
acctTmp := (*[1 << 30]*C.char)(unsafe.Pointer(acctsC))[:listLen:listLen]
|
acctTmp := (*[1 << 30]*C.char)(unsafe.Pointer(acctsC))[:listLen:listLen]
|
||||||
resp := make(map[string]string)
|
|
||||||
for i := 0; i < listLen; i++ {
|
for i := 0; i < listLen; i++ {
|
||||||
resp[C.GoString(pathTmp[i])] = C.GoString(acctTmp[i])
|
resp[C.GoString(pathTmp[i])] = C.GoString(acctTmp[i])
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ const SecretSchema *docker_get_schema(void) G_GNUC_CONST;
|
|||||||
|
|
||||||
#define DOCKER_SCHEMA docker_get_schema()
|
#define DOCKER_SCHEMA docker_get_schema()
|
||||||
|
|
||||||
GError *add(char *server, char *username, char *secret);
|
GError *add(char *label, char *server, char *username, char *secret);
|
||||||
GError *delete(char *server);
|
GError *delete(char *server);
|
||||||
GError *get(char *server, char **username, char **secret);
|
GError *get(char *server, char **username, char **secret);
|
||||||
GError *list(char *** paths, char *** accts, unsigned int *list_l);
|
GError *list(char *label, char *** paths, char *** accts, unsigned int *list_l);
|
||||||
void freeListData(char *** data, unsigned int length);
|
void freeListData(char *** data, unsigned int length);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package secretservice
|
package secretservice
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/docker/docker-credential-helpers/credentials"
|
"github.com/docker/docker-credential-helpers/credentials"
|
||||||
@@ -16,10 +17,36 @@ func TestSecretServiceHelper(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
helper := Secretservice{}
|
helper := Secretservice{}
|
||||||
|
|
||||||
|
// Check how many docker credentials we have when starting the test
|
||||||
|
old_auths, err := helper.List()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// If any docker credentials with the tests values we are providing, we
|
||||||
|
// remove them as they probably come from a previous failed test
|
||||||
|
for k, v := range old_auths {
|
||||||
|
if strings.Compare(k, creds.ServerURL) == 0 && strings.Compare(v, creds.Username) == 0 {
|
||||||
|
|
||||||
|
if err := helper.Delete(creds.ServerURL); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check again how many docker credentials we have when starting the test
|
||||||
|
old_auths, err = helper.List()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add new credentials
|
||||||
if err := helper.Add(creds); err != nil {
|
if err := helper.Add(creds); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Verify that it is inside the secret service store
|
||||||
username, secret, err := helper.Get(creds.ServerURL)
|
username, secret, err := helper.Get(creds.ServerURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -33,15 +60,21 @@ func TestSecretServiceHelper(t *testing.T) {
|
|||||||
t.Fatalf("expected %s, got %s\n", "foobarbaz", secret)
|
t.Fatalf("expected %s, got %s\n", "foobarbaz", secret)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We should have one more credential than before adding
|
||||||
|
new_auths, err := helper.List()
|
||||||
|
if err != nil || (len(new_auths)-len(old_auths) != 1) {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
old_auths = new_auths
|
||||||
|
|
||||||
|
// Deleting the credentials associated to current server url should succeed
|
||||||
if err := helper.Delete(creds.ServerURL); err != nil {
|
if err := helper.Delete(creds.ServerURL); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
auths, err := helper.List()
|
|
||||||
if err != nil || len(auths) == 0 {
|
// We should have one less credential than before deleting
|
||||||
t.Fatal(err)
|
new_auths, err = helper.List()
|
||||||
}
|
if err != nil || (len(old_auths)-len(new_auths) != 1) {
|
||||||
helper.Add(creds)
|
|
||||||
if newauths, err := helper.List(); (len(newauths) - len(auths)) != 1 {
|
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package wincred
|
package wincred
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"strings"
|
||||||
|
|
||||||
winc "github.com/danieljoos/wincred"
|
winc "github.com/danieljoos/wincred"
|
||||||
"github.com/docker/docker-credential-helpers/credentials"
|
"github.com/docker/docker-credential-helpers/credentials"
|
||||||
)
|
)
|
||||||
@@ -14,6 +17,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{{"label", []byte(credentials.CredsLabel)}}
|
||||||
|
|
||||||
return g.Write()
|
return g.Write()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,10 +40,17 @@ func (h Wincred) Get(serverURL string) (string, string, error) {
|
|||||||
if g == nil {
|
if g == nil {
|
||||||
return "", "", credentials.NewErrCredentialsNotFound()
|
return "", "", credentials.NewErrCredentialsNotFound()
|
||||||
}
|
}
|
||||||
return g.UserName, string(g.CredentialBlob), nil
|
for _, attr := range g.Attributes {
|
||||||
|
if strings.Compare(attr.Keyword, "label") == 0 &&
|
||||||
|
bytes.Compare(attr.Value, []byte(credentials.CredsLabel)) == 0 {
|
||||||
|
|
||||||
|
return g.UserName, string(g.CredentialBlob), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", "", credentials.NewErrCredentialsNotFound()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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() (map[string]string, error) {
|
||||||
creds, err := winc.List()
|
creds, err := winc.List()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -47,7 +59,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") == 0 &&
|
||||||
|
bytes.Compare(attr.Value, []byte(credentials.CredsLabel)) == 0 {
|
||||||
|
|
||||||
|
resp[creds[i].TargetName] = creds[i].UserName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package wincred
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/docker/docker-credential-helpers/credentials"
|
"github.com/docker/docker-credential-helpers/credentials"
|
||||||
)
|
)
|
||||||
@@ -19,6 +20,31 @@ func TestWinCredHelper(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
helper := Wincred{}
|
helper := Wincred{}
|
||||||
|
|
||||||
|
// check for and remove remaining credentials from previous fail tests
|
||||||
|
oldauths, err := helper.List()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range oldauths {
|
||||||
|
if strings.Compare(k, creds.ServerURL) == 0 && strings.Compare(v, creds.Username) == 0 {
|
||||||
|
if err := helper.Delete(creds.ServerURL); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
} else if strings.Compare(k, creds1.ServerURL) == 0 && strings.Compare(v, creds1.Username) == 0 {
|
||||||
|
if err := helper.Delete(creds1.ServerURL); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// recount for credentials
|
||||||
|
oldauths, err = helper.List()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
if err := helper.Add(creds); err != nil {
|
if err := helper.Add(creds); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -37,7 +63,7 @@ func TestWinCredHelper(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
auths, err := helper.List()
|
auths, err := helper.List()
|
||||||
if err != nil || len(auths) == 0 {
|
if err != nil || len(auths) - len(oldauths) != 1 {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user