-
Notifications
You must be signed in to change notification settings - Fork 2
/
vaultkv2.go
335 lines (272 loc) · 8.43 KB
/
vaultkv2.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package cryptoengines
import (
"context"
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/hashicorp/vault/api"
"github.com/lamassuiot/lamassuiot/v2/pkg/config"
"github.com/lamassuiot/lamassuiot/v2/pkg/helpers"
"github.com/lamassuiot/lamassuiot/v2/pkg/models"
"github.com/sirupsen/logrus"
)
var lVault *logrus.Entry
type VaultKV2Engine struct {
kvv2Client *api.KVv2
}
func NewVaultKV2Engine(logger *logrus.Entry, conf config.HashicorpVaultCryptoEngineConfig) (CryptoEngine, error) {
var err error
lVault = logger.WithField("subsystem-provider", "Vault-KV2")
address := fmt.Sprintf("%s://%s:%d", conf.Protocol, conf.Hostname, conf.Port)
lVault.Debugf("configuring VaultKV2 Engine")
vaultClientConf := api.DefaultConfig()
httpClient, err := helpers.BuildHTTPClientWithTLSOptions(&http.Client{}, conf.TLSConfig)
if err != nil {
return nil, err
}
httpClient, err = helpers.BuildHTTPClientWithTracerLogger(httpClient, lVault)
if err != nil {
return nil, err
}
vaultClientConf.HttpClient = httpClient
vaultClientConf.Address = address
vaultClient, err := api.NewClient(vaultClientConf)
if err != nil {
lVault.Errorf("could not create Vault API client: %s", err)
return nil, errors.New("could not create Vault API client: " + err.Error())
}
if conf.AutoUnsealEnabled {
err = Unseal(vaultClient, conf.AutoUnsealKeys)
if err != nil {
lVault.Errorf("could not unseal Vault: %s", err)
return nil, errors.New("could not unseal Vault: " + err.Error())
}
}
err = Login(vaultClient, conf.RoleID, string(conf.SecretID))
if err != nil {
lVault.Errorf("could not login into Vault: %s", err)
return nil, errors.New("could not login into Vault: " + err.Error())
}
mounts, err := vaultClient.Sys().ListMounts()
if err != nil {
return nil, err
}
hasMount := false
for mountPath := range mounts {
if mountPath == conf.MountPath {
hasMount = true
}
}
if !hasMount {
err = vaultClient.Sys().Mount(conf.MountPath, &api.MountInput{
Type: "kv-v2",
})
if err != nil {
return nil, err
}
}
kv2 := vaultClient.KVv2(conf.MountPath)
return &VaultKV2Engine{
kvv2Client: kv2,
}, nil
}
func (vaultCli *VaultKV2Engine) GetEngineConfig() models.CryptoEngineInfo {
return models.CryptoEngineInfo{
Type: models.VaultKV2,
SecurityLevel: models.SL1,
Provider: "Hashicorp",
Name: "Key Value - V2",
Metadata: map[string]any{},
SupportedKeyTypes: []models.SupportedKeyTypeInfo{
{
Type: models.KeyType(x509.RSA),
Sizes: []int{
2048,
3072,
4096,
},
},
{
Type: models.KeyType(x509.ECDSA),
Sizes: []int{
224,
256,
521,
},
},
},
}
}
func (vaultCli *VaultKV2Engine) GetPrivateKeyByID(keyID string) (crypto.Signer, error) {
lVault.Debugf("requesting private key with ID [%s]", keyID)
key, err := vaultCli.kvv2Client.Get(context.Background(), keyID)
if err != nil {
lVault.Errorf("could not get private key: %s", err)
return nil, errors.New("could not get private key")
}
lVault.Debugf("successfully retrieved private key")
var b64Key string
mapValue, ok := key.Data["key"]
if !ok {
return nil, fmt.Errorf("'key' not found in secret")
}
if b64Key, ok = mapValue.(string); !ok {
return nil, fmt.Errorf("'key' not in string format")
}
pemBytes, err := base64.StdEncoding.DecodeString(b64Key)
if err != nil {
return nil, err
}
block, _ := pem.Decode(pemBytes)
if block == nil {
return nil, fmt.Errorf("no key found")
}
switch block.Type {
case "RSA PRIVATE KEY":
return x509.ParsePKCS1PrivateKey(block.Bytes)
case "EC PRIVATE KEY":
return x509.ParseECPrivateKey(block.Bytes)
default:
return nil, fmt.Errorf("unsupported key type %q", block.Type)
}
}
func (vaultCli *VaultKV2Engine) CreateRSAPrivateKey(keySize int, keyID string) (crypto.Signer, error) {
lVault.Debugf("creating RSA private key of size [%d] with ID [%s]", keySize, keyID)
key, err := vaultCli.GetPrivateKeyByID(keyID)
if key != nil {
lVault.Warnf("RSA private key already exists and will be overwritten: %s", err)
err = vaultCli.DeleteKey(keyID)
if err != nil {
return nil, err
}
}
rsaKey, err := rsa.GenerateKey(rand.Reader, keySize)
if err != nil {
lVault.Errorf("could not create RSA private key: %s", err)
return nil, err
}
//output, err := client.Logical().Write("secret/data/abd", inputData)
keyPem := pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(rsaKey),
})
keyBase64 := base64.StdEncoding.EncodeToString([]byte(keyPem))
var keyMap = map[string]interface{}{
"key": keyBase64,
}
_, err = vaultCli.kvv2Client.Put(context.Background(), keyID, keyMap)
if err != nil {
lVault.Errorf("could not create RSA key: %s", err)
return nil, err
}
lVault.Debugf("RSA key successfully generated")
return rsaKey, nil
}
func (vaultCli *VaultKV2Engine) CreateECDSAPrivateKey(c elliptic.Curve, keyID string) (crypto.Signer, error) {
lVault.Debugf("creating ECDSA private key of size [%d] with ID [%s]", c.Params().BitSize, keyID)
key, err := ecdsa.GenerateKey(c, rand.Reader)
if err != nil {
lVault.Errorf("Could not create RSA private key: %s", err)
return nil, err
}
keyBytes, err := x509.MarshalECPrivateKey(key)
if err != nil {
lVault.Errorf("Could not create RSA private key: %s", err)
return nil, err
}
keyPem := pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: keyBytes})
keyBase64 := base64.StdEncoding.EncodeToString([]byte(keyPem))
var keyMap = map[string]interface{}{
"key": keyBase64,
}
_, err = vaultCli.kvv2Client.Put(context.Background(), keyID, keyMap)
return key, err
}
func (vaultCli *VaultKV2Engine) ImportRSAPrivateKey(key *rsa.PrivateKey, keyID string) (crypto.Signer, error) {
keyPem := pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(key),
})
keyBase64 := base64.StdEncoding.EncodeToString([]byte(keyPem))
var keyMap = map[string]interface{}{
"key": keyBase64,
}
_, err := vaultCli.kvv2Client.Put(context.Background(), keyID, keyMap)
if err != nil {
lVault.Errorf("could not save the private key in vault: %s", err)
return nil, err
}
lVault.Debugf("RSA key successfully imported")
return vaultCli.GetPrivateKeyByID(keyID)
}
func (vaultCli *VaultKV2Engine) ImportECDSAPrivateKey(key *ecdsa.PrivateKey, keyID string) (crypto.Signer, error) {
keyBytes, err := x509.MarshalECPrivateKey(key)
if err != nil {
lVault.Errorf("Could not create RSA private key: %s", err)
return nil, err
}
keyPem := pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: keyBytes})
keyBase64 := base64.StdEncoding.EncodeToString([]byte(keyPem))
var keyMap = map[string]interface{}{
"key": keyBase64,
}
_, err = vaultCli.kvv2Client.Put(context.Background(), keyID, keyMap)
if err != nil {
lVault.Errorf("Could not save the private key in vault: %s", err)
return nil, err
}
lVault.Debugf("ECDSA key successfully imported")
return vaultCli.GetPrivateKeyByID(keyID)
}
func (vaultCli *VaultKV2Engine) DeleteKey(keyID string) error {
err := vaultCli.kvv2Client.Delete(context.Background(), keyID)
return err
}
// ---------------------
func CreateVaultSdkClient(httpClient *http.Client, vaultAddress string) (*api.Client, error) {
conf := api.DefaultConfig()
conf.HttpClient = httpClient
conf.Address = strings.ReplaceAll(conf.Address, "https://127.0.0.1:8200", vaultAddress)
return api.NewClient(conf)
}
func Unseal(client *api.Client, unsealKeys []config.Password) error {
providedSharesCount := 0
sealed := true
for sealed {
unsealStatusProgress, err := client.Sys().Unseal(string(unsealKeys[providedSharesCount]))
if err != nil {
lVault.Error("Error while unsealing vault: ", err)
return err
}
lVault.Info("Unseal progress shares=" + strconv.Itoa(unsealStatusProgress.N) + " threshold=" + strconv.Itoa(unsealStatusProgress.T) + " remaining_shares=" + strconv.Itoa(unsealStatusProgress.Progress))
providedSharesCount++
if !unsealStatusProgress.Sealed {
lVault.Info("Vault is unsealed")
sealed = false
}
}
return nil
}
func Login(client *api.Client, roleID string, secretID string) error {
loginPath := "auth/approle/login"
options := map[string]interface{}{
"role_id": roleID,
"secret_id": secretID,
}
resp, err := client.Logical().Write(loginPath, options)
if err != nil {
return err
}
client.SetToken(resp.Auth.ClientToken)
return nil
}