/
dogo_vault.go
272 lines (236 loc) · 6.36 KB
/
dogo_vault.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package main
import (
"bytes"
"errors"
"fmt"
"os"
"runtime"
"strings"
"path/filepath"
"encoding/hex"
"sync"
"github.com/docker/docker-credential-helpers/credentials"
"github.com/oliverkofoed/dogo/commandtree"
"github.com/oliverkofoed/dogo/neaterror"
"github.com/oliverkofoed/dogo/term"
"github.com/oliverkofoed/dogo/vault"
"golang.org/x/crypto/ssh/terminal"
)
func dogoVaultCreate(filename string, keyStrength string) error {
passphrase := ""
// check the file doesn't already exist
if _, err := os.Stat(filename); err == nil {
return fmt.Errorf("There is already a file called %v", flagVault)
}
// ask the user for a repeated passphrase
for {
fmt.Printf("Please enter passphrase for %v: \n", flagVault)
firstBytes, err := terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
fmt.Println(neaterror.String("", err, term.IsTerminal))
continue
}
fmt.Printf("Please repeat passphrase for %v: \n", flagVault)
secondBytes, err := terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
fmt.Println(neaterror.String("", err, term.IsTerminal))
continue
}
if !bytes.Equal(firstBytes, secondBytes) {
fmt.Println(neaterror.String("", errors.New("That passwords didn't match. Please try again"), term.IsTerminal))
continue
}
passphrase = string(firstBytes)
break
}
// decide on the right scrypt key strength
strength := vault.SCryptSensitive
switch keyStrength {
case "sensitive":
strength = vault.SCryptSensitive
case "interactive":
strength = vault.SCryptInteractive
default:
return fmt.Errorf("Invalid key strength '%v'. Valid values are 'sensitive' (slow but more secure) and 'interactive' (faster, reasonably secure)", flagKeyStrength)
}
// create the vault
_, key, err := vault.Create(filename, passphrase, strength)
if err != nil {
fmt.Println(neaterror.String("", err, term.IsTerminal))
}
// store passphrase in keychain.
store := getCredStore(flagCredentialsStore)
if store != nil {
path, err := filepath.Abs(filename)
url := "file://" + path
if err == nil {
store.Delete(url)
err := store.Add(&credentials.Credentials{
Secret: fmt.Sprintf("%X: %v", key, passphrase),
Username: filepath.Base(path),
ServerURL: url,
})
if err != nil {
fmt.Println(neaterror.String("", err, term.IsTerminal))
return nil
}
}
}
return nil
}
func vaultAction(filename string, action func(v *vault.Vault) error) error {
v, err := getOpenVault(filename)
if err != nil {
fmt.Println(neaterror.String("", err, term.IsTerminal))
return nil
}
err = action(v)
if err != nil {
fmt.Println(neaterror.String("", err, term.IsTerminal))
}
return nil
}
func dogVault(filename string) error {
vault, err := getOpenVault(filename)
if err != nil {
return err
}
for _, e := range vault.List() {
fmt.Println(e.Key)
}
return nil
}
func defaultCredStore() string {
switch runtime.GOOS {
case "darwin":
return "osxkeychain"
case "linux":
return "secretservice"
case "windows":
return "wincred"
default:
return ""
}
}
func getCredStore(store string) credentials.Helper {
switch store {
case "none":
return nil
default:
return getPlatformCredStore(store)
}
}
var openVaultsLock sync.RWMutex
var openVaults = make(map[string]*vault.Vault)
func getOpenVaultFile(filename string, key string) ([]byte, error) {
v, err := getOpenVault(filename)
if err != nil {
return nil, err
}
return v.GetBytes(key)
}
func getOpenVaultString(filename string, key string) (string, error) {
v, err := getOpenVault(filename)
if err != nil {
return "", err
}
return v.GetString(key)
}
func getOpenVault(filename string) (*vault.Vault, error) {
path, err := filepath.Abs(filename)
if err != nil {
return nil, err
}
url := "https://stored2credentials.dogo" + path
// only one thread at a time
openVaultsLock.Lock()
defer openVaultsLock.Unlock()
// check if we already have this vault.
if v, found := openVaults[path]; found {
//openVaultsLock.Unlock()
return v, nil
}
//openVaultsLock.Unlock()
// check the file exists
if _, err := os.Stat(filename); err != nil && os.IsNotExist(err) {
return nil, fmt.Errorf("File does not exist: %v", filename)
}
// check if we have a credentials store we can use.
store := getCredStore(flagCredentialsStore)
if store != nil {
_, pass, err := store.Get(url)
if err == nil {
parts := strings.Split(pass, ":")
key, _ := hex.DecodeString(parts[0])
passphrase := pass[len(parts[0])+2:]
v, _, err := vault.Open(path, passphrase, key)
if err != nil && !strings.Contains(err.Error(), "wrong passphrase") {
return nil, err
}
if v != nil {
openVaults[path] = v
return v, nil
}
}
}
// did we get a passphrase from ENV?
envPassphrase := strings.TrimSpace(os.Getenv(strings.ToUpper(fmt.Sprintf("%vPASS", strings.Replace(filename, ".", "", -1)))))
if envPassphrase != "" {
v, _, err := vault.Open(path, envPassphrase, nil)
if err != nil && !strings.Contains(err.Error(), "wrong passphrase") {
return nil, err
}
if v != nil {
openVaults[path] = v
return v, nil
}
}
// check that we actually can ask for a pssword.
if !term.IsTerminal {
return nil, fmt.Errorf("can't ask for password for vault %v in non-terminal context", filename)
}
// ask for passphrase
var v *vault.Vault
var passphraseBytes []byte
commandtree.ConsoleUIInterupt(func() {
// check if we already have this vault.
//openVaultsLock.Lock()
if vx, found := openVaults[path]; found {
v = vx
//openVaultsLock.Unlock()
} else {
//openVaultsLock.Unlock()
fmt.Printf("Please enter passphrase for %v: \n", filename)
passphraseBytes, err = terminal.ReadPassword(int(os.Stdin.Fd()))
term.MoveUp(1)
term.EraseCurrentLine()
if err == nil {
fmt.Printf("Opening vault... \n")
// open vault
var key []byte
v, key, err = vault.Open(path, string(passphraseBytes), nil)
term.MoveUp(1)
term.EraseCurrentLine()
if err == nil {
// store if a credstore is specified
if store != nil {
store.Delete(url)
store.Add(&credentials.Credentials{
Secret: fmt.Sprintf("%X: %v", key, string(passphraseBytes)),
Username: filepath.Base(path),
ServerURL: url,
})
}
// save the vault in memory for future requests
//openVaultsLock.Lock()
openVaults[path] = v
//openVaultsLock.Unlock()
}
}
}
})
if err != nil {
return nil, err
}
return v, nil
}