2020-05-14 13:19:27 +00:00
|
|
|
package configutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"time"
|
|
|
|
|
2021-07-16 00:17:31 +00:00
|
|
|
"github.com/hashicorp/go-secure-stdlib/parseutil"
|
2020-05-14 13:19:27 +00:00
|
|
|
"github.com/hashicorp/hcl"
|
|
|
|
"github.com/hashicorp/hcl/hcl/ast"
|
2021-06-23 22:35:52 +00:00
|
|
|
"github.com/hashicorp/hcl/hcl/token"
|
2020-05-14 13:19:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SharedConfig contains some shared values
|
|
|
|
type SharedConfig struct {
|
2021-05-04 19:47:56 +00:00
|
|
|
FoundKeys []string `hcl:",decodedFields"`
|
|
|
|
UnusedKeys UnusedKeyMap `hcl:",unusedKeyPositions"`
|
|
|
|
Sections map[string][]token.Pos
|
|
|
|
|
2020-05-14 13:19:27 +00:00
|
|
|
EntSharedConfig
|
|
|
|
|
|
|
|
Listeners []*Listener `hcl:"-"`
|
|
|
|
|
|
|
|
Seals []*KMS `hcl:"-"`
|
|
|
|
Entropy *Entropy `hcl:"-"`
|
|
|
|
|
|
|
|
DisableMlock bool `hcl:"-"`
|
|
|
|
DisableMlockRaw interface{} `hcl:"disable_mlock"`
|
|
|
|
|
|
|
|
Telemetry *Telemetry `hcl:"telemetry"`
|
|
|
|
|
|
|
|
DefaultMaxRequestDuration time.Duration `hcl:"-"`
|
|
|
|
DefaultMaxRequestDurationRaw interface{} `hcl:"default_max_request_duration"`
|
|
|
|
|
|
|
|
// LogFormat specifies the log format. Valid values are "standard" and
|
|
|
|
// "json". The values are case-insenstive. If no log format is specified,
|
|
|
|
// then standard format will be used.
|
|
|
|
LogFormat string `hcl:"log_format"`
|
|
|
|
LogLevel string `hcl:"log_level"`
|
|
|
|
|
|
|
|
PidFile string `hcl:"pid_file"`
|
|
|
|
|
|
|
|
ClusterName string `hcl:"cluster_name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadConfigFile loads the configuration from the given file.
|
|
|
|
func LoadConfigFile(path string) (*SharedConfig, error) {
|
|
|
|
// Read the file
|
|
|
|
d, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ParseConfig(string(d))
|
|
|
|
}
|
|
|
|
|
2020-05-19 22:15:30 +00:00
|
|
|
func LoadConfigKMSes(path string) ([]*KMS, error) {
|
|
|
|
// Read the file
|
|
|
|
d, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ParseKMSes(string(d))
|
|
|
|
}
|
|
|
|
|
2020-05-14 13:19:27 +00:00
|
|
|
func ParseConfig(d string) (*SharedConfig, error) {
|
|
|
|
// Parse!
|
|
|
|
obj, err := hcl.Parse(d)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start building the result
|
|
|
|
var result SharedConfig
|
2021-05-04 19:47:56 +00:00
|
|
|
|
2020-05-14 13:19:27 +00:00
|
|
|
if err := hcl.DecodeObject(&result, obj); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.DefaultMaxRequestDurationRaw != nil {
|
|
|
|
if result.DefaultMaxRequestDuration, err = parseutil.ParseDurationSecond(result.DefaultMaxRequestDurationRaw); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-04 19:47:56 +00:00
|
|
|
result.FoundKeys = append(result.FoundKeys, "DefaultMaxRequestDuration")
|
2020-05-14 13:19:27 +00:00
|
|
|
result.DefaultMaxRequestDurationRaw = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.DisableMlockRaw != nil {
|
|
|
|
if result.DisableMlock, err = parseutil.ParseBool(result.DisableMlockRaw); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-04 19:47:56 +00:00
|
|
|
result.FoundKeys = append(result.FoundKeys, "DisableMlock")
|
2020-05-14 13:19:27 +00:00
|
|
|
result.DisableMlockRaw = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
list, ok := obj.Node.(*ast.ObjectList)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("error parsing: file doesn't contain a root object")
|
|
|
|
}
|
|
|
|
|
|
|
|
if o := list.Filter("hsm"); len(o.Items) > 0 {
|
2021-06-17 18:09:37 +00:00
|
|
|
result.found("hsm", "hsm")
|
2020-05-19 22:15:30 +00:00
|
|
|
if err := parseKMS(&result.Seals, o, "hsm", 2); err != nil {
|
2021-05-10 16:58:00 +00:00
|
|
|
return nil, fmt.Errorf("error parsing 'hsm': %w", err)
|
2020-05-14 13:19:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if o := list.Filter("seal"); len(o.Items) > 0 {
|
2021-06-17 18:09:37 +00:00
|
|
|
result.found("seal", "Seal")
|
2020-05-19 22:15:30 +00:00
|
|
|
if err := parseKMS(&result.Seals, o, "seal", 3); err != nil {
|
2021-05-10 16:58:00 +00:00
|
|
|
return nil, fmt.Errorf("error parsing 'seal': %w", err)
|
2020-05-14 13:19:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if o := list.Filter("kms"); len(o.Items) > 0 {
|
2021-06-17 18:09:37 +00:00
|
|
|
result.found("kms", "Seal")
|
2020-05-19 22:15:30 +00:00
|
|
|
if err := parseKMS(&result.Seals, o, "kms", 3); err != nil {
|
2021-05-10 16:58:00 +00:00
|
|
|
return nil, fmt.Errorf("error parsing 'kms': %w", err)
|
2020-05-14 13:19:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if o := list.Filter("entropy"); len(o.Items) > 0 {
|
2021-06-17 18:09:37 +00:00
|
|
|
result.found("entropy", "Entropy")
|
2020-05-14 13:19:27 +00:00
|
|
|
if err := ParseEntropy(&result, o, "entropy"); err != nil {
|
2021-05-10 16:58:00 +00:00
|
|
|
return nil, fmt.Errorf("error parsing 'entropy': %w", err)
|
2020-05-14 13:19:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if o := list.Filter("listener"); len(o.Items) > 0 {
|
2021-06-17 18:09:37 +00:00
|
|
|
result.found("listener", "Listener")
|
2020-05-14 13:19:27 +00:00
|
|
|
if err := ParseListeners(&result, o); err != nil {
|
2021-05-10 16:58:00 +00:00
|
|
|
return nil, fmt.Errorf("error parsing 'listener': %w", err)
|
2020-05-14 13:19:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if o := list.Filter("telemetry"); len(o.Items) > 0 {
|
2021-06-17 18:09:37 +00:00
|
|
|
result.found("telemetry", "Telemetry")
|
2020-05-14 13:19:27 +00:00
|
|
|
if err := parseTelemetry(&result, o); err != nil {
|
2021-05-10 16:58:00 +00:00
|
|
|
return nil, fmt.Errorf("error parsing 'telemetry': %w", err)
|
2020-05-14 13:19:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
entConfig := &(result.EntSharedConfig)
|
|
|
|
if err := entConfig.ParseConfig(list); err != nil {
|
2021-05-10 16:58:00 +00:00
|
|
|
return nil, fmt.Errorf("error parsing enterprise config: %w", err)
|
2020-05-14 13:19:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanitized returns a copy of the config with all values that are considered
|
|
|
|
// sensitive stripped. It also strips all `*Raw` values that are mainly
|
|
|
|
// used for parsing.
|
|
|
|
//
|
|
|
|
// Specifically, the fields that this method strips are:
|
|
|
|
// - KMS.Config
|
|
|
|
// - Telemetry.CirconusAPIToken
|
|
|
|
func (c *SharedConfig) Sanitized() map[string]interface{} {
|
|
|
|
if c == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
result := map[string]interface{}{
|
|
|
|
"disable_mlock": c.DisableMlock,
|
|
|
|
|
|
|
|
"default_max_request_duration": c.DefaultMaxRequestDuration,
|
|
|
|
|
|
|
|
"log_level": c.LogLevel,
|
|
|
|
"log_format": c.LogFormat,
|
|
|
|
|
|
|
|
"pid_file": c.PidFile,
|
|
|
|
|
|
|
|
"cluster_name": c.ClusterName,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanitize listeners
|
|
|
|
if len(c.Listeners) != 0 {
|
|
|
|
var sanitizedListeners []interface{}
|
|
|
|
for _, ln := range c.Listeners {
|
|
|
|
cleanLn := map[string]interface{}{
|
|
|
|
"type": ln.Type,
|
|
|
|
"config": ln.RawConfig,
|
|
|
|
}
|
|
|
|
sanitizedListeners = append(sanitizedListeners, cleanLn)
|
|
|
|
}
|
|
|
|
result["listeners"] = sanitizedListeners
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanitize seals stanza
|
|
|
|
if len(c.Seals) != 0 {
|
|
|
|
var sanitizedSeals []interface{}
|
|
|
|
for _, s := range c.Seals {
|
|
|
|
cleanSeal := map[string]interface{}{
|
|
|
|
"type": s.Type,
|
|
|
|
"disabled": s.Disabled,
|
|
|
|
}
|
|
|
|
sanitizedSeals = append(sanitizedSeals, cleanSeal)
|
|
|
|
}
|
|
|
|
result["seals"] = sanitizedSeals
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanitize telemetry stanza
|
|
|
|
if c.Telemetry != nil {
|
|
|
|
sanitizedTelemetry := map[string]interface{}{
|
|
|
|
"statsite_address": c.Telemetry.StatsiteAddr,
|
|
|
|
"statsd_address": c.Telemetry.StatsdAddr,
|
|
|
|
"disable_hostname": c.Telemetry.DisableHostname,
|
|
|
|
"metrics_prefix": c.Telemetry.MetricsPrefix,
|
2020-06-17 15:07:33 +00:00
|
|
|
"usage_gauge_period": c.Telemetry.UsageGaugePeriod,
|
|
|
|
"maximum_gauge_cardinality": c.Telemetry.MaximumGaugeCardinality,
|
2020-05-14 13:19:27 +00:00
|
|
|
"circonus_api_token": "",
|
|
|
|
"circonus_api_app": c.Telemetry.CirconusAPIApp,
|
|
|
|
"circonus_api_url": c.Telemetry.CirconusAPIURL,
|
|
|
|
"circonus_submission_interval": c.Telemetry.CirconusSubmissionInterval,
|
|
|
|
"circonus_submission_url": c.Telemetry.CirconusCheckSubmissionURL,
|
|
|
|
"circonus_check_id": c.Telemetry.CirconusCheckID,
|
|
|
|
"circonus_check_force_metric_activation": c.Telemetry.CirconusCheckForceMetricActivation,
|
|
|
|
"circonus_check_instance_id": c.Telemetry.CirconusCheckInstanceID,
|
|
|
|
"circonus_check_search_tag": c.Telemetry.CirconusCheckSearchTag,
|
|
|
|
"circonus_check_tags": c.Telemetry.CirconusCheckTags,
|
|
|
|
"circonus_check_display_name": c.Telemetry.CirconusCheckDisplayName,
|
|
|
|
"circonus_broker_id": c.Telemetry.CirconusBrokerID,
|
|
|
|
"circonus_broker_select_tag": c.Telemetry.CirconusBrokerSelectTag,
|
|
|
|
"dogstatsd_addr": c.Telemetry.DogStatsDAddr,
|
|
|
|
"dogstatsd_tags": c.Telemetry.DogStatsDTags,
|
|
|
|
"prometheus_retention_time": c.Telemetry.PrometheusRetentionTime,
|
|
|
|
"stackdriver_project_id": c.Telemetry.StackdriverProjectID,
|
|
|
|
"stackdriver_location": c.Telemetry.StackdriverLocation,
|
|
|
|
"stackdriver_namespace": c.Telemetry.StackdriverNamespace,
|
|
|
|
"stackdriver_debug_logs": c.Telemetry.StackdriverDebugLogs,
|
2020-11-13 18:26:58 +00:00
|
|
|
"lease_metrics_epsilon": c.Telemetry.LeaseMetricsEpsilon,
|
|
|
|
"num_lease_metrics_buckets": c.Telemetry.NumLeaseMetricsTimeBuckets,
|
|
|
|
"add_lease_metrics_namespace_labels": c.Telemetry.LeaseMetricsNameSpaceLabels,
|
2020-05-14 13:19:27 +00:00
|
|
|
}
|
|
|
|
result["telemetry"] = sanitizedTelemetry
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2021-06-17 18:09:37 +00:00
|
|
|
|
|
|
|
func (c *SharedConfig) found(s, k string) {
|
|
|
|
delete(c.UnusedKeys, s)
|
|
|
|
c.FoundKeys = append(c.FoundKeys, k)
|
|
|
|
}
|