-
Notifications
You must be signed in to change notification settings - Fork 20
/
path_venafi_secrets.go
349 lines (308 loc) · 10.5 KB
/
path_venafi_secrets.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package pki
import (
"context"
"fmt"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
func pathCredentialsList(b *backend) *framework.Path {
return &framework.Path{
Pattern: CredentialsRootPath + "?$",
Fields: nil,
Operations: map[logical.Operation]framework.OperationHandler{
logical.ListOperation: &framework.PathOperation{
Callback: b.pathVenafiSecretList,
Summary: "List all venafi secrets",
},
},
HelpSynopsis: pathListVenafiSecretsHelpSyn,
HelpDescription: pathListVenafiSecretsHelpDesc,
}
}
func pathCredentials(b *backend) *framework.Path {
return &framework.Path{
Pattern: CredentialsRootPath + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{
"name": {
Type: framework.TypeString,
Description: "Name of the authentication object",
Required: true,
},
"zone": {
Type: framework.TypeString,
Description: `Name of Venafi Platform policy or Venafi Cloud project zone.
Example for Platform: testpolicy\\vault
Example for Venafi Cloud: e33f3e40-4e7e-11ea-8da3-b3c196ebeb0b`,
Required: true,
},
"tpp_url": {
Type: framework.TypeString,
Description: `URL of Venafi Platform. Example: https://tpp.venafi.example/vedsdk. Deprecated, use 'url' instead`,
Deprecated: true,
},
"url": {
Type: framework.TypeString,
Description: `URL of Venafi API Endpoint. Example: https://tpp.venafi.example`,
Required: true,
},
"cloud_url": {
Type: framework.TypeString,
Description: `URL for Venafi Cloud. Set it only if you want to use non production Cloud. Deprecated, use 'url' instead`,
Deprecated: true,
},
"tpp_user": {
Type: framework.TypeString,
Description: `WebSDK username for Venafi Platform API`,
Deprecated: true,
},
"tpp_password": {
Type: framework.TypeString,
Description: `Password for WebSDK user`,
Deprecated: true,
},
"access_token": {
Type: framework.TypeString,
Description: `Access token for TPP, user should use this for authentication`,
},
"refresh_token": {
Type: framework.TypeString,
Description: `Refresh token for updating TPP access token when it expires`,
},
"apikey": {
Type: framework.TypeString,
Description: `API key for Venafi Cloud. Example: 142231b7-cvb0-412e-886b-6aeght0bc93d`,
},
"trust_bundle_file": {
Type: framework.TypeString,
Description: `Use to specify a PEM formatted file with certificates to be used as trust anchors when communicating with the remote server.
Example: trust_bundle_file="/path-to/bundle.pem""`,
},
"fakemode": {
Type: framework.TypeBool,
Description: `Set it to true to use fake CA instead of Cloud or Platform to issue certificates. Useful for testing.`,
Default: false,
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.ReadOperation: &framework.PathOperation{
Callback: b.pathVenafiSecretRead,
Summary: "Read the properties of a venafi secret and displays it to the user.",
},
logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathVenafiSecretCreate,
Summary: "Create a venafi secret",
},
logical.DeleteOperation: &framework.PathOperation{
Callback: b.pathVenafiSecretDelete,
Summary: "Delete a venafi secret",
},
},
HelpSynopsis: pathVenafiSecretsHelpSyn,
HelpDescription: pathVenafiSecretsHelpDesc,
}
}
const (
CredentialsRootPath = `venafi/`
tokenMode = `TPP Token (access_token, refresh_token)` // #nosec G101
tppMode = `TPP Credentials (tpp_user, tpp_password)`
cloudMode = `Cloud API Key (apikey)`
errorMultiModeMessage = `can't specify both: %s and %s modes in the same venafi secret`
errorTextURLEmpty = `"url" argument is required`
errorTextZoneEmpty = `"zone" argument is required`
errorTextInvalidMode = "invalid mode: fakemode or apikey or tpp credentials or tpp access token required"
)
var (
errorTextMixedTPPAndToken = fmt.Sprintf(errorMultiModeMessage, tppMode, tokenMode)
errorTextMixedTPPAndCloud = fmt.Sprintf(errorMultiModeMessage, tppMode, cloudMode)
errorTextMixedTokenAndCloud = fmt.Sprintf(errorMultiModeMessage, tokenMode, cloudMode)
)
func (b *backend) pathVenafiSecretList(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
entries, err := req.Storage.List(ctx, CredentialsRootPath)
if err != nil {
return nil, err
}
return logical.ListResponse(entries), nil
}
func (b *backend) pathVenafiSecretRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
policyName := data.Get("name").(string)
if policyName == "" {
return logical.ErrorResponse("missing policy name"), nil
}
cred, err := b.getVenafiSecret(ctx, req.Storage, policyName)
if err != nil {
return nil, err
}
if cred == nil {
return nil, nil
}
resp := &logical.Response{
Data: cred.ToResponseData(),
}
return resp, nil
}
func (b *backend) pathVenafiSecretDelete(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
err := req.Storage.Delete(ctx, CredentialsRootPath+data.Get("name").(string))
if err != nil {
return nil, err
}
return nil, nil
}
func (b *backend) pathVenafiSecretCreate(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
var err error
name := data.Get("name").(string)
url := data.Get("url").(string)
var tppUrl, cloudUrl string
if url == "" {
tppUrl = data.Get("tpp_url").(string)
url = tppUrl
}
if url == "" {
cloudUrl = data.Get("cloud_url").(string)
url = cloudUrl
}
entry := &venafiSecretEntry{
URL: url,
Zone: data.Get("zone").(string),
TppURL: tppUrl,
TppUser: data.Get("tpp_user").(string),
TppPassword: data.Get("tpp_password").(string),
AccessToken: data.Get("access_token").(string),
RefreshToken: data.Get("refresh_token").(string),
CloudURL: cloudUrl,
Apikey: data.Get("apikey").(string),
TrustBundleFile: data.Get("trust_bundle_file").(string),
Fakemode: data.Get("fakemode").(bool),
}
err = validateVenafiSecretEntry(entry)
if err != nil {
return logical.ErrorResponse(err.Error()), nil
}
//Store it
jsonEntry, err := logical.StorageEntryJSON(CredentialsRootPath+name, entry)
if err != nil {
return nil, err
}
err = req.Storage.Put(ctx, jsonEntry)
if err != nil {
return nil, err
}
var logResp *logical.Response
warnings := getWarnings(entry, name)
if cap(warnings) > 0 {
logResp = &logical.Response{
Data: map[string]interface{}{},
Redirect: "",
Warnings: warnings,
}
return logResp, nil
}
return nil, nil
}
func (b *backend) getVenafiSecret(ctx context.Context, s logical.Storage, name string) (*venafiSecretEntry, error) {
entry, err := s.Get(ctx, CredentialsRootPath+name)
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
var result venafiSecretEntry
err = entry.DecodeJSON(&result)
if err != nil {
return nil, err
}
return &result, nil
}
func validateVenafiSecretEntry(entry *venafiSecretEntry) error {
if !entry.Fakemode && entry.Apikey == "" && (entry.TppUser == "" || entry.TppPassword == "") && entry.AccessToken == "" {
return fmt.Errorf(errorTextInvalidMode)
}
//Only validate other fields if mode is not fakemode
if !entry.Fakemode {
//When api key is null, that means
if entry.URL == "" && entry.Apikey == "" {
return fmt.Errorf(errorTextURLEmpty)
}
if entry.Zone == "" {
return fmt.Errorf(errorTextZoneEmpty)
}
if entry.TppUser != "" && entry.Apikey != "" {
return fmt.Errorf(errorTextMixedTPPAndCloud)
}
if entry.TppUser != "" && entry.AccessToken != "" {
return fmt.Errorf(errorTextMixedTPPAndToken)
}
if entry.AccessToken != "" && entry.Apikey != "" {
return fmt.Errorf(errorTextMixedTokenAndCloud)
}
}
return nil
}
func getWarnings(entry *venafiSecretEntry, name string) []string {
warnings := []string{}
if entry.TppURL != "" {
warnings = append(warnings, "tpp_url is deprecated, please use url instead")
}
if entry.CloudURL != "" {
warnings = append(warnings, "cloud_url is deprecated, please use url instead")
}
if entry.TppUser != "" {
warnings = append(warnings, "tpp_user is deprecated, please use access_token token instead")
}
if entry.TppPassword != "" {
warnings = append(warnings, "tpp_password is deprecated, please use access_token instead")
}
//Include success message in warnings
if len(warnings) > 0 {
warnings = append(warnings, "Venafi secret "+name+" saved successfully")
}
return warnings
}
type venafiSecretEntry struct {
URL string `json:"url"`
Zone string `json:"zone"`
TppURL string `json:"tpp_url"`
TppUser string `json:"tpp_user"`
TppPassword string `json:"tpp_password"`
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
CloudURL string `json:"cloud_url"`
Apikey string `json:"apikey"`
TrustBundleFile string `json:"trust_bundle_file"`
Fakemode bool `json:"fakemode"`
}
func (p *venafiSecretEntry) ToResponseData() map[string]interface{} {
var tppPass, accessToken, refreshToken, apiKey string
if p.TppPassword != "" {
tppPass = "********"
}
if p.AccessToken != "" {
accessToken = "********"
}
if p.RefreshToken != "" {
refreshToken = "********"
}
if p.Apikey != "" {
apiKey = "********"
}
responseData := map[string]interface{}{
//Sensible data will not be returned.
//tpp_password, api_key, access_token, refresh_token
"url": p.URL,
"zone": p.Zone,
"tpp_user": p.TppUser,
"tpp_password": tppPass,
"access_token": accessToken,
"refresh_token": refreshToken,
"apikey": apiKey,
"trust_bundle_file": p.TrustBundleFile,
"fakemode": p.Fakemode,
}
return responseData
}
const (
pathListVenafiSecretsHelpSyn = `List the existing Venafi Secrets in this backend` // #nosec
pathListVenafiSecretsHelpDesc = `Venafi Secrets will be listed by the secret name.` // #nosec
pathVenafiSecretsHelpSyn = `Manage the Venafi Secrets that can be created with this backend.` // #nosec
pathVenafiSecretsHelpDesc = `This path lets you manage the Venafi Secrets that can be created with this backend.` // #nosec
)