mirror of
https://github.com/docker/docker-credential-helpers.git
synced 2026-06-28 07:11:36 +05:30
Compare commits
93 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2d19ebb7f4 | |||
| 054c53824f | |||
| be1808e3ed | |||
| e1d4c012bc | |||
| ad6ee5d58d | |||
| 94be56b6f4 | |||
| 37bf8afe8b | |||
| 3d7e1817f2 | |||
| 5651367281 | |||
| d8f57a18c6 | |||
| 4bc0bc2a85 | |||
| 167b137eb4 | |||
| 2a67ef1524 | |||
| 6ba4edf6e9 | |||
| fa4a4d4f71 | |||
| 4d60b372aa | |||
| 607bf3c174 | |||
| 74f4f75bc1 | |||
| 0e7779e5a1 | |||
| fb9549d396 | |||
| ed11c58ebf | |||
| 79ab7059b0 | |||
| 18d35e4984 | |||
| cdde659563 | |||
| 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 | |||
| 2514d5e8b2 | |||
| 01ed4b811b | |||
| 9b6be7c243 | |||
| f853612e07 | |||
| 69c9d2eab8 | |||
| eecc09c974 | |||
| 48079a964a | |||
| cfbce1c845 | |||
| a994ca1d54 | |||
| f72c04f1d8 | |||
| c45d9e9e28 | |||
| 80833adff5 | |||
| f1498a0524 | |||
| c2abee4c0c | |||
| 9e96a4905d | |||
| cf9b6df432 | |||
| b0c64357eb | |||
| 5b764cc13a | |||
| b63a32e7a2 | |||
| 173fe2dbc2 | |||
| b3ebaa455d | |||
| 029e094488 | |||
| 8fa18eb16c | |||
| 887a66459a | |||
| d2e6ed779a | |||
| 9557dc3c3f | |||
| 59b3d54595 | |||
| 205e3b3056 | |||
| 7566a1e399 | |||
| 72661b3103 | |||
| 5a8fb214ed |
@@ -1 +1,2 @@
|
|||||||
bin
|
bin
|
||||||
|
release
|
||||||
|
|||||||
+1
-1
@@ -9,7 +9,7 @@
|
|||||||
notifications:
|
notifications:
|
||||||
email: false
|
email: false
|
||||||
go:
|
go:
|
||||||
- 1.6
|
- 1.8
|
||||||
install: make deps
|
install: make deps
|
||||||
addons:
|
addons:
|
||||||
apt:
|
apt:
|
||||||
|
|||||||
@@ -4,6 +4,37 @@ This changelog tracks the releases of docker-credential-helpers.
|
|||||||
This project includes different binaries per platform.
|
This project includes different binaries per platform.
|
||||||
The platform released is identified after the tag name.
|
The platform released is identified after the tag name.
|
||||||
|
|
||||||
|
## v0.5.2 (Mac OS X, Windows, Linux)
|
||||||
|
|
||||||
|
- Add a `version` command to output the version
|
||||||
|
- Fix storing URLs without scheme, and use `https://` by default
|
||||||
|
|
||||||
|
## v0.5.1 (Go client, Mac OS X, Windows, Linux)
|
||||||
|
|
||||||
|
- Redirect credential helpers' standard error to the caller's
|
||||||
|
- Prevent invalid credentials and credentials queries
|
||||||
|
|
||||||
|
## v0.5.0 (Mac OS X)
|
||||||
|
|
||||||
|
- Add a label for Docker credentials and filter credentials lookup to filter keychain lookups
|
||||||
|
|
||||||
|
## v0.4.2 (Mac OS X, Windows)
|
||||||
|
|
||||||
|
- Fix osxkeychain list
|
||||||
|
- macOS binary is now signed on release
|
||||||
|
- Generate a `.exe` instead
|
||||||
|
|
||||||
|
## v0.4.1 (Mac OS X)
|
||||||
|
|
||||||
|
- Fixes to support older version of OSX (10.10, 10.11)
|
||||||
|
|
||||||
|
## v0.4.0 (Go client, Mac OS X, Windows, Linux)
|
||||||
|
|
||||||
|
- Full implementation for OSX ready
|
||||||
|
- Fix some windows issues
|
||||||
|
- Implement client.List, change list API
|
||||||
|
- mac: delete credentials before adding them to avoid already exist error (fixes #37)
|
||||||
|
|
||||||
## v0.3.0 (Go client)
|
## v0.3.0 (Go client)
|
||||||
|
|
||||||
- Add Go client library to talk with the native programs.
|
- Add Go client library to talk with the native programs.
|
||||||
|
|||||||
@@ -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,38 @@
|
|||||||
.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 := $(shell grep 'const Version' credentials/version.go | awk -F'"' '{ print $$2 }')
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
|
osxcodesign: 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 vet_osx lint fmt test osxcodesign
|
||||||
|
mkdir -p release
|
||||||
|
@echo "\nPackaging version ${VERSION}\n"
|
||||||
|
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
|
||||||
@@ -26,14 +41,21 @@ test:
|
|||||||
vet: vet_$(TRAVIS_OS_NAME)
|
vet: vet_$(TRAVIS_OS_NAME)
|
||||||
go vet ./credentials
|
go vet ./credentials
|
||||||
|
|
||||||
|
vet_win:
|
||||||
|
go vet ./wincred
|
||||||
|
|
||||||
vet_osx:
|
vet_osx:
|
||||||
go vet ./osxkeychain
|
go vet ./osxkeychain
|
||||||
|
|
||||||
vet_linux:
|
vet_linux:
|
||||||
go vet ./secretservice
|
go vet ./secretservice
|
||||||
|
|
||||||
validate: vet
|
lint:
|
||||||
for p in `go list ./... | grep -v /vendor/`; do \
|
for p in `go list ./... | grep -v /vendor/`; do \
|
||||||
golint $$p ; \
|
golint $$p ; \
|
||||||
done
|
done
|
||||||
|
|
||||||
|
fmt:
|
||||||
gofmt -s -l `ls **/*.go | grep -v vendor`
|
gofmt -s -l `ls **/*.go | grep -v vendor`
|
||||||
|
|
||||||
|
validate: vet lint fmt
|
||||||
|
|||||||
@@ -58,11 +58,12 @@ You can see examples of each function in the [client](https://godoc.org/github.c
|
|||||||
|
|
||||||
## Development
|
## Development
|
||||||
|
|
||||||
A credential helper can be any program that can read values from the standard input. We use the first argument in the command line to differentiate the kind of command to execute. There are three valid values:
|
A credential helper can be any program that can read values from the standard input. We use the first argument in the command line to differentiate the kind of command to execute. There are four valid values:
|
||||||
|
|
||||||
- `store`: Adds credentials to the keychain. The payload in the standard input is a JSON document with `ServerURL`, `Username` and `Secret`.
|
- `store`: Adds credentials to the keychain. The payload in the standard input is a JSON document with `ServerURL`, `Username` and `Secret`.
|
||||||
- `get`: Retrieves credentials from the keychain. The payload in the standard input is the raw value for the `ServerURL`.
|
- `get`: Retrieves credentials from the keychain. The payload in the standard input is the raw value for the `ServerURL`.
|
||||||
- `erase`: Removes credentials from the keychain. The payload in the standard input is the raw value for the `ServerURL`.
|
- `erase`: Removes credentials from the keychain. The payload in the standard input is the raw value for the `ServerURL`.
|
||||||
|
- `list`: Lists stored credentials. There is no standard input payload.
|
||||||
|
|
||||||
This repository also includes libraries to implement new credentials programs in Go. Adding a new helper program is pretty easy. You can see how the OS X keychain helper works in the [osxkeychain](osxkeychain) directory.
|
This repository also includes libraries to implement new credentials programs in Go. Adding a new helper program is pretty easy. You can see how the OS X keychain helper works in the [osxkeychain](osxkeychain) directory.
|
||||||
|
|
||||||
|
|||||||
+13
-56
@@ -1,67 +1,24 @@
|
|||||||
version: "{build}"
|
image: Visual Studio 2015
|
||||||
|
|
||||||
# Source Config
|
|
||||||
clone_folder: c:\gopath\src\github.com\docker\docker-credential-helpers
|
|
||||||
|
|
||||||
# Build host
|
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
global:
|
|
||||||
GOPATH: c:\gopath
|
GOPATH: c:\gopath
|
||||||
CGO_ENABLED: 1
|
|
||||||
GOVERSION: 1.6
|
|
||||||
matrix:
|
|
||||||
- platform: x86
|
|
||||||
GOARCH: 386
|
|
||||||
MSYS2_BITS: 32
|
|
||||||
- platform: x64
|
|
||||||
GOARCH: amd64
|
|
||||||
MSYS2_BITS: 64
|
|
||||||
|
|
||||||
init:
|
clone_folder: c:\gopath\src\github.com\docker\docker-credential-helpers
|
||||||
- git config --global core.autocrlf input
|
clone_depth: 10
|
||||||
|
|
||||||
# Build
|
before_build:
|
||||||
|
- set PATH=%PATH%;C:\MinGW\bin;
|
||||||
|
- set PATH=%PATH%;C:\go18\bin;
|
||||||
|
- set GOROOT=C:\go18
|
||||||
|
|
||||||
install:
|
build_script:
|
||||||
# Install Go 1.6.
|
- mingw32-make vet_win wincred
|
||||||
- rmdir c:\go /s /q
|
|
||||||
- appveyor DownloadFile https://storage.googleapis.com/golang/go%GOVERSION%.windows-%GOARCH%.msi
|
|
||||||
- msiexec /i go%GOVERSION%.windows-%GOARCH%.msi /q
|
|
||||||
- set Path=c:\msys64\mingw%MSYS2_BITS%\bin;c:\go\bin;%Path%
|
|
||||||
- go version
|
|
||||||
- go env
|
|
||||||
|
|
||||||
build: false
|
|
||||||
|
|
||||||
test_script:
|
test_script:
|
||||||
- go vet ./wincred
|
- mingw32-make test
|
||||||
- go test -v github.com/docker/docker-credential-helpers/wincred
|
|
||||||
|
|
||||||
# Equivalent to `before_deploy` phase
|
deploy: off
|
||||||
after_test:
|
|
||||||
# build binary
|
|
||||||
- mkdir bin
|
|
||||||
- go build -o bin/docker-credential-wincred wincred/cmd/main_windows.go
|
|
||||||
# build zipfile, will look like docker-credential-wincred-v0.1.0-amd64.zip in the root directory
|
|
||||||
- cd bin && 7z a ../docker-credential-wincred-%APPVEYOR_REPO_TAG_NAME%-%GOARCH%.zip docker-credential-wincred
|
|
||||||
|
|
||||||
# IMPORTANT All the artifacts need to be listed here, or they won't be uploaded to GitHub
|
|
||||||
artifacts:
|
artifacts:
|
||||||
- path: docker-credential-wincred-$(APPVEYOR_REPO_TAG_NAME)-$(GOARCH).zip
|
- path: bin/docker-credential-wincred.exe
|
||||||
name: docker-credential-wincred-$(APPVEYOR_REPO_TAG_NAME)-$(GOARCH).zip
|
|
||||||
|
|
||||||
deploy:
|
configuration: Release
|
||||||
# All the zipped artifacts will be deployed
|
|
||||||
description: "Visit the [Changelog](https://github.com/docker/docker-credential-helpers/blob/master/CHANGELOG.md) for a detailed description of what's new in this release."
|
|
||||||
artifact: /.*\.zip/
|
|
||||||
auth_token:
|
|
||||||
secure: ixWmTXZs8aV5+9s6vPXziIcdMMLd+lBVINJ0K/Sy++2wllpRxUec4/TPVKUGLqvL
|
|
||||||
provider: GitHub
|
|
||||||
# deploy when a new tag is pushed
|
|
||||||
on:
|
|
||||||
appveyor_repo_tag: true
|
|
||||||
|
|
||||||
branches:
|
|
||||||
only:
|
|
||||||
- master
|
|
||||||
|
|||||||
+55
-4
@@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,16 +79,43 @@ func Get(program ProgramFunc, serverURL string) (*credentials.Credentials, error
|
|||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Erase executes a program to remove the server credentails from the native store.
|
// Erase executes a program to remove the server credentials from the native store.
|
||||||
func Erase(program ProgramFunc, serverURL string) error {
|
func Erase(program ProgramFunc, serverURL string) error {
|
||||||
cmd := program("erase")
|
cmd := program("erase")
|
||||||
cmd.Input(strings.NewReader(serverURL))
|
cmd.Input(strings.NewReader(serverURL))
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// List executes a program to list server credentials in the native store.
|
||||||
|
func List(program ProgramFunc) (map[string]string, error) {
|
||||||
|
cmd := program("list")
|
||||||
|
cmd.Input(strings.NewReader("unused"))
|
||||||
|
out, err := cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
var resp map[string]string
|
||||||
|
if err = json.NewDecoder(bytes.NewReader(out)).Decode(&resp); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
validServerAddress = "https://index.docker.io/v1"
|
validServerAddress = "https://index.docker.io/v1"
|
||||||
|
validUsername = "linus"
|
||||||
validServerAddress2 = "https://example.com:5002"
|
validServerAddress2 = "https://example.com:5002"
|
||||||
invalidServerAddress = "https://foobar.example.com"
|
invalidServerAddress = "https://foobar.example.com"
|
||||||
missingCredsAddress = "https://missing.docker.io/v1"
|
missingCredsAddress = "https://missing.docker.io/v1"
|
||||||
@@ -55,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
|
||||||
@@ -70,6 +73,9 @@ func (m *mockProgram) Output() ([]byte, error) {
|
|||||||
default:
|
default:
|
||||||
return []byte("error storing credentials"), errProgramExited
|
return []byte("error storing credentials"), errProgramExited
|
||||||
}
|
}
|
||||||
|
case "list":
|
||||||
|
return []byte(fmt.Sprintf(`{"%s": "%s"}`, validServerAddress, validUsername)), nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return []byte(fmt.Sprintf("unknown argument %q with %q", m.arg, inS)), errProgramExited
|
return []byte(fmt.Sprintf("unknown argument %q with %q", m.arg, inS)), errProgramExited
|
||||||
@@ -154,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 {
|
||||||
@@ -190,3 +200,14 @@ func TestErase(t *testing.T) {
|
|||||||
t.Fatalf("Expected error for server %s, got nil", invalidServerAddress)
|
t.Fatalf("Expected error for server %s, got nil", invalidServerAddress)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestList(t *testing.T) {
|
||||||
|
auths, err := List(mockProgramFn)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if username, exists := auths[validServerAddress]; !exists || username != validUsername {
|
||||||
|
t.Fatalf("auths[%s] returned %s, %t; expected %s, %t", validServerAddress, username, exists, validUsername, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+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,32 @@ 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
|
||||||
|
}
|
||||||
|
|
||||||
|
// CredsLabel holds the way 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"
|
||||||
|
|
||||||
|
// SetCredsLabel is a simple setter for CredsLabel
|
||||||
|
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.
|
||||||
@@ -25,7 +51,7 @@ type Credentials struct {
|
|||||||
func Serve(helper Helper) {
|
func Serve(helper Helper) {
|
||||||
var err error
|
var err error
|
||||||
if len(os.Args) != 2 {
|
if len(os.Args) != 2 {
|
||||||
err = fmt.Errorf("Usage: %s <store|get|erase>", os.Args[0])
|
err = fmt.Errorf("Usage: %s <store|get|erase|list|version>", os.Args[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -47,6 +73,10 @@ func HandleCommand(helper Helper, key string, in io.Reader, out io.Writer) error
|
|||||||
return Get(helper, in, out)
|
return Get(helper, in, out)
|
||||||
case "erase":
|
case "erase":
|
||||||
return Erase(helper, in)
|
return Erase(helper, in)
|
||||||
|
case "list":
|
||||||
|
return List(helper, out)
|
||||||
|
case "version":
|
||||||
|
return PrintVersion(out)
|
||||||
}
|
}
|
||||||
return fmt.Errorf("Unknown credential action `%s`", key)
|
return fmt.Errorf("Unknown credential action `%s`", key)
|
||||||
}
|
}
|
||||||
@@ -70,6 +100,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)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,6 +123,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 {
|
||||||
@@ -96,8 +133,9 @@ func Get(helper Helper, reader io.Reader, writer io.Writer) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
resp := Credentials{
|
resp := Credentials{
|
||||||
Username: username,
|
ServerURL: serverURL,
|
||||||
Secret: secret,
|
Username: username,
|
||||||
|
Secret: secret,
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer.Reset()
|
buffer.Reset()
|
||||||
@@ -124,6 +162,25 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//List returns all the serverURLs of keys in
|
||||||
|
//the OS store as a list of strings
|
||||||
|
func List(helper Helper, writer io.Writer) error {
|
||||||
|
accts, err := helper.List()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return json.NewEncoder(writer).Encode(accts)
|
||||||
|
}
|
||||||
|
|
||||||
|
//PrintVersion outputs the current version.
|
||||||
|
func PrintVersion(writer io.Writer) error {
|
||||||
|
fmt.Fprintln(writer, Version)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -36,6 +36,11 @@ func (m *memoryStore) Get(serverURL string) (string, string, error) {
|
|||||||
return c.Username, c.Secret, nil
|
return c.Username, c.Secret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *memoryStore) List() (map[string]string, error) {
|
||||||
|
//Simply a placeholder to let memoryStore be a valid implementation of Helper interface
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
func TestStore(t *testing.T) {
|
func TestStore(t *testing.T) {
|
||||||
serverURL := "https://index.docker.io/v1/"
|
serverURL := "https://index.docker.io/v1/"
|
||||||
creds := &Credentials{
|
creds := &Credentials{
|
||||||
@@ -68,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{
|
||||||
@@ -110,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{
|
||||||
@@ -138,3 +209,41 @@ func TestErase(t *testing.T) {
|
|||||||
t.Fatal("expected error getting missing creds, got empty")
|
t.Fatal("expected error getting missing creds, got empty")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
//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
|
||||||
|
out := new(bytes.Buffer)
|
||||||
|
h := newMemoryStore()
|
||||||
|
if err := List(h, out); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
//testing that there is an output
|
||||||
|
if out.Len() == 0 {
|
||||||
|
t.Fatalf("expected output in the writer, got %d", 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+68
-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,61 @@ 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
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,4 +9,6 @@ type Helper interface {
|
|||||||
// Get retrieves credentials from the store.
|
// Get retrieves credentials from the store.
|
||||||
// It returns username and secret as strings.
|
// It returns username and secret as strings.
|
||||||
Get(serverURL string) (string, string, error)
|
Get(serverURL string) (string, string, error)
|
||||||
|
// List returns the stored serverURLs and their associated usernames.
|
||||||
|
List() (map[string]string, error)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
package credentials
|
||||||
|
|
||||||
|
// Version holds a string describing the current version
|
||||||
|
const Version = "0.5.2"
|
||||||
@@ -1,4 +1,8 @@
|
|||||||
#include "osxkeychain_darwin.h"
|
#include "osxkeychain_darwin.h"
|
||||||
|
#include <CoreFoundation/CoreFoundation.h>
|
||||||
|
#include <Foundation/NSValue.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
char *get_error(OSStatus status) {
|
char *get_error(OSStatus status) {
|
||||||
char *buf = malloc(128);
|
char *buf = malloc(128);
|
||||||
@@ -10,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,
|
||||||
@@ -21,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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,3 +118,111 @@ char *keychain_delete(struct Server *server) {
|
|||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char * CFStringToCharArr(CFStringRef aString) {
|
||||||
|
if (aString == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
CFIndex length = CFStringGetLength(aString);
|
||||||
|
CFIndex maxSize =
|
||||||
|
CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1;
|
||||||
|
char *buffer = (char *)malloc(maxSize);
|
||||||
|
if (CFStringGetCString(aString, buffer, maxSize,
|
||||||
|
kCFStringEncodingUTF8)) {
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
CFDictionaryAddValue(query, kSecClass, kSecClassInternetPassword);
|
||||||
|
CFDictionaryAddValue(query, kSecReturnAttributes, kCFBooleanTrue);
|
||||||
|
CFDictionaryAddValue(query, kSecMatchLimit, kSecMatchLimitAll);
|
||||||
|
CFDictionaryAddValue(query, kSecAttrLabel, credsLabelCF);
|
||||||
|
//Use this query dictionary
|
||||||
|
CFTypeRef result= NULL;
|
||||||
|
OSStatus status = SecItemCopyMatching(
|
||||||
|
query,
|
||||||
|
&result);
|
||||||
|
|
||||||
|
CFRelease(credsLabelCF);
|
||||||
|
|
||||||
|
//Ran a search and store the results in result
|
||||||
|
if (status) {
|
||||||
|
return get_error(status);
|
||||||
|
}
|
||||||
|
CFIndex numKeys = CFArrayGetCount(result);
|
||||||
|
*paths = (char **) malloc((int)sizeof(char *)*numKeys);
|
||||||
|
*accts = (char **) malloc((int)sizeof(char *)*numKeys);
|
||||||
|
//result is of type CFArray
|
||||||
|
for(CFIndex i=0; i<numKeys; i++) {
|
||||||
|
CFDictionaryRef currKey = CFArrayGetValueAtIndex(result,i);
|
||||||
|
|
||||||
|
CFStringRef protocolTmp = CFDictionaryGetValue(currKey, CFSTR("ptcl"));
|
||||||
|
if (protocolTmp != NULL) {
|
||||||
|
CFStringRef protocolStr = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@"), protocolTmp);
|
||||||
|
if (CFStringCompare(protocolStr, CFSTR("htps"), 0) == kCFCompareEqualTo) {
|
||||||
|
protocolTmp = CFSTR("https://");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
protocolTmp = CFSTR("http://");
|
||||||
|
}
|
||||||
|
CFRelease(protocolStr);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
char * path = "0";
|
||||||
|
char * acct = "0";
|
||||||
|
(*paths)[i] = (char *) malloc(sizeof(char)*(strlen(path)));
|
||||||
|
memcpy((*paths)[i], path, sizeof(char)*(strlen(path)));
|
||||||
|
(*accts)[i] = (char *) malloc(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 = (int)numKeys;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void freeListData(char *** data, unsigned int length) {
|
||||||
|
for(int i=0; i<length; i++) {
|
||||||
|
free((*data)[i]);
|
||||||
|
}
|
||||||
|
free(*data);
|
||||||
|
}
|
||||||
|
|||||||
@@ -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>
|
||||||
@@ -27,18 +27,22 @@ type Osxkeychain struct{}
|
|||||||
|
|
||||||
// Add adds new credentials to the keychain.
|
// Add adds new credentials to the keychain.
|
||||||
func (h Osxkeychain) Add(creds *credentials.Credentials) error {
|
func (h Osxkeychain) Add(creds *credentials.Credentials) error {
|
||||||
|
h.Delete(creds.ServerURL)
|
||||||
|
|
||||||
s, err := splitServer(creds.ServerURL)
|
s, err := splitServer(creds.ServerURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
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))
|
||||||
@@ -83,7 +87,6 @@ func (h Osxkeychain) Get(serverURL string) (string, string, error) {
|
|||||||
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)
|
||||||
|
|
||||||
if goMsg == errCredentialsNotFound {
|
if goMsg == errCredentialsNotFound {
|
||||||
return "", "", credentials.NewErrCredentialsNotFound()
|
return "", "", credentials.NewErrCredentialsNotFound()
|
||||||
}
|
}
|
||||||
@@ -96,31 +99,63 @@ func (h Osxkeychain) Get(serverURL string) (string, string, error) {
|
|||||||
return user, pass, nil
|
return user, pass, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// List returns the stored URLs and corresponding usernames.
|
||||||
|
func (h Osxkeychain) List() (map[string]string, error) {
|
||||||
|
credsLabelC := C.CString(credentials.CredsLabel)
|
||||||
|
defer C.free(unsafe.Pointer(credsLabelC))
|
||||||
|
|
||||||
|
var pathsC **C.char
|
||||||
|
defer C.free(unsafe.Pointer(pathsC))
|
||||||
|
var acctsC **C.char
|
||||||
|
defer C.free(unsafe.Pointer(acctsC))
|
||||||
|
var listLenC C.uint
|
||||||
|
errMsg := C.keychain_list(credsLabelC, &pathsC, &acctsC, &listLenC)
|
||||||
|
if errMsg != nil {
|
||||||
|
defer C.free(unsafe.Pointer(errMsg))
|
||||||
|
goMsg := C.GoString(errMsg)
|
||||||
|
return nil, errors.New(goMsg)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer C.freeListData(&pathsC, listLenC)
|
||||||
|
defer C.freeListData(&acctsC, listLenC)
|
||||||
|
|
||||||
|
var listLen int
|
||||||
|
listLen = int(listLenC)
|
||||||
|
pathTmp := (*[1 << 30]*C.char)(unsafe.Pointer(pathsC))[:listLen:listLen]
|
||||||
|
acctTmp := (*[1 << 30]*C.char)(unsafe.Pointer(acctsC))[:listLen:listLen]
|
||||||
|
//taking the array of c strings into go while ignoring all the stuff irrelevant to credentials-helper
|
||||||
|
resp := make(map[string]string)
|
||||||
|
for i := 0; i < listLen; i++ {
|
||||||
|
if C.GoString(pathTmp[i]) == "0" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
resp[C.GoString(pathTmp[i])] = C.GoString(acctTmp[i])
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
func splitServer(serverURL string) (*C.struct_Server, error) {
|
func splitServer(serverURL string) (*C.struct_Server, error) {
|
||||||
u, err := url.Parse(serverURL)
|
u, err := parseURL(serverURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
hostAndPort := strings.Split(u.Host, ":")
|
proto := C.kSecProtocolTypeHTTPS
|
||||||
host := hostAndPort[0]
|
if u.Scheme == "http" {
|
||||||
|
proto = C.kSecProtocolTypeHTTP
|
||||||
|
}
|
||||||
var port int
|
var port int
|
||||||
if len(hostAndPort) == 2 {
|
p := getPort(u)
|
||||||
p, err := strconv.Atoi(hostAndPort[1])
|
if p != "" {
|
||||||
|
port, err = strconv.Atoi(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
port = p
|
|
||||||
}
|
|
||||||
|
|
||||||
proto := C.kSecProtocolTypeHTTPS
|
|
||||||
if u.Scheme != "https" {
|
|
||||||
proto = C.kSecProtocolTypeHTTP
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &C.struct_Server{
|
return &C.struct_Server{
|
||||||
proto: C.SecProtocolType(proto),
|
proto: C.SecProtocolType(proto),
|
||||||
host: C.CString(host),
|
host: C.CString(getHostname(u)),
|
||||||
port: C.uint(port),
|
port: C.uint(port),
|
||||||
path: C.CString(u.Path),
|
path: C.CString(u.Path),
|
||||||
}, nil
|
}, nil
|
||||||
@@ -130,3 +165,32 @@ func freeServer(s *C.struct_Server) {
|
|||||||
C.free(unsafe.Pointer(s.host))
|
C.free(unsafe.Pointer(s.host))
|
||||||
C.free(unsafe.Pointer(s.path))
|
C.free(unsafe.Pointer(s.path))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parseURL parses and validates a given serverURL to an url.URL, and
|
||||||
|
// returns an error if validation failed. Querystring parameters are
|
||||||
|
// omitted in the resulting URL, because they are not used in the helper.
|
||||||
|
//
|
||||||
|
// If serverURL does not have a valid scheme, `//` is used as scheme
|
||||||
|
// before parsing. This prevents the hostname being used as path,
|
||||||
|
// and the credentials being stored without host.
|
||||||
|
func parseURL(serverURL string) (*url.URL, error) {
|
||||||
|
// Check if serverURL has a scheme, otherwise add `//` as scheme.
|
||||||
|
if !strings.Contains(serverURL, "://") && !strings.HasPrefix(serverURL, "//") {
|
||||||
|
serverURL = "//" + serverURL
|
||||||
|
}
|
||||||
|
|
||||||
|
u, err := url.Parse(serverURL)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if u.Scheme != "" && u.Scheme != "https" && u.Scheme != "http" {
|
||||||
|
return nil, errors.New("unsupported scheme: " + u.Scheme)
|
||||||
|
}
|
||||||
|
if getHostname(u) == "" {
|
||||||
|
return nil, errors.New("no hostname in URL")
|
||||||
|
}
|
||||||
|
|
||||||
|
u.RawQuery = ""
|
||||||
|
return u, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,6 +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 *credsLabel, char *** data, char *** accts, unsigned int *list_l);
|
||||||
|
void freeListData(char *** data, unsigned int length);
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
package osxkeychain
|
package osxkeychain
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"github.com/docker/docker-credential-helpers/credentials"
|
"github.com/docker/docker-credential-helpers/credentials"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestOSXKeychainHelper(t *testing.T) {
|
func TestOSXKeychainHelper(t *testing.T) {
|
||||||
@@ -12,7 +13,11 @@ func TestOSXKeychainHelper(t *testing.T) {
|
|||||||
Username: "foobar",
|
Username: "foobar",
|
||||||
Secret: "foobarbaz",
|
Secret: "foobarbaz",
|
||||||
}
|
}
|
||||||
|
creds1 := &credentials.Credentials{
|
||||||
|
ServerURL: "https://foobar.docker.io:2376/v2",
|
||||||
|
Username: "foobarbaz",
|
||||||
|
Secret: "foobar",
|
||||||
|
}
|
||||||
helper := Osxkeychain{}
|
helper := Osxkeychain{}
|
||||||
if err := helper.Add(creds); err != nil {
|
if err := helper.Add(creds); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -31,11 +36,214 @@ func TestOSXKeychainHelper(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()
|
||||||
|
if err != nil || len(auths) == 0 {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
helper.Add(creds1)
|
||||||
|
defer helper.Delete(creds1.ServerURL)
|
||||||
|
newauths, err := helper.List()
|
||||||
|
if len(newauths)-len(auths) != 1 {
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("Error: len(newauths): %d, len(auths): %d", len(newauths), len(auths))
|
||||||
|
}
|
||||||
|
t.Fatalf("Error: len(newauths): %d, len(auths): %d\n Error= %v", len(newauths), len(auths), err)
|
||||||
|
}
|
||||||
|
|
||||||
if err := helper.Delete(creds.ServerURL); err != nil {
|
if err := helper.Delete(creds.ServerURL); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestOSXKeychainHelperParseURL verifies that a // "scheme" is added to URLs,
|
||||||
|
// and that invalid URLs produce an error.
|
||||||
|
func TestOSXKeychainHelperParseURL(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
url string
|
||||||
|
expectedURL string
|
||||||
|
err error
|
||||||
|
}{
|
||||||
|
{url: "foobar.docker.io", expectedURL: "//foobar.docker.io"},
|
||||||
|
{url: "foobar.docker.io:2376", expectedURL: "//foobar.docker.io:2376"},
|
||||||
|
{url: "//foobar.docker.io:2376", expectedURL: "//foobar.docker.io:2376"},
|
||||||
|
{url: "http://foobar.docker.io:2376", expectedURL: "http://foobar.docker.io:2376"},
|
||||||
|
{url: "https://foobar.docker.io:2376", expectedURL: "https://foobar.docker.io:2376"},
|
||||||
|
{url: "https://foobar.docker.io:2376/some/path", expectedURL: "https://foobar.docker.io:2376/some/path"},
|
||||||
|
{url: "https://foobar.docker.io:2376/some/other/path?foo=bar", expectedURL: "https://foobar.docker.io:2376/some/other/path"},
|
||||||
|
{url: "/foobar.docker.io", err: errors.New("no hostname in URL")},
|
||||||
|
{url: "ftp://foobar.docker.io:2376", err: errors.New("unsupported scheme: ftp")},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, te := range tests {
|
||||||
|
u, err := parseURL(te.url)
|
||||||
|
|
||||||
|
if te.err == nil && err != nil {
|
||||||
|
t.Errorf("Error: failed to parse URL %q: %s", te.url, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if te.err != nil && err == nil {
|
||||||
|
t.Errorf("Error: expected error %q, got none when parsing URL %q", te.err, te.url)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if te.err != nil && err.Error() != te.err.Error() {
|
||||||
|
t.Errorf("Error: expected error %q, got %q when parsing URL %q", te.err, err, te.url)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if u != nil && u.String() != te.expectedURL {
|
||||||
|
t.Errorf("Error: expected URL: %q, but got %q for URL: %q", te.expectedURL, u.String(), te.url)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestOSXKeychainHelperRetrieveAliases verifies that secrets can be accessed
|
||||||
|
// through variations on the URL
|
||||||
|
func TestOSXKeychainHelperRetrieveAliases(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
storeURL string
|
||||||
|
readURL string
|
||||||
|
}{
|
||||||
|
// stored with port, retrieved without
|
||||||
|
{"https://foobar.docker.io:2376", "https://foobar.docker.io"},
|
||||||
|
|
||||||
|
// stored as https, retrieved without scheme
|
||||||
|
{"https://foobar.docker.io:2376", "foobar.docker.io"},
|
||||||
|
|
||||||
|
// stored with path, retrieved without
|
||||||
|
{"https://foobar.docker.io:1234/one/two", "https://foobar.docker.io:1234"},
|
||||||
|
}
|
||||||
|
|
||||||
|
helper := Osxkeychain{}
|
||||||
|
defer func() {
|
||||||
|
for _, te := range tests {
|
||||||
|
helper.Delete(te.storeURL)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Clean store before testing.
|
||||||
|
for _, te := range tests {
|
||||||
|
helper.Delete(te.storeURL)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, te := range tests {
|
||||||
|
c := &credentials.Credentials{ServerURL: te.storeURL, Username: "hello", Secret: "world"}
|
||||||
|
if err := helper.Add(c); err != nil {
|
||||||
|
t.Errorf("Error: failed to store secret for URL %q: %s", te.storeURL, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, _, err := helper.Get(te.readURL); err != nil {
|
||||||
|
t.Errorf("Error: failed to read secret for URL %q using %q", te.storeURL, te.readURL)
|
||||||
|
}
|
||||||
|
helper.Delete(te.storeURL)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestOSXKeychainHelperRetrieveStrict verifies that only matching secrets are
|
||||||
|
// returned.
|
||||||
|
func TestOSXKeychainHelperRetrieveStrict(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
storeURL string
|
||||||
|
readURL string
|
||||||
|
}{
|
||||||
|
// stored as https, retrieved using http
|
||||||
|
{"https://foobar.docker.io:2376", "http://foobar.docker.io:2376"},
|
||||||
|
|
||||||
|
// stored as http, retrieved using https
|
||||||
|
{"http://foobar.docker.io:2376", "https://foobar.docker.io:2376"},
|
||||||
|
|
||||||
|
// same: stored as http, retrieved without a scheme specified (hence, using the default https://)
|
||||||
|
{"http://foobar.docker.io", "foobar.docker.io:5678"},
|
||||||
|
|
||||||
|
// non-matching ports
|
||||||
|
{"https://foobar.docker.io:1234", "https://foobar.docker.io:5678"},
|
||||||
|
|
||||||
|
// non-matching ports TODO is this desired behavior? The other way round does work
|
||||||
|
//{"https://foobar.docker.io", "https://foobar.docker.io:5678"},
|
||||||
|
|
||||||
|
// non-matching paths
|
||||||
|
{"https://foobar.docker.io:1234/one/two", "https://foobar.docker.io:1234/five/six"},
|
||||||
|
}
|
||||||
|
|
||||||
|
helper := Osxkeychain{}
|
||||||
|
defer func() {
|
||||||
|
for _, te := range tests {
|
||||||
|
helper.Delete(te.storeURL)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Clean store before testing.
|
||||||
|
for _, te := range tests {
|
||||||
|
helper.Delete(te.storeURL)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, te := range tests {
|
||||||
|
c := &credentials.Credentials{ServerURL: te.storeURL, Username: "hello", Secret: "world"}
|
||||||
|
if err := helper.Add(c); err != nil {
|
||||||
|
t.Errorf("Error: failed to store secret for URL %q: %s", te.storeURL, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, _, err := helper.Get(te.readURL); err == nil {
|
||||||
|
t.Errorf("Error: managed to read secret for URL %q using %q, but should not be able to", te.storeURL, te.readURL)
|
||||||
|
}
|
||||||
|
helper.Delete(te.storeURL)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestOSXKeychainHelperStoreRetrieve verifies that secrets stored in the
|
||||||
|
// the keychain can be read back using the URL that was used to store them.
|
||||||
|
func TestOSXKeychainHelperStoreRetrieve(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
url string
|
||||||
|
}{
|
||||||
|
{url: "foobar.docker.io"},
|
||||||
|
{url: "foobar.docker.io:2376"},
|
||||||
|
{url: "//foobar.docker.io:2376"},
|
||||||
|
{url: "https://foobar.docker.io:2376"},
|
||||||
|
{url: "http://foobar.docker.io:2376"},
|
||||||
|
{url: "https://foobar.docker.io:2376/some/path"},
|
||||||
|
{url: "https://foobar.docker.io:2376/some/other/path"},
|
||||||
|
{url: "https://foobar.docker.io:2376/some/other/path?foo=bar"},
|
||||||
|
}
|
||||||
|
|
||||||
|
helper := Osxkeychain{}
|
||||||
|
defer func() {
|
||||||
|
for _, te := range tests {
|
||||||
|
helper.Delete(te.url)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Clean store before testing.
|
||||||
|
for _, te := range tests {
|
||||||
|
helper.Delete(te.url)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Note that we don't delete between individual tests here, to verify that
|
||||||
|
// subsequent stores/overwrites don't affect storing / retrieving secrets.
|
||||||
|
for i, te := range tests {
|
||||||
|
c := &credentials.Credentials{
|
||||||
|
ServerURL: te.url,
|
||||||
|
Username: fmt.Sprintf("user-%d", i),
|
||||||
|
Secret: fmt.Sprintf("secret-%d", i),
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := helper.Add(c); err != nil {
|
||||||
|
t.Errorf("Error: failed to store secret for URL: %s: %s", te.url, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
user, secret, err := helper.Get(te.url)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error: failed to read secret for URL %q: %s", te.url, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if user != c.Username {
|
||||||
|
t.Errorf("Error: expected username %s, got username %s for URL: %s", c.Username, user, te.url)
|
||||||
|
}
|
||||||
|
if secret != c.Secret {
|
||||||
|
t.Errorf("Error: expected secret %s, got secret %s for URL: %s", c.Secret, secret, te.url)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestMissingCredentials(t *testing.T) {
|
func TestMissingCredentials(t *testing.T) {
|
||||||
helper := Osxkeychain{}
|
helper := Osxkeychain{}
|
||||||
_, _, err := helper.Get("https://adsfasdf.wrewerwer.com/asdfsdddd")
|
_, _, err := helper.Get("https://adsfasdf.wrewerwer.com/asdfsdddd")
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
//+build go1.8
|
||||||
|
|
||||||
|
package osxkeychain
|
||||||
|
|
||||||
|
import "net/url"
|
||||||
|
|
||||||
|
func getHostname(u *url.URL) string {
|
||||||
|
return u.Hostname()
|
||||||
|
}
|
||||||
|
|
||||||
|
func getPort(u *url.URL) string {
|
||||||
|
return u.Port()
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
//+build !go1.8
|
||||||
|
|
||||||
|
package osxkeychain
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getHostname(u *url.URL) string {
|
||||||
|
return stripPort(u.Host)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getPort(u *url.URL) string {
|
||||||
|
return portOnly(u.Host)
|
||||||
|
}
|
||||||
|
|
||||||
|
func stripPort(hostport string) string {
|
||||||
|
colon := strings.IndexByte(hostport, ':')
|
||||||
|
if colon == -1 {
|
||||||
|
return hostport
|
||||||
|
}
|
||||||
|
if i := strings.IndexByte(hostport, ']'); i != -1 {
|
||||||
|
return strings.TrimPrefix(hostport[:i], "[")
|
||||||
|
}
|
||||||
|
return hostport[:colon]
|
||||||
|
}
|
||||||
|
|
||||||
|
func portOnly(hostport string) string {
|
||||||
|
colon := strings.IndexByte(hostport, ':')
|
||||||
|
if colon == -1 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if i := strings.Index(hostport, "]:"); i != -1 {
|
||||||
|
return hostport[i+len("]:"):]
|
||||||
|
}
|
||||||
|
if strings.Contains(hostport, "]") {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return hostport[colon+len(":"):]
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include "secretservice_linux.h"
|
#include "secretservice_linux.h"
|
||||||
|
|
||||||
const SecretSchema *docker_get_schema(void)
|
const SecretSchema *docker_get_schema(void)
|
||||||
@@ -6,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 },
|
||||||
@@ -15,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",
|
||||||
@@ -39,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;
|
||||||
@@ -47,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);
|
||||||
@@ -70,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);
|
||||||
@@ -84,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);
|
||||||
}
|
}
|
||||||
@@ -96,3 +99,64 @@ GError *get(char *server, char **username, char **secret) {
|
|||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GError *list(char *ref_label, char *** paths, char *** accts, unsigned int *list_l) {
|
||||||
|
GList *items;
|
||||||
|
GError *err = NULL;
|
||||||
|
SecretService *service;
|
||||||
|
SecretSearchFlags flags = SECRET_SEARCH_LOAD_SECRETS | SECRET_SEARCH_ALL | SECRET_SEARCH_UNLOCK;
|
||||||
|
GHashTable *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);
|
||||||
|
if (err != NULL) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
items = secret_service_search_sync(service, NULL, attributes, flags, NULL, &err);
|
||||||
|
int numKeys = g_list_length(items);
|
||||||
|
if (err != NULL) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
// we will now put it in our two lists to return it to go
|
||||||
|
GList *current;
|
||||||
|
int listNumber = 0;
|
||||||
|
for(current = items; current!=NULL; current = current->next) {
|
||||||
|
char *pathTmp = secret_item_get_label(current->data);
|
||||||
|
// you cannot have a key without a label in the gnome keyring
|
||||||
|
char *acctTmp = get_attribute("username",current->data);
|
||||||
|
if (acctTmp==NULL) {
|
||||||
|
acctTmp = "account not defined";
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp_paths[listNumber] = (char *) calloc(1, sizeof(char)*(strlen(pathTmp)+1));
|
||||||
|
tmp_accts[listNumber] = (char *) calloc(1, sizeof(char)*(strlen(acctTmp)+1));
|
||||||
|
|
||||||
|
memcpy(tmp_paths[listNumber], pathTmp, sizeof(char)*(strlen(pathTmp)+1));
|
||||||
|
memcpy(tmp_accts[listNumber], acctTmp, sizeof(char)*(strlen(acctTmp)+1));
|
||||||
|
|
||||||
|
listNumber = listNumber + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
*paths = (char **) realloc(tmp_paths, (int)sizeof(char *)*listNumber);
|
||||||
|
*accts = (char **) realloc(tmp_accts, (int)sizeof(char *)*listNumber);
|
||||||
|
|
||||||
|
*list_l = listNumber;
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void freeListData(char *** data, unsigned int length) {
|
||||||
|
int i;
|
||||||
|
for(i=0; i<length; i++) {
|
||||||
|
free((*data)[i]);
|
||||||
|
}
|
||||||
|
free(*data);
|
||||||
|
}
|
||||||
|
|||||||
@@ -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))
|
||||||
@@ -78,3 +80,39 @@ 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 for a given credentials label
|
||||||
|
func (h Secretservice) List() (map[string]string, error) {
|
||||||
|
credsLabelC := C.CString(credentials.CredsLabel)
|
||||||
|
defer C.free(unsafe.Pointer(credsLabelC))
|
||||||
|
|
||||||
|
var pathsC **C.char
|
||||||
|
defer C.free(unsafe.Pointer(pathsC))
|
||||||
|
var acctsC **C.char
|
||||||
|
defer C.free(unsafe.Pointer(acctsC))
|
||||||
|
var listLenC C.uint
|
||||||
|
err := C.list(credsLabelC, &pathsC, &acctsC, &listLenC)
|
||||||
|
if err != nil {
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
defer C.freeListData(&pathsC, listLenC)
|
||||||
|
defer C.freeListData(&acctsC, listLenC)
|
||||||
|
|
||||||
|
resp := make(map[string]string)
|
||||||
|
|
||||||
|
listLen := int(listLenC)
|
||||||
|
if listLen == 0 {
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
// The maximum capacity of the following two slices is limited to (2^29)-1 to remain compatible
|
||||||
|
// with 32-bit platforms. The size of a `*C.char` (a pointer) is 4 Byte on a 32-bit system
|
||||||
|
// and (2^29)*4 == math.MaxInt32 + 1. -- See issue golang/go#13656
|
||||||
|
pathTmp := (*[(1 << 29) - 1]*C.char)(unsafe.Pointer(pathsC))[:listLen:listLen]
|
||||||
|
acctTmp := (*[(1 << 29) - 1]*C.char)(unsafe.Pointer(acctsC))[:listLen:listLen]
|
||||||
|
for i := 0; i < listLen; i++ {
|
||||||
|
resp[C.GoString(pathTmp[i])] = C.GoString(acctTmp[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,6 +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 *label, char *** paths, char *** accts, unsigned int *list_l);
|
||||||
|
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,9 +60,23 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We should have one less credential than before deleting
|
||||||
|
new_auths, err = helper.List()
|
||||||
|
if err != nil || (len(old_auths)-len(new_auths) != 1) {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMissingCredentials(t *testing.T) {
|
func TestMissingCredentials(t *testing.T) {
|
||||||
|
|||||||
+21
-4
@@ -1,7 +1,6 @@
|
|||||||
package wincred
|
package wincred
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"C"
|
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"reflect"
|
"reflect"
|
||||||
"syscall"
|
"syscall"
|
||||||
@@ -10,6 +9,8 @@ import (
|
|||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var nullPointer = unsafe.Pointer(uintptr(0))
|
||||||
|
|
||||||
// Create a Go string using a pointer to a zero-terminated UTF 16 encoded string.
|
// Create a Go string using a pointer to a zero-terminated UTF 16 encoded string.
|
||||||
// See github.com/AllenDang/w32
|
// See github.com/AllenDang/w32
|
||||||
func utf16PtrToString(wstr *uint16) string {
|
func utf16PtrToString(wstr *uint16) string {
|
||||||
@@ -36,8 +37,22 @@ func utf16ToByte(wstr []uint16) (result []byte) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Copies the given C byte array to a Go byte array (see `C.GoBytes`)
|
||||||
|
func goBytes(src unsafe.Pointer, len uint32) []byte {
|
||||||
|
if src == nullPointer {
|
||||||
|
return []byte{}
|
||||||
|
}
|
||||||
|
slice := (*[1 << 30]byte)(src)[0:len]
|
||||||
|
rv := make([]byte, len)
|
||||||
|
copy(rv, slice)
|
||||||
|
return rv[:]
|
||||||
|
}
|
||||||
|
|
||||||
// Convert the given CREDENTIAL struct to a more usable structure
|
// Convert the given CREDENTIAL struct to a more usable structure
|
||||||
func nativeToCredential(cred *nativeCREDENTIAL) (result *Credential) {
|
func nativeToCredential(cred *nativeCREDENTIAL) (result *Credential) {
|
||||||
|
if unsafe.Pointer(cred) == nullPointer {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
result = new(Credential)
|
result = new(Credential)
|
||||||
result.Comment = utf16PtrToString(cred.Comment)
|
result.Comment = utf16PtrToString(cred.Comment)
|
||||||
result.TargetName = utf16PtrToString(cred.TargetName)
|
result.TargetName = utf16PtrToString(cred.TargetName)
|
||||||
@@ -45,7 +60,7 @@ func nativeToCredential(cred *nativeCREDENTIAL) (result *Credential) {
|
|||||||
result.UserName = utf16PtrToString(cred.UserName)
|
result.UserName = utf16PtrToString(cred.UserName)
|
||||||
result.LastWritten = time.Unix(0, cred.LastWritten.Nanoseconds())
|
result.LastWritten = time.Unix(0, cred.LastWritten.Nanoseconds())
|
||||||
result.Persist = CredentialPersistence(cred.Persist)
|
result.Persist = CredentialPersistence(cred.Persist)
|
||||||
result.CredentialBlob = C.GoBytes(unsafe.Pointer(cred.CredentialBlob), C.int(cred.CredentialBlobSize))
|
result.CredentialBlob = goBytes(unsafe.Pointer(cred.CredentialBlob), cred.CredentialBlobSize)
|
||||||
result.Attributes = make([]CredentialAttribute, cred.AttributeCount)
|
result.Attributes = make([]CredentialAttribute, cred.AttributeCount)
|
||||||
attrSliceHeader := reflect.SliceHeader{
|
attrSliceHeader := reflect.SliceHeader{
|
||||||
Data: cred.Attributes,
|
Data: cred.Attributes,
|
||||||
@@ -56,15 +71,17 @@ func nativeToCredential(cred *nativeCREDENTIAL) (result *Credential) {
|
|||||||
for i, attr := range attrSlice {
|
for i, attr := range attrSlice {
|
||||||
resultAttr := &result.Attributes[i]
|
resultAttr := &result.Attributes[i]
|
||||||
resultAttr.Keyword = utf16PtrToString(attr.Keyword)
|
resultAttr.Keyword = utf16PtrToString(attr.Keyword)
|
||||||
resultAttr.Value = C.GoBytes(unsafe.Pointer(attr.Value), C.int(attr.ValueSize))
|
resultAttr.Value = goBytes(unsafe.Pointer(attr.Value), attr.ValueSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert the given Credential object back to a CREDENTIAL struct, which can be used for calling the
|
// Convert the given Credential object back to a CREDENTIAL struct, which can be used for calling the
|
||||||
// Windows APIs
|
// Windows APIs
|
||||||
func nativeFromCredential(cred *Credential) (result *nativeCREDENTIAL) {
|
func nativeFromCredential(cred *Credential) (result *nativeCREDENTIAL) {
|
||||||
|
if cred == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
result = new(nativeCREDENTIAL)
|
result = new(nativeCREDENTIAL)
|
||||||
result.Flags = 0
|
result.Flags = 0
|
||||||
result.Type = 0
|
result.Type = 0
|
||||||
|
|||||||
+42
-4
@@ -8,12 +8,18 @@ import (
|
|||||||
var (
|
var (
|
||||||
modadvapi32 = syscall.NewLazyDLL("advapi32.dll")
|
modadvapi32 = syscall.NewLazyDLL("advapi32.dll")
|
||||||
|
|
||||||
procCredRead = modadvapi32.NewProc("CredReadW")
|
procCredRead proc = modadvapi32.NewProc("CredReadW")
|
||||||
procCredWrite = modadvapi32.NewProc("CredWriteW")
|
procCredWrite proc = modadvapi32.NewProc("CredWriteW")
|
||||||
procCredDelete = modadvapi32.NewProc("CredDeleteW")
|
procCredDelete proc = modadvapi32.NewProc("CredDeleteW")
|
||||||
procCredFree = modadvapi32.NewProc("CredFree")
|
procCredFree proc = modadvapi32.NewProc("CredFree")
|
||||||
|
procCredEnumerate proc = modadvapi32.NewProc("CredEnumerateW")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Interface for syscall.Proc: helps testing
|
||||||
|
type proc interface {
|
||||||
|
Call(a ...uintptr) (r1, r2 uintptr, lastErr error)
|
||||||
|
}
|
||||||
|
|
||||||
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa374788(v=vs.85).aspx
|
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa374788(v=vs.85).aspx
|
||||||
type nativeCREDENTIAL struct {
|
type nativeCREDENTIAL struct {
|
||||||
Flags uint32
|
Flags uint32
|
||||||
@@ -48,6 +54,8 @@ const (
|
|||||||
naCRED_TYPE_DOMAIN_VISIBLE_PASSWORD nativeCRED_TYPE = 0x4
|
naCRED_TYPE_DOMAIN_VISIBLE_PASSWORD nativeCRED_TYPE = 0x4
|
||||||
naCRED_TYPE_GENERIC_CERTIFICATE nativeCRED_TYPE = 0x5
|
naCRED_TYPE_GENERIC_CERTIFICATE nativeCRED_TYPE = 0x5
|
||||||
naCRED_TYPE_DOMAIN_EXTENDED nativeCRED_TYPE = 0x6
|
naCRED_TYPE_DOMAIN_EXTENDED nativeCRED_TYPE = 0x6
|
||||||
|
|
||||||
|
naERROR_NOT_FOUND = "Element not found."
|
||||||
)
|
)
|
||||||
|
|
||||||
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa374804(v=vs.85).aspx
|
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa374804(v=vs.85).aspx
|
||||||
@@ -97,3 +105,33 @@ func nativeCredDelete(cred *Credential, typ nativeCRED_TYPE) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa374794(v=vs.85).aspx
|
||||||
|
func nativeCredEnumerate(filter string, all bool) ([]*Credential, error) {
|
||||||
|
var count int
|
||||||
|
var pcreds uintptr
|
||||||
|
var filterPtr uintptr
|
||||||
|
if !all {
|
||||||
|
filterUtf16Ptr, _ := syscall.UTF16PtrFromString(filter)
|
||||||
|
filterPtr = uintptr(unsafe.Pointer(filterUtf16Ptr))
|
||||||
|
} else {
|
||||||
|
filterPtr = 0
|
||||||
|
}
|
||||||
|
ret, _, err := procCredEnumerate.Call(
|
||||||
|
filterPtr,
|
||||||
|
0,
|
||||||
|
uintptr(unsafe.Pointer(&count)),
|
||||||
|
uintptr(unsafe.Pointer(&pcreds)),
|
||||||
|
)
|
||||||
|
if ret == 0 {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer procCredFree.Call(pcreds)
|
||||||
|
pcredsSlice := (*[1 << 30]uintptr)(unsafe.Pointer(pcreds))[:count:count]
|
||||||
|
creds := make([]*Credential, count)
|
||||||
|
for i := range creds {
|
||||||
|
creds[i] = nativeToCredential((*nativeCREDENTIAL)(unsafe.Pointer(pcredsSlice[i])))
|
||||||
|
}
|
||||||
|
|
||||||
|
return creds, nil
|
||||||
|
}
|
||||||
|
|||||||
+11
@@ -67,3 +67,14 @@ func (t *DomainPassword) Delete() (err error) {
|
|||||||
func (t *DomainPassword) SetPassword(pw string) {
|
func (t *DomainPassword) SetPassword(pw string) {
|
||||||
t.CredentialBlob = utf16ToByte(syscall.StringToUTF16(pw))
|
t.CredentialBlob = utf16ToByte(syscall.StringToUTF16(pw))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// List the contents of the Credentials store
|
||||||
|
func List() ([]*Credential, error) {
|
||||||
|
creds, err := nativeCredEnumerate("", true)
|
||||||
|
if err != nil && err.Error() == naERROR_NOT_FOUND {
|
||||||
|
// Ignore ERROR_NOT_FOUND and return an empty list instead
|
||||||
|
creds = []*Credential{}
|
||||||
|
err = nil
|
||||||
|
}
|
||||||
|
return creds, 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,5 +40,35 @@ 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 for a given credentials label.
|
||||||
|
func (h Wincred) List() (map[string]string, error) {
|
||||||
|
creds, err := winc.List()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
resp := make(map[string]string)
|
||||||
|
for i := range creds {
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package wincred
|
package wincred
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/docker/docker-credential-helpers/credentials"
|
"github.com/docker/docker-credential-helpers/credentials"
|
||||||
@@ -12,8 +13,38 @@ func TestWinCredHelper(t *testing.T) {
|
|||||||
Username: "foobar",
|
Username: "foobar",
|
||||||
Secret: "foobarbaz",
|
Secret: "foobarbaz",
|
||||||
}
|
}
|
||||||
|
creds1 := &credentials.Credentials{
|
||||||
|
ServerURL: "https://foobar.docker.io:2376/v2",
|
||||||
|
Username: "foobarbaz",
|
||||||
|
Secret: "foobar",
|
||||||
|
}
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
@@ -31,6 +62,25 @@ 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()
|
||||||
|
if err != nil || len(auths)-len(oldauths) != 1 {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
helper.Add(creds1)
|
||||||
|
defer helper.Delete(creds1.ServerURL)
|
||||||
|
newauths, err := helper.List()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(newauths)-len(auths) != 1 {
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("Error: len(newauths): %d, len(auths): %d", len(newauths), len(auths))
|
||||||
|
}
|
||||||
|
t.Fatalf("Error: len(newauths): %d, len(auths): %d\n Error= %v", len(newauths), len(auths), err)
|
||||||
|
}
|
||||||
|
|
||||||
if err := helper.Delete(creds.ServerURL); err != nil {
|
if err := helper.Delete(creds.ServerURL); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user