-
Notifications
You must be signed in to change notification settings - Fork 6
/
op_config_ca.go
132 lines (108 loc) · 3.18 KB
/
op_config_ca.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
/*
* Copyright (c) 2019 Entrust Datacard Corporation.
* All rights reserved.
*/
package main
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
)
func (b *backend) opWriteConfigRole(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
roleName := data.Get("roleName").(string)
caId := data.Get("ca_id").(string)
profileId := data.Get("profile_id").(string)
certPem := data.Get("pem_bundle").(string)
url := data.Get("url").(string)
caCertPem := data.Get("cacerts").(string)
b.Logger().Info(fmt.Sprintf(`
Configuring new CA role configuration
role name: %s
ca id: %s
profile id: %s
url: %s
`,
roleName, caId, profileId, url))
if len(roleName) == 0 {
return logical.ErrorResponse("must provide name for role configuration"), nil
}
if len(certPem) == 0 {
return logical.ErrorResponse("must provide PEM encoded certificate"), nil
}
if len(caId) == 0 {
caId = roleName
}
if len(url) == 0 {
return logical.ErrorResponse("must provide gateway URL"), nil
}
if len(caCertPem) == 0 {
return logical.ErrorResponse("must provide gateway CA certificate"), nil
}
configCa := &CAGWConfigRole{
certPem,
url,
caCertPem,
caId,
profileId,
}
profiles, err := configCa.ProfileIDs(ctx, req, data, caId)
if err != nil {
return logical.ErrorResponse("error fetching profile configurations from CAGW: " + err.Error()), err
}
caAndProfiles := CAGWConfigCAConfigProfileIDs{
*configCa,
profiles,
}
if len(profileId) > 0 {
profile, err := findProfile(profiles, profileId)
if err != nil {
return logical.ErrorResponse("Profile with ID " + profileId + " not found for CA " + caId), nil
}
caAndProfiles.Profiles = []CAGWConfigProfileID{*profile}
} else {
caAndProfiles.Profiles = profiles
}
storageEntry, err := logical.StorageEntryJSON("config/"+roleName, caAndProfiles)
if err != nil {
return logical.ErrorResponse("error creating config storage entry: " + err.Error()), err
}
err = req.Storage.Put(ctx, storageEntry)
if err != nil {
return logical.ErrorResponse("could not store configuration: " + err.Error()), err
}
respData := map[string]interface{}{
"Message": "Configuration successful",
"RoleName": roleName,
"CaId": caId,
"URL": url,
}
return &logical.Response{
Data: respData,
}, nil
}
func (b *backend) opReadConfigRole(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
roleName := data.Get("roleName").(string)
storageEntry, err := req.Storage.Get(ctx, "config/"+roleName)
if err != nil {
return logical.ErrorResponse("could not read configuration: " + err.Error()), err
}
var rawData map[string]interface{}
err = storageEntry.DecodeJSON(&rawData)
if err != nil {
return logical.ErrorResponse("json decoding failed: " + err.Error()), err
}
resp := &logical.Response{
Data: rawData,
}
return resp, nil
}
func findProfile(profiles []CAGWConfigProfileID, profileId string) (*CAGWConfigProfileID, error) {
for _, v := range profiles {
if v.Id == profileId {
return &v, nil
}
}
return nil, errors.New("Could not find profile")
}