open-vault/builtin/logical/pki/path_config_urls.go

158 lines
4.1 KiB
Go
Raw Normal View History

package pki
import (
"context"
"fmt"
"github.com/asaskevich/govalidator"
"github.com/fatih/structs"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/helper/certutil"
"github.com/hashicorp/vault/sdk/logical"
)
func pathConfigURLs(b *backend) *framework.Path {
return &framework.Path{
Pattern: "config/urls",
Fields: map[string]*framework.FieldSchema{
"issuing_certificates": {
Type: framework.TypeCommaStringSlice,
Description: `Comma-separated list of URLs to be used
for the issuing certificate attribute`,
},
"crl_distribution_points": {
Type: framework.TypeCommaStringSlice,
Description: `Comma-separated list of URLs to be used
for the CRL distribution points attribute`,
},
"ocsp_servers": {
Type: framework.TypeCommaStringSlice,
Description: `Comma-separated list of URLs to be used
for the OCSP servers attribute`,
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
2016-01-07 15:30:47 +00:00
logical.UpdateOperation: b.pathWriteURL,
2016-07-19 17:54:18 +00:00
logical.ReadOperation: b.pathReadURL,
},
HelpSynopsis: pathConfigURLsHelpSyn,
HelpDescription: pathConfigURLsHelpDesc,
}
}
func validateURLs(urls []string) string {
2015-11-16 19:19:10 +00:00
for _, curr := range urls {
if !govalidator.IsURL(curr) {
return curr
2015-11-16 19:19:10 +00:00
}
}
return ""
2015-11-16 19:19:10 +00:00
}
func getURLs(ctx context.Context, req *logical.Request) (*certutil.URLEntries, error) {
entry, err := req.Storage.Get(ctx, "urls")
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
var entries certutil.URLEntries
if err := entry.DecodeJSON(&entries); err != nil {
return nil, err
}
return &entries, nil
}
func writeURLs(ctx context.Context, req *logical.Request, entries *certutil.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(ctx, entry)
if err != nil {
return err
}
return nil
}
func (b *backend) pathReadURL(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
entries, err := getURLs(ctx, 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(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
entries, err := getURLs(ctx, req)
if err != nil {
return nil, err
}
if entries == nil {
entries = &certutil.URLEntries{
IssuingCertificates: []string{},
CRLDistributionPoints: []string{},
OCSPServers: []string{},
}
}
if urlsInt, ok := data.GetOk("issuing_certificates"); ok {
entries.IssuingCertificates = urlsInt.([]string)
2016-07-19 17:54:18 +00:00
if badURL := validateURLs(entries.IssuingCertificates); badURL != "" {
2015-11-16 19:19:10 +00:00
return logical.ErrorResponse(fmt.Sprintf(
2016-07-19 17:54:18 +00:00
"invalid URL found in issuing certificates: %s", badURL)), nil
2015-11-16 19:19:10 +00:00
}
}
if urlsInt, ok := data.GetOk("crl_distribution_points"); ok {
entries.CRLDistributionPoints = urlsInt.([]string)
2016-07-19 17:54:18 +00:00
if badURL := validateURLs(entries.CRLDistributionPoints); badURL != "" {
2015-11-16 19:19:10 +00:00
return logical.ErrorResponse(fmt.Sprintf(
2016-07-19 17:54:18 +00:00
"invalid URL found in CRL distribution points: %s", badURL)), nil
2015-11-16 19:19:10 +00:00
}
}
if urlsInt, ok := data.GetOk("ocsp_servers"); ok {
entries.OCSPServers = urlsInt.([]string)
2016-07-19 17:54:18 +00:00
if badURL := validateURLs(entries.OCSPServers); badURL != "" {
2015-11-16 19:19:10 +00:00
return logical.ErrorResponse(fmt.Sprintf(
2016-07-19 17:54:18 +00:00
"invalid URL found in OCSP servers: %s", badURL)), nil
2015-11-16 19:19:10 +00:00
}
}
return nil, writeURLs(ctx, req, entries)
}
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.
`