2015-05-15 16:13:05 +00:00
|
|
|
package pki
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-12-01 04:49:11 +00:00
|
|
|
"strings"
|
2015-05-15 16:13:05 +00:00
|
|
|
"time"
|
|
|
|
|
2015-06-15 17:33:23 +00:00
|
|
|
"github.com/fatih/structs"
|
2015-05-15 16:13:05 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
2016-01-28 20:18:07 +00:00
|
|
|
func pathListRoles(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
|
|
|
Pattern: "roles/?$",
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
|
|
logical.ListOperation: b.pathRoleList,
|
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathRoleHelpSyn,
|
|
|
|
HelpDescription: pathRoleHelpDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 16:13:05 +00:00
|
|
|
func pathRoles(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
2015-08-21 07:56:13 +00:00
|
|
|
Pattern: "roles/" + framework.GenericNameRegex("name"),
|
2015-05-15 16:13:05 +00:00
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"name": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
2015-06-18 14:44:02 +00:00
|
|
|
Description: "Name of the role",
|
2015-05-15 16:13:05 +00:00
|
|
|
},
|
|
|
|
|
2015-08-27 19:24:37 +00:00
|
|
|
"ttl": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Default: "",
|
|
|
|
Description: `The lease duration if no specific lease duration is
|
|
|
|
requested. The lease duration controls the expiration
|
|
|
|
of certificates issued by this backend. Defaults to
|
|
|
|
the value of max_ttl.`,
|
|
|
|
},
|
|
|
|
|
|
|
|
"max_ttl": &framework.FieldSchema{
|
2015-05-15 16:13:05 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Default: "",
|
2015-08-27 19:24:37 +00:00
|
|
|
Description: "The maximum allowed lease duration",
|
2015-05-15 16:13:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"allow_localhost": &framework.FieldSchema{
|
2015-06-18 14:44:02 +00:00
|
|
|
Type: framework.TypeBool,
|
|
|
|
Default: true,
|
|
|
|
Description: `Whether to allow "localhost" as a valid common
|
|
|
|
name in a request`,
|
2015-05-15 16:13:05 +00:00
|
|
|
},
|
|
|
|
|
2015-12-01 04:49:11 +00:00
|
|
|
"allowed_domains": &framework.FieldSchema{
|
2015-06-18 14:44:02 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Default: "",
|
|
|
|
Description: `If set, clients can request certificates for
|
2015-12-01 04:49:11 +00:00
|
|
|
subdomains directly beneath these domains, including
|
|
|
|
the wildcard subdomains. See the documentation for more
|
|
|
|
information. This parameter accepts a comma-separated list
|
|
|
|
of domains.`,
|
2015-10-02 16:22:02 +00:00
|
|
|
},
|
|
|
|
|
2015-12-01 04:49:11 +00:00
|
|
|
"allow_bare_domains": &framework.FieldSchema{
|
2015-10-02 16:22:02 +00:00
|
|
|
Type: framework.TypeBool,
|
|
|
|
Default: false,
|
|
|
|
Description: `If set, clients can request certificates
|
2015-12-01 04:49:11 +00:00
|
|
|
for the base domains themselves, e.g. "example.com".
|
|
|
|
This is a separate option as in some cases this can
|
|
|
|
be considered a security threat.`,
|
2015-05-15 16:13:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"allow_subdomains": &framework.FieldSchema{
|
2015-06-18 14:44:02 +00:00
|
|
|
Type: framework.TypeBool,
|
|
|
|
Default: false,
|
|
|
|
Description: `If set, clients can request certificates for
|
|
|
|
subdomains of the CNs allowed by the other role options,
|
|
|
|
including wildcard subdomains. See the documentation for
|
|
|
|
more information.`,
|
2015-05-15 16:13:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"allow_any_name": &framework.FieldSchema{
|
2015-06-18 14:44:02 +00:00
|
|
|
Type: framework.TypeBool,
|
|
|
|
Default: false,
|
|
|
|
Description: `If set, clients can request certificates for
|
|
|
|
any CN they like. See the documentation for more
|
|
|
|
information.`,
|
2015-05-15 16:13:05 +00:00
|
|
|
},
|
|
|
|
|
2015-08-20 21:33:37 +00:00
|
|
|
"enforce_hostnames": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeBool,
|
2015-11-16 15:10:03 +00:00
|
|
|
Default: true,
|
2015-08-20 21:33:37 +00:00
|
|
|
Description: `If set, only valid host names are allowed for
|
2015-11-16 15:10:03 +00:00
|
|
|
CN and SANs. Defaults to true.`,
|
2015-08-20 21:33:37 +00:00
|
|
|
},
|
|
|
|
|
2015-05-15 16:13:05 +00:00
|
|
|
"allow_ip_sans": &framework.FieldSchema{
|
2015-06-18 14:44:02 +00:00
|
|
|
Type: framework.TypeBool,
|
|
|
|
Default: true,
|
|
|
|
Description: `If set, IP Subject Alternative Names are allowed.
|
|
|
|
Any valid IP is accepted.`,
|
2015-05-15 16:13:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"server_flag": &framework.FieldSchema{
|
2015-06-18 14:44:02 +00:00
|
|
|
Type: framework.TypeBool,
|
|
|
|
Default: true,
|
2015-08-29 13:03:02 +00:00
|
|
|
Description: `If set, certificates are flagged for server auth use.
|
2015-06-18 14:44:02 +00:00
|
|
|
Defaults to true.`,
|
2015-05-15 16:13:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"client_flag": &framework.FieldSchema{
|
2015-06-18 14:44:02 +00:00
|
|
|
Type: framework.TypeBool,
|
|
|
|
Default: true,
|
2015-08-29 13:03:02 +00:00
|
|
|
Description: `If set, certificates are flagged for client auth use.
|
2015-06-18 14:44:02 +00:00
|
|
|
Defaults to true.`,
|
2015-05-15 16:13:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"code_signing_flag": &framework.FieldSchema{
|
2015-06-18 14:44:02 +00:00
|
|
|
Type: framework.TypeBool,
|
|
|
|
Default: false,
|
|
|
|
Description: `If set, certificates are flagged for code signing
|
|
|
|
use. Defaults to false.`,
|
2015-05-15 16:13:05 +00:00
|
|
|
},
|
|
|
|
|
2015-10-02 15:55:30 +00:00
|
|
|
"email_protection_flag": &framework.FieldSchema{
|
2015-08-29 13:03:02 +00:00
|
|
|
Type: framework.TypeBool,
|
|
|
|
Default: false,
|
|
|
|
Description: `If set, certificates are flagged for email
|
|
|
|
protection use. Defaults to false.`,
|
|
|
|
},
|
|
|
|
|
2015-05-15 16:13:05 +00:00
|
|
|
"key_type": &framework.FieldSchema{
|
2015-06-18 14:44:02 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Default: "rsa",
|
|
|
|
Description: `The type of key to use; defaults to RSA. "rsa"
|
|
|
|
and "ec" are the only valid values.`,
|
2015-05-15 16:13:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"key_bits": &framework.FieldSchema{
|
2015-06-18 14:44:02 +00:00
|
|
|
Type: framework.TypeInt,
|
|
|
|
Default: 2048,
|
|
|
|
Description: `The number of bits to use. You will almost
|
|
|
|
certainly want to change this if you adjust
|
|
|
|
the key_type.`,
|
2015-05-15 16:13:05 +00:00
|
|
|
},
|
2015-10-09 17:45:17 +00:00
|
|
|
|
|
|
|
"use_csr_common_name": &framework.FieldSchema{
|
2015-11-12 16:24:32 +00:00
|
|
|
Type: framework.TypeBool,
|
2015-11-16 15:10:03 +00:00
|
|
|
Default: true,
|
2015-10-09 17:45:17 +00:00
|
|
|
Description: `If set, when used with a signing profile,
|
|
|
|
the common name in the CSR will be used. This
|
|
|
|
does *not* include any requested Subject Alternative
|
2015-11-16 15:10:03 +00:00
|
|
|
Names. Defaults to true.`,
|
2015-10-09 17:45:17 +00:00
|
|
|
},
|
2015-05-15 16:13:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
|
|
logical.ReadOperation: b.pathRoleRead,
|
2016-01-28 20:18:07 +00:00
|
|
|
logical.UpdateOperation: b.pathRoleCreate,
|
2015-05-15 16:13:05 +00:00
|
|
|
logical.DeleteOperation: b.pathRoleDelete,
|
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathRoleHelpSyn,
|
|
|
|
HelpDescription: pathRoleHelpDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backend) getRole(s logical.Storage, n string) (*roleEntry, error) {
|
|
|
|
entry, err := s.Get("role/" + n)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if entry == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var result roleEntry
|
|
|
|
if err := entry.DecodeJSON(&result); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-08-27 19:24:37 +00:00
|
|
|
// Migrate existing saved entries and save back if changed
|
|
|
|
modified := false
|
|
|
|
if len(result.TTL) == 0 && len(result.Lease) != 0 {
|
|
|
|
result.TTL = result.Lease
|
|
|
|
result.Lease = ""
|
|
|
|
modified = true
|
|
|
|
}
|
|
|
|
if len(result.MaxTTL) == 0 && len(result.LeaseMax) != 0 {
|
|
|
|
result.MaxTTL = result.LeaseMax
|
|
|
|
result.LeaseMax = ""
|
|
|
|
modified = true
|
|
|
|
}
|
2015-12-01 04:49:11 +00:00
|
|
|
if result.AllowBaseDomain {
|
|
|
|
result.AllowBaseDomain = false
|
|
|
|
result.AllowBareDomains = true
|
|
|
|
modified = true
|
|
|
|
}
|
|
|
|
if result.AllowedBaseDomain != "" {
|
|
|
|
found := false
|
|
|
|
allowedDomains := strings.Split(result.AllowedDomains, ",")
|
|
|
|
if len(allowedDomains) != 0 {
|
|
|
|
for _, v := range allowedDomains {
|
|
|
|
if v == result.AllowedBaseDomain {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
if result.AllowedDomains == "" {
|
|
|
|
result.AllowedDomains = result.AllowedBaseDomain
|
|
|
|
} else {
|
|
|
|
result.AllowedDomains += "," + result.AllowedBaseDomain
|
|
|
|
}
|
|
|
|
result.AllowedBaseDomain = ""
|
|
|
|
modified = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-27 19:24:37 +00:00
|
|
|
if modified {
|
|
|
|
jsonEntry, err := logical.StorageEntryJSON("role/"+n, &result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := s.Put(jsonEntry); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 16:13:05 +00:00
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backend) pathRoleDelete(
|
|
|
|
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
|
|
|
err := req.Storage.Delete("role/" + data.Get("name").(string))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backend) pathRoleRead(
|
|
|
|
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
|
|
|
role, err := b.getRole(req.Storage, data.Get("name").(string))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if role == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-08-27 19:24:37 +00:00
|
|
|
hasMax := true
|
|
|
|
if len(role.MaxTTL) == 0 {
|
|
|
|
role.MaxTTL = "(system default)"
|
|
|
|
hasMax = false
|
|
|
|
}
|
|
|
|
if len(role.TTL) == 0 {
|
|
|
|
if hasMax {
|
|
|
|
role.TTL = "(system default, capped to role max)"
|
|
|
|
} else {
|
|
|
|
role.TTL = "(system default)"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 16:13:05 +00:00
|
|
|
resp := &logical.Response{
|
2015-06-15 17:33:23 +00:00
|
|
|
Data: structs.New(role).Map(),
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2016-01-28 20:18:07 +00:00
|
|
|
func (b *backend) pathRoleList(
|
|
|
|
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
|
|
|
entries, err := req.Storage.List("role/")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return logical.ListResponse(entries), nil
|
|
|
|
}
|
|
|
|
|
2015-05-15 16:13:05 +00:00
|
|
|
func (b *backend) pathRoleCreate(
|
|
|
|
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2015-08-27 19:24:37 +00:00
|
|
|
var err error
|
2015-05-15 16:13:05 +00:00
|
|
|
name := data.Get("name").(string)
|
|
|
|
|
|
|
|
entry := &roleEntry{
|
2015-11-30 18:41:16 +00:00
|
|
|
MaxTTL: data.Get("max_ttl").(string),
|
|
|
|
TTL: data.Get("ttl").(string),
|
|
|
|
AllowLocalhost: data.Get("allow_localhost").(bool),
|
2015-12-01 04:49:11 +00:00
|
|
|
AllowedDomains: data.Get("allowed_domains").(string),
|
|
|
|
AllowBareDomains: data.Get("allow_bare_domains").(bool),
|
2015-11-30 18:41:16 +00:00
|
|
|
AllowSubdomains: data.Get("allow_subdomains").(bool),
|
|
|
|
AllowAnyName: data.Get("allow_any_name").(bool),
|
|
|
|
EnforceHostnames: data.Get("enforce_hostnames").(bool),
|
|
|
|
AllowIPSANs: data.Get("allow_ip_sans").(bool),
|
|
|
|
ServerFlag: data.Get("server_flag").(bool),
|
|
|
|
ClientFlag: data.Get("client_flag").(bool),
|
|
|
|
CodeSigningFlag: data.Get("code_signing_flag").(bool),
|
|
|
|
EmailProtectionFlag: data.Get("email_protection_flag").(bool),
|
|
|
|
KeyType: data.Get("key_type").(string),
|
|
|
|
KeyBits: data.Get("key_bits").(int),
|
|
|
|
UseCSRCommonName: data.Get("use_csr_common_name").(bool),
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
2015-08-27 19:24:37 +00:00
|
|
|
var maxTTL time.Duration
|
2015-09-10 19:09:34 +00:00
|
|
|
maxSystemTTL := b.System().MaxLeaseTTL()
|
2015-08-27 19:24:37 +00:00
|
|
|
if len(entry.MaxTTL) == 0 {
|
2015-09-02 19:56:58 +00:00
|
|
|
maxTTL = maxSystemTTL
|
2015-08-27 19:24:37 +00:00
|
|
|
} else {
|
|
|
|
maxTTL, err = time.ParseDuration(entry.MaxTTL)
|
|
|
|
if err != nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf(
|
|
|
|
"Invalid ttl: %s", err)), nil
|
|
|
|
}
|
|
|
|
}
|
2015-09-02 19:56:58 +00:00
|
|
|
if maxTTL > maxSystemTTL {
|
2015-09-10 19:09:34 +00:00
|
|
|
return logical.ErrorResponse("Requested max TTL is higher than backend maximum"), nil
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 19:09:34 +00:00
|
|
|
ttl := b.System().DefaultLeaseTTL()
|
2015-09-02 19:56:58 +00:00
|
|
|
if len(entry.TTL) != 0 {
|
2015-08-27 19:24:37 +00:00
|
|
|
ttl, err = time.ParseDuration(entry.TTL)
|
2015-05-15 16:13:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf(
|
2015-08-27 19:24:37 +00:00
|
|
|
"Invalid ttl: %s", err)), nil
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
2015-08-27 19:24:37 +00:00
|
|
|
}
|
|
|
|
if ttl > maxTTL {
|
|
|
|
// If they are using the system default, cap it to the role max;
|
|
|
|
// if it was specified on the command line, make it an error
|
|
|
|
if len(entry.TTL) == 0 {
|
|
|
|
ttl = maxTTL
|
|
|
|
} else {
|
2015-10-05 17:00:45 +00:00
|
|
|
return logical.ErrorResponse(
|
|
|
|
`"ttl" value must be less than "max_ttl" and/or backend default max lease TTL value`,
|
|
|
|
), nil
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-16 21:32:49 +00:00
|
|
|
if errResp := validateKeyTypeLength(entry.KeyType, entry.KeyBits); errResp != nil {
|
|
|
|
return errResp, nil
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store it
|
|
|
|
jsonEntry, err := logical.StorageEntryJSON("role/"+name, entry)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := req.Storage.Put(jsonEntry); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type roleEntry struct {
|
2015-06-05 18:03:57 +00:00
|
|
|
LeaseMax string `json:"lease_max" structs:"lease_max" mapstructure:"lease_max"`
|
|
|
|
Lease string `json:"lease" structs:"lease" mapstructure:"lease"`
|
2015-08-27 19:24:37 +00:00
|
|
|
MaxTTL string `json:"max_ttl" structs:"max_ttl" mapstructure:"max_ttl"`
|
|
|
|
TTL string `json:"ttl" structs:"ttl" mapstructure:"ttl"`
|
2015-06-05 18:03:57 +00:00
|
|
|
AllowLocalhost bool `json:"allow_localhost" structs:"allow_localhost" mapstructure:"allow_localhost"`
|
|
|
|
AllowedBaseDomain string `json:"allowed_base_domain" structs:"allowed_base_domain" mapstructure:"allowed_base_domain"`
|
2015-12-01 04:49:11 +00:00
|
|
|
AllowedDomains string `json:"allowed_domains" structs:"allowed_domains" mapstructure:"allowed_domains"`
|
2015-10-02 16:22:02 +00:00
|
|
|
AllowBaseDomain bool `json:"allow_base_domain" structs:"allow_base_domain" mapstructure:"allow_base_domain"`
|
2015-12-01 04:49:11 +00:00
|
|
|
AllowBareDomains bool `json:"allow_bare_domains" structs:"allow_bare_domains" mapstructure:"allow_bare_domains"`
|
2015-06-05 18:03:57 +00:00
|
|
|
AllowTokenDisplayName bool `json:"allow_token_displayname" structs:"allow_token_displayname" mapstructure:"allow_token_displayname"`
|
|
|
|
AllowSubdomains bool `json:"allow_subdomains" structs:"allow_subdomains" mapstructure:"allow_subdomains"`
|
|
|
|
AllowAnyName bool `json:"allow_any_name" structs:"allow_any_name" mapstructure:"allow_any_name"`
|
2015-08-20 21:33:37 +00:00
|
|
|
EnforceHostnames bool `json:"enforce_hostnames" structs:"enforce_hostnames" mapstructure:"enforce_hostnames"`
|
2015-06-05 18:03:57 +00:00
|
|
|
AllowIPSANs bool `json:"allow_ip_sans" structs:"allow_ip_sans" mapstructure:"allow_ip_sans"`
|
|
|
|
ServerFlag bool `json:"server_flag" structs:"server_flag" mapstructure:"server_flag"`
|
|
|
|
ClientFlag bool `json:"client_flag" structs:"client_flag" mapstructure:"client_flag"`
|
|
|
|
CodeSigningFlag bool `json:"code_signing_flag" structs:"code_signing_flag" mapstructure:"code_signing_flag"`
|
2015-10-02 15:55:30 +00:00
|
|
|
EmailProtectionFlag bool `json:"email_protection_flag" structs:"email_protection_flag" mapstructure:"email_protection_flag"`
|
2015-10-09 17:45:17 +00:00
|
|
|
UseCSRCommonName bool `json:"use_csr_common_name" structs:"use_csr_common_name" mapstructure:"use_csr_common_name"`
|
2015-06-05 18:03:57 +00:00
|
|
|
KeyType string `json:"key_type" structs:"key_type" mapstructure:"key_type"`
|
|
|
|
KeyBits int `json:"key_bits" structs:"key_bits" mapstructure:"key_bits"`
|
2015-11-19 16:32:18 +00:00
|
|
|
MaxPathLength *int `json:",omitempty" structs:",omitempty"`
|
2015-05-15 16:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const pathRoleHelpSyn = `
|
|
|
|
Manage the roles that can be created with this backend.
|
|
|
|
`
|
|
|
|
|
|
|
|
const pathRoleHelpDesc = `
|
|
|
|
This path lets you manage the roles that can be created with this backend.
|
|
|
|
`
|