forked from 99designs/keyring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
159 lines (130 loc) · 2.86 KB
/
file.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
package keyring
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
jose "github.com/dvsekhvalnov/jose2go"
"github.com/mitchellh/go-homedir"
"golang.org/x/crypto/ssh/terminal"
)
type passwordFunc func(string) (string, error)
func terminalPrompt(prompt string) (string, error) {
if password := os.Getenv("AWS_VAULT_FILE_PASSPHRASE"); password != "" {
return password, nil
}
fmt.Printf("%s: ", prompt)
b, err := terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return "", err
}
fmt.Println()
return string(b), nil
}
func init() {
supportedBackends[FileBackend] = opener(func(name string) (Keyring, error) {
return &fileKeyring{
PasswordFunc: terminalPrompt,
}, nil
})
}
type fileKeyring struct {
Dir string
PasswordFunc passwordFunc
password string
}
func (k *fileKeyring) dir() (string, error) {
dir := k.Dir
if dir == "" {
home, err := homedir.Dir()
if err != nil {
return "", err
}
dir = filepath.Join(home, "/.awsvault/keys/")
}
stat, err := os.Stat(dir)
if os.IsNotExist(err) {
err = os.MkdirAll(dir, 0700)
} else if err != nil && !stat.IsDir() {
err = fmt.Errorf("%s is a file, not a directory", dir)
}
return dir, err
}
func (k *fileKeyring) unlock() error {
dir, err := k.dir()
if err != nil {
return err
}
if k.password == "" {
pwd, err := k.PasswordFunc(fmt.Sprintf("Enter passphrase to unlock %s", dir))
if err != nil {
return err
}
k.password = pwd
}
return nil
}
func (k *fileKeyring) Get(key string) (Item, error) {
dir, err := k.dir()
if err != nil {
return Item{}, err
}
bytes, err := ioutil.ReadFile(filepath.Join(dir, key))
if os.IsNotExist(err) {
return Item{}, ErrKeyNotFound
} else if err != nil {
return Item{}, err
}
if err = k.unlock(); err != nil {
return Item{}, err
}
payload, _, err := jose.Decode(string(bytes), k.password)
if err != nil {
return Item{}, err
}
var decoded Item
err = json.Unmarshal([]byte(payload), &decoded)
return decoded, err
}
func (k *fileKeyring) Set(i Item) error {
bytes, err := json.Marshal(i)
if err != nil {
return err
}
dir, err := k.dir()
if err != nil {
return err
}
if err = k.unlock(); err != nil {
return err
}
token, err := jose.Encrypt(string(bytes), jose.PBES2_HS256_A128KW, jose.A256GCM, k.password,
jose.Headers(map[string]interface{}{
"created": time.Now().String(),
}))
if err != nil {
return err
}
return ioutil.WriteFile(filepath.Join(dir, i.Key), []byte(token), 0600)
}
func (k *fileKeyring) Remove(key string) error {
dir, err := k.dir()
if err != nil {
return err
}
return os.Remove(filepath.Join(dir, key))
}
func (k *fileKeyring) Keys() ([]string, error) {
dir, err := k.dir()
if err != nil {
return nil, err
}
var keys = []string{}
files, _ := ioutil.ReadDir(dir)
for _, f := range files {
keys = append(keys, f.Name())
}
return keys, nil
}