2018-05-23 21:43:40 +00:00
|
|
|
package ca
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ParseConsulCAConfig(raw map[string]interface{}) (*structs.ConsulCAProviderConfig, error) {
|
2018-07-16 09:46:10 +00:00
|
|
|
config := structs.ConsulCAProviderConfig{
|
|
|
|
CommonCAProviderConfig: defaultCommonConfig(),
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-05-23 21:43:40 +00:00
|
|
|
return &config, nil
|
|
|
|
}
|
|
|
|
|
2018-07-16 09:46:10 +00:00
|
|
|
func defaultCommonConfig() structs.CommonCAProviderConfig {
|
|
|
|
return structs.CommonCAProviderConfig{
|
|
|
|
LeafCertTTL: 3 * 24 * time.Hour,
|
|
|
|
}
|
|
|
|
}
|