forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_config_urls.go
167 lines (142 loc) · 4.43 KB
/
path_config_urls.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
package pki
import (
"fmt"
"strings"
"github.com/asaskevich/govalidator"
"github.com/fatih/structs"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
)
func pathConfigURLs(b *backend) *framework.Path {
return &framework.Path{
Pattern: "config/urls",
Fields: map[string]*framework.FieldSchema{
"issuing_certificates": &framework.FieldSchema{
Type: framework.TypeString,
Description: `Comma-separated list of URLs to be used
for the issuing certificate attribute`,
},
"crl_distribution_points": &framework.FieldSchema{
Type: framework.TypeString,
Description: `Comma-separated list of URLs to be used
for the CRL distribution points attribute`,
},
"ocsp_servers": &framework.FieldSchema{
Type: framework.TypeString,
Description: `Comma-separated list of URLs to be used
for the OCSP servers attribute`,
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathWriteURL,
logical.ReadOperation: b.pathReadURL,
},
HelpSynopsis: pathConfigURLsHelpSyn,
HelpDescription: pathConfigURLsHelpDesc,
}
}
func validateURLs(urls []string) string {
for _, curr := range urls {
if !govalidator.IsURL(curr) {
return curr
}
}
return ""
}
func getURLs(req *logical.Request) (*urlEntries, error) {
entry, err := req.Storage.Get("urls")
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
var entries urlEntries
if err := entry.DecodeJSON(&entries); err != nil {
return nil, err
}
return &entries, nil
}
func writeURLs(req *logical.Request, entries *urlEntries) error {
entry, err := logical.StorageEntryJSON("urls", entries)
if err != nil {
return err
}
if entry == nil {
return fmt.Errorf("Unable to marshal entry into JSON")
}
err = req.Storage.Put(entry)
if err != nil {
return err
}
return nil
}
func (b *backend) pathReadURL(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
entries, err := getURLs(req)
if err != nil {
return nil, err
}
if entries == nil {
return nil, nil
}
resp := &logical.Response{
Data: structs.New(entries).Map(),
}
return resp, nil
}
func (b *backend) pathWriteURL(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
entries, err := getURLs(req)
if err != nil {
return nil, err
}
if entries == nil {
entries = &urlEntries{
IssuingCertificates: []string{},
CRLDistributionPoints: []string{},
OCSPServers: []string{},
}
}
if urlsInt, ok := data.GetOk("issuing_certificates"); ok {
splitURLs := strings.Split(urlsInt.(string), ",")
entries.IssuingCertificates = splitURLs
if badURL := validateURLs(entries.IssuingCertificates); badURL != "" {
return logical.ErrorResponse(fmt.Sprintf(
"invalid URL found in issuing certificates: %s", badURL)), nil
}
}
if urlsInt, ok := data.GetOk("crl_distribution_points"); ok {
splitURLs := strings.Split(urlsInt.(string), ",")
entries.CRLDistributionPoints = splitURLs
if badURL := validateURLs(entries.CRLDistributionPoints); badURL != "" {
return logical.ErrorResponse(fmt.Sprintf(
"invalid URL found in CRL distribution points: %s", badURL)), nil
}
}
if urlsInt, ok := data.GetOk("ocsp_servers"); ok {
splitURLs := strings.Split(urlsInt.(string), ",")
entries.OCSPServers = splitURLs
if badURL := validateURLs(entries.OCSPServers); badURL != "" {
return logical.ErrorResponse(fmt.Sprintf(
"invalid URL found in OCSP servers: %s", badURL)), nil
}
}
return nil, writeURLs(req, entries)
}
type urlEntries struct {
IssuingCertificates []string `json:"issuing_certificates" structs:"issuing_certificates" mapstructure:"issuing_certificates"`
CRLDistributionPoints []string `json:"crl_distribution_points" structs:"crl_distribution_points" mapstructure:"crl_distribution_points"`
OCSPServers []string `json:"ocsp_servers" structs:"ocsp_servers" mapstructure:"ocsp_servers"`
}
const pathConfigURLsHelpSyn = `
Set the URLs for the issuing CA, CRL distribution points, and OCSP servers.
`
const pathConfigURLsHelpDesc = `
This path allows you to set the issuing CA, CRL distribution points, and
OCSP server URLs that will be encoded into issued certificates. If these
values are not set, no such information will be encoded in the issued
certificates. To delete URLs, simply re-set the appropriate value with an
empty string.
Multiple URLs can be specified for each type; use commas to separate them.
`