2015-03-12 22:21:11 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2020-05-19 23:13:05 +00:00
|
|
|
"encoding/json"
|
2019-03-04 22:11:56 +00:00
|
|
|
"errors"
|
2015-03-12 22:21:11 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2022-05-16 17:57:19 +00:00
|
|
|
"math"
|
2015-03-12 22:21:11 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-08-15 13:42:42 +00:00
|
|
|
"strconv"
|
2015-03-12 22:21:11 +00:00
|
|
|
"strings"
|
2015-07-30 17:21:41 +00:00
|
|
|
"time"
|
2015-03-12 22:21:11 +00:00
|
|
|
|
2019-07-18 19:59:27 +00:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2021-07-16 00:17:31 +00:00
|
|
|
"github.com/hashicorp/go-secure-stdlib/parseutil"
|
2015-03-12 22:21:11 +00:00
|
|
|
"github.com/hashicorp/hcl"
|
2016-03-09 23:59:44 +00:00
|
|
|
"github.com/hashicorp/hcl/hcl/ast"
|
2023-01-16 16:07:18 +00:00
|
|
|
"github.com/hashicorp/vault/helper/experiments"
|
2022-04-04 16:45:41 +00:00
|
|
|
"github.com/hashicorp/vault/helper/osutil"
|
2020-10-13 23:38:21 +00:00
|
|
|
"github.com/hashicorp/vault/internalshared/configutil"
|
2022-04-04 16:45:41 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/consts"
|
2023-01-16 16:07:18 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/strutil"
|
2015-03-12 22:21:11 +00:00
|
|
|
)
|
|
|
|
|
2022-07-22 18:04:03 +00:00
|
|
|
const (
|
|
|
|
VaultDevCAFilename = "vault-ca.pem"
|
|
|
|
VaultDevCertFilename = "vault-cert.pem"
|
|
|
|
VaultDevKeyFilename = "vault-key.pem"
|
|
|
|
)
|
|
|
|
|
2023-01-16 16:07:18 +00:00
|
|
|
var (
|
|
|
|
entConfigValidate = func(_ *Config, _ string) []configutil.ConfigError {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Modified internally for testing.
|
|
|
|
validExperiments = experiments.ValidExperiments()
|
|
|
|
)
|
2021-07-12 21:33:17 +00:00
|
|
|
|
2015-03-12 22:21:11 +00:00
|
|
|
// Config is the configuration for the vault server.
|
|
|
|
type Config struct {
|
2021-05-11 16:59:08 +00:00
|
|
|
UnusedKeys configutil.UnusedKeyMap `hcl:",unusedKeyPositions"`
|
2021-06-17 18:09:37 +00:00
|
|
|
FoundKeys []string `hcl:",decodedFields"`
|
2020-02-15 00:39:13 +00:00
|
|
|
entConfig
|
|
|
|
|
2020-05-14 13:19:27 +00:00
|
|
|
*configutil.SharedConfig `hcl:"-"`
|
2015-04-17 18:25:20 +00:00
|
|
|
|
2020-05-14 13:19:27 +00:00
|
|
|
Storage *Storage `hcl:"-"`
|
|
|
|
HAStorage *Storage `hcl:"-"`
|
2019-12-06 14:46:39 +00:00
|
|
|
|
2020-05-14 13:19:27 +00:00
|
|
|
ServiceRegistration *ServiceRegistration `hcl:"-"`
|
2017-02-24 15:45:29 +00:00
|
|
|
|
2023-01-16 16:07:18 +00:00
|
|
|
Experiments []string `hcl:"experiments"`
|
|
|
|
|
2018-07-12 20:29:36 +00:00
|
|
|
CacheSize int `hcl:"cache_size"`
|
|
|
|
DisableCache bool `hcl:"-"`
|
|
|
|
DisableCacheRaw interface{} `hcl:"disable_cache"`
|
|
|
|
DisablePrintableCheck bool `hcl:"-"`
|
|
|
|
DisablePrintableCheckRaw interface{} `hcl:"disable_printable_check"`
|
2015-07-14 22:27:18 +00:00
|
|
|
|
2017-03-07 16:21:22 +00:00
|
|
|
EnableUI bool `hcl:"-"`
|
|
|
|
EnableUIRaw interface{} `hcl:"ui"`
|
2017-02-24 15:45:29 +00:00
|
|
|
|
2015-08-27 14:50:16 +00:00
|
|
|
MaxLeaseTTL time.Duration `hcl:"-"`
|
2021-05-04 19:47:56 +00:00
|
|
|
MaxLeaseTTLRaw interface{} `hcl:"max_lease_ttl,alias:MaxLeaseTTL"`
|
2015-08-27 14:50:16 +00:00
|
|
|
DefaultLeaseTTL time.Duration `hcl:"-"`
|
2021-05-04 19:47:56 +00:00
|
|
|
DefaultLeaseTTLRaw interface{} `hcl:"default_lease_ttl,alias:DefaultLeaseTTL"`
|
2016-07-26 06:25:33 +00:00
|
|
|
|
2017-08-30 20:28:23 +00:00
|
|
|
ClusterCipherSuites string `hcl:"cluster_cipher_suites"`
|
|
|
|
|
2017-04-04 00:52:29 +00:00
|
|
|
PluginDirectory string `hcl:"plugin_directory"`
|
2017-09-15 04:21:35 +00:00
|
|
|
|
2022-04-04 16:45:41 +00:00
|
|
|
PluginFileUid int `hcl:"plugin_file_uid"`
|
|
|
|
|
|
|
|
PluginFilePermissions int `hcl:"-"`
|
|
|
|
PluginFilePermissionsRaw interface{} `hcl:"plugin_file_permissions,alias:PluginFilePermissions"`
|
|
|
|
|
2022-12-15 20:19:19 +00:00
|
|
|
EnableIntrospectionEndpoint bool `hcl:"-"`
|
|
|
|
EnableIntrospectionEndpointRaw interface{} `hcl:"introspection_endpoint,alias:EnableIntrospectionEndpoint"`
|
|
|
|
|
2017-09-15 04:21:35 +00:00
|
|
|
EnableRawEndpoint bool `hcl:"-"`
|
2021-05-04 19:47:56 +00:00
|
|
|
EnableRawEndpointRaw interface{} `hcl:"raw_storage_endpoint,alias:EnableRawEndpoint"`
|
2017-11-08 19:19:41 +00:00
|
|
|
|
|
|
|
APIAddr string `hcl:"api_addr"`
|
|
|
|
ClusterAddr string `hcl:"cluster_addr"`
|
|
|
|
DisableClustering bool `hcl:"-"`
|
2021-05-04 19:47:56 +00:00
|
|
|
DisableClusteringRaw interface{} `hcl:"disable_clustering,alias:DisableClustering"`
|
2018-05-30 12:34:27 +00:00
|
|
|
|
2018-08-24 16:09:03 +00:00
|
|
|
DisablePerformanceStandby bool `hcl:"-"`
|
2021-05-04 19:47:56 +00:00
|
|
|
DisablePerformanceStandbyRaw interface{} `hcl:"disable_performance_standby,alias:DisablePerformanceStandby"`
|
2018-08-24 16:09:03 +00:00
|
|
|
|
2018-05-30 12:34:27 +00:00
|
|
|
DisableSealWrap bool `hcl:"-"`
|
2021-05-04 19:47:56 +00:00
|
|
|
DisableSealWrapRaw interface{} `hcl:"disable_sealwrap,alias:DisableSealWrap"`
|
2018-10-23 19:03:17 +00:00
|
|
|
|
2018-10-23 19:09:35 +00:00
|
|
|
DisableIndexing bool `hcl:"-"`
|
2021-05-04 19:47:56 +00:00
|
|
|
DisableIndexingRaw interface{} `hcl:"disable_indexing,alias:DisableIndexing"`
|
2020-08-10 10:23:44 +00:00
|
|
|
|
|
|
|
DisableSentinelTrace bool `hcl:"-"`
|
2021-05-04 19:47:56 +00:00
|
|
|
DisableSentinelTraceRaw interface{} `hcl:"disable_sentinel_trace,alias:DisableSentinelTrace"`
|
2021-04-20 22:25:04 +00:00
|
|
|
|
|
|
|
EnableResponseHeaderHostname bool `hcl:"-"`
|
|
|
|
EnableResponseHeaderHostnameRaw interface{} `hcl:"enable_response_header_hostname"`
|
|
|
|
|
2021-12-08 22:34:42 +00:00
|
|
|
LogRequestsLevel string `hcl:"-"`
|
|
|
|
LogRequestsLevelRaw interface{} `hcl:"log_requests_level"`
|
|
|
|
|
2023-01-11 19:32:05 +00:00
|
|
|
DetectDeadlocks string `hcl:"detect_deadlocks"`
|
|
|
|
|
2021-04-20 22:25:04 +00:00
|
|
|
EnableResponseHeaderRaftNodeID bool `hcl:"-"`
|
|
|
|
EnableResponseHeaderRaftNodeIDRaw interface{} `hcl:"enable_response_header_raft_node_id"`
|
2021-05-07 12:55:41 +00:00
|
|
|
|
2022-02-17 19:43:07 +00:00
|
|
|
License string `hcl:"-"`
|
|
|
|
LicensePath string `hcl:"license_path"`
|
|
|
|
DisableSSCTokens bool `hcl:"-"`
|
2015-03-12 22:21:11 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 19:47:56 +00:00
|
|
|
const (
|
|
|
|
sectionSeal = "Seal"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Config) Validate(sourceFilePath string) []configutil.ConfigError {
|
|
|
|
results := configutil.ValidateUnusedFields(c.UnusedKeys, sourceFilePath)
|
|
|
|
if c.Telemetry != nil {
|
|
|
|
results = append(results, c.Telemetry.Validate(sourceFilePath)...)
|
|
|
|
}
|
2021-07-12 21:33:17 +00:00
|
|
|
if c.ServiceRegistration != nil {
|
|
|
|
results = append(results, c.ServiceRegistration.Validate(sourceFilePath)...)
|
|
|
|
}
|
|
|
|
for _, l := range c.Listeners {
|
|
|
|
results = append(results, l.Validate(sourceFilePath)...)
|
|
|
|
}
|
|
|
|
results = append(results, c.validateEnt(sourceFilePath)...)
|
2021-05-04 19:47:56 +00:00
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
2021-07-12 21:33:17 +00:00
|
|
|
func (c *Config) validateEnt(sourceFilePath string) []configutil.ConfigError {
|
|
|
|
return entConfigValidate(c, sourceFilePath)
|
|
|
|
}
|
|
|
|
|
2015-03-31 23:44:47 +00:00
|
|
|
// DevConfig is a Config that is used for dev mode of Vault.
|
2020-05-14 13:19:27 +00:00
|
|
|
func DevConfig(storageType string) (*Config, error) {
|
|
|
|
hclStr := `
|
|
|
|
disable_mlock = true
|
|
|
|
|
|
|
|
listener "tcp" {
|
|
|
|
address = "127.0.0.1:8200"
|
|
|
|
tls_disable = true
|
|
|
|
proxy_protocol_behavior = "allow_authorized"
|
|
|
|
proxy_protocol_authorized_addrs = "127.0.0.1:8200"
|
2015-03-31 23:44:47 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 13:19:27 +00:00
|
|
|
telemetry {
|
|
|
|
prometheus_retention_time = "24h"
|
|
|
|
disable_hostname = true
|
2015-03-12 22:21:11 +00:00
|
|
|
}
|
2020-05-14 13:19:27 +00:00
|
|
|
enable_raw_endpoint = true
|
2015-03-12 22:21:11 +00:00
|
|
|
|
2020-05-14 13:19:27 +00:00
|
|
|
storage "%s" {
|
|
|
|
}
|
2019-10-17 17:33:00 +00:00
|
|
|
|
2020-07-02 22:18:57 +00:00
|
|
|
ui = true
|
2020-05-14 13:19:27 +00:00
|
|
|
`
|
2019-10-17 17:33:00 +00:00
|
|
|
|
2020-05-14 13:19:27 +00:00
|
|
|
hclStr = fmt.Sprintf(hclStr, storageType)
|
2021-05-04 19:47:56 +00:00
|
|
|
parsed, err := ParseConfig(hclStr, "")
|
2020-05-14 13:19:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error parsing dev config: %w", err)
|
|
|
|
}
|
|
|
|
return parsed, nil
|
2019-10-17 17:33:00 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 18:04:03 +00:00
|
|
|
// DevTLSConfig is a Config that is used for dev tls mode of Vault.
|
|
|
|
func DevTLSConfig(storageType, certDir string) (*Config, error) {
|
|
|
|
ca, err := GenerateCA()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, key, err := GenerateCert(ca.Template, ca.Signer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.WriteFile(fmt.Sprintf("%s/%s", certDir, VaultDevCAFilename), []byte(ca.PEM), 0o444); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.WriteFile(fmt.Sprintf("%s/%s", certDir, VaultDevCertFilename), []byte(cert), 0o400); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.WriteFile(fmt.Sprintf("%s/%s", certDir, VaultDevKeyFilename), []byte(key), 0o400); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
hclStr := `
|
|
|
|
disable_mlock = true
|
|
|
|
|
|
|
|
listener "tcp" {
|
|
|
|
address = "[::]:8200"
|
|
|
|
tls_cert_file = "%s/vault-cert.pem"
|
|
|
|
tls_key_file = "%s/vault-key.pem"
|
|
|
|
proxy_protocol_behavior = "allow_authorized"
|
|
|
|
proxy_protocol_authorized_addrs = "[::]:8200"
|
|
|
|
}
|
|
|
|
|
|
|
|
telemetry {
|
|
|
|
prometheus_retention_time = "24h"
|
|
|
|
disable_hostname = true
|
|
|
|
}
|
|
|
|
enable_raw_endpoint = true
|
|
|
|
|
|
|
|
storage "%s" {
|
|
|
|
}
|
|
|
|
|
|
|
|
ui = true
|
|
|
|
`
|
|
|
|
|
|
|
|
hclStr = fmt.Sprintf(hclStr, certDir, certDir, storageType)
|
|
|
|
parsed, err := ParseConfig(hclStr, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsed, nil
|
|
|
|
}
|
|
|
|
|
2017-03-08 14:17:00 +00:00
|
|
|
// Storage is the underlying storage configuration for the server.
|
|
|
|
type Storage struct {
|
2016-08-15 13:42:42 +00:00
|
|
|
Type string
|
|
|
|
RedirectAddr string
|
|
|
|
ClusterAddr string
|
|
|
|
DisableClustering bool
|
|
|
|
Config map[string]string
|
2015-03-12 22:21:11 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 14:17:00 +00:00
|
|
|
func (b *Storage) GoString() string {
|
2015-03-12 22:21:11 +00:00
|
|
|
return fmt.Sprintf("*%#v", *b)
|
|
|
|
}
|
|
|
|
|
2019-12-06 14:46:39 +00:00
|
|
|
// ServiceRegistration is the optional service discovery for the server.
|
|
|
|
type ServiceRegistration struct {
|
2021-07-12 21:33:17 +00:00
|
|
|
UnusedKeys configutil.UnusedKeyMap `hcl:",unusedKeyPositions"`
|
|
|
|
Type string
|
|
|
|
Config map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *ServiceRegistration) Validate(source string) []configutil.ConfigError {
|
|
|
|
return configutil.ValidateUnusedFields(b.UnusedKeys, source)
|
2019-12-06 14:46:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *ServiceRegistration) GoString() string {
|
|
|
|
return fmt.Sprintf("*%#v", *b)
|
|
|
|
}
|
|
|
|
|
2020-05-14 13:19:27 +00:00
|
|
|
func NewConfig() *Config {
|
|
|
|
return &Config{
|
|
|
|
SharedConfig: new(configutil.SharedConfig),
|
|
|
|
}
|
2015-07-14 22:27:18 +00:00
|
|
|
}
|
|
|
|
|
2015-03-12 22:21:11 +00:00
|
|
|
// Merge merges two configurations.
|
|
|
|
func (c *Config) Merge(c2 *Config) *Config {
|
2016-07-05 20:49:15 +00:00
|
|
|
if c2 == nil {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-05-14 13:19:27 +00:00
|
|
|
result := NewConfig()
|
|
|
|
|
|
|
|
result.SharedConfig = c.SharedConfig
|
|
|
|
if c2.SharedConfig != nil {
|
|
|
|
result.SharedConfig = c.SharedConfig.Merge(c2.SharedConfig)
|
2015-03-12 22:21:11 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 14:17:00 +00:00
|
|
|
result.Storage = c.Storage
|
|
|
|
if c2.Storage != nil {
|
|
|
|
result.Storage = c2.Storage
|
2015-03-12 22:21:11 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 14:17:00 +00:00
|
|
|
result.HAStorage = c.HAStorage
|
|
|
|
if c2.HAStorage != nil {
|
|
|
|
result.HAStorage = c2.HAStorage
|
2016-03-21 20:56:13 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 14:46:39 +00:00
|
|
|
result.ServiceRegistration = c.ServiceRegistration
|
|
|
|
if c2.ServiceRegistration != nil {
|
|
|
|
result.ServiceRegistration = c2.ServiceRegistration
|
|
|
|
}
|
|
|
|
|
2016-08-26 14:27:06 +00:00
|
|
|
result.CacheSize = c.CacheSize
|
|
|
|
if c2.CacheSize != 0 {
|
|
|
|
result.CacheSize = c2.CacheSize
|
|
|
|
}
|
|
|
|
|
2015-10-12 16:55:02 +00:00
|
|
|
// merging these booleans via an OR operation
|
|
|
|
result.DisableCache = c.DisableCache
|
|
|
|
if c2.DisableCache {
|
|
|
|
result.DisableCache = c2.DisableCache
|
|
|
|
}
|
|
|
|
|
2020-08-10 10:23:44 +00:00
|
|
|
result.DisableSentinelTrace = c.DisableSentinelTrace
|
|
|
|
if c2.DisableSentinelTrace {
|
|
|
|
result.DisableSentinelTrace = c2.DisableSentinelTrace
|
|
|
|
}
|
|
|
|
|
2019-01-23 16:27:21 +00:00
|
|
|
result.DisablePrintableCheck = c.DisablePrintableCheck
|
|
|
|
if c2.DisablePrintableCheckRaw != nil {
|
|
|
|
result.DisablePrintableCheck = c2.DisablePrintableCheck
|
|
|
|
}
|
|
|
|
|
2015-07-30 13:42:49 +00:00
|
|
|
// merge these integers via a MAX operation
|
2015-08-27 14:50:16 +00:00
|
|
|
result.MaxLeaseTTL = c.MaxLeaseTTL
|
|
|
|
if c2.MaxLeaseTTL > result.MaxLeaseTTL {
|
|
|
|
result.MaxLeaseTTL = c2.MaxLeaseTTL
|
2015-07-30 13:42:49 +00:00
|
|
|
}
|
|
|
|
|
2015-08-27 14:50:16 +00:00
|
|
|
result.DefaultLeaseTTL = c.DefaultLeaseTTL
|
|
|
|
if c2.DefaultLeaseTTL > result.DefaultLeaseTTL {
|
|
|
|
result.DefaultLeaseTTL = c2.DefaultLeaseTTL
|
2015-07-30 13:42:49 +00:00
|
|
|
}
|
|
|
|
|
2017-08-30 20:28:23 +00:00
|
|
|
result.ClusterCipherSuites = c.ClusterCipherSuites
|
|
|
|
if c2.ClusterCipherSuites != "" {
|
|
|
|
result.ClusterCipherSuites = c2.ClusterCipherSuites
|
|
|
|
}
|
|
|
|
|
2017-02-24 15:45:29 +00:00
|
|
|
result.EnableUI = c.EnableUI
|
|
|
|
if c2.EnableUI {
|
|
|
|
result.EnableUI = c2.EnableUI
|
|
|
|
}
|
|
|
|
|
2017-09-15 04:21:35 +00:00
|
|
|
result.EnableRawEndpoint = c.EnableRawEndpoint
|
|
|
|
if c2.EnableRawEndpoint {
|
|
|
|
result.EnableRawEndpoint = c2.EnableRawEndpoint
|
|
|
|
}
|
|
|
|
|
2022-12-15 20:19:19 +00:00
|
|
|
result.EnableIntrospectionEndpoint = c.EnableIntrospectionEndpoint
|
|
|
|
if c2.EnableIntrospectionEndpoint {
|
|
|
|
result.EnableIntrospectionEndpoint = c2.EnableIntrospectionEndpoint
|
|
|
|
}
|
|
|
|
|
2019-01-23 16:27:21 +00:00
|
|
|
result.APIAddr = c.APIAddr
|
|
|
|
if c2.APIAddr != "" {
|
|
|
|
result.APIAddr = c2.APIAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
result.ClusterAddr = c.ClusterAddr
|
|
|
|
if c2.ClusterAddr != "" {
|
|
|
|
result.ClusterAddr = c2.ClusterAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retain raw value so that it can be assigned to storage objects
|
|
|
|
result.DisableClustering = c.DisableClustering
|
|
|
|
result.DisableClusteringRaw = c.DisableClusteringRaw
|
|
|
|
if c2.DisableClusteringRaw != nil {
|
|
|
|
result.DisableClustering = c2.DisableClustering
|
|
|
|
result.DisableClusteringRaw = c2.DisableClusteringRaw
|
|
|
|
}
|
|
|
|
|
2017-04-13 18:22:53 +00:00
|
|
|
result.PluginDirectory = c.PluginDirectory
|
|
|
|
if c2.PluginDirectory != "" {
|
|
|
|
result.PluginDirectory = c2.PluginDirectory
|
|
|
|
}
|
|
|
|
|
2022-04-04 16:45:41 +00:00
|
|
|
result.PluginFileUid = c.PluginFileUid
|
|
|
|
if c2.PluginFileUid != 0 {
|
|
|
|
result.PluginFileUid = c2.PluginFileUid
|
|
|
|
}
|
|
|
|
|
|
|
|
result.PluginFilePermissions = c.PluginFilePermissions
|
|
|
|
if c2.PluginFilePermissionsRaw != nil {
|
|
|
|
result.PluginFilePermissions = c2.PluginFilePermissions
|
|
|
|
result.PluginFilePermissionsRaw = c2.PluginFilePermissionsRaw
|
|
|
|
}
|
|
|
|
|
2018-08-24 16:09:03 +00:00
|
|
|
result.DisablePerformanceStandby = c.DisablePerformanceStandby
|
|
|
|
if c2.DisablePerformanceStandby {
|
|
|
|
result.DisablePerformanceStandby = c2.DisablePerformanceStandby
|
|
|
|
}
|
|
|
|
|
2018-05-30 12:34:27 +00:00
|
|
|
result.DisableSealWrap = c.DisableSealWrap
|
|
|
|
if c2.DisableSealWrap {
|
|
|
|
result.DisableSealWrap = c2.DisableSealWrap
|
|
|
|
}
|
|
|
|
|
2018-10-23 19:03:17 +00:00
|
|
|
result.DisableIndexing = c.DisableIndexing
|
|
|
|
if c2.DisableIndexing {
|
|
|
|
result.DisableIndexing = c2.DisableIndexing
|
|
|
|
}
|
|
|
|
|
2021-04-20 22:25:04 +00:00
|
|
|
result.EnableResponseHeaderHostname = c.EnableResponseHeaderHostname
|
|
|
|
if c2.EnableResponseHeaderHostname {
|
|
|
|
result.EnableResponseHeaderHostname = c2.EnableResponseHeaderHostname
|
|
|
|
}
|
|
|
|
|
2021-12-08 22:34:42 +00:00
|
|
|
result.LogRequestsLevel = c.LogRequestsLevel
|
|
|
|
if c2.LogRequestsLevel != "" {
|
|
|
|
result.LogRequestsLevel = c2.LogRequestsLevel
|
|
|
|
}
|
|
|
|
|
2023-01-11 19:32:05 +00:00
|
|
|
result.DetectDeadlocks = c.DetectDeadlocks
|
|
|
|
if c2.DetectDeadlocks != "" {
|
|
|
|
result.DetectDeadlocks = c2.DetectDeadlocks
|
|
|
|
}
|
|
|
|
|
2021-04-20 22:25:04 +00:00
|
|
|
result.EnableResponseHeaderRaftNodeID = c.EnableResponseHeaderRaftNodeID
|
|
|
|
if c2.EnableResponseHeaderRaftNodeID {
|
|
|
|
result.EnableResponseHeaderRaftNodeID = c2.EnableResponseHeaderRaftNodeID
|
|
|
|
}
|
|
|
|
|
2021-05-07 12:55:41 +00:00
|
|
|
result.LicensePath = c.LicensePath
|
|
|
|
if c2.LicensePath != "" {
|
|
|
|
result.LicensePath = c2.LicensePath
|
|
|
|
}
|
|
|
|
|
2019-01-23 16:27:21 +00:00
|
|
|
// Use values from top-level configuration for storage if set
|
|
|
|
if storage := result.Storage; storage != nil {
|
|
|
|
if result.APIAddr != "" {
|
|
|
|
storage.RedirectAddr = result.APIAddr
|
|
|
|
}
|
|
|
|
if result.ClusterAddr != "" {
|
|
|
|
storage.ClusterAddr = result.ClusterAddr
|
|
|
|
}
|
|
|
|
if result.DisableClusteringRaw != nil {
|
|
|
|
storage.DisableClustering = result.DisableClustering
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if haStorage := result.HAStorage; haStorage != nil {
|
|
|
|
if result.APIAddr != "" {
|
|
|
|
haStorage.RedirectAddr = result.APIAddr
|
|
|
|
}
|
|
|
|
if result.ClusterAddr != "" {
|
|
|
|
haStorage.ClusterAddr = result.ClusterAddr
|
|
|
|
}
|
|
|
|
if result.DisableClusteringRaw != nil {
|
|
|
|
haStorage.DisableClustering = result.DisableClustering
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 23:47:34 +00:00
|
|
|
result.entConfig = c.entConfig.Merge(c2.entConfig)
|
|
|
|
|
2023-01-16 16:07:18 +00:00
|
|
|
result.Experiments = mergeExperiments(c.Experiments, c2.Experiments)
|
|
|
|
|
2015-03-12 22:21:11 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2015-03-12 22:30:07 +00:00
|
|
|
// LoadConfig loads the configuration at the given path, regardless if
|
|
|
|
// its a file or directory.
|
2019-07-18 19:59:27 +00:00
|
|
|
func LoadConfig(path string) (*Config, error) {
|
2015-03-12 22:30:07 +00:00
|
|
|
fi, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.IsDir() {
|
2022-04-04 16:45:41 +00:00
|
|
|
// check permissions on the config directory
|
2022-05-17 18:34:31 +00:00
|
|
|
var enableFilePermissionsCheck bool
|
|
|
|
if enableFilePermissionsCheckEnv := os.Getenv(consts.VaultEnableFilePermissionsCheckEnv); enableFilePermissionsCheckEnv != "" {
|
|
|
|
var err error
|
|
|
|
enableFilePermissionsCheck, err = strconv.ParseBool(enableFilePermissionsCheckEnv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("Error parsing the environment variable VAULT_ENABLE_FILE_PERMISSIONS_CHECK")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if enableFilePermissionsCheck {
|
2022-04-04 16:45:41 +00:00
|
|
|
err = osutil.OwnerPermissionsMatch(path, 0, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 13:19:27 +00:00
|
|
|
return CheckConfig(LoadConfigDir(path))
|
2015-03-12 22:30:07 +00:00
|
|
|
}
|
2020-05-14 13:19:27 +00:00
|
|
|
return CheckConfig(LoadConfigFile(path))
|
|
|
|
}
|
|
|
|
|
|
|
|
func CheckConfig(c *Config, e error) (*Config, error) {
|
|
|
|
if e != nil {
|
|
|
|
return c, e
|
|
|
|
}
|
|
|
|
|
2023-02-01 19:34:53 +00:00
|
|
|
if len(c.Seals) == 2 {
|
2020-05-14 13:19:27 +00:00
|
|
|
switch {
|
|
|
|
case c.Seals[0].Disabled && c.Seals[1].Disabled:
|
|
|
|
return nil, errors.New("seals: two seals provided but both are disabled")
|
|
|
|
case !c.Seals[0].Disabled && !c.Seals[1].Disabled:
|
|
|
|
return nil, errors.New("seals: two seals provided but neither is disabled")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
2015-03-12 22:30:07 +00:00
|
|
|
}
|
|
|
|
|
2015-03-12 22:21:11 +00:00
|
|
|
// LoadConfigFile loads the configuration from the given file.
|
2019-07-18 19:59:27 +00:00
|
|
|
func LoadConfigFile(path string) (*Config, error) {
|
2015-03-12 22:21:11 +00:00
|
|
|
// Read the file
|
|
|
|
d, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-14 13:19:27 +00:00
|
|
|
|
2021-05-04 19:47:56 +00:00
|
|
|
conf, err := ParseConfig(string(d), path)
|
2020-05-14 13:19:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-17 18:34:31 +00:00
|
|
|
var enableFilePermissionsCheck bool
|
|
|
|
if enableFilePermissionsCheckEnv := os.Getenv(consts.VaultEnableFilePermissionsCheckEnv); enableFilePermissionsCheckEnv != "" {
|
|
|
|
var err error
|
|
|
|
enableFilePermissionsCheck, err = strconv.ParseBool(enableFilePermissionsCheckEnv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("Error parsing the environment variable VAULT_ENABLE_FILE_PERMISSIONS_CHECK")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if enableFilePermissionsCheck {
|
2022-04-04 16:45:41 +00:00
|
|
|
// check permissions of the config file
|
|
|
|
err = osutil.OwnerPermissionsMatch(path, 0, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// check permissions of the plugin directory
|
|
|
|
if conf.PluginDirectory != "" {
|
|
|
|
|
|
|
|
err = osutil.OwnerPermissionsMatch(conf.PluginDirectory, conf.PluginFileUid, conf.PluginFilePermissions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 13:19:27 +00:00
|
|
|
return conf, nil
|
2016-03-09 23:59:44 +00:00
|
|
|
}
|
2015-03-12 22:21:11 +00:00
|
|
|
|
2021-05-04 19:47:56 +00:00
|
|
|
func ParseConfig(d, source string) (*Config, error) {
|
2015-03-12 22:21:11 +00:00
|
|
|
// Parse!
|
2016-03-09 23:59:44 +00:00
|
|
|
obj, err := hcl.Parse(d)
|
2015-03-12 22:21:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start building the result
|
2020-05-14 13:19:27 +00:00
|
|
|
result := NewConfig()
|
|
|
|
if err := hcl.DecodeObject(result, obj); err != nil {
|
2015-04-17 18:21:40 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-21 14:10:48 +00:00
|
|
|
if rendered, err := configutil.ParseSingleIPTemplate(result.APIAddr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
result.APIAddr = rendered
|
|
|
|
}
|
|
|
|
if rendered, err := configutil.ParseSingleIPTemplate(result.ClusterAddr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
result.ClusterAddr = rendered
|
|
|
|
}
|
|
|
|
|
2020-05-14 13:19:27 +00:00
|
|
|
sharedConfig, err := configutil.ParseConfig(d)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result.SharedConfig = sharedConfig
|
|
|
|
|
2017-03-07 16:21:22 +00:00
|
|
|
if result.MaxLeaseTTLRaw != nil {
|
|
|
|
if result.MaxLeaseTTL, err = parseutil.ParseDurationSecond(result.MaxLeaseTTLRaw); err != nil {
|
2015-07-30 17:21:41 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2017-03-07 16:21:22 +00:00
|
|
|
if result.DefaultLeaseTTLRaw != nil {
|
|
|
|
if result.DefaultLeaseTTL, err = parseutil.ParseDurationSecond(result.DefaultLeaseTTLRaw); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.EnableUIRaw != nil {
|
|
|
|
if result.EnableUI, err = parseutil.ParseBool(result.EnableUIRaw); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.DisableCacheRaw != nil {
|
|
|
|
if result.DisableCache, err = parseutil.ParseBool(result.DisableCacheRaw); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 20:29:36 +00:00
|
|
|
if result.DisablePrintableCheckRaw != nil {
|
|
|
|
if result.DisablePrintableCheck, err = parseutil.ParseBool(result.DisablePrintableCheckRaw); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-15 04:21:35 +00:00
|
|
|
if result.EnableRawEndpointRaw != nil {
|
|
|
|
if result.EnableRawEndpoint, err = parseutil.ParseBool(result.EnableRawEndpointRaw); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 20:19:19 +00:00
|
|
|
if result.EnableIntrospectionEndpointRaw != nil {
|
|
|
|
if result.EnableIntrospectionEndpoint, err = parseutil.ParseBool(result.EnableIntrospectionEndpointRaw); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-08 19:19:41 +00:00
|
|
|
if result.DisableClusteringRaw != nil {
|
|
|
|
if result.DisableClustering, err = parseutil.ParseBool(result.DisableClusteringRaw); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-04 16:45:41 +00:00
|
|
|
if result.PluginFilePermissionsRaw != nil {
|
|
|
|
octalPermissionsString, err := parseutil.ParseString(result.PluginFilePermissionsRaw)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pluginFilePermissions, err := strconv.ParseInt(octalPermissionsString, 8, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-16 17:57:19 +00:00
|
|
|
if pluginFilePermissions < math.MinInt || pluginFilePermissions > math.MaxInt {
|
|
|
|
return nil, fmt.Errorf("file permission value %v cannot be safely cast to int: exceeds bounds (%v, %v)", pluginFilePermissions, math.MinInt, math.MaxInt)
|
|
|
|
}
|
2022-04-04 16:45:41 +00:00
|
|
|
result.PluginFilePermissions = int(pluginFilePermissions)
|
|
|
|
}
|
|
|
|
|
2020-08-10 10:23:44 +00:00
|
|
|
if result.DisableSentinelTraceRaw != nil {
|
|
|
|
if result.DisableSentinelTrace, err = parseutil.ParseBool(result.DisableSentinelTraceRaw); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-24 16:09:03 +00:00
|
|
|
if result.DisablePerformanceStandbyRaw != nil {
|
|
|
|
if result.DisablePerformanceStandby, err = parseutil.ParseBool(result.DisablePerformanceStandbyRaw); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 12:34:27 +00:00
|
|
|
if result.DisableSealWrapRaw != nil {
|
|
|
|
if result.DisableSealWrap, err = parseutil.ParseBool(result.DisableSealWrapRaw); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 19:09:35 +00:00
|
|
|
if result.DisableIndexingRaw != nil {
|
|
|
|
if result.DisableIndexing, err = parseutil.ParseBool(result.DisableIndexingRaw); err != nil {
|
2018-10-23 19:03:17 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 22:25:04 +00:00
|
|
|
if result.EnableResponseHeaderHostnameRaw != nil {
|
|
|
|
if result.EnableResponseHeaderHostname, err = parseutil.ParseBool(result.EnableResponseHeaderHostnameRaw); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 22:34:42 +00:00
|
|
|
if result.LogRequestsLevelRaw != nil {
|
|
|
|
result.LogRequestsLevel = strings.ToLower(strings.TrimSpace(result.LogRequestsLevelRaw.(string)))
|
|
|
|
result.LogRequestsLevelRaw = ""
|
|
|
|
}
|
|
|
|
|
2021-04-20 22:25:04 +00:00
|
|
|
if result.EnableResponseHeaderRaftNodeIDRaw != nil {
|
|
|
|
if result.EnableResponseHeaderRaftNodeID, err = parseutil.ParseBool(result.EnableResponseHeaderRaftNodeIDRaw); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-09 23:59:44 +00:00
|
|
|
list, ok := obj.Node.(*ast.ObjectList)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("error parsing: file doesn't contain a root object")
|
2015-03-12 22:21:11 +00:00
|
|
|
}
|
2016-03-09 23:59:44 +00:00
|
|
|
|
2017-03-08 14:17:00 +00:00
|
|
|
// Look for storage but still support old backend
|
|
|
|
if o := list.Filter("storage"); len(o.Items) > 0 {
|
2021-06-17 18:09:37 +00:00
|
|
|
delete(result.UnusedKeys, "storage")
|
2020-05-14 13:19:27 +00:00
|
|
|
if err := ParseStorage(result, o, "storage"); err != nil {
|
2021-06-02 13:22:31 +00:00
|
|
|
return nil, fmt.Errorf("error parsing 'storage': %w", err)
|
2017-03-08 14:17:00 +00:00
|
|
|
}
|
2022-05-12 16:04:56 +00:00
|
|
|
result.found(result.Storage.Type, result.Storage.Type)
|
2017-03-08 14:17:00 +00:00
|
|
|
} else {
|
2021-06-17 18:09:37 +00:00
|
|
|
delete(result.UnusedKeys, "backend")
|
2017-03-08 14:17:00 +00:00
|
|
|
if o := list.Filter("backend"); len(o.Items) > 0 {
|
2020-05-14 13:19:27 +00:00
|
|
|
if err := ParseStorage(result, o, "backend"); err != nil {
|
2021-06-02 13:22:31 +00:00
|
|
|
return nil, fmt.Errorf("error parsing 'backend': %w", err)
|
2017-03-08 14:17:00 +00:00
|
|
|
}
|
2016-03-09 23:59:44 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-14 22:27:18 +00:00
|
|
|
|
2017-03-08 14:17:00 +00:00
|
|
|
if o := list.Filter("ha_storage"); len(o.Items) > 0 {
|
2021-06-17 18:09:37 +00:00
|
|
|
delete(result.UnusedKeys, "ha_storage")
|
2020-05-14 13:19:27 +00:00
|
|
|
if err := parseHAStorage(result, o, "ha_storage"); err != nil {
|
2021-06-02 13:22:31 +00:00
|
|
|
return nil, fmt.Errorf("error parsing 'ha_storage': %w", err)
|
2017-03-08 14:17:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if o := list.Filter("ha_backend"); len(o.Items) > 0 {
|
2021-06-17 18:09:37 +00:00
|
|
|
delete(result.UnusedKeys, "ha_backend")
|
2020-05-14 13:19:27 +00:00
|
|
|
if err := parseHAStorage(result, o, "ha_backend"); err != nil {
|
2021-06-02 13:22:31 +00:00
|
|
|
return nil, fmt.Errorf("error parsing 'ha_backend': %w", err)
|
2017-03-08 14:17:00 +00:00
|
|
|
}
|
2015-07-14 22:27:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-06 14:46:39 +00:00
|
|
|
// Parse service discovery
|
|
|
|
if o := list.Filter("service_registration"); len(o.Items) > 0 {
|
2021-06-17 18:09:37 +00:00
|
|
|
delete(result.UnusedKeys, "service_registration")
|
2020-05-14 13:19:27 +00:00
|
|
|
if err := parseServiceRegistration(result, o, "service_registration"); err != nil {
|
2021-06-02 13:22:31 +00:00
|
|
|
return nil, fmt.Errorf("error parsing 'service_registration': %w", err)
|
2019-12-06 14:46:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 16:07:18 +00:00
|
|
|
if err := validateExperiments(result.Experiments); err != nil {
|
|
|
|
return nil, fmt.Errorf("error validating experiment(s) from config: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-12-16 20:57:18 +00:00
|
|
|
if err := result.parseConfig(list); err != nil {
|
2021-06-02 13:22:31 +00:00
|
|
|
return nil, fmt.Errorf("error parsing enterprise config: %w", err)
|
2020-02-15 00:39:13 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 19:47:56 +00:00
|
|
|
// Remove all unused keys from Config that were satisfied by SharedConfig.
|
2021-06-17 18:09:37 +00:00
|
|
|
result.UnusedKeys = configutil.UnusedFieldDifference(result.UnusedKeys, nil, append(result.FoundKeys, sharedConfig.FoundKeys...))
|
2021-05-04 19:47:56 +00:00
|
|
|
// Assign file info
|
|
|
|
for _, v := range result.UnusedKeys {
|
2021-09-23 13:30:44 +00:00
|
|
|
for i := range v {
|
|
|
|
v[i].Filename = source
|
2021-05-04 19:47:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 13:19:27 +00:00
|
|
|
return result, nil
|
2015-07-14 22:27:18 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 16:07:18 +00:00
|
|
|
func ExperimentsFromEnvAndCLI(config *Config, envKey string, flagExperiments []string) error {
|
|
|
|
if envExperimentsRaw := os.Getenv(envKey); envExperimentsRaw != "" {
|
|
|
|
envExperiments := strings.Split(envExperimentsRaw, ",")
|
|
|
|
err := validateExperiments(envExperiments)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error validating experiment(s) from environment variable %q: %w", envKey, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config.Experiments = mergeExperiments(config.Experiments, envExperiments)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(flagExperiments) != 0 {
|
|
|
|
err := validateExperiments(flagExperiments)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error validating experiment(s) from command line flag: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config.Experiments = mergeExperiments(config.Experiments, flagExperiments)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate checks each experiment is a known experiment.
|
|
|
|
func validateExperiments(experiments []string) error {
|
|
|
|
var invalid []string
|
|
|
|
|
|
|
|
for _, experiment := range experiments {
|
|
|
|
if !strutil.StrListContains(validExperiments, experiment) {
|
|
|
|
invalid = append(invalid, experiment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(invalid) != 0 {
|
|
|
|
return fmt.Errorf("valid experiment(s) are %s, but received the following invalid experiment(s): %s",
|
|
|
|
strings.Join(validExperiments, ", "),
|
|
|
|
strings.Join(invalid, ", "))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// mergeExperiments returns the logical OR of the two sets.
|
|
|
|
func mergeExperiments(left, right []string) []string {
|
|
|
|
processed := map[string]struct{}{}
|
|
|
|
var result []string
|
|
|
|
for _, l := range left {
|
|
|
|
if _, seen := processed[l]; !seen {
|
|
|
|
result = append(result, l)
|
|
|
|
}
|
|
|
|
processed[l] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range right {
|
|
|
|
if _, seen := processed[r]; !seen {
|
|
|
|
result = append(result, r)
|
|
|
|
processed[r] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2015-03-12 22:21:11 +00:00
|
|
|
// LoadConfigDir loads all the configurations in the given directory
|
|
|
|
// in alphabetical order.
|
2019-07-18 19:59:27 +00:00
|
|
|
func LoadConfigDir(dir string) (*Config, error) {
|
2015-03-12 22:21:11 +00:00
|
|
|
f, err := os.Open(dir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
fi, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !fi.IsDir() {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, fmt.Errorf("configuration path must be a directory: %q", dir)
|
2015-03-12 22:21:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var files []string
|
|
|
|
err = nil
|
|
|
|
for err != io.EOF {
|
|
|
|
var fis []os.FileInfo
|
|
|
|
fis, err = f.Readdir(128)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fi := range fis {
|
|
|
|
// Ignore directories
|
|
|
|
if fi.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only care about files that are valid to load.
|
|
|
|
name := fi.Name()
|
|
|
|
skip := true
|
|
|
|
if strings.HasSuffix(name, ".hcl") {
|
|
|
|
skip = false
|
|
|
|
} else if strings.HasSuffix(name, ".json") {
|
|
|
|
skip = false
|
|
|
|
}
|
|
|
|
if skip || isTemporaryFile(name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
path := filepath.Join(dir, name)
|
|
|
|
files = append(files, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 13:19:27 +00:00
|
|
|
result := NewConfig()
|
2015-03-12 22:21:11 +00:00
|
|
|
for _, f := range files {
|
2019-07-18 19:59:27 +00:00
|
|
|
config, err := LoadConfigFile(f)
|
2015-03-12 22:21:11 +00:00
|
|
|
if err != nil {
|
2021-06-02 13:22:31 +00:00
|
|
|
return nil, fmt.Errorf("error loading %q: %w", f, err)
|
2015-03-12 22:21:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if result == nil {
|
|
|
|
result = config
|
|
|
|
} else {
|
|
|
|
result = result.Merge(config)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isTemporaryFile returns true or false depending on whether the
|
|
|
|
// provided file name is a temporary file for the following editors:
|
|
|
|
// emacs or vim.
|
|
|
|
func isTemporaryFile(name string) bool {
|
|
|
|
return strings.HasSuffix(name, "~") || // vim
|
|
|
|
strings.HasPrefix(name, ".#") || // emacs
|
|
|
|
(strings.HasPrefix(name, "#") && strings.HasSuffix(name, "#")) // emacs
|
|
|
|
}
|
|
|
|
|
2018-09-25 23:18:22 +00:00
|
|
|
func ParseStorage(result *Config, list *ast.ObjectList, name string) error {
|
2016-03-09 23:59:44 +00:00
|
|
|
if len(list.Items) > 1 {
|
2017-03-08 14:17:00 +00:00
|
|
|
return fmt.Errorf("only one %q block is permitted", name)
|
2016-03-09 23:59:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get our item
|
|
|
|
item := list.Items[0]
|
|
|
|
|
2017-03-08 14:17:00 +00:00
|
|
|
key := name
|
2016-03-09 23:59:44 +00:00
|
|
|
if len(item.Keys) > 0 {
|
|
|
|
key = item.Keys[0].Token.Value().(string)
|
|
|
|
}
|
|
|
|
|
2020-01-14 01:02:16 +00:00
|
|
|
var config map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&config, item.Val); err != nil {
|
2017-03-08 14:17:00 +00:00
|
|
|
return multierror.Prefix(err, fmt.Sprintf("%s.%s:", name, key))
|
2016-03-09 23:59:44 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 01:02:16 +00:00
|
|
|
m := make(map[string]string)
|
|
|
|
for key, val := range config {
|
|
|
|
valStr, ok := val.(string)
|
|
|
|
if ok {
|
|
|
|
m[key] = valStr
|
|
|
|
continue
|
|
|
|
}
|
2020-05-19 23:13:05 +00:00
|
|
|
valBytes, err := json.Marshal(val)
|
2020-01-14 01:02:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m[key] = string(valBytes)
|
|
|
|
}
|
|
|
|
|
2016-08-15 13:42:42 +00:00
|
|
|
// Pull out the redirect address since it's common to all backends
|
|
|
|
var redirectAddr string
|
|
|
|
if v, ok := m["redirect_addr"]; ok {
|
|
|
|
redirectAddr = v
|
|
|
|
delete(m, "redirect_addr")
|
|
|
|
} else if v, ok := m["advertise_addr"]; ok {
|
|
|
|
redirectAddr = v
|
2016-03-09 23:59:44 +00:00
|
|
|
delete(m, "advertise_addr")
|
2015-03-12 22:21:11 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 13:42:42 +00:00
|
|
|
// Pull out the cluster address since it's common to all backends
|
|
|
|
var clusterAddr string
|
|
|
|
if v, ok := m["cluster_addr"]; ok {
|
|
|
|
clusterAddr = v
|
|
|
|
delete(m, "cluster_addr")
|
|
|
|
}
|
|
|
|
|
2016-10-02 18:54:01 +00:00
|
|
|
var disableClustering bool
|
2016-08-15 13:42:42 +00:00
|
|
|
var err error
|
|
|
|
if v, ok := m["disable_clustering"]; ok {
|
|
|
|
disableClustering, err = strconv.ParseBool(v)
|
|
|
|
if err != nil {
|
2017-03-08 14:17:00 +00:00
|
|
|
return multierror.Prefix(err, fmt.Sprintf("%s.%s:", name, key))
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
delete(m, "disable_clustering")
|
|
|
|
}
|
|
|
|
|
2017-11-08 19:19:41 +00:00
|
|
|
// Override with top-level values if they are set
|
|
|
|
if result.APIAddr != "" {
|
|
|
|
redirectAddr = result.APIAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.ClusterAddr != "" {
|
|
|
|
clusterAddr = result.ClusterAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.DisableClusteringRaw != nil {
|
|
|
|
disableClustering = result.DisableClustering
|
|
|
|
}
|
|
|
|
|
2017-03-08 14:17:00 +00:00
|
|
|
result.Storage = &Storage{
|
2016-08-15 13:42:42 +00:00
|
|
|
RedirectAddr: redirectAddr,
|
|
|
|
ClusterAddr: clusterAddr,
|
|
|
|
DisableClustering: disableClustering,
|
|
|
|
Type: strings.ToLower(key),
|
|
|
|
Config: m,
|
2016-03-09 23:59:44 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-08 14:17:00 +00:00
|
|
|
func parseHAStorage(result *Config, list *ast.ObjectList, name string) error {
|
2016-03-09 23:59:44 +00:00
|
|
|
if len(list.Items) > 1 {
|
2017-03-08 14:17:00 +00:00
|
|
|
return fmt.Errorf("only one %q block is permitted", name)
|
2015-03-12 22:21:11 +00:00
|
|
|
}
|
|
|
|
|
2016-03-09 23:59:44 +00:00
|
|
|
// Get our item
|
|
|
|
item := list.Items[0]
|
|
|
|
|
2017-03-08 14:17:00 +00:00
|
|
|
key := name
|
2016-03-09 23:59:44 +00:00
|
|
|
if len(item.Keys) > 0 {
|
|
|
|
key = item.Keys[0].Token.Value().(string)
|
|
|
|
}
|
|
|
|
|
2022-06-09 22:55:49 +00:00
|
|
|
var config map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&config, item.Val); err != nil {
|
2017-03-08 14:17:00 +00:00
|
|
|
return multierror.Prefix(err, fmt.Sprintf("%s.%s:", name, key))
|
2016-03-09 23:59:44 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 22:55:49 +00:00
|
|
|
m := make(map[string]string)
|
|
|
|
for key, val := range config {
|
|
|
|
valStr, ok := val.(string)
|
|
|
|
if ok {
|
|
|
|
m[key] = valStr
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
valBytes, err := json.Marshal(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m[key] = string(valBytes)
|
|
|
|
}
|
|
|
|
|
2016-08-15 13:42:42 +00:00
|
|
|
// Pull out the redirect address since it's common to all backends
|
|
|
|
var redirectAddr string
|
|
|
|
if v, ok := m["redirect_addr"]; ok {
|
|
|
|
redirectAddr = v
|
|
|
|
delete(m, "redirect_addr")
|
|
|
|
} else if v, ok := m["advertise_addr"]; ok {
|
|
|
|
redirectAddr = v
|
2016-03-09 23:59:44 +00:00
|
|
|
delete(m, "advertise_addr")
|
|
|
|
}
|
|
|
|
|
2016-08-15 13:42:42 +00:00
|
|
|
// Pull out the cluster address since it's common to all backends
|
|
|
|
var clusterAddr string
|
|
|
|
if v, ok := m["cluster_addr"]; ok {
|
|
|
|
clusterAddr = v
|
|
|
|
delete(m, "cluster_addr")
|
|
|
|
}
|
|
|
|
|
2016-10-02 18:54:01 +00:00
|
|
|
var disableClustering bool
|
2016-08-15 13:42:42 +00:00
|
|
|
var err error
|
|
|
|
if v, ok := m["disable_clustering"]; ok {
|
|
|
|
disableClustering, err = strconv.ParseBool(v)
|
|
|
|
if err != nil {
|
2017-03-08 14:17:00 +00:00
|
|
|
return multierror.Prefix(err, fmt.Sprintf("%s.%s:", name, key))
|
2016-08-15 13:42:42 +00:00
|
|
|
}
|
|
|
|
delete(m, "disable_clustering")
|
|
|
|
}
|
|
|
|
|
2017-11-08 19:19:41 +00:00
|
|
|
// Override with top-level values if they are set
|
|
|
|
if result.APIAddr != "" {
|
|
|
|
redirectAddr = result.APIAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.ClusterAddr != "" {
|
|
|
|
clusterAddr = result.ClusterAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.DisableClusteringRaw != nil {
|
|
|
|
disableClustering = result.DisableClustering
|
|
|
|
}
|
|
|
|
|
2017-03-08 14:17:00 +00:00
|
|
|
result.HAStorage = &Storage{
|
2016-08-15 13:42:42 +00:00
|
|
|
RedirectAddr: redirectAddr,
|
|
|
|
ClusterAddr: clusterAddr,
|
|
|
|
DisableClustering: disableClustering,
|
|
|
|
Type: strings.ToLower(key),
|
|
|
|
Config: m,
|
2016-03-09 23:59:44 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-06 14:46:39 +00:00
|
|
|
func parseServiceRegistration(result *Config, list *ast.ObjectList, name string) error {
|
|
|
|
if len(list.Items) > 1 {
|
|
|
|
return fmt.Errorf("only one %q block is permitted", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get our item
|
|
|
|
item := list.Items[0]
|
|
|
|
key := name
|
|
|
|
if len(item.Keys) > 0 {
|
|
|
|
key = item.Keys[0].Token.Value().(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
var m map[string]string
|
|
|
|
if err := hcl.DecodeObject(&m, item.Val); err != nil {
|
|
|
|
return multierror.Prefix(err, fmt.Sprintf("%s.%s:", name, key))
|
|
|
|
}
|
|
|
|
|
|
|
|
result.ServiceRegistration = &ServiceRegistration{
|
|
|
|
Type: strings.ToLower(key),
|
|
|
|
Config: m,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-08 17:57:15 +00:00
|
|
|
// 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:
|
|
|
|
// - Storage.Config
|
|
|
|
// - HAStorage.Config
|
|
|
|
// - Seals.Config
|
|
|
|
// - Telemetry.CirconusAPIToken
|
|
|
|
func (c *Config) Sanitized() map[string]interface{} {
|
2020-05-14 13:19:27 +00:00
|
|
|
// Create shared config if it doesn't exist (e.g. in tests) so that map
|
|
|
|
// keys are actually populated
|
|
|
|
if c.SharedConfig == nil {
|
|
|
|
c.SharedConfig = new(configutil.SharedConfig)
|
|
|
|
}
|
|
|
|
sharedResult := c.SharedConfig.Sanitized()
|
2019-10-08 17:57:15 +00:00
|
|
|
result := map[string]interface{}{
|
|
|
|
"cache_size": c.CacheSize,
|
2020-08-10 10:23:44 +00:00
|
|
|
"disable_sentinel_trace": c.DisableSentinelTrace,
|
2019-10-08 17:57:15 +00:00
|
|
|
"disable_cache": c.DisableCache,
|
|
|
|
"disable_printable_check": c.DisablePrintableCheck,
|
|
|
|
|
|
|
|
"enable_ui": c.EnableUI,
|
|
|
|
|
2022-02-23 22:08:52 +00:00
|
|
|
"max_lease_ttl": c.MaxLeaseTTL / time.Second,
|
|
|
|
"default_lease_ttl": c.DefaultLeaseTTL / time.Second,
|
2019-10-08 17:57:15 +00:00
|
|
|
|
|
|
|
"cluster_cipher_suites": c.ClusterCipherSuites,
|
|
|
|
|
|
|
|
"plugin_directory": c.PluginDirectory,
|
|
|
|
|
2022-04-04 16:45:41 +00:00
|
|
|
"plugin_file_uid": c.PluginFileUid,
|
|
|
|
|
|
|
|
"plugin_file_permissions": c.PluginFilePermissions,
|
|
|
|
|
2019-10-08 17:57:15 +00:00
|
|
|
"raw_storage_endpoint": c.EnableRawEndpoint,
|
|
|
|
|
2022-12-15 20:19:19 +00:00
|
|
|
"introspection_endpoint": c.EnableIntrospectionEndpoint,
|
|
|
|
|
2019-10-08 17:57:15 +00:00
|
|
|
"api_addr": c.APIAddr,
|
|
|
|
"cluster_addr": c.ClusterAddr,
|
|
|
|
"disable_clustering": c.DisableClustering,
|
|
|
|
|
|
|
|
"disable_performance_standby": c.DisablePerformanceStandby,
|
|
|
|
|
|
|
|
"disable_sealwrap": c.DisableSealWrap,
|
|
|
|
|
|
|
|
"disable_indexing": c.DisableIndexing,
|
2021-04-20 22:25:04 +00:00
|
|
|
|
|
|
|
"enable_response_header_hostname": c.EnableResponseHeaderHostname,
|
|
|
|
|
|
|
|
"enable_response_header_raft_node_id": c.EnableResponseHeaderRaftNodeID,
|
2021-12-08 22:34:42 +00:00
|
|
|
|
|
|
|
"log_requests_level": c.LogRequestsLevel,
|
2023-01-16 16:07:18 +00:00
|
|
|
"experiments": c.Experiments,
|
2023-01-11 19:32:05 +00:00
|
|
|
|
|
|
|
"detect_deadlocks": c.DetectDeadlocks,
|
2019-10-08 17:57:15 +00:00
|
|
|
}
|
2020-05-14 13:19:27 +00:00
|
|
|
for k, v := range sharedResult {
|
|
|
|
result[k] = v
|
2019-10-08 17:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sanitize storage stanza
|
|
|
|
if c.Storage != nil {
|
|
|
|
sanitizedStorage := map[string]interface{}{
|
|
|
|
"type": c.Storage.Type,
|
|
|
|
"redirect_addr": c.Storage.RedirectAddr,
|
|
|
|
"cluster_addr": c.Storage.ClusterAddr,
|
|
|
|
"disable_clustering": c.Storage.DisableClustering,
|
|
|
|
}
|
|
|
|
result["storage"] = sanitizedStorage
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanitize HA storage stanza
|
|
|
|
if c.HAStorage != nil {
|
|
|
|
sanitizedHAStorage := map[string]interface{}{
|
|
|
|
"type": c.HAStorage.Type,
|
|
|
|
"redirect_addr": c.HAStorage.RedirectAddr,
|
|
|
|
"cluster_addr": c.HAStorage.ClusterAddr,
|
|
|
|
"disable_clustering": c.HAStorage.DisableClustering,
|
|
|
|
}
|
|
|
|
result["ha_storage"] = sanitizedHAStorage
|
|
|
|
}
|
|
|
|
|
2019-12-06 14:46:39 +00:00
|
|
|
// Sanitize service_registration stanza
|
|
|
|
if c.ServiceRegistration != nil {
|
|
|
|
sanitizedServiceRegistration := map[string]interface{}{
|
|
|
|
"type": c.ServiceRegistration.Type,
|
|
|
|
}
|
|
|
|
result["service_registration"] = sanitizedServiceRegistration
|
|
|
|
}
|
|
|
|
|
2020-10-29 23:47:34 +00:00
|
|
|
entConfigResult := c.entConfig.Sanitized()
|
|
|
|
for k, v := range entConfigResult {
|
|
|
|
result[k] = v
|
|
|
|
}
|
|
|
|
|
2019-10-08 17:57:15 +00:00
|
|
|
return result
|
|
|
|
}
|
2021-05-04 19:47:56 +00:00
|
|
|
|
|
|
|
func (c *Config) Prune() {
|
|
|
|
for _, l := range c.Listeners {
|
|
|
|
l.RawConfig = nil
|
2021-06-17 18:09:37 +00:00
|
|
|
l.UnusedKeys = nil
|
2021-05-04 19:47:56 +00:00
|
|
|
}
|
|
|
|
c.FoundKeys = nil
|
|
|
|
c.UnusedKeys = nil
|
|
|
|
c.SharedConfig.FoundKeys = nil
|
|
|
|
c.SharedConfig.UnusedKeys = nil
|
|
|
|
if c.Telemetry != nil {
|
|
|
|
c.Telemetry.FoundKeys = nil
|
|
|
|
c.Telemetry.UnusedKeys = nil
|
|
|
|
}
|
|
|
|
}
|
2022-05-12 16:04:56 +00:00
|
|
|
|
|
|
|
func (c *Config) found(s, k string) {
|
|
|
|
delete(c.UnusedKeys, s)
|
|
|
|
c.FoundKeys = append(c.FoundKeys, k)
|
|
|
|
}
|