2018-06-13 08:40:03 +00:00
|
|
|
package ca
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2018-06-14 17:56:17 +00:00
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
2018-06-13 08:40:03 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
vaultapi "github.com/hashicorp/vault/api"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
|
|
|
const VaultCALeafCertRole = "leaf-cert"
|
|
|
|
|
|
|
|
var ErrBackendNotMounted = fmt.Errorf("backend not mounted")
|
|
|
|
var ErrBackendNotInitialized = fmt.Errorf("backend not initialized")
|
|
|
|
|
|
|
|
type VaultProvider struct {
|
|
|
|
config *structs.VaultCAProviderConfig
|
|
|
|
client *vaultapi.Client
|
2018-09-07 02:18:54 +00:00
|
|
|
isRoot bool
|
2019-11-11 17:11:54 +00:00
|
|
|
clusterID string
|
2019-09-23 17:04:40 +00:00
|
|
|
spiffeID *connect.SpiffeIDSigning
|
2018-06-13 08:40:03 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 16:09:22 +00:00
|
|
|
func vaultTLSConfig(config *structs.VaultCAProviderConfig) *vaultapi.TLSConfig {
|
|
|
|
return &vaultapi.TLSConfig{
|
|
|
|
CACert: config.CAFile,
|
|
|
|
CAPath: config.CAPath,
|
|
|
|
ClientCert: config.CertFile,
|
|
|
|
ClientKey: config.KeyFile,
|
|
|
|
Insecure: config.TLSSkipVerify,
|
|
|
|
TLSServerName: config.TLSServerName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-11 23:43:04 +00:00
|
|
|
// Configure sets up the provider using the given configuration.
|
2019-11-11 20:57:16 +00:00
|
|
|
func (v *VaultProvider) Configure(clusterID string, isRoot bool, rawConfig map[string]interface{}, state map[string]string) error {
|
2018-09-07 02:18:54 +00:00
|
|
|
config, err := ParseVaultCAConfig(rawConfig)
|
2018-06-13 08:40:03 +00:00
|
|
|
if err != nil {
|
2018-09-07 02:18:54 +00:00
|
|
|
return err
|
2018-06-13 08:40:03 +00:00
|
|
|
}
|
|
|
|
|
2018-06-14 17:56:17 +00:00
|
|
|
clientConf := &vaultapi.Config{
|
2018-09-07 02:18:54 +00:00
|
|
|
Address: config.Address,
|
2018-06-13 08:40:03 +00:00
|
|
|
}
|
2019-01-08 16:09:22 +00:00
|
|
|
err = clientConf.ConfigureTLS(vaultTLSConfig(config))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-06-14 17:56:17 +00:00
|
|
|
client, err := vaultapi.NewClient(clientConf)
|
|
|
|
if err != nil {
|
2018-09-07 02:18:54 +00:00
|
|
|
return err
|
2018-06-14 17:56:17 +00:00
|
|
|
}
|
|
|
|
|
2018-09-07 02:18:54 +00:00
|
|
|
client.SetToken(config.Token)
|
|
|
|
v.config = config
|
|
|
|
v.client = client
|
|
|
|
v.isRoot = isRoot
|
2019-11-11 17:11:54 +00:00
|
|
|
v.clusterID = clusterID
|
|
|
|
v.spiffeID = connect.SpiffeIDSigningForCluster(&structs.CAConfiguration{ClusterID: clusterID})
|
2018-06-13 08:40:03 +00:00
|
|
|
|
2018-09-07 02:18:54 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-06-13 08:40:03 +00:00
|
|
|
|
2019-11-11 20:57:16 +00:00
|
|
|
// State implements Provider. Vault provider needs no state other than the
|
|
|
|
// user-provided config currently.
|
|
|
|
func (v *VaultProvider) State() (map[string]string, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-09-11 23:43:04 +00:00
|
|
|
// ActiveRoot returns the active root CA certificate.
|
|
|
|
func (v *VaultProvider) ActiveRoot() (string, error) {
|
|
|
|
return v.getCA(v.config.RootPKIPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateRoot mounts and initializes a new root PKI backend if needed.
|
2018-09-07 02:18:54 +00:00
|
|
|
func (v *VaultProvider) GenerateRoot() error {
|
|
|
|
if !v.isRoot {
|
|
|
|
return fmt.Errorf("provider is not the root certificate authority")
|
2018-06-13 08:40:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the root PKI backend if necessary.
|
2018-09-07 02:18:54 +00:00
|
|
|
_, err := v.ActiveRoot()
|
2018-06-13 08:40:03 +00:00
|
|
|
switch err {
|
|
|
|
case ErrBackendNotMounted:
|
2018-09-07 02:18:54 +00:00
|
|
|
err := v.client.Sys().Mount(v.config.RootPKIPath, &vaultapi.MountInput{
|
2018-06-13 08:40:03 +00:00
|
|
|
Type: "pki",
|
|
|
|
Description: "root CA backend for Consul Connect",
|
|
|
|
Config: vaultapi.MountConfigInput{
|
|
|
|
MaxLeaseTTL: "8760h",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2018-09-07 02:18:54 +00:00
|
|
|
return err
|
2018-06-13 08:40:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fallthrough
|
|
|
|
case ErrBackendNotInitialized:
|
2019-11-11 17:11:54 +00:00
|
|
|
uid, err := connect.CompactUID()
|
2019-08-27 21:45:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-07 02:18:54 +00:00
|
|
|
_, err = v.client.Logical().Write(v.config.RootPKIPath+"root/generate/internal", map[string]interface{}{
|
2019-11-11 17:11:54 +00:00
|
|
|
"common_name": connect.CACN("vault", uid, v.clusterID, v.isRoot),
|
2019-09-23 17:04:40 +00:00
|
|
|
"uri_sans": v.spiffeID.URI().String(),
|
2019-07-30 21:47:39 +00:00
|
|
|
"key_type": v.config.PrivateKeyType,
|
|
|
|
"key_bits": v.config.PrivateKeyBits,
|
2018-06-13 08:40:03 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2018-09-07 02:18:54 +00:00
|
|
|
return err
|
2018-06-13 08:40:03 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
if err != nil {
|
2018-09-07 02:18:54 +00:00
|
|
|
return err
|
2018-06-13 08:40:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-07 02:18:54 +00:00
|
|
|
return nil
|
2018-06-13 08:40:03 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 20:09:07 +00:00
|
|
|
// GenerateIntermediateCSR creates a private key and generates a CSR
|
|
|
|
// for another datacenter's root to sign, overwriting the intermediate backend
|
|
|
|
// in the process.
|
2018-09-13 02:52:24 +00:00
|
|
|
func (v *VaultProvider) GenerateIntermediateCSR() (string, error) {
|
2018-09-13 20:09:07 +00:00
|
|
|
if v.isRoot {
|
|
|
|
return "", fmt.Errorf("provider is the root certificate authority, " +
|
|
|
|
"cannot generate an intermediate CSR")
|
2018-06-13 08:40:03 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 20:09:07 +00:00
|
|
|
return v.generateIntermediateCSR()
|
2018-06-13 08:40:03 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 20:09:07 +00:00
|
|
|
func (v *VaultProvider) generateIntermediateCSR() (string, error) {
|
2018-06-13 08:40:03 +00:00
|
|
|
mounts, err := v.client.Sys().ListMounts()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mount the backend if it isn't mounted already.
|
|
|
|
if _, ok := mounts[v.config.IntermediatePKIPath]; !ok {
|
|
|
|
err := v.client.Sys().Mount(v.config.IntermediatePKIPath, &vaultapi.MountInput{
|
|
|
|
Type: "pki",
|
|
|
|
Description: "intermediate CA backend for Consul Connect",
|
|
|
|
Config: vaultapi.MountConfigInput{
|
|
|
|
MaxLeaseTTL: "2160h",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the role for issuing leaf certs if it doesn't exist yet
|
|
|
|
rolePath := v.config.IntermediatePKIPath + "roles/" + VaultCALeafCertRole
|
|
|
|
role, err := v.client.Logical().Read(rolePath)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if role == nil {
|
|
|
|
_, err := v.client.Logical().Write(rolePath, map[string]interface{}{
|
2019-08-27 21:45:58 +00:00
|
|
|
"allow_any_name": true,
|
|
|
|
"allowed_uri_sans": "spiffe://*",
|
|
|
|
"key_type": "any",
|
|
|
|
"max_ttl": v.config.LeafCertTTL.String(),
|
|
|
|
"no_store": true,
|
|
|
|
"require_cn": false,
|
2018-06-13 08:40:03 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a new intermediate CSR for the root to sign.
|
2019-11-11 17:11:54 +00:00
|
|
|
uid, err := connect.CompactUID()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-09-13 20:09:07 +00:00
|
|
|
data, err := v.client.Logical().Write(v.config.IntermediatePKIPath+"intermediate/generate/internal", map[string]interface{}{
|
2019-11-11 17:11:54 +00:00
|
|
|
"common_name": connect.CACN("vault", uid, v.clusterID, v.isRoot),
|
2019-07-30 21:47:39 +00:00
|
|
|
"key_type": v.config.PrivateKeyType,
|
|
|
|
"key_bits": v.config.PrivateKeyBits,
|
2019-09-23 17:04:40 +00:00
|
|
|
"uri_sans": v.spiffeID.URI().String(),
|
2018-06-13 08:40:03 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-09-13 20:09:07 +00:00
|
|
|
if data == nil || data.Data["csr"] == "" {
|
2018-06-13 08:40:03 +00:00
|
|
|
return "", fmt.Errorf("got empty value when generating intermediate CSR")
|
|
|
|
}
|
2018-09-13 20:09:07 +00:00
|
|
|
csr, ok := data.Data["csr"].(string)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("csr result is not a string")
|
|
|
|
}
|
|
|
|
|
|
|
|
return csr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetIntermediate writes the incoming intermediate and root certificates to the
|
|
|
|
// intermediate backend (as a chain).
|
|
|
|
func (v *VaultProvider) SetIntermediate(intermediatePEM, rootPEM string) error {
|
|
|
|
if v.isRoot {
|
|
|
|
return fmt.Errorf("cannot set an intermediate using another root in the primary datacenter")
|
|
|
|
}
|
|
|
|
|
2019-09-23 17:04:40 +00:00
|
|
|
err := validateSetIntermediate(
|
|
|
|
intermediatePEM, rootPEM,
|
|
|
|
"", // we don't have access to the private key directly
|
|
|
|
v.spiffeID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = v.client.Logical().Write(v.config.IntermediatePKIPath+"intermediate/set-signed", map[string]interface{}{
|
2018-09-13 20:09:07 +00:00
|
|
|
"certificate": fmt.Sprintf("%s\n%s", intermediatePEM, rootPEM),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ActiveIntermediate returns the current intermediate certificate.
|
|
|
|
func (v *VaultProvider) ActiveIntermediate() (string, error) {
|
|
|
|
return v.getCA(v.config.IntermediatePKIPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getCA returns the raw CA cert for the given endpoint if there is one.
|
|
|
|
// We have to use the raw NewRequest call here instead of Logical().Read
|
|
|
|
// because the endpoint only returns the raw PEM contents of the CA cert
|
|
|
|
// and not the typical format of the secrets endpoints.
|
|
|
|
func (v *VaultProvider) getCA(path string) (string, error) {
|
|
|
|
req := v.client.NewRequest("GET", "/v1/"+path+"/ca/pem")
|
|
|
|
resp, err := v.client.RawRequest(req)
|
|
|
|
if resp != nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
}
|
|
|
|
if resp != nil && resp.StatusCode == http.StatusNotFound {
|
|
|
|
return "", ErrBackendNotMounted
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := string(bytes)
|
|
|
|
if root == "" {
|
|
|
|
return "", ErrBackendNotInitialized
|
|
|
|
}
|
|
|
|
|
|
|
|
return root, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateIntermediate mounts the configured intermediate PKI backend if
|
|
|
|
// necessary, then generates and signs a new CA CSR using the root PKI backend
|
|
|
|
// and updates the intermediate backend to use that new certificate.
|
|
|
|
func (v *VaultProvider) GenerateIntermediate() (string, error) {
|
|
|
|
csr, err := v.generateIntermediateCSR()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-06-13 08:40:03 +00:00
|
|
|
|
|
|
|
// Sign the CSR with the root backend.
|
|
|
|
intermediate, err := v.client.Logical().Write(v.config.RootPKIPath+"root/sign-intermediate", map[string]interface{}{
|
2019-09-23 17:04:40 +00:00
|
|
|
"csr": csr,
|
|
|
|
"use_csr_values": true,
|
|
|
|
"format": "pem_bundle",
|
2018-06-13 08:40:03 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-06-15 23:25:53 +00:00
|
|
|
if intermediate == nil || intermediate.Data["certificate"] == "" {
|
2018-06-13 08:40:03 +00:00
|
|
|
return "", fmt.Errorf("got empty value when generating intermediate certificate")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the intermediate backend to use the new certificate.
|
|
|
|
_, err = v.client.Logical().Write(v.config.IntermediatePKIPath+"intermediate/set-signed", map[string]interface{}{
|
|
|
|
"certificate": intermediate.Data["certificate"],
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return v.ActiveIntermediate()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign calls the configured role in the intermediate PKI backend to issue
|
|
|
|
// a new leaf certificate based on the provided CSR, with the issuing
|
|
|
|
// intermediate CA cert attached.
|
|
|
|
func (v *VaultProvider) Sign(csr *x509.CertificateRequest) (string, error) {
|
|
|
|
var pemBuf bytes.Buffer
|
|
|
|
if err := pem.Encode(&pemBuf, &pem.Block{Type: "CERTIFICATE REQUEST", Bytes: csr.Raw}); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the leaf cert role to sign a new cert for this CSR.
|
|
|
|
response, err := v.client.Logical().Write(v.config.IntermediatePKIPath+"sign/"+VaultCALeafCertRole, map[string]interface{}{
|
2018-06-14 21:20:10 +00:00
|
|
|
"csr": pemBuf.String(),
|
2018-07-26 00:51:45 +00:00
|
|
|
"ttl": v.config.LeafCertTTL.String(),
|
2018-06-13 08:40:03 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2018-06-14 21:20:10 +00:00
|
|
|
return "", fmt.Errorf("error issuing cert: %v", err)
|
2018-06-13 08:40:03 +00:00
|
|
|
}
|
|
|
|
if response == nil || response.Data["certificate"] == "" || response.Data["issuing_ca"] == "" {
|
|
|
|
return "", fmt.Errorf("certificate info returned from Vault was blank")
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, ok := response.Data["certificate"].(string)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("certificate was not a string")
|
|
|
|
}
|
|
|
|
ca, ok := response.Data["issuing_ca"].(string)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("issuing_ca was not a string")
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s\n%s", cert, ca), nil
|
|
|
|
}
|
|
|
|
|
2018-09-13 20:09:07 +00:00
|
|
|
// SignIntermediate returns a signed CA certificate with a path length constraint
|
|
|
|
// of 0 to ensure that the certificate cannot be used to generate further CA certs.
|
|
|
|
func (v *VaultProvider) SignIntermediate(csr *x509.CertificateRequest) (string, error) {
|
|
|
|
var pemBuf bytes.Buffer
|
|
|
|
err := pem.Encode(&pemBuf, &pem.Block{Type: "CERTIFICATE REQUEST", Bytes: csr.Raw})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign the CSR with the root backend.
|
|
|
|
data, err := v.client.Logical().Write(v.config.RootPKIPath+"root/sign-intermediate", map[string]interface{}{
|
|
|
|
"csr": pemBuf.String(),
|
2019-09-23 17:04:40 +00:00
|
|
|
"use_csr_values": true,
|
2018-09-13 20:09:07 +00:00
|
|
|
"format": "pem_bundle",
|
|
|
|
"max_path_length": 0,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if data == nil || data.Data["certificate"] == "" {
|
|
|
|
return "", fmt.Errorf("got empty value when generating intermediate certificate")
|
|
|
|
}
|
|
|
|
|
|
|
|
intermediate, ok := data.Data["certificate"].(string)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("signed intermediate result is not a string")
|
|
|
|
}
|
|
|
|
|
|
|
|
return intermediate, nil
|
2018-09-13 02:52:24 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 23:46:18 +00:00
|
|
|
// CrossSignCA takes a CA certificate and cross-signs it to form a trust chain
|
|
|
|
// back to our active root.
|
|
|
|
func (v *VaultProvider) CrossSignCA(cert *x509.Certificate) (string, error) {
|
|
|
|
var pemBuf bytes.Buffer
|
|
|
|
err := pem.Encode(&pemBuf, &pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw})
|
2018-06-15 23:25:53 +00:00
|
|
|
if err != nil {
|
2018-06-13 08:40:03 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2018-06-19 23:46:18 +00:00
|
|
|
// Have the root PKI backend sign this cert.
|
|
|
|
response, err := v.client.Logical().Write(v.config.RootPKIPath+"root/sign-self-issued", map[string]interface{}{
|
|
|
|
"certificate": pemBuf.String(),
|
2018-06-15 23:25:53 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error having Vault cross-sign cert: %v", err)
|
|
|
|
}
|
|
|
|
if response == nil || response.Data["certificate"] == "" {
|
|
|
|
return "", fmt.Errorf("certificate info returned from Vault was blank")
|
|
|
|
}
|
|
|
|
|
|
|
|
xcCert, ok := response.Data["certificate"].(string)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("certificate was not a string")
|
|
|
|
}
|
|
|
|
|
|
|
|
return xcCert, nil
|
2018-06-13 08:40:03 +00:00
|
|
|
}
|
|
|
|
|
2019-11-11 21:36:22 +00:00
|
|
|
// SupportsCrossSigning implements Provider
|
|
|
|
func (c *VaultProvider) SupportsCrossSigning() (bool, error) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2018-06-13 08:40:03 +00:00
|
|
|
// Cleanup unmounts the configured intermediate PKI backend. It's fine to tear
|
|
|
|
// this down and recreate it on small config changes because the intermediate
|
|
|
|
// certs get bundled with the leaf certs, so there's no cost to the CA changing.
|
|
|
|
func (v *VaultProvider) Cleanup() error {
|
|
|
|
return v.client.Sys().Unmount(v.config.IntermediatePKIPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseVaultCAConfig(raw map[string]interface{}) (*structs.VaultCAProviderConfig, error) {
|
2018-07-16 09:46:10 +00:00
|
|
|
config := structs.VaultCAProviderConfig{
|
|
|
|
CommonCAProviderConfig: defaultCommonConfig(),
|
|
|
|
}
|
2018-06-13 08:40:03 +00:00
|
|
|
|
2018-06-20 21:33:02 +00:00
|
|
|
decodeConf := &mapstructure.DecoderConfig{
|
2018-09-13 14:43:00 +00:00
|
|
|
DecodeHook: structs.ParseDurationFunc(),
|
2018-06-20 21:33:02 +00:00
|
|
|
Result: &config,
|
|
|
|
WeaklyTypedInput: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder, err := mapstructure.NewDecoder(decodeConf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := decoder.Decode(raw); err != nil {
|
2018-06-13 08:40:03 +00:00
|
|
|
return nil, fmt.Errorf("error decoding config: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Token == "" {
|
|
|
|
return nil, fmt.Errorf("must provide a Vault token")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.RootPKIPath == "" {
|
|
|
|
return nil, fmt.Errorf("must provide a valid path to a root PKI backend")
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(config.RootPKIPath, "/") {
|
|
|
|
config.RootPKIPath += "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.IntermediatePKIPath == "" {
|
|
|
|
return nil, fmt.Errorf("must provide a valid path for the intermediate PKI backend")
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(config.IntermediatePKIPath, "/") {
|
|
|
|
config.IntermediatePKIPath += "/"
|
|
|
|
}
|
|
|
|
|
2018-07-20 23:04:04 +00:00
|
|
|
if err := config.CommonCAProviderConfig.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-06-13 08:40:03 +00:00
|
|
|
return &config, nil
|
|
|
|
}
|