|
| 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 | + gopassDir = os.ExpandEnv(gopassDir) |
| 118 | + |
| 119 | + if strings.HasPrefix(gopassDir, "~/") { |
| 120 | + d, err := os.UserHomeDir() |
| 121 | + |
| 122 | + if err != nil { |
| 123 | + message := fmt.Sprintf("unable to get user home directory: %v", err.Error()) |
| 124 | + return "", errors.New(message) |
| 125 | + } |
| 126 | + |
| 127 | + gopassDir = path.Join(d, gopassDir[2:]) |
| 128 | + } |
| 129 | + |
| 130 | + return gopassDir, nil |
| 131 | +} |
| 132 | + |
| 133 | +// listGopassDir lists all the contents of a directory in the password store. |
| 134 | +// Gopass uses fancy unicode to emit stuff to stdout, so rather than try |
| 135 | +// and parse this, let's just look at the directory structure instead. |
| 136 | +func (g Gopass) listGopassDir(args ...string) ([]os.FileInfo, error) { |
| 137 | + gopassDir, err := g.getGopassDir() |
| 138 | + if err != nil { |
| 139 | + return nil, err |
| 140 | + } |
| 141 | + |
| 142 | + p := os.ExpandEnv(path.Join(append([]string{gopassDir, GOPASS_FOLDER}, args...)...)) |
| 143 | + |
| 144 | + entries, err := os.ReadDir(p) |
| 145 | + if err != nil { |
| 146 | + if os.IsNotExist(err) { |
| 147 | + return []os.FileInfo{}, nil |
| 148 | + } |
| 149 | + return nil, err |
| 150 | + } |
| 151 | + |
| 152 | + infos := make([]fs.FileInfo, 0, len(entries)) |
| 153 | + for _, entry := range entries { |
| 154 | + info, err := entry.Info() |
| 155 | + if err != nil { |
| 156 | + return nil, err |
| 157 | + } |
| 158 | + infos = append(infos, info) |
| 159 | + } |
| 160 | + return infos, nil |
| 161 | +} |
| 162 | + |
| 163 | +// Get returns the username and secret to use for a given registry server URL. |
| 164 | +func (g Gopass) Get(serverURL string) (string, string, error) { |
| 165 | + if serverURL == "" { |
| 166 | + return "", "", errors.New("missing server url") |
| 167 | + } |
| 168 | + |
| 169 | + gopassDir, err := g.getGopassDir() |
| 170 | + if err != nil { |
| 171 | + return "", "", err |
| 172 | + } |
| 173 | + |
| 174 | + encoded := base64.URLEncoding.EncodeToString([]byte(serverURL)) |
| 175 | + search := path.Join(gopassDir, GOPASS_FOLDER, encoded) |
| 176 | + |
| 177 | + stat, _ := os.Stat(search) |
| 178 | + |
| 179 | + if _, err := os.Stat(search); err != nil { |
| 180 | + if os.IsNotExist(err) { |
| 181 | + return "", "", credentials.NewErrCredentialsNotFound() |
| 182 | + } |
| 183 | + |
| 184 | + return "", "", err |
| 185 | + } |
| 186 | + |
| 187 | + usernames, err := g.listGopassDir(encoded) |
| 188 | + if err != nil { |
| 189 | + return "", "", err |
| 190 | + } |
| 191 | + |
| 192 | + if len(usernames) < 1 { |
| 193 | + return "", "", fmt.Errorf("no usernames for %s", serverURL) |
| 194 | + } |
| 195 | + |
| 196 | + actual := strings.TrimSuffix(usernames[0].Name(), ".gpg") |
| 197 | + secret, err := g.runGopass("", "show", "-o", path.Join(GOPASS_FOLDER, encoded, actual)) |
| 198 | + |
| 199 | + return actual, secret, err |
| 200 | +} |
| 201 | + |
| 202 | +// List returns the stored URLs and corresponding usernames for a given credentials label |
| 203 | +func (g Gopass) List() (map[string]string, error) { |
| 204 | + servers, err := g.listGopassDir() |
| 205 | + if err != nil { |
| 206 | + return nil, err |
| 207 | + } |
| 208 | + |
| 209 | + resp := map[string]string{} |
| 210 | + |
| 211 | + for _, server := range servers { |
| 212 | + if !server.IsDir() { |
| 213 | + continue |
| 214 | + } |
| 215 | + |
| 216 | + serverURL, err := base64.URLEncoding.DecodeString(server.Name()) |
| 217 | + if err != nil { |
| 218 | + return nil, err |
| 219 | + } |
| 220 | + |
| 221 | + usernames, err := g.listGopassDir(server.Name()) |
| 222 | + if err != nil { |
| 223 | + return nil, err |
| 224 | + } |
| 225 | + |
| 226 | + if len(usernames) < 1 { |
| 227 | + return nil, fmt.Errorf("no usernames for %s", serverURL) |
| 228 | + } |
| 229 | + |
| 230 | + resp[string(serverURL)] = strings.TrimSuffix(usernames[0].Name(), ".gpg") |
| 231 | + } |
| 232 | + |
| 233 | + return resp, nil |
| 234 | +} |
0 commit comments