-
Notifications
You must be signed in to change notification settings - Fork 53
/
encryption.go
202 lines (179 loc) · 5.45 KB
/
encryption.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
package config
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
ioutils "github.com/jfrog/gofrog/io"
"io"
"os"
"strconv"
"syscall"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
"github.com/jfrog/jfrog-client-go/utils/log"
"github.com/spf13/viper"
"golang.org/x/term"
)
type SecurityConf struct {
Version string `yaml:"version,omitempty"`
MasterKey string `yaml:"masterKey,omitempty"`
}
const masterKeyField = "masterKey"
const masterKeyLength = 32
const encryptErrorPrefix = "cannot encrypt config: "
const decryptErrorPrefix = "cannot decrypt config: "
type secretHandler func(string, string) (string, error)
// Encrypt config file if security configuration file exists and contains master key.
func (config *Config) encrypt() error {
key, err := getEncryptionKey()
if err != nil || key == "" {
return err
}
// Mark config as encrypted.
config.Enc = true
return handleSecrets(config, encrypt, key)
}
// Decrypt config if encrypted and master key exists.
func (config *Config) decrypt() error {
if !config.Enc {
return updateEncryptionIfNeeded(config)
}
key, err := getEncryptionKey()
if err != nil {
return err
}
if key == "" {
return errorutils.CheckErrorf(decryptErrorPrefix+"security configuration file was not found or the '%s' environment variable was not configured", coreutils.EncryptionKey)
}
config.Enc = false
return handleSecrets(config, decrypt, key)
}
// Encrypt the config file if it is decrypted while security configuration file exists and contains a master key, or if the JFROG_CLI_ENCRYPTION_KEY environment variable exist.
func updateEncryptionIfNeeded(config *Config) error {
masterKey, err := getEncryptionKey()
if err != nil || masterKey == "" {
return err
}
// The encryption key exists and will be loaded again in encrypt()
return saveConfig(config)
}
// Encrypt/Decrypt all secrets in the provided config, with the provided master key.
func handleSecrets(config *Config, handler secretHandler, key string) error {
var err error
for _, serverDetails := range config.Servers {
serverDetails.Password, err = handler(serverDetails.Password, key)
if err != nil {
return err
}
serverDetails.AccessToken, err = handler(serverDetails.AccessToken, key)
if err != nil {
return err
}
serverDetails.SshPassphrase, err = handler(serverDetails.SshPassphrase, key)
if err != nil {
return err
}
serverDetails.RefreshToken, err = handler(serverDetails.RefreshToken, key)
if err != nil {
return err
}
serverDetails.ArtifactoryRefreshToken, err = handler(serverDetails.ArtifactoryRefreshToken, key)
if err != nil {
return err
}
}
return nil
}
func getEncryptionKey() (string, error) {
if key, exist := os.LookupEnv(coreutils.EncryptionKey); exist {
return key, nil
}
return getEncryptionKeyFromSecurityConfFile()
}
func getEncryptionKeyFromSecurityConfFile() (key string, err error) {
secFile, err := coreutils.GetJfrogSecurityConfFilePath()
if err != nil {
return "", err
}
exists, err := fileutils.IsFileExists(secFile, false)
if err != nil || !exists {
return "", err
}
config := viper.New()
config.SetConfigType("yaml")
f, err := os.Open(secFile)
defer ioutils.Close(f, &err)
if err != nil {
return "", errorutils.CheckError(err)
}
err = config.ReadConfig(f)
if err != nil {
return "", errorutils.CheckError(err)
}
key = config.GetString(masterKeyField)
if key == "" {
return "", errorutils.CheckErrorf(decryptErrorPrefix + "security configuration file does not contain an encryption master key")
}
return key, nil
}
func readMasterKeyFromConsole() (string, error) {
log.Output("Please enter the master key: ")
bytePassword, err := term.ReadPassword(int(syscall.Stdin)) //nolint:unconvert
if err != nil {
return "", errorutils.CheckError(err)
}
// New-line required after the input:
log.Output()
return string(bytePassword), nil
}
func encrypt(secret string, key string) (string, error) {
if secret == "" {
return "", nil
}
if len(key) != 32 {
return "", errorutils.CheckErrorf(encryptErrorPrefix + "Wrong length for master key. Key should have a length of exactly: " + strconv.Itoa(masterKeyLength) + " bytes")
}
c, err := aes.NewCipher([]byte(key))
if err != nil {
return "", errorutils.CheckError(err)
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return "", errorutils.CheckError(err)
}
nonce := make([]byte, gcm.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
return "", errorutils.CheckError(err)
}
sealed := gcm.Seal(nonce, nonce, []byte(secret), nil)
return base64.StdEncoding.EncodeToString(sealed), nil
}
func decrypt(encryptedSecret string, key string) (string, error) {
if encryptedSecret == "" {
return "", nil
}
cipherText, err := base64.StdEncoding.DecodeString(encryptedSecret)
if err != nil {
return "", errorutils.CheckError(err)
}
c, err := aes.NewCipher([]byte(key))
if err != nil {
return "", errorutils.CheckError(err)
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return "", errorutils.CheckError(err)
}
nonceSize := gcm.NonceSize()
if len(cipherText) < nonceSize {
return "", errorutils.CheckErrorf(decryptErrorPrefix + "unexpected cipher text size")
}
nonce, cipherText := cipherText[:nonceSize], cipherText[nonceSize:]
plaintext, err := gcm.Open(nil, nonce, cipherText, nil)
if err != nil {
return "", errorutils.CheckError(err)
}
return string(plaintext), nil
}