-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconfig.go
80 lines (66 loc) · 2.21 KB
/
config.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
/*
* Copyright (c) 2019 Entrust Datacard Corporation.
* All rights reserved.
*/
package main
import (
"context"
"fmt"
"time"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
"github.com/pkg/errors"
)
func getConfigRole(ctx context.Context, req *logical.Request, roleName string) (*CAGWConfigRole, error) {
storageEntry, err := req.Storage.Get(ctx, "config/"+roleName)
if err != nil {
return nil, errors.Wrapf(err, "config/%s configuration could not be loaded", roleName)
}
configRole := CAGWConfigRole{}
if storageEntry != nil {
err = storageEntry.DecodeJSON(&configRole)
if err != nil {
return nil, errors.Wrapf(err, "config/%s configuration could not be parsed", roleName)
}
} else {
return nil, errors.Errorf("config/%s could not be found", roleName)
}
return &configRole, nil
}
func getConfigProfile(ctx context.Context, req *logical.Request, roleName string, profileId string) (*CAGWConfigProfile, error) {
profileStorageEntry, err := req.Storage.Get(ctx, "config/"+roleName+"/profiles/"+profileId)
if err != nil {
return nil, errors.Wrapf(err, "config/%s/profiles/%s could not be loaded", roleName, profileId)
}
configProfile := CAGWConfigProfile{}
// If there is a storage entry, decode it, else use defaults
if profileStorageEntry != nil {
err = profileStorageEntry.DecodeJSON(&configProfile)
if err != nil {
return nil, errors.Wrapf(err, "config/%s/profiles/%s could not be parsed", roleName, profileId)
}
} else {
return nil, errors.Errorf("config/%s/profiles/%s could not be found", roleName, profileId)
}
return &configProfile, nil
}
func getFormat(data *framework.FieldData) (*string, error) {
format := data.Get("format").(string)
if len(format) <= 0 {
format = "pem"
}
if format != "pem" && format != "pem_bundle" && format != "der" {
return nil, errors.New(fmt.Sprintf("Invalid format specified: %s", format))
}
return &format, nil
}
func getTTL(data *framework.FieldData, configProfile *CAGWConfigProfile) time.Duration {
ttl := time.Duration(data.Get("ttl").(int)) * time.Second
if ttl <= 0 {
ttl = configProfile.TTL
}
if configProfile.MaxTTL > 0 && ttl > configProfile.MaxTTL {
ttl = configProfile.MaxTTL
}
return ttl
}