Skip to content

Commit 942cc1e

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
1 parent da93839 commit 942cc1e

File tree

7 files changed

+365
-10
lines changed

7 files changed

+365
-10
lines changed

.github/workflows/build.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ 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+
run: |
70+
curl https://packages.gopass.pw/repos/gopass/gopass-archive-keyring.gpg | sudo tee /usr/share/keyrings/gopass-archive-keyring.gpg >/dev/null
71+
cat << EOF | sudo tee /etc/apt/sources.list.d/gopass.sources
72+
Types: deb
73+
URIs: https://packages.gopass.pw/repos/gopass
74+
Suites: stable
75+
Architectures: all amd64 arm64 armhf
76+
Components: main
77+
Signed-By: /usr/share/keyrings/gopass-archive-keyring.gpg
78+
EOF
79+
sudo apt update
80+
sudo apt install gopass gopass-archive-keyring
6681
-
6782
name: GPG conf
6883
if: ${{ matrix.os == 'ubuntu-20.04' }}

Dockerfile

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

0 commit comments

Comments
 (0)