mirror of
https://github.com/docker/docker-credential-helpers.git
synced 2026-06-28 07:11:36 +05:30
Compare commits
23 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fc9290adbc | |||
| 8369960895 | |||
| e4a625d24b | |||
| f552261f32 | |||
| 7a2694fc98 | |||
| 4b9fe97b79 | |||
| 0232333efa | |||
| 38bea2ce27 | |||
| c9a35c136e | |||
| 431b64c703 | |||
| 951b97d7f5 | |||
| f7d32862eb | |||
| 699f1d6790 | |||
| f4b8a8531e | |||
| f76f0b3f33 | |||
| f78081d1f7 | |||
| 4e7d4a98a4 | |||
| 43d1f2919f | |||
| 7b072d4d2e | |||
| c0c41f47b2 | |||
| 69fb197018 | |||
| 86b653a9f1 | |||
| 88f932172b |
+1
-27
@@ -19,32 +19,6 @@
|
||||
before_script:
|
||||
- make deps
|
||||
- "export DISPLAY=:99.0"
|
||||
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sh ci/before_script_linux.sh; fi
|
||||
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sh ci/before_script_linux.sh; fi
|
||||
- make validate
|
||||
script: make test
|
||||
|
||||
before_deploy:
|
||||
- sh ci/before_deploy.sh
|
||||
|
||||
deploy:
|
||||
provider: releases
|
||||
api_key:
|
||||
secure: "$GITHUB_TOKEN"
|
||||
# upload file artifacts using a glob expression.
|
||||
# It requires both options `file_glob` and `file`:
|
||||
# https://github.com/travis-ci/dpl/blob/master/lib/dpl/provider/releases.rb#L47-L53
|
||||
file_glob: true
|
||||
file: docker-credential-*-${TRAVIS_TAG}-amd64.tar.gz
|
||||
# don't delete the artifacts from previous phases
|
||||
skip_cleanup: true
|
||||
# deploy when a new tag is pushed
|
||||
on:
|
||||
tags: true
|
||||
|
||||
branches:
|
||||
only:
|
||||
# Pushes and PR to the master branch
|
||||
- master
|
||||
# IMPORTANT Ruby regex to match tags. Required, or travis won't trigger deploys when a new tag
|
||||
# is pushed. This regex matches semantic versions like v1.2.3-rc4+2016.02.22
|
||||
- /^v\d+\.\d+\.\d+.*$/
|
||||
|
||||
Vendored
+6
-4
@@ -28,7 +28,8 @@ pipeline {
|
||||
sh 'apt-get update && apt-get install -y libsecret-1-dev pass'
|
||||
sh 'make deps fmt lint test'
|
||||
sh 'make pass secretservice'
|
||||
archiveArtifacts 'bin/docker-credential-*'
|
||||
sh 'make linuxrelease'
|
||||
archiveArtifacts 'release/docker-credential-*'
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -45,7 +46,8 @@ pipeline {
|
||||
dir('src/github.com/docker/docker-credential-helpers') {
|
||||
sh 'make deps fmt lint test'
|
||||
sh 'make osxcodesign'
|
||||
archiveArtifacts 'bin/docker-credential-*'
|
||||
sh 'make osxrelease'
|
||||
archiveArtifacts 'release/docker-credential-*'
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -56,8 +58,8 @@ pipeline {
|
||||
environment {
|
||||
GOPATH = pwd()
|
||||
PATH = "${pwd()}/bin;$PATH"
|
||||
PFX = credentials('windows-build-pfx-sanitize')
|
||||
PFXPASSWORD = credentials('windows-build-pfx-password')
|
||||
PFX = credentials('windows-build-2019-pfx')
|
||||
PFXPASSWORD = credentials('windows-build-2019-pfx-password')
|
||||
}
|
||||
steps {
|
||||
dir('src/github.com/docker/docker-credential-helpers') {
|
||||
|
||||
@@ -21,26 +21,29 @@ osxcodesign: osxkeychain
|
||||
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:
|
||||
mkdir -p bin
|
||||
go build -o bin/docker-credential-secretservice secretservice/cmd/main_linux.go
|
||||
|
||||
pass:
|
||||
mkdir -p bin
|
||||
go build -o bin/docker-credential-pass pass/cmd/main_linux.go
|
||||
go build -o bin/docker-credential-pass pass/cmd/main.go
|
||||
|
||||
wincred:
|
||||
mkdir -p bin
|
||||
go build -o bin/docker-credential-wincred.exe wincred/cmd/main_windows.go
|
||||
|
||||
winrelease: clean vet_win lint fmt test wincred
|
||||
linuxrelease:
|
||||
mkdir -p release
|
||||
cd bin && tar cvfz ../release/docker-credential-pass-v$(VERSION)-amd64.tar.gz docker-credential-pass
|
||||
cd bin && tar cvfz ../release/docker-credential-secretservice-v$(VERSION)-amd64.tar.gz docker-credential-secretservice
|
||||
|
||||
osxrelease:
|
||||
mkdir -p release
|
||||
cd bin && tar cvfz ../release/docker-credential-osxkeychain-v$(VERSION)-amd64.tar.gz docker-credential-osxkeychain
|
||||
|
||||
winrelease:
|
||||
mkdir -p release
|
||||
@echo "\nPackaging version ${VERSION}\n"
|
||||
cd bin && zip ../release/docker-credential-wincred-v$(VERSION)-amd64.zip docker-credential-wincred.exe
|
||||
|
||||
test:
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
image: Visual Studio 2015
|
||||
environment:
|
||||
GOPATH: c:\gopath
|
||||
|
||||
stack: go 1.8.7
|
||||
|
||||
clone_folder: c:\gopath\src\github.com\docker\docker-credential-helpers
|
||||
clone_depth: 10
|
||||
|
||||
before_build:
|
||||
- set PATH=%PATH%;C:\MinGW\bin;
|
||||
|
||||
build_script:
|
||||
- mingw32-make vet_win wincred
|
||||
|
||||
test_script:
|
||||
- mingw32-make test
|
||||
|
||||
deploy: off
|
||||
|
||||
artifacts:
|
||||
- path: bin/docker-credential-wincred.exe
|
||||
|
||||
configuration: Release
|
||||
@@ -1,15 +0,0 @@
|
||||
set -ex
|
||||
|
||||
mkdir bin
|
||||
case "$TRAVIS_OS_NAME" in
|
||||
"osx")
|
||||
go build -o bin/docker-credential-osxkeychain osxkeychain/cmd/main_darwin.go
|
||||
cd bin
|
||||
tar czf ../docker-credential-osxkeychain-${TRAVIS_TAG}-amd64.tar.gz docker-credential-osxkeychain
|
||||
;;
|
||||
"linux")
|
||||
go build -o bin/docker-credential-secretservice secretservice/cmd/main_linux.go
|
||||
cd bin
|
||||
tar czf ../docker-credential-secretservice-${TRAVIS_TAG}-amd64.tar.gz docker-credential-secretservice
|
||||
;;
|
||||
esac
|
||||
+2
-1
@@ -4,7 +4,8 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
exec "golang.org/x/sys/execabs"
|
||||
)
|
||||
|
||||
// Program is an interface to execute external programs.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package credentials
|
||||
|
||||
// Version holds a string describing the current version
|
||||
const Version = "0.6.3"
|
||||
const Version = "0.6.4"
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
module github.com/docker/docker-credential-helpers
|
||||
|
||||
go 1.13
|
||||
|
||||
require (
|
||||
github.com/danieljoos/wincred v1.1.0
|
||||
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4
|
||||
)
|
||||
@@ -0,0 +1,16 @@
|
||||
github.com/danieljoos/wincred v1.1.0 h1:3RNcEpBg4IhIChZdFRSdlQt1QjCp1sMAPIrOnm7Yf8g=
|
||||
github.com/danieljoos/wincred v1.1.0/go.mod h1:XYlo+eRTsVA9aHGp7NGjFkPla4m+DCL7hqDjlFjiygg=
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
|
||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 h1:myAQVi0cGEoqQVR5POX+8RR2mrocKqNN1hmeMqhX27k=
|
||||
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
@@ -21,6 +21,13 @@ import (
|
||||
// when the credentials are not in the keychain.
|
||||
const errCredentialsNotFound = "The specified item could not be found in the keychain."
|
||||
|
||||
// errCredentialsNotFound is the specific error message returned by OS X
|
||||
// when environment does not allow showing dialog to unlock keychain.
|
||||
const errInteractionNotAllowed = "User interaction is not allowed."
|
||||
|
||||
// ErrInteractionNotAllowed is returned if keychain password prompt can not be shown.
|
||||
var ErrInteractionNotAllowed = errors.New(`keychain cannot be accessed because the current session does not allow user interaction. The keychain may be locked; unlock it by running "security -v unlock-keychain ~/Library/Keychains/login.keychain-db" and try again`)
|
||||
|
||||
// Osxkeychain handles secrets using the OS X Keychain as store.
|
||||
type Osxkeychain struct{}
|
||||
|
||||
@@ -89,6 +96,9 @@ func (h Osxkeychain) Get(serverURL string) (string, string, error) {
|
||||
if goMsg == errCredentialsNotFound {
|
||||
return "", "", credentials.NewErrCredentialsNotFound()
|
||||
}
|
||||
if goMsg == errInteractionNotAllowed {
|
||||
return "", "", ErrInteractionNotAllowed
|
||||
}
|
||||
|
||||
return "", "", errors.New(goMsg)
|
||||
}
|
||||
@@ -117,6 +127,9 @@ func (h Osxkeychain) List() (map[string]string, error) {
|
||||
if goMsg == errCredentialsNotFound {
|
||||
return make(map[string]string), nil
|
||||
}
|
||||
if goMsg == errInteractionNotAllowed {
|
||||
return nil, ErrInteractionNotAllowed
|
||||
}
|
||||
|
||||
return nil, errors.New(goMsg)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// A `pass` based credential helper. Passwords are stored as arguments to pass
|
||||
// of the form: "$PASS_FOLDER/base64-url(serverURL)/username". We base64-url
|
||||
// encode the serverURL, because under the hood pass uses files and folders, so
|
||||
// /s will get translated into additional folders.
|
||||
// Package pass implements a `pass` based credential helper. Passwords are stored
|
||||
// as arguments to pass of the form: "$PASS_FOLDER/base64-url(serverURL)/username".
|
||||
// We base64-url encode the serverURL, because under the hood pass uses files and
|
||||
// folders, so /s will get translated into additional folders.
|
||||
package pass
|
||||
|
||||
import (
|
||||
@@ -19,7 +19,8 @@ import (
|
||||
"github.com/docker/docker-credential-helpers/credentials"
|
||||
)
|
||||
|
||||
const PASS_FOLDER = "docker-credential-helpers"
|
||||
// PASS_FOLDER contains the directory where credentials are stored
|
||||
const PASS_FOLDER = "docker-credential-helpers" //nolint: golint
|
||||
|
||||
// Pass handles secrets using Linux secret-service as a store.
|
||||
type Pass struct{}
|
||||
@@ -79,25 +80,25 @@ func (p Pass) runPassHelper(stdinContent string, args ...string) (string, error)
|
||||
}
|
||||
|
||||
// Add adds new credentials to the keychain.
|
||||
func (h Pass) Add(creds *credentials.Credentials) error {
|
||||
func (p Pass) Add(creds *credentials.Credentials) error {
|
||||
if creds == nil {
|
||||
return errors.New("missing credentials")
|
||||
}
|
||||
|
||||
encoded := base64.URLEncoding.EncodeToString([]byte(creds.ServerURL))
|
||||
|
||||
_, err := h.runPass(creds.Secret, "insert", "-f", "-m", path.Join(PASS_FOLDER, encoded, creds.Username))
|
||||
_, err := p.runPass(creds.Secret, "insert", "-f", "-m", path.Join(PASS_FOLDER, encoded, creds.Username))
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete removes credentials from the store.
|
||||
func (h Pass) Delete(serverURL string) error {
|
||||
func (p Pass) Delete(serverURL string) error {
|
||||
if serverURL == "" {
|
||||
return errors.New("missing server url")
|
||||
}
|
||||
|
||||
encoded := base64.URLEncoding.EncodeToString([]byte(serverURL))
|
||||
_, err := h.runPass("", "rm", "-rf", path.Join(PASS_FOLDER, encoded))
|
||||
_, err := p.runPass("", "rm", "-rf", path.Join(PASS_FOLDER, encoded))
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -128,7 +129,7 @@ func listPassDir(args ...string) ([]os.FileInfo, error) {
|
||||
}
|
||||
|
||||
// Get returns the username and secret to use for a given registry server URL.
|
||||
func (h Pass) Get(serverURL string) (string, string, error) {
|
||||
func (p Pass) Get(serverURL string) (string, string, error) {
|
||||
if serverURL == "" {
|
||||
return "", "", errors.New("missing server url")
|
||||
}
|
||||
@@ -153,12 +154,12 @@ func (h Pass) Get(serverURL string) (string, string, error) {
|
||||
}
|
||||
|
||||
actual := strings.TrimSuffix(usernames[0].Name(), ".gpg")
|
||||
secret, err := h.runPass("", "show", path.Join(PASS_FOLDER, encoded, actual))
|
||||
secret, err := p.runPass("", "show", path.Join(PASS_FOLDER, encoded, actual))
|
||||
return actual, secret, err
|
||||
}
|
||||
|
||||
// List returns the stored URLs and corresponding usernames for a given credentials label
|
||||
func (h Pass) List() (map[string]string, error) {
|
||||
func (p Pass) List() (map[string]string, error) {
|
||||
servers, err := listPassDir()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -6,10 +6,12 @@ import (
|
||||
url "net/url"
|
||||
)
|
||||
|
||||
// GetHostname returns the hostname of the URL
|
||||
func GetHostname(u *url.URL) string {
|
||||
return u.Hostname()
|
||||
}
|
||||
|
||||
// GetPort returns the port number of the URL
|
||||
func GetPort(u *url.URL) string {
|
||||
return u.Port()
|
||||
}
|
||||
|
||||
@@ -19,14 +19,14 @@ func TestSecretServiceHelper(t *testing.T) {
|
||||
helper := Secretservice{}
|
||||
|
||||
// Check how many docker credentials we have when starting the test
|
||||
old_auths, err := helper.List()
|
||||
oldAuths, 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 {
|
||||
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 {
|
||||
@@ -36,7 +36,7 @@ func TestSecretServiceHelper(t *testing.T) {
|
||||
}
|
||||
|
||||
// Check again how many docker credentials we have when starting the test
|
||||
old_auths, err = helper.List()
|
||||
oldAuths, err = helper.List()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -61,11 +61,11 @@ func TestSecretServiceHelper(t *testing.T) {
|
||||
}
|
||||
|
||||
// We should have one more credential than before adding
|
||||
new_auths, err := helper.List()
|
||||
if err != nil || (len(new_auths)-len(old_auths) != 1) {
|
||||
newAuths, err := helper.List()
|
||||
if err != nil || (len(newAuths)-len(oldAuths) != 1) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
old_auths = new_auths
|
||||
oldAuths = newAuths
|
||||
|
||||
// Deleting the credentials associated to current server url should succeed
|
||||
if err := helper.Delete(creds.ServerURL); err != nil {
|
||||
@@ -73,8 +73,8 @@ func TestSecretServiceHelper(t *testing.T) {
|
||||
}
|
||||
|
||||
// We should have one less credential than before deleting
|
||||
new_auths, err = helper.List()
|
||||
if err != nil || (len(old_auths)-len(new_auths) != 1) {
|
||||
newAuths, err = helper.List()
|
||||
if err != nil || (len(oldAuths)-len(newAuths) != 1) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
*.go text eol=lf
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||
*.o
|
||||
*.a
|
||||
*.so
|
||||
|
||||
# Folders
|
||||
_obj
|
||||
_test
|
||||
|
||||
# Architecture specific extensions/prefixes
|
||||
*.[568vq]
|
||||
[568vq].out
|
||||
|
||||
*.cgo1.go
|
||||
*.cgo2.c
|
||||
_cgo_defun.c
|
||||
_cgo_gotypes.go
|
||||
_cgo_export.*
|
||||
|
||||
_testmain.go
|
||||
|
||||
*.exe
|
||||
*.test
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
wincred
|
||||
=======
|
||||
|
||||
Go wrapper around the Windows Credential Manager API functions.
|
||||
|
||||

|
||||
[](https://godoc.org/github.com/danieljoos/wincred)
|
||||
|
||||
|
||||
Installation
|
||||
------------
|
||||
|
||||
```Go
|
||||
go get github.com/danieljoos/wincred
|
||||
```
|
||||
|
||||
|
||||
Usage
|
||||
-----
|
||||
|
||||
See the following examples:
|
||||
|
||||
### Create and store a new generic credential object
|
||||
```Go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/danieljoos/wincred"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cred := wincred.NewGenericCredential("myGoApplication")
|
||||
cred.CredentialBlob = []byte("my secret")
|
||||
err := cred.Write()
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Retrieve a credential object
|
||||
```Go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/danieljoos/wincred"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cred, err := wincred.GetGenericCredential("myGoApplication")
|
||||
if err == nil {
|
||||
fmt.Println(string(cred.CredentialBlob))
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Remove a credential object
|
||||
```Go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/danieljoos/wincred"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cred, err := wincred.GetGenericCredential("myGoApplication")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
cred.Delete()
|
||||
}
|
||||
```
|
||||
|
||||
### List all available credentials
|
||||
```Go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/danieljoos/wincred"
|
||||
)
|
||||
|
||||
func main() {
|
||||
creds, err := wincred.List()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
for i := range(creds) {
|
||||
fmt.Println(creds[i].TargetName)
|
||||
}
|
||||
}
|
||||
```
|
||||
+40
-30
@@ -1,3 +1,5 @@
|
||||
// +build windows
|
||||
|
||||
package wincred
|
||||
|
||||
import (
|
||||
@@ -9,48 +11,57 @@ import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
var nullPointer = unsafe.Pointer(uintptr(0))
|
||||
|
||||
// Create a Go string using a pointer to a zero-terminated UTF 16 encoded string.
|
||||
// See github.com/AllenDang/w32
|
||||
// uf16PtrToString creates a Go string from a pointer to a UTF16 encoded zero-terminated string.
|
||||
// Such pointers are returned from the Windows API calls.
|
||||
// The function creates a copy of the string.
|
||||
func utf16PtrToString(wstr *uint16) string {
|
||||
if wstr != nil {
|
||||
buf := make([]uint16, 0, 256)
|
||||
for ptr := uintptr(unsafe.Pointer(wstr)); ; ptr += 2 {
|
||||
rune := *(*uint16)(unsafe.Pointer(ptr))
|
||||
if rune == 0 {
|
||||
return string(utf16.Decode(buf))
|
||||
for len := 0; ; len++ {
|
||||
ptr := unsafe.Pointer(uintptr(unsafe.Pointer(wstr)) + uintptr(len)*unsafe.Sizeof(*wstr)) // see https://golang.org/pkg/unsafe/#Pointer (3)
|
||||
if *(*uint16)(ptr) == 0 {
|
||||
return string(utf16.Decode(*(*[]uint16)(unsafe.Pointer(&reflect.SliceHeader{
|
||||
Data: uintptr(unsafe.Pointer(wstr)),
|
||||
Len: len,
|
||||
Cap: len,
|
||||
}))))
|
||||
}
|
||||
buf = append(buf, rune)
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// Create a byte array from a given UTF 16 char array
|
||||
// utf16ToByte creates a byte array from a given UTF 16 char array.
|
||||
func utf16ToByte(wstr []uint16) (result []byte) {
|
||||
result = make([]byte, len(wstr)*2)
|
||||
for i, _ := range wstr {
|
||||
for i := range wstr {
|
||||
binary.LittleEndian.PutUint16(result[(i*2):(i*2)+2], wstr[i])
|
||||
}
|
||||
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 {
|
||||
// utf16FromString creates a UTF16 char array from a string.
|
||||
func utf16FromString(str string) []uint16 {
|
||||
return syscall.StringToUTF16(str)
|
||||
}
|
||||
|
||||
// goBytes copies the given C byte array to a Go byte array (see `C.GoBytes`).
|
||||
// This function avoids having cgo as dependency.
|
||||
func goBytes(src uintptr, len uint32) []byte {
|
||||
if src == uintptr(0) {
|
||||
return []byte{}
|
||||
}
|
||||
slice := (*[1 << 30]byte)(src)[0:len]
|
||||
rv := make([]byte, len)
|
||||
copy(rv, slice)
|
||||
return rv[:]
|
||||
copy(rv, *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
||||
Data: src,
|
||||
Len: int(len),
|
||||
Cap: int(len),
|
||||
})))
|
||||
return rv
|
||||
}
|
||||
|
||||
// Convert the given CREDENTIAL struct to a more usable structure
|
||||
func nativeToCredential(cred *nativeCREDENTIAL) (result *Credential) {
|
||||
if unsafe.Pointer(cred) == nullPointer {
|
||||
func sysToCredential(cred *sysCREDENTIAL) (result *Credential) {
|
||||
if cred == nil {
|
||||
return nil
|
||||
}
|
||||
result = new(Credential)
|
||||
@@ -60,29 +71,28 @@ func nativeToCredential(cred *nativeCREDENTIAL) (result *Credential) {
|
||||
result.UserName = utf16PtrToString(cred.UserName)
|
||||
result.LastWritten = time.Unix(0, cred.LastWritten.Nanoseconds())
|
||||
result.Persist = CredentialPersistence(cred.Persist)
|
||||
result.CredentialBlob = goBytes(unsafe.Pointer(cred.CredentialBlob), cred.CredentialBlobSize)
|
||||
result.CredentialBlob = goBytes(cred.CredentialBlob, cred.CredentialBlobSize)
|
||||
result.Attributes = make([]CredentialAttribute, cred.AttributeCount)
|
||||
attrSliceHeader := reflect.SliceHeader{
|
||||
attrSlice := *(*[]sysCREDENTIAL_ATTRIBUTE)(unsafe.Pointer(&reflect.SliceHeader{
|
||||
Data: cred.Attributes,
|
||||
Len: int(cred.AttributeCount),
|
||||
Cap: int(cred.AttributeCount),
|
||||
}
|
||||
attrSlice := *(*[]nativeCREDENTIAL_ATTRIBUTE)(unsafe.Pointer(&attrSliceHeader))
|
||||
}))
|
||||
for i, attr := range attrSlice {
|
||||
resultAttr := &result.Attributes[i]
|
||||
resultAttr.Keyword = utf16PtrToString(attr.Keyword)
|
||||
resultAttr.Value = goBytes(unsafe.Pointer(attr.Value), attr.ValueSize)
|
||||
resultAttr.Value = goBytes(attr.Value, attr.ValueSize)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Convert the given Credential object back to a CREDENTIAL struct, which can be used for calling the
|
||||
// Windows APIs
|
||||
func nativeFromCredential(cred *Credential) (result *nativeCREDENTIAL) {
|
||||
func sysFromCredential(cred *Credential) (result *sysCREDENTIAL) {
|
||||
if cred == nil {
|
||||
return nil
|
||||
}
|
||||
result = new(nativeCREDENTIAL)
|
||||
result = new(sysCREDENTIAL)
|
||||
result.Flags = 0
|
||||
result.Type = 0
|
||||
result.TargetName, _ = syscall.UTF16PtrFromString(cred.TargetName)
|
||||
@@ -96,13 +106,13 @@ func nativeFromCredential(cred *Credential) (result *nativeCREDENTIAL) {
|
||||
}
|
||||
result.Persist = uint32(cred.Persist)
|
||||
result.AttributeCount = uint32(len(cred.Attributes))
|
||||
attributes := make([]nativeCREDENTIAL_ATTRIBUTE, len(cred.Attributes))
|
||||
attributes := make([]sysCREDENTIAL_ATTRIBUTE, len(cred.Attributes))
|
||||
if len(attributes) > 0 {
|
||||
result.Attributes = uintptr(unsafe.Pointer(&attributes[0]))
|
||||
} else {
|
||||
result.Attributes = 0
|
||||
}
|
||||
for i, _ := range cred.Attributes {
|
||||
for i := range cred.Attributes {
|
||||
inAttr := &cred.Attributes[i]
|
||||
outAttr := &attributes[i]
|
||||
outAttr.Keyword, _ = syscall.UTF16PtrFromString(inAttr.Keyword)
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// +build !windows
|
||||
|
||||
package wincred
|
||||
|
||||
func utf16ToByte(...interface{}) []byte {
|
||||
return nil
|
||||
}
|
||||
|
||||
func utf16FromString(...interface{}) []uint16 {
|
||||
return nil
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
module github.com/danieljoos/wincred
|
||||
|
||||
go 1.13
|
||||
|
||||
require github.com/stretchr/testify v1.5.1
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
|
||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
-137
@@ -1,137 +0,0 @@
|
||||
package wincred
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
var (
|
||||
modadvapi32 = syscall.NewLazyDLL("advapi32.dll")
|
||||
|
||||
procCredRead proc = modadvapi32.NewProc("CredReadW")
|
||||
procCredWrite proc = modadvapi32.NewProc("CredWriteW")
|
||||
procCredDelete proc = modadvapi32.NewProc("CredDeleteW")
|
||||
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
|
||||
type nativeCREDENTIAL struct {
|
||||
Flags uint32
|
||||
Type uint32
|
||||
TargetName *uint16
|
||||
Comment *uint16
|
||||
LastWritten syscall.Filetime
|
||||
CredentialBlobSize uint32
|
||||
CredentialBlob uintptr
|
||||
Persist uint32
|
||||
AttributeCount uint32
|
||||
Attributes uintptr
|
||||
TargetAlias *uint16
|
||||
UserName *uint16
|
||||
}
|
||||
|
||||
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa374790(v=vs.85).aspx
|
||||
type nativeCREDENTIAL_ATTRIBUTE struct {
|
||||
Keyword *uint16
|
||||
Flags uint32
|
||||
ValueSize uint32
|
||||
Value uintptr
|
||||
}
|
||||
|
||||
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa374788(v=vs.85).aspx
|
||||
type nativeCRED_TYPE uint32
|
||||
|
||||
const (
|
||||
naCRED_TYPE_GENERIC nativeCRED_TYPE = 0x1
|
||||
naCRED_TYPE_DOMAIN_PASSWORD nativeCRED_TYPE = 0x2
|
||||
naCRED_TYPE_DOMAIN_CERTIFICATE nativeCRED_TYPE = 0x3
|
||||
naCRED_TYPE_DOMAIN_VISIBLE_PASSWORD nativeCRED_TYPE = 0x4
|
||||
naCRED_TYPE_GENERIC_CERTIFICATE nativeCRED_TYPE = 0x5
|
||||
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
|
||||
func nativeCredRead(targetName string, typ nativeCRED_TYPE) (*Credential, error) {
|
||||
var pcred uintptr
|
||||
targetNamePtr, _ := syscall.UTF16PtrFromString(targetName)
|
||||
ret, _, err := procCredRead.Call(
|
||||
uintptr(unsafe.Pointer(targetNamePtr)),
|
||||
uintptr(typ),
|
||||
0,
|
||||
uintptr(unsafe.Pointer(&pcred)),
|
||||
)
|
||||
if ret == 0 {
|
||||
return nil, err
|
||||
}
|
||||
defer procCredFree.Call(pcred)
|
||||
|
||||
return nativeToCredential((*nativeCREDENTIAL)(unsafe.Pointer(pcred))), nil
|
||||
}
|
||||
|
||||
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa375187(v=vs.85).aspx
|
||||
func nativeCredWrite(cred *Credential, typ nativeCRED_TYPE) error {
|
||||
ncred := nativeFromCredential(cred)
|
||||
ncred.Type = uint32(typ)
|
||||
ret, _, err := procCredWrite.Call(
|
||||
uintptr(unsafe.Pointer(ncred)),
|
||||
0,
|
||||
)
|
||||
if ret == 0 {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa374787(v=vs.85).aspx
|
||||
func nativeCredDelete(cred *Credential, typ nativeCRED_TYPE) error {
|
||||
targetNamePtr, _ := syscall.UTF16PtrFromString(cred.TargetName)
|
||||
ret, _, err := procCredDelete.Call(
|
||||
uintptr(unsafe.Pointer(targetNamePtr)),
|
||||
uintptr(typ),
|
||||
0,
|
||||
)
|
||||
if ret == 0 {
|
||||
return err
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
+143
@@ -0,0 +1,143 @@
|
||||
// +build windows
|
||||
|
||||
package wincred
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
var (
|
||||
modadvapi32 = syscall.NewLazyDLL("advapi32.dll")
|
||||
|
||||
procCredRead proc = modadvapi32.NewProc("CredReadW")
|
||||
procCredWrite proc = modadvapi32.NewProc("CredWriteW")
|
||||
procCredDelete proc = modadvapi32.NewProc("CredDeleteW")
|
||||
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)
|
||||
}
|
||||
|
||||
// https://docs.microsoft.com/en-us/windows/desktop/api/wincred/ns-wincred-_credentialw
|
||||
type sysCREDENTIAL struct {
|
||||
Flags uint32
|
||||
Type uint32
|
||||
TargetName *uint16
|
||||
Comment *uint16
|
||||
LastWritten syscall.Filetime
|
||||
CredentialBlobSize uint32
|
||||
CredentialBlob uintptr
|
||||
Persist uint32
|
||||
AttributeCount uint32
|
||||
Attributes uintptr
|
||||
TargetAlias *uint16
|
||||
UserName *uint16
|
||||
}
|
||||
|
||||
// https://docs.microsoft.com/en-us/windows/desktop/api/wincred/ns-wincred-_credential_attributew
|
||||
type sysCREDENTIAL_ATTRIBUTE struct {
|
||||
Keyword *uint16
|
||||
Flags uint32
|
||||
ValueSize uint32
|
||||
Value uintptr
|
||||
}
|
||||
|
||||
// https://docs.microsoft.com/en-us/windows/desktop/api/wincred/ns-wincred-_credentialw
|
||||
type sysCRED_TYPE uint32
|
||||
|
||||
const (
|
||||
sysCRED_TYPE_GENERIC sysCRED_TYPE = 0x1
|
||||
sysCRED_TYPE_DOMAIN_PASSWORD sysCRED_TYPE = 0x2
|
||||
sysCRED_TYPE_DOMAIN_CERTIFICATE sysCRED_TYPE = 0x3
|
||||
sysCRED_TYPE_DOMAIN_VISIBLE_PASSWORD sysCRED_TYPE = 0x4
|
||||
sysCRED_TYPE_GENERIC_CERTIFICATE sysCRED_TYPE = 0x5
|
||||
sysCRED_TYPE_DOMAIN_EXTENDED sysCRED_TYPE = 0x6
|
||||
|
||||
// https://docs.microsoft.com/en-us/windows/desktop/Debug/system-error-codes
|
||||
sysERROR_NOT_FOUND = syscall.Errno(1168)
|
||||
sysERROR_INVALID_PARAMETER = syscall.Errno(87)
|
||||
)
|
||||
|
||||
// https://docs.microsoft.com/en-us/windows/desktop/api/wincred/nf-wincred-credreadw
|
||||
func sysCredRead(targetName string, typ sysCRED_TYPE) (*Credential, error) {
|
||||
var pcred *sysCREDENTIAL
|
||||
targetNamePtr, _ := syscall.UTF16PtrFromString(targetName)
|
||||
ret, _, err := procCredRead.Call(
|
||||
uintptr(unsafe.Pointer(targetNamePtr)),
|
||||
uintptr(typ),
|
||||
0,
|
||||
uintptr(unsafe.Pointer(&pcred)),
|
||||
)
|
||||
if ret == 0 {
|
||||
return nil, err
|
||||
}
|
||||
defer procCredFree.Call(uintptr(unsafe.Pointer(pcred)))
|
||||
|
||||
return sysToCredential(pcred), nil
|
||||
}
|
||||
|
||||
// https://docs.microsoft.com/en-us/windows/desktop/api/wincred/nf-wincred-credwritew
|
||||
func sysCredWrite(cred *Credential, typ sysCRED_TYPE) error {
|
||||
ncred := sysFromCredential(cred)
|
||||
ncred.Type = uint32(typ)
|
||||
ret, _, err := procCredWrite.Call(
|
||||
uintptr(unsafe.Pointer(ncred)),
|
||||
0,
|
||||
)
|
||||
if ret == 0 {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// https://docs.microsoft.com/en-us/windows/desktop/api/wincred/nf-wincred-creddeletew
|
||||
func sysCredDelete(cred *Credential, typ sysCRED_TYPE) error {
|
||||
targetNamePtr, _ := syscall.UTF16PtrFromString(cred.TargetName)
|
||||
ret, _, err := procCredDelete.Call(
|
||||
uintptr(unsafe.Pointer(targetNamePtr)),
|
||||
uintptr(typ),
|
||||
0,
|
||||
)
|
||||
if ret == 0 {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// https://docs.microsoft.com/en-us/windows/desktop/api/wincred/nf-wincred-credenumeratew
|
||||
func sysCredEnumerate(filter string, all bool) ([]*Credential, error) {
|
||||
var count int
|
||||
var pcreds uintptr
|
||||
var filterPtr *uint16
|
||||
if !all {
|
||||
filterPtr, _ = syscall.UTF16PtrFromString(filter)
|
||||
}
|
||||
ret, _, err := procCredEnumerate.Call(
|
||||
uintptr(unsafe.Pointer(filterPtr)),
|
||||
0,
|
||||
uintptr(unsafe.Pointer(&count)),
|
||||
uintptr(unsafe.Pointer(&pcreds)),
|
||||
)
|
||||
if ret == 0 {
|
||||
return nil, err
|
||||
}
|
||||
defer procCredFree.Call(pcreds)
|
||||
credsSlice := *(*[]*sysCREDENTIAL)(unsafe.Pointer(&reflect.SliceHeader{
|
||||
Data: pcreds,
|
||||
Len: count,
|
||||
Cap: count,
|
||||
}))
|
||||
creds := make([]*Credential, count, count)
|
||||
for i, cred := range credsSlice {
|
||||
creds[i] = sysToCredential(cred)
|
||||
}
|
||||
|
||||
return creds, nil
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
// +build !windows
|
||||
|
||||
package wincred
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
const (
|
||||
sysCRED_TYPE_GENERIC = 0
|
||||
sysCRED_TYPE_DOMAIN_PASSWORD = 0
|
||||
sysCRED_TYPE_DOMAIN_CERTIFICATE = 0
|
||||
sysCRED_TYPE_DOMAIN_VISIBLE_PASSWORD = 0
|
||||
sysCRED_TYPE_GENERIC_CERTIFICATE = 0
|
||||
sysCRED_TYPE_DOMAIN_EXTENDED = 0
|
||||
|
||||
sysERROR_NOT_FOUND = syscall.Errno(1)
|
||||
sysERROR_INVALID_PARAMETER = syscall.Errno(1)
|
||||
)
|
||||
|
||||
func sysCredRead(...interface{}) (*Credential, error) {
|
||||
return nil, errors.New("Operation not supported")
|
||||
}
|
||||
|
||||
func sysCredWrite(...interface{}) error {
|
||||
return errors.New("Operation not supported")
|
||||
}
|
||||
|
||||
func sysCredDelete(...interface{}) error {
|
||||
return errors.New("Operation not supported")
|
||||
}
|
||||
|
||||
func sysCredEnumerate(...interface{}) ([]*Credential, error) {
|
||||
return nil, errors.New("Operation not supported")
|
||||
}
|
||||
+34
-2
@@ -4,19 +4,38 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// CredentialPersistence describes one of three persistence modes of a credential.
|
||||
// A detailed description of the available modes can be found on
|
||||
// Docs: https://docs.microsoft.com/en-us/windows/desktop/api/wincred/ns-wincred-_credentialw
|
||||
type CredentialPersistence uint32
|
||||
|
||||
const (
|
||||
PersistSession CredentialPersistence = 0x1
|
||||
// PersistSession indicates that the credential only persists for the life
|
||||
// of the current Windows login session. Such a credential is not visible in
|
||||
// any other logon session, even from the same user.
|
||||
PersistSession CredentialPersistence = 0x1
|
||||
|
||||
// PersistLocalMachine indicates that the credential persists for this and
|
||||
// all subsequent logon sessions on this local machine/computer. It is
|
||||
// however not visible for logon sessions of this user on a different
|
||||
// machine.
|
||||
PersistLocalMachine CredentialPersistence = 0x2
|
||||
PersistEnterprise CredentialPersistence = 0x3
|
||||
|
||||
// PersistEnterprise indicates that the credential persists for this and all
|
||||
// subsequent logon sessions for this user. It is also visible for logon
|
||||
// sessions on different computers.
|
||||
PersistEnterprise CredentialPersistence = 0x3
|
||||
)
|
||||
|
||||
// CredentialAttribute represents an application-specific attribute of a credential.
|
||||
type CredentialAttribute struct {
|
||||
Keyword string
|
||||
Value []byte
|
||||
}
|
||||
|
||||
// Credential is the basic credential structure.
|
||||
// A credential is identified by its target name.
|
||||
// The actual credential secret is available in the CredentialBlob field.
|
||||
type Credential struct {
|
||||
TargetName string
|
||||
Comment string
|
||||
@@ -28,10 +47,23 @@ type Credential struct {
|
||||
Persist CredentialPersistence
|
||||
}
|
||||
|
||||
// GenericCredential holds a credential for generic usage.
|
||||
// It is typically defined and used by applications that need to manage user
|
||||
// secrets.
|
||||
//
|
||||
// More information about the available kinds of credentials of the Windows
|
||||
// Credential Management API can be found on Docs:
|
||||
// https://docs.microsoft.com/en-us/windows/desktop/SecAuthN/kinds-of-credentials
|
||||
type GenericCredential struct {
|
||||
Credential
|
||||
}
|
||||
|
||||
// DomainPassword holds a domain credential that is typically used by the
|
||||
// operating system for user logon.
|
||||
//
|
||||
// More information about the available kinds of credentials of the Windows
|
||||
// Credential Management API can be found on Docs:
|
||||
// https://docs.microsoft.com/en-us/windows/desktop/SecAuthN/kinds-of-credentials
|
||||
type DomainPassword struct {
|
||||
Credential
|
||||
}
|
||||
|
||||
+53
-22
@@ -1,19 +1,36 @@
|
||||
// Package wincred provides primitives for accessing the Windows Credentials Management API.
|
||||
// This includes functions for retrieval, listing and storage of credentials as well as Go structures for convenient access to the credential data.
|
||||
//
|
||||
// A more detailed description of Windows Credentials Management can be found on
|
||||
// Docs: https://docs.microsoft.com/en-us/windows/desktop/SecAuthN/credentials-management
|
||||
package wincred
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
import "errors"
|
||||
|
||||
const (
|
||||
// ErrElementNotFound is the error that is returned if a requested element cannot be found.
|
||||
// This error constant can be used to check if a credential could not be found.
|
||||
ErrElementNotFound = sysERROR_NOT_FOUND
|
||||
|
||||
// ErrInvalidParameter is the error that is returned for invalid parameters.
|
||||
// This error constant can be used to check if the given function parameters were invalid.
|
||||
// For example when trying to create a new generic credential with an empty target name.
|
||||
ErrInvalidParameter = sysERROR_INVALID_PARAMETER
|
||||
)
|
||||
|
||||
// Get the generic credential with the given name from Windows credential manager
|
||||
// GetGenericCredential fetches the generic credential with the given name from Windows credential manager.
|
||||
// It returns nil and an error if the credential could not be found or an error occurred.
|
||||
func GetGenericCredential(targetName string) (*GenericCredential, error) {
|
||||
cred, err := nativeCredRead(targetName, naCRED_TYPE_GENERIC)
|
||||
cred, err := sysCredRead(targetName, sysCRED_TYPE_GENERIC)
|
||||
if cred != nil {
|
||||
return &GenericCredential{*cred}, err
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Create a new generic credential with the given name
|
||||
// NewGenericCredential creates a new generic credential object with the given name.
|
||||
// The persist mode of the newly created object is set to a default value that indicates local-machine-wide storage.
|
||||
// The credential object is NOT yet persisted to the Windows credential vault.
|
||||
func NewGenericCredential(targetName string) (result *GenericCredential) {
|
||||
result = new(GenericCredential)
|
||||
result.TargetName = targetName
|
||||
@@ -21,28 +38,31 @@ func NewGenericCredential(targetName string) (result *GenericCredential) {
|
||||
return
|
||||
}
|
||||
|
||||
// Persist the credential to Windows credential manager
|
||||
// Write persists the generic credential object to Windows credential manager.
|
||||
func (t *GenericCredential) Write() (err error) {
|
||||
err = nativeCredWrite(&t.Credential, naCRED_TYPE_GENERIC)
|
||||
err = sysCredWrite(&t.Credential, sysCRED_TYPE_GENERIC)
|
||||
return
|
||||
}
|
||||
|
||||
// Delete the credential from Windows credential manager
|
||||
// Delete removes the credential object from Windows credential manager.
|
||||
func (t *GenericCredential) Delete() (err error) {
|
||||
err = nativeCredDelete(&t.Credential, naCRED_TYPE_GENERIC)
|
||||
err = sysCredDelete(&t.Credential, sysCRED_TYPE_GENERIC)
|
||||
return
|
||||
}
|
||||
|
||||
// Get the domain password credential with the given target host name
|
||||
// GetDomainPassword fetches the domain-password credential with the given target host name from Windows credential manager.
|
||||
// It returns nil and an error if the credential could not be found or an error occurred.
|
||||
func GetDomainPassword(targetName string) (*DomainPassword, error) {
|
||||
cred, err := nativeCredRead(targetName, naCRED_TYPE_DOMAIN_PASSWORD)
|
||||
cred, err := sysCredRead(targetName, sysCRED_TYPE_DOMAIN_PASSWORD)
|
||||
if cred != nil {
|
||||
return &DomainPassword{*cred}, err
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Create a new domain password credential used for login to the given target host name
|
||||
// NewDomainPassword creates a new domain-password credential used for login to the given target host name.
|
||||
// The persist mode of the newly created object is set to a default value that indicates local-machine-wide storage.
|
||||
// The credential object is NOT yet persisted to the Windows credential vault.
|
||||
func NewDomainPassword(targetName string) (result *DomainPassword) {
|
||||
result = new(DomainPassword)
|
||||
result.TargetName = targetName
|
||||
@@ -50,28 +70,39 @@ func NewDomainPassword(targetName string) (result *DomainPassword) {
|
||||
return
|
||||
}
|
||||
|
||||
// Persist the domain password credential to Windows credential manager
|
||||
// Write persists the domain-password credential to Windows credential manager.
|
||||
func (t *DomainPassword) Write() (err error) {
|
||||
err = nativeCredWrite(&t.Credential, naCRED_TYPE_DOMAIN_PASSWORD)
|
||||
err = sysCredWrite(&t.Credential, sysCRED_TYPE_DOMAIN_PASSWORD)
|
||||
return
|
||||
}
|
||||
|
||||
// Delete the domain password credential from Windows credential manager
|
||||
// Delete removes the domain-password credential from Windows credential manager.
|
||||
func (t *DomainPassword) Delete() (err error) {
|
||||
err = nativeCredDelete(&t.Credential, naCRED_TYPE_DOMAIN_PASSWORD)
|
||||
err = sysCredDelete(&t.Credential, sysCRED_TYPE_DOMAIN_PASSWORD)
|
||||
return
|
||||
}
|
||||
|
||||
// Set the CredentialBlob field of a domain password credential
|
||||
// using an UTF16 encoded password string
|
||||
// SetPassword sets the CredentialBlob field of a domain password credential to the given string.
|
||||
func (t *DomainPassword) SetPassword(pw string) {
|
||||
t.CredentialBlob = utf16ToByte(syscall.StringToUTF16(pw))
|
||||
t.CredentialBlob = utf16ToByte(utf16FromString(pw))
|
||||
}
|
||||
|
||||
// List the contents of the Credentials store
|
||||
// List retrieves all credentials of the Credentials store.
|
||||
func List() ([]*Credential, error) {
|
||||
creds, err := nativeCredEnumerate("", true)
|
||||
if err != nil && err.Error() == naERROR_NOT_FOUND {
|
||||
creds, err := sysCredEnumerate("", true)
|
||||
if err != nil && errors.Is(err, ErrElementNotFound) {
|
||||
// Ignore ERROR_NOT_FOUND and return an empty list instead
|
||||
creds = []*Credential{}
|
||||
err = nil
|
||||
}
|
||||
return creds, err
|
||||
}
|
||||
|
||||
// FilteredList retrieves the list of credentials from the Credentials store that match the given filter.
|
||||
// The filter string defines the prefix followed by an asterisk for the `TargetName` attribute of the credentials.
|
||||
func FilteredList(filter string) ([]*Credential, error) {
|
||||
creds, err := sysCredEnumerate(filter, false)
|
||||
if err != nil && errors.Is(err, ErrElementNotFound) {
|
||||
// Ignore ERROR_NOT_FOUND and return an empty list instead
|
||||
creds = []*Credential{}
|
||||
err = nil
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
# This source code refers to The Go Authors for copyright purposes.
|
||||
# The master list of authors is in the main Go distribution,
|
||||
# visible at http://tip.golang.org/AUTHORS.
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
# This source code was written by the Go contributors.
|
||||
# The master list of contributors is in the main Go distribution,
|
||||
# visible at http://tip.golang.org/CONTRIBUTORS.
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
Copyright (c) 2009 The Go Authors. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following disclaimer
|
||||
in the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
* Neither the name of Google Inc. nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
Additional IP Rights Grant (Patents)
|
||||
|
||||
"This implementation" means the copyrightable works distributed by
|
||||
Google as part of the Go project.
|
||||
|
||||
Google hereby grants to You a perpetual, worldwide, non-exclusive,
|
||||
no-charge, royalty-free, irrevocable (except as stated in this section)
|
||||
patent license to make, have made, use, offer to sell, sell, import,
|
||||
transfer and otherwise run, modify and propagate the contents of this
|
||||
implementation of Go, where such license applies only to those patent
|
||||
claims, both currently owned or controlled by Google and acquired in
|
||||
the future, licensable by Google that are necessarily infringed by this
|
||||
implementation of Go. This grant does not include claims that would be
|
||||
infringed only as a consequence of further modification of this
|
||||
implementation. If you or your agent or exclusive licensee institute or
|
||||
order or agree to the institution of patent litigation against any
|
||||
entity (including a cross-claim or counterclaim in a lawsuit) alleging
|
||||
that this implementation of Go or any code incorporated within this
|
||||
implementation of Go constitutes direct or contributory patent
|
||||
infringement, or inducement of patent infringement, then any patent
|
||||
rights granted to you under this License for this implementation of Go
|
||||
shall terminate as of the date such litigation is filed.
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
// Copyright 2020 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package execabs is a drop-in replacement for os/exec
|
||||
// that requires PATH lookups to find absolute paths.
|
||||
// That is, execabs.Command("cmd") runs the same PATH lookup
|
||||
// as exec.Command("cmd"), but if the result is a path
|
||||
// which is relative, the Run and Start methods will report
|
||||
// an error instead of running the executable.
|
||||
//
|
||||
// See https://blog.golang.org/path-security for more information
|
||||
// about when it may be necessary or appropriate to use this package.
|
||||
package execabs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// ErrNotFound is the error resulting if a path search failed to find an executable file.
|
||||
// It is an alias for exec.ErrNotFound.
|
||||
var ErrNotFound = exec.ErrNotFound
|
||||
|
||||
// Cmd represents an external command being prepared or run.
|
||||
// It is an alias for exec.Cmd.
|
||||
type Cmd = exec.Cmd
|
||||
|
||||
// Error is returned by LookPath when it fails to classify a file as an executable.
|
||||
// It is an alias for exec.Error.
|
||||
type Error = exec.Error
|
||||
|
||||
// An ExitError reports an unsuccessful exit by a command.
|
||||
// It is an alias for exec.ExitError.
|
||||
type ExitError = exec.ExitError
|
||||
|
||||
func relError(file, path string) error {
|
||||
return fmt.Errorf("%s resolves to executable in current directory (.%c%s)", file, filepath.Separator, path)
|
||||
}
|
||||
|
||||
// LookPath searches for an executable named file in the directories
|
||||
// named by the PATH environment variable. If file contains a slash,
|
||||
// it is tried directly and the PATH is not consulted. The result will be
|
||||
// an absolute path.
|
||||
//
|
||||
// LookPath differs from exec.LookPath in its handling of PATH lookups,
|
||||
// which are used for file names without slashes. If exec.LookPath's
|
||||
// PATH lookup would have returned an executable from the current directory,
|
||||
// LookPath instead returns an error.
|
||||
func LookPath(file string) (string, error) {
|
||||
path, err := exec.LookPath(file)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if filepath.Base(file) == file && !filepath.IsAbs(path) {
|
||||
return "", relError(file, path)
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
|
||||
func fixCmd(name string, cmd *exec.Cmd) {
|
||||
if filepath.Base(name) == name && !filepath.IsAbs(cmd.Path) {
|
||||
// exec.Command was called with a bare binary name and
|
||||
// exec.LookPath returned a path which is not absolute.
|
||||
// Set cmd.lookPathErr and clear cmd.Path so that it
|
||||
// cannot be run.
|
||||
lookPathErr := (*error)(unsafe.Pointer(reflect.ValueOf(cmd).Elem().FieldByName("lookPathErr").Addr().Pointer()))
|
||||
if *lookPathErr == nil {
|
||||
*lookPathErr = relError(name, cmd.Path)
|
||||
}
|
||||
cmd.Path = ""
|
||||
}
|
||||
}
|
||||
|
||||
// CommandContext is like Command but includes a context.
|
||||
//
|
||||
// The provided context is used to kill the process (by calling os.Process.Kill)
|
||||
// if the context becomes done before the command completes on its own.
|
||||
func CommandContext(ctx context.Context, name string, arg ...string) *exec.Cmd {
|
||||
cmd := exec.CommandContext(ctx, name, arg...)
|
||||
fixCmd(name, cmd)
|
||||
return cmd
|
||||
|
||||
}
|
||||
|
||||
// Command returns the Cmd struct to execute the named program with the given arguments.
|
||||
// See exec.Command for most details.
|
||||
//
|
||||
// Command differs from exec.Command in its handling of PATH lookups,
|
||||
// which are used when the program name contains no slashes.
|
||||
// If exec.Command would have returned an exec.Cmd configured to run an
|
||||
// executable from the current directory, Command instead
|
||||
// returns an exec.Cmd that will return an error from Start or Run.
|
||||
func Command(name string, arg ...string) *exec.Cmd {
|
||||
cmd := exec.Command(name, arg...)
|
||||
fixCmd(name, cmd)
|
||||
return cmd
|
||||
}
|
||||
Vendored
+4
@@ -0,0 +1,4 @@
|
||||
# github.com/danieljoos/wincred v1.1.0
|
||||
github.com/danieljoos/wincred
|
||||
# golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4
|
||||
golang.org/x/sys/execabs
|
||||
Reference in New Issue
Block a user