Skip to content

Commit 0062d3e

Browse files
committed
feat: add support for gopass as a credential store
This change adds support for `gopass` as a credential store, based on the `pass` implementation. Closes: docker#138 Closes: docker#166 Signed-off-by: sudoforge <[email protected]>
1 parent 4ede49c commit 0062d3e

File tree

7 files changed

+349
-12
lines changed

7 files changed

+349
-12
lines changed

.github/workflows/build.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ jobs:
6363
run: |
6464
sudo apt-get update
6565
sudo apt-get install -y dbus-x11 gnome-keyring libsecret-1-dev pass
66+
-
67+
name: Install gopass
68+
if: ${{ matrix.os == 'ubuntu-20.04' }}
69+
env:
70+
GOPASS_VERSION: 1.15.5
71+
run: go install github.com/gopasspw/gopass@${{ env.GOPASS_VERSION }}
72+
6673
-
6774
name: GPG conf
6875
if: ${{ matrix.os == 'ubuntu-20.04' }}

Dockerfile

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ ARG XX_VERSION=1.2.1
55
ARG OSXCROSS_VERSION=11.3-r7-debian
66
ARG GOLANGCI_LINT_VERSION=v1.51.1
77
ARG DEBIAN_FRONTEND=noninteractive
8+
ARG GOPASS_VERSION=1.15.5
89

910
ARG PACKAGE=github.com/docker/docker-credential-helpers
1011

@@ -69,6 +70,10 @@ RUN xx-apt-get install -y binutils gcc libc6-dev libgcc-10-dev libsecret-1-dev p
6970
FROM base AS test
7071
ARG DEBIAN_FRONTEND
7172
RUN xx-apt-get install -y dbus-x11 gnome-keyring gpg-agent gpgconf libsecret-1-dev pass
73+
RUN --mount=type=bind,target=. \
74+
--mount=type=cache,target=/root/.cache \
75+
--mount=type=cache,target=/go/pkg/mod \
76+
go install github.com/gopasspw/gopass@${GOPASS_VERSION}
7277
RUN --mount=type=bind,target=. \
7378
--mount=type=cache,target=/root/.cache \
7479
--mount=type=cache,target=/go/pkg/mod <<EOT
@@ -106,7 +111,8 @@ RUN --mount=type=bind,target=. \
106111
--mount=type=bind,source=/tmp/.revision,target=/tmp/.revision,from=version <<EOT
107112
set -ex
108113
xx-go --wrap
109-
make build-pass build-secretservice PACKAGE=$PACKAGE VERSION=$(cat /tmp/.version) REVISION=$(cat /tmp/.revision) DESTDIR=/out
114+
make build-gopass build-pass build-secretservice PACKAGE=$PACKAGE VERSION=$(cat /tmp/.version) REVISION=$(cat /tmp/.revision) DESTDIR=/out
115+
xx-verify /out/docker-credential-gopass
110116
xx-verify /out/docker-credential-pass
111117
xx-verify /out/docker-credential-secretservice
112118
EOT
@@ -122,7 +128,8 @@ RUN --mount=type=bind,target=. \
122128
set -ex
123129
xx-go --wrap
124130
go install std
125-
make build-osxkeychain build-pass PACKAGE=$PACKAGE VERSION=$(cat /tmp/.version) REVISION=$(cat /tmp/.revision) DESTDIR=/out
131+
make build-gopass build-osxkeychain build-pass PACKAGE=$PACKAGE VERSION=$(cat /tmp/.version) REVISION=$(cat /tmp/.revision) DESTDIR=/out
132+
xx-verify /out/docker-credential-gopass
126133
xx-verify /out/docker-credential-osxkeychain
127134
xx-verify /out/docker-credential-pass
128135
EOT
@@ -136,7 +143,8 @@ RUN --mount=type=bind,target=. \
136143
--mount=type=bind,source=/tmp/.revision,target=/tmp/.revision,from=version <<EOT
137144
set -ex
138145
xx-go --wrap
139-
make build-wincred PACKAGE=$PACKAGE VERSION=$(cat /tmp/.version) REVISION=$(cat /tmp/.revision) DESTDIR=/out
146+
make build-gopass build-wincred PACKAGE=$PACKAGE VERSION=$(cat /tmp/.version) REVISION=$(cat /tmp/.revision) DESTDIR=/out
147+
xx-verify /out/docker-credential-gopass.exe
140148
mv /out/docker-credential-wincred /out/docker-credential-wincred.exe
141149
xx-verify /out/docker-credential-wincred.exe
142150
EOT

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ clean:
1616
rm -rf bin
1717

1818
.PHONY: build-%
19-
build-%: # build, can be one of build-osxkeychain build-pass build-secretservice build-wincred
19+
build-%: # build, can be one of build-gopass build-osxkeychain build-pass build-secretservice build-wincred
2020
go build -trimpath -ldflags="$(GO_LDFLAGS) -X ${GO_PKG}/credentials.Name=docker-credential-$*" -o "$(DESTDIR)/docker-credential-$*" ./$*/cmd/
2121

2222
# aliases for build-* targets
23-
.PHONY: osxkeychain secretservice pass wincred
23+
.PHONY: gopass osxkeychain secretservice pass wincred
24+
gopass: build-gopass
2425
osxkeychain: build-osxkeychain
2526
secretservice: build-secretservice
2627
pass: build-pass

README.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,26 @@ You can see examples of each function in the [client](https://godoc.org/github.c
7777

7878
### Available programs
7979

80-
1. osxkeychain: Provides a helper to use the OS X keychain as credentials store.
81-
2. secretservice: Provides a helper to use the D-Bus secret service as credentials store.
82-
3. wincred: Provides a helper to use Windows credentials manager as store.
83-
4. pass: Provides a helper to use `pass` as credentials store.
80+
- gopass: Provides a helper to use `gopass` as credentials store.
81+
- osxkeychain: Provides a helper to use the OS X keychain as credentials store.
82+
- pass: Provides a helper to use `pass` as credentials store.
83+
- secretservice: Provides a helper to use the D-Bus secret service as credentials store.
84+
- wincred: Provides a helper to use Windows credentials manager as store.
8485

85-
#### Note
86+
#### Note regarding `gopass`
8687

87-
`pass` needs to be configured for `docker-credential-pass` to work properly.
88-
It must be initialized with a `gpg2` key ID. Make sure your GPG key exists is in `gpg2` keyring as `pass` uses `gpg2` instead of the regular `gpg`.
88+
`gopass` requires manual intervention in order for `docker-credential-gopass` to
89+
work properly: a password store must be initialized. Please ensure to review the
90+
upstream [quick start guide][gopass-quick-start] for more information.
91+
92+
[gopass-quick-start]: https://github.com/gopasspw/gopass#quick-start-guide
93+
94+
#### Note regarding `pass`
95+
96+
`pass` requires manual interview in order for `docker-credential-pass` to
97+
work properly. It must be initialized with a `gpg2` key ID. Make sure your GPG
98+
key exists is in `gpg2` keyring as `pass` uses `gpg2` instead of the regular
99+
`gpg`.
89100

90101
## Development
91102

gopass/cmd/main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package main
2+
3+
import (
4+
"github.com/docker/docker-credential-helpers/credentials"
5+
"github.com/docker/docker-credential-helpers/gopass"
6+
)
7+
8+
func main() {
9+
credentials.Serve(gopass.Gopass{})
10+
}

gopass/gopass.go

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
// Package gopass implements a `gopass` based credential helper. Passwords are
2+
// stored as arguments to gopass of the form:
3+
//
4+
// "$GOPASS_FOLDER/base64-url(serverURL)/username"
5+
//
6+
// We base64-url encode the serverURL, because under the hood gopass uses files
7+
// and folders, so /s will get translated into additional folders.
8+
package gopass
9+
10+
import (
11+
"bytes"
12+
"encoding/base64"
13+
"errors"
14+
"fmt"
15+
"io/fs"
16+
"os"
17+
"os/exec"
18+
"path"
19+
"strings"
20+
"sync"
21+
22+
"github.com/docker/docker-credential-helpers/credentials"
23+
)
24+
25+
// GOPASS_FOLDER contains the directory where credentials are stored
26+
const GOPASS_FOLDER = "docker-credential-helpers" //nolint:revive
27+
28+
// Gopass handles secrets using gopass as a store.
29+
type Gopass struct{}
30+
31+
// Ideally these would be stored as members of Gopass, but since all of Gopass's
32+
// methods have value receivers, not pointer receivers, and changing that is
33+
// backwards incompatible, we assume that all Gopass instances share the same
34+
// configuration
35+
36+
// initializationMutex is held while initializing so that only one 'gopass'
37+
// round-tripping is done to check that gopass is functioning.
38+
var initializationMutex sync.Mutex
39+
var gopassInitialized bool
40+
41+
// CheckInitialized checks whether the password helper can be used. It
42+
// internally caches and so may be safely called multiple times with no impact
43+
// on performance, though the first call may take longer.
44+
func (g Gopass) CheckInitialized() bool {
45+
return g.checkInitialized() == nil
46+
}
47+
48+
func (g Gopass) checkInitialized() error {
49+
initializationMutex.Lock()
50+
defer initializationMutex.Unlock()
51+
if gopassInitialized {
52+
return nil
53+
}
54+
55+
// We just run a `gopass ls`, if it fails then gopass is not initialized.
56+
_, err := g.runGopassHelper("", "ls", "--flat")
57+
if err != nil {
58+
return fmt.Errorf("gopass is not initialized: %v", err)
59+
}
60+
gopassInitialized = true
61+
return nil
62+
}
63+
64+
func (g Gopass) runGopass(stdinContent string, args ...string) (string, error) {
65+
if err := g.checkInitialized(); err != nil {
66+
return "", err
67+
}
68+
return g.runGopassHelper(stdinContent, args...)
69+
}
70+
71+
func (g Gopass) runGopassHelper(stdinContent string, args ...string) (string, error) {
72+
var stdout, stderr bytes.Buffer
73+
cmd := exec.Command("gopass", args...)
74+
cmd.Stdin = strings.NewReader(stdinContent)
75+
cmd.Stdout = &stdout
76+
cmd.Stderr = &stderr
77+
78+
err := cmd.Run()
79+
if err != nil {
80+
return "", fmt.Errorf("%s: %s", err, stderr.String())
81+
}
82+
83+
// trim newlines; gopass includes a newline at the end of `show` output
84+
return strings.TrimRight(stdout.String(), "\n\r"), nil
85+
}
86+
87+
// Add adds new credentials to the keychain.
88+
func (g Gopass) Add(creds *credentials.Credentials) error {
89+
if creds == nil {
90+
return errors.New("missing credentials")
91+
}
92+
93+
encoded := base64.URLEncoding.EncodeToString([]byte(creds.ServerURL))
94+
95+
_, err := g.runGopass(creds.Secret, "insert", "-f", path.Join(GOPASS_FOLDER, encoded, creds.Username))
96+
return err
97+
}
98+
99+
// Delete removes credentials from the store.
100+
func (g Gopass) Delete(serverURL string) error {
101+
if serverURL == "" {
102+
return errors.New("missing server url")
103+
}
104+
105+
encoded := base64.URLEncoding.EncodeToString([]byte(serverURL))
106+
_, err := g.runGopass("", "rm", "-rf", path.Join(GOPASS_FOLDER, encoded))
107+
return err
108+
}
109+
110+
func (g Gopass) getGopassDir() (string, error) {
111+
gopassDir, err := g.runGopass("", "config", "mounts.path")
112+
113+
if err != nil {
114+
return "", fmt.Errorf("error getting gopass dir: %v", err)
115+
}
116+
117+
return gopassDir, nil
118+
}
119+
120+
// listGopassDir lists all the contents of a directory in the password store.
121+
// Gopass uses fancy unicode to emit stuff to stdout, so rather than try
122+
// and parse this, let's just look at the directory structure instead.
123+
func (g Gopass) listGopassDir(args ...string) ([]os.FileInfo, error) {
124+
gopassDir, err := g.getGopassDir()
125+
if err != nil {
126+
return nil, err
127+
}
128+
129+
p := path.Join(append([]string{gopassDir, GOPASS_FOLDER}, args...)...)
130+
131+
entries, err := os.ReadDir(p)
132+
if err != nil {
133+
if os.IsNotExist(err) {
134+
return []os.FileInfo{}, nil
135+
}
136+
return nil, err
137+
}
138+
139+
infos := make([]fs.FileInfo, 0, len(entries))
140+
for _, entry := range entries {
141+
info, err := entry.Info()
142+
if err != nil {
143+
return nil, err
144+
}
145+
infos = append(infos, info)
146+
}
147+
return infos, nil
148+
}
149+
150+
// Get returns the username and secret to use for a given registry server URL.
151+
func (g Gopass) Get(serverURL string) (string, string, error) {
152+
if serverURL == "" {
153+
return "", "", errors.New("missing server url")
154+
}
155+
156+
gopassDir, err := g.getGopassDir()
157+
if err != nil {
158+
return "", "", err
159+
}
160+
161+
encoded := base64.URLEncoding.EncodeToString([]byte(serverURL))
162+
163+
if _, err := os.Stat(path.Join(gopassDir, GOPASS_FOLDER, encoded)); err != nil {
164+
if os.IsNotExist(err) {
165+
return "", "", credentials.NewErrCredentialsNotFound()
166+
}
167+
168+
return "", "", err
169+
}
170+
171+
usernames, err := g.listGopassDir(encoded)
172+
if err != nil {
173+
return "", "", err
174+
}
175+
176+
if len(usernames) < 1 {
177+
return "", "", fmt.Errorf("no usernames for %s", serverURL)
178+
}
179+
180+
actual := strings.TrimSuffix(usernames[0].Name(), ".gpg")
181+
secret, err := g.runGopass("", "show", "-o", path.Join(GOPASS_FOLDER, encoded, actual))
182+
183+
return actual, secret, err
184+
}
185+
186+
// List returns the stored URLs and corresponding usernames for a given credentials label
187+
func (g Gopass) List() (map[string]string, error) {
188+
servers, err := g.listGopassDir()
189+
if err != nil {
190+
return nil, err
191+
}
192+
193+
resp := map[string]string{}
194+
195+
for _, server := range servers {
196+
if !server.IsDir() {
197+
continue
198+
}
199+
200+
serverURL, err := base64.URLEncoding.DecodeString(server.Name())
201+
if err != nil {
202+
return nil, err
203+
}
204+
205+
usernames, err := g.listGopassDir(server.Name())
206+
if err != nil {
207+
return nil, err
208+
}
209+
210+
if len(usernames) < 1 {
211+
return nil, fmt.Errorf("no usernames for %s", serverURL)
212+
}
213+
214+
resp[string(serverURL)] = strings.TrimSuffix(usernames[0].Name(), ".gpg")
215+
}
216+
217+
return resp, nil
218+
}

0 commit comments

Comments
 (0)