2018-05-23 21:43:40 +00:00
|
|
|
package ca
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2019-07-30 21:47:39 +00:00
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
2018-05-23 21:43:40 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ParseConsulCAConfig(raw map[string]interface{}) (*structs.ConsulCAProviderConfig, error) {
|
2020-02-10 23:05:49 +00:00
|
|
|
config := defaultConsulCAProviderConfig()
|
2018-05-23 21:43:40 +00:00
|
|
|
decodeConf := &mapstructure.DecoderConfig{
|
2018-09-13 14:43:00 +00:00
|
|
|
DecodeHook: structs.ParseDurationFunc(),
|
2018-05-23 21:43:40 +00:00
|
|
|
Result: &config,
|
|
|
|
WeaklyTypedInput: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder, err := mapstructure.NewDecoder(decodeConf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := decoder.Decode(raw); err != nil {
|
|
|
|
return nil, fmt.Errorf("error decoding config: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.PrivateKey == "" && config.RootCert != "" {
|
|
|
|
return nil, fmt.Errorf("must provide a private key when providing a root cert")
|
|
|
|
}
|
|
|
|
|
2018-07-20 23:04:04 +00:00
|
|
|
if err := config.CommonCAProviderConfig.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-10 23:05:49 +00:00
|
|
|
if err := config.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-23 21:43:40 +00:00
|
|
|
return &config, nil
|
|
|
|
}
|
|
|
|
|
2020-02-10 23:05:49 +00:00
|
|
|
func defaultConsulCAProviderConfig() structs.ConsulCAProviderConfig {
|
|
|
|
return structs.ConsulCAProviderConfig{
|
|
|
|
CommonCAProviderConfig: defaultCommonConfig(),
|
|
|
|
}
|
|
|
|
}
|
2018-07-16 09:46:10 +00:00
|
|
|
func defaultCommonConfig() structs.CommonCAProviderConfig {
|
|
|
|
return structs.CommonCAProviderConfig{
|
2020-09-10 06:04:56 +00:00
|
|
|
LeafCertTTL: 3 * 24 * time.Hour,
|
|
|
|
IntermediateCertTTL: 24 * 365 * time.Hour,
|
|
|
|
PrivateKeyType: connect.DefaultPrivateKeyType,
|
|
|
|
PrivateKeyBits: connect.DefaultPrivateKeyBits,
|
2018-07-16 09:46:10 +00:00
|
|
|
}
|
|
|
|
}
|