-
Notifications
You must be signed in to change notification settings - Fork 2
/
local.go
133 lines (114 loc) · 2.73 KB
/
local.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
package internal
import (
"crypto/rand"
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"os/user"
"path"
"runtime"
"sync"
)
type LocalCrypter struct{}
var keyCached []byte
var keyCacheLock sync.RWMutex
var pathGetter keyPathGetter = userDataKeyPathGetter{}
func (c LocalCrypter) Name() string {
return "local"
}
func (c LocalCrypter) Encrypt(plaintext string, encryptParams EncryptParams) (Ciphertext, DecryptParams, error) {
key, err := localKey()
if err != nil {
return "", nil, fmt.Errorf("Error retrieving local key: %s", err)
}
ciphertext, err := AESEncrypt(key, plaintext)
if err != nil {
return "", nil, fmt.Errorf("Error encrypting plaintext: %s", err)
}
return Ciphertext(ciphertext), nil, nil
}
func (c LocalCrypter) Decrypt(b64ciphertext Ciphertext, decryptParams DecryptParams) (string, error) {
key, err := localKey()
if err != nil {
return "", fmt.Errorf("Error retrieving local key: %s", err)
}
plaintext, err := AESDecrypt(key, string(b64ciphertext))
if err != nil {
return "", fmt.Errorf("Error decrypting secret: %s", err)
}
return string(plaintext), nil
}
func localKey() ([]byte, error) {
keyCacheLock.RLock()
key := keyCached
keyCacheLock.RUnlock()
if len(key) > 0 {
return key, nil
}
keyCacheLock.Lock()
defer keyCacheLock.Unlock()
key = keyCached
if len(key) > 0 {
return key, nil
}
// retrieve or generate key
keyDir, keyFilePath, err := pathGetter.keyPaths()
if err != nil {
return nil, err
}
// if key file already exists
if _, err := os.Stat(keyFilePath); err == nil {
keyB64, err := ioutil.ReadFile(keyFilePath)
if err != nil {
return nil, err
}
key, err = base64.StdEncoding.DecodeString(string(keyB64))
if err != nil {
return nil, err
}
return key, nil
}
// else generate the key
key = make([]byte, 16)
_, err = rand.Read(key)
if err != nil {
return nil, err
}
// and write it into the key file
err = os.MkdirAll(keyDir, 0755)
if err != nil {
return nil, err
}
keyB64 := base64.StdEncoding.EncodeToString(key)
err = ioutil.WriteFile(keyFilePath, []byte(keyB64), 0644)
if err != nil {
return nil, err
}
return key, nil
}
type keyPathGetter interface {
keyPaths() (string, string, error)
}
type userDataKeyPathGetter struct{}
func (g userDataKeyPathGetter) keyPaths() (string, string, error) {
currentUser, err := user.Current()
if err != nil {
return "", "", err
}
homeDir := currentUser.HomeDir
var dataDir string
switch runtime.GOOS {
case "darwin":
dataDir = "Library/Application Support"
case "windows":
dataDir = "AppData\\Local\\"
default:
dataDir = os.Getenv("XDG_DATA_HOME")
if len(dataDir) == 0 {
dataDir = ".local/share"
}
}
keyDir := path.Join(homeDir, dataDir, "secretcrypt")
return keyDir, path.Join(keyDir, "key"), nil
}