forked from 99designs/keyring
-
Notifications
You must be signed in to change notification settings - Fork 9
/
pass.go
166 lines (135 loc) · 3.01 KB
/
pass.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//go:build !windows
// +build !windows
package keyring
import (
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
func init() {
supportedBackends[PassBackend] = opener(func(cfg Config) (Keyring, error) {
var err error
pass := &passKeyring{
passcmd: cfg.PassCmd,
dir: cfg.PassDir,
prefix: cfg.PassPrefix,
}
if pass.passcmd == "" {
pass.passcmd = "pass"
}
if pass.dir == "" {
if passDir, found := os.LookupEnv("PASSWORD_STORE_DIR"); found {
pass.dir = passDir
} else {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, err
}
pass.dir = filepath.Join(homeDir, ".password-store")
}
}
pass.dir, err = ExpandTilde(pass.dir)
if err != nil {
return nil, err
}
// fail if the pass program is not available
_, err = exec.LookPath(pass.passcmd)
if err != nil {
return nil, errors.New("The pass program is not available")
}
return pass, nil
})
}
type passKeyring struct {
dir string
passcmd string
prefix string
}
func (k *passKeyring) pass(args ...string) *exec.Cmd {
cmd := exec.Command(k.passcmd, args...)
if k.dir != "" {
cmd.Env = append(os.Environ(), fmt.Sprintf("PASSWORD_STORE_DIR=%s", k.dir))
}
cmd.Stderr = os.Stderr
return cmd
}
func (k *passKeyring) Get(key string) (Item, error) {
if !k.itemExists(key) {
return Item{}, ErrKeyNotFound
}
name := filepath.Join(k.prefix, key)
cmd := k.pass("show", name)
output, err := cmd.Output()
if err != nil {
return Item{}, err
}
var decoded Item
err = json.Unmarshal(output, &decoded)
return decoded, err
}
func (k *passKeyring) GetMetadata(key string) (Metadata, error) {
return Metadata{}, nil
}
func (k *passKeyring) Set(i Item) error {
bytes, err := json.Marshal(i)
if err != nil {
return err
}
name := filepath.Join(k.prefix, i.Key)
cmd := k.pass("insert", "-m", "-f", name)
cmd.Stdin = strings.NewReader(string(bytes))
err = cmd.Run()
if err != nil {
return err
}
return nil
}
func (k *passKeyring) Remove(key string) error {
if !k.itemExists(key) {
return ErrKeyNotFound
}
name := filepath.Join(k.prefix, key)
cmd := k.pass("rm", "-f", name)
err := cmd.Run()
if err != nil {
return err
}
return nil
}
func (k *passKeyring) itemExists(key string) bool {
var path = filepath.Join(k.dir, k.prefix, key+".gpg")
_, err := os.Stat(path)
return err == nil
}
func (k *passKeyring) Keys() ([]string, error) {
var keys = []string{}
var path = filepath.Join(k.dir, k.prefix)
info, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return keys, nil
}
return keys, err
}
if !info.IsDir() {
return keys, fmt.Errorf("%s is not a directory", path)
}
err = filepath.Walk(path, func(p string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && filepath.Ext(p) == ".gpg" {
name := strings.TrimPrefix(p, path)
if name[0] == os.PathSeparator {
name = name[1:]
}
keys = append(keys, name[:len(name)-4])
}
return nil
})
return keys, err
}