-
Notifications
You must be signed in to change notification settings - Fork 6
/
op_config_profile.go
101 lines (78 loc) · 2.52 KB
/
op_config_profile.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
/*
* Copyright (c) 2019 Entrust Datacard Corporation.
* All rights reserved.
*/
package main
import (
"context"
"fmt"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
)
func (b *backend) opWriteConfigProfile(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
roleName := data.Get("roleName").(string)
id := getProfileId(data)
profileId := CAGWConfigProfileID{id, ""}
profile, err := profileId.Profile(ctx, req, data)
if err != nil {
return logical.ErrorResponse(fmt.Sprintf("Error retrieving the profile properties from CAGW: %s", err)), err
}
storageEntry, err := logical.StorageEntryJSON("config/"+roleName+"/profiles/"+profile.Id, profile)
if err != nil {
return logical.ErrorResponse("error creating config storage entry for profile"), err
}
err = req.Storage.Put(ctx, storageEntry)
if err != nil {
return logical.ErrorResponse("could not store configuration"), err
}
respData := map[string]interface{}{
"Message": "Configuration successful",
"Profile ID": profile.Id,
"Profile Name": profile.Name,
"Subject Variable Requirements": profile.SubjectVariableRequirements,
"Subject Alt Name Requirements": profile.SubjectAltNameRequirements,
}
return &logical.Response{
Data: respData,
}, nil
}
func (b *backend) opReadConfigProfile(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
roleName := data.Get("roleName").(string)
profileId := getProfileId(data)
configCa, err := getConfigRole(ctx, req, roleName)
if err != nil {
return logical.ErrorResponse("invalid CAGW role configuration"), err
}
if len(profileId) <= 0 {
profileId = configCa.ProfileId
if len(profileId) <= 0 {
return logical.ErrorResponse("missing the profile ID"), nil
}
}
storageEntry, err := req.Storage.Get(ctx, "config/"+roleName+"/profiles/"+profileId)
if err != nil {
return logical.ErrorResponse("could not read configuration"), err
}
if storageEntry == nil {
return logical.ErrorResponse("could not find configuration"), nil
}
var rawData map[string]interface{}
err = storageEntry.DecodeJSON(&rawData)
if err != nil {
return logical.ErrorResponse("json decoding failed"), err
}
resp := &logical.Response{
Data: rawData,
}
return resp, nil
}
func getProfileId(data *framework.FieldData) string {
idInt, flag := data.GetOk("profile")
var id string
if !flag {
id = ""
} else {
id = idInt.(string)
}
return id
}