-
Notifications
You must be signed in to change notification settings - Fork 10
/
vault.go
160 lines (141 loc) · 4 KB
/
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
package vaultlib
import (
"encoding/json"
"strings"
"github.com/pkg/errors"
)
// Secret holds the secret.
//
// KV contains data in case of KV secret.
//
// JSONSecret contains data in case of JSON raw secret.
type Secret struct {
KV map[string]string
JSONSecret json.RawMessage
}
type rawSecretData struct {
Data json.RawMessage
}
// GetSecret returns the Vault secret object
//
// KV: map[string]string if the secret is a KV
//
// JSONSecret: json.RawMessage if the secret is a json
func (c *Client) GetSecret(path string) (secret Secret, err error) {
var v2Secret vaultSecretKV2
var vaultRsp rawSecretData
secret.KV = make(map[string]string)
kvVersion, kvName, err := c.getKVInfo(path)
if err != nil {
return secret, errors.Wrap(errors.WithStack(err), errInfo())
}
url := c.address.String()
if kvVersion == "2" {
url = url + "/v1/" + kvName + "data/" + strings.TrimPrefix(path, kvName)
} else {
url = url + "/v1/" + path
}
req, _ := c.newRequest("GET", url)
rsp, err := req.execute()
if err != nil {
return secret, errors.Wrap(errors.WithStack(err), errInfo())
}
if kvVersion == "2" {
err = json.Unmarshal([]byte(rsp.Data), &v2Secret)
if err != nil {
return secret, errors.Wrap(errors.WithStack(err), errInfo())
}
for k, v := range v2Secret.Data {
switch t := v.(type) {
case string:
secret.KV[k] = t
case interface{}:
//secret.JSONSecret = rsp.Data
//Parse twice to remove
err = json.Unmarshal([]byte(rsp.Data), &vaultRsp)
if err != nil {
return secret, err
}
err := json.Unmarshal([]byte(vaultRsp.Data), &secret.JSONSecret)
if err != nil {
return secret, err
}
return secret, err
}
}
} else if kvVersion == "1" {
raw := make(map[string]interface{})
err = json.Unmarshal([]byte(rsp.Data), &raw)
if err != nil {
return secret, errors.Wrap(errors.WithStack(err), errInfo())
}
for k, v := range raw {
switch t := v.(type) {
case string:
secret.KV[k] = t
case interface{}:
secret.JSONSecret = rsp.Data
return secret, err
}
}
}
return secret, nil
}
// vaultMountResponse holds the Vault Mount list response (used to unmarshall the global vault response)
type vaultMountResponse struct {
Auth json.RawMessage `json:"auth"`
Secret json.RawMessage `json:"secret"`
}
// vaultSecretMounts holds the vault secret engine def
type vaultSecretMounts struct {
Name string `json:"??,string"`
Accessor string `json:"accessor"`
Config struct {
DefaultLeaseTTL int `json:"default_lease_ttl"`
ForceNoCache bool `json:"force_no_cache"`
MaxLeaseTTL int `json:"max_lease_ttl"`
PluginName string `json:"plugin_name"`
} `json:"config"`
Description string `json:"description"`
Local bool `json:"local"`
Options map[string]interface{} `json:"options"`
SealWrap bool `json:"seal_wrap"`
Type string `json:"type"`
}
func (c *Client) getKVInfo(path string) (version, name string, err error) {
var mountResponse vaultMountResponse
var vaultSecretMount = make(map[string]vaultSecretMounts)
url := c.address.String() + "/v1/sys/internal/ui/mounts"
req, _ := c.newRequest("GET", url)
rsp, err := req.execute()
if err != nil {
return "", "", errors.Wrap(errors.WithStack(err), errInfo())
}
jsonErr := json.Unmarshal([]byte(rsp.Data), &mountResponse)
if jsonErr != nil {
return "", "", errors.Wrap(errors.WithStack(err), errInfo())
}
jsonErr = json.Unmarshal([]byte(mountResponse.Secret), &vaultSecretMount)
if jsonErr != nil {
return "", "", errors.Wrap(errors.WithStack(err), errInfo())
}
for kvName, v := range vaultSecretMount {
if strings.HasPrefix(path, kvName) {
name = kvName
if len(v.Options) > 0 {
switch v.Options["version"].(type) {
case string:
version = v.Options["version"].(string)
}
} else {
//kv v1
version = "1"
}
break
}
}
if len(version) == 0 {
return "", "", errors.New("Could not get kv version")
}
return version, name, nil
}