Skip to content

Commit

Permalink
return sanitized config as a map
Browse files Browse the repository at this point in the history
  • Loading branch information
calvn committed Sep 20, 2019
1 parent 543f765 commit 26fdcb5
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 141 deletions.
174 changes: 91 additions & 83 deletions command/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -915,110 +915,118 @@ func parseTelemetry(result *Config, list *ast.ObjectList) error {
// - HAStorage.Config
// - Seals.Config
// - Telemetry.CirconusAPIToken
func (c *Config) Sanitized() *Config {
func (c *Config) Sanitized() map[string]interface{} {
result := map[string]interface{}{
"CacheSize": c.CacheSize,
"DisableCache": c.DisableCache,
"DisableMlock": c.DisableMlock,
"DisablePrintableCheck": c.DisablePrintableCheck,

"EnableUI": c.EnableUI,

"MaxLeaseTTL": c.MaxLeaseTTL,
"DefaultLeaseTTL": c.DefaultLeaseTTL,

"DefaultMaxRequestDuration": c.DefaultMaxRequestDuration,

"ClusterName": c.ClusterName,
"ClusterCipherSuites": c.ClusterCipherSuites,

"PluginDirectory": c.PluginDirectory,

"LogLevel": c.LogLevel,
"LogFormat": c.LogFormat,

"PidFile": c.PidFile,
"EnableRawEndpoint": c.EnableRawEndpoint,

"APIAddr": c.APIAddr,
"ClusterAddr": c.ClusterAddr,
"DisableClustering": c.DisableClustering,

"DisablePerformanceStandby": c.DisablePerformanceStandby,

"DisableSealWrap": c.DisableSealWrap,

"DisableIndexing": c.DisableIndexing,
}

// Sanitize listeners
if len(c.Listeners) != 0 {
var sanitizedListeners []interface{}
for _, ln := range c.Listeners {
cleanLn := map[string]interface{}{
"Type": ln.Type,
"Config": ln.Config,
}
sanitizedListeners = append(sanitizedListeners, cleanLn)
}
result["Listeners"] = sanitizedListeners
}

// Sanitize storage stanza
var sanitizedStorage *Storage
if c.Storage != nil {
sanitizedStorage = &Storage{
Type: c.Storage.Type,
RedirectAddr: c.Storage.RedirectAddr,
ClusterAddr: c.Storage.ClusterAddr,
DisableClustering: c.Storage.DisableClustering,
sanitizedStorage := map[string]interface{}{
"Type": c.Storage.Type,
"RedirectAddr": c.Storage.RedirectAddr,
"ClusterAddr": c.Storage.ClusterAddr,
"DisableClustering": c.Storage.DisableClustering,
}
result["Storage"] = sanitizedStorage
}

// Sanitize HA storage stanza
var sanitizedHAStorage *Storage
if c.HAStorage != nil {
sanitizedHAStorage = &Storage{
Type: c.HAStorage.Type,
RedirectAddr: c.HAStorage.RedirectAddr,
ClusterAddr: c.HAStorage.ClusterAddr,
DisableClustering: c.HAStorage.DisableClustering,
sanitizedHAStorage := map[string]interface{}{
"Type": c.HAStorage.Type,
"RedirectAddr": c.HAStorage.RedirectAddr,
"ClusterAddr": c.HAStorage.ClusterAddr,
"DisableClustering": c.HAStorage.DisableClustering,
}
result["HAStorage"] = sanitizedHAStorage
}

// Sanitize seals stanza
var sanitizedSeals []*Seal
if len(c.Seals) != 0 {
var sanitizedSeals []interface{}
for _, s := range c.Seals {
cleanSeal := &Seal{
Type: s.Type,
Disabled: s.Disabled,
cleanSeal := map[string]interface{}{
"Type": s.Type,
"Disabled": s.Disabled,
}
sanitizedSeals = append(sanitizedSeals, cleanSeal)
}
result["Seals"] = sanitizedSeals
}

// Sanitize telemetry stanza
var sanitizedTelemetry *Telemetry
if c.Telemetry != nil {
sanitizedTelemetry = &Telemetry{
StatsiteAddr: c.Telemetry.StatsiteAddr,
StatsdAddr: c.Telemetry.StatsdAddr,
DisableHostname: c.Telemetry.DisableHostname,
CirconusAPIToken: "",
CirconusAPIApp: c.Telemetry.CirconusAPIApp,
CirconusAPIURL: c.Telemetry.CirconusAPIURL,
CirconusSubmissionInterval: c.Telemetry.CirconusSubmissionInterval,
CirconusCheckSubmissionURL: c.Telemetry.CirconusCheckSubmissionURL,
CirconusCheckID: c.Telemetry.CirconusCheckID,
CirconusCheckForceMetricActivation: c.Telemetry.CirconusCheckForceMetricActivation,
CirconusCheckInstanceID: c.Telemetry.CirconusCheckInstanceID,
CirconusCheckSearchTag: c.Telemetry.CirconusCheckSearchTag,
CirconusCheckTags: c.Telemetry.CirconusCheckTags,
CirconusCheckDisplayName: c.Telemetry.CirconusCheckDisplayName,
CirconusBrokerID: c.Telemetry.CirconusBrokerID,
CirconusBrokerSelectTag: c.Telemetry.CirconusBrokerSelectTag,
DogStatsDAddr: c.Telemetry.DogStatsDAddr,
DogStatsDTags: c.Telemetry.DogStatsDTags,
PrometheusRetentionTime: c.Telemetry.PrometheusRetentionTime,
PrometheusRetentionTimeRaw: c.Telemetry.PrometheusRetentionTimeRaw,
StackdriverProjectID: c.Telemetry.StackdriverProjectID,
StackdriverLocation: c.Telemetry.StackdriverLocation,
StackdriverNamespace: c.Telemetry.StackdriverNamespace,
sanitizedTelemetry := map[string]interface{}{
"StatsiteAddr": c.Telemetry.StatsiteAddr,
"StatsdAddr": c.Telemetry.StatsdAddr,
"DisableHostname": c.Telemetry.DisableHostname,
"CirconusAPIToken": "",
"CirconusAPIApp": c.Telemetry.CirconusAPIApp,
"CirconusAPIURL": c.Telemetry.CirconusAPIURL,
"CirconusSubmissionInterval": c.Telemetry.CirconusSubmissionInterval,
"CirconusCheckSubmissionURL": c.Telemetry.CirconusCheckSubmissionURL,
"CirconusCheckID": c.Telemetry.CirconusCheckID,
"CirconusCheckForceMetricActivation": c.Telemetry.CirconusCheckForceMetricActivation,
"CirconusCheckInstanceID": c.Telemetry.CirconusCheckInstanceID,
"CirconusCheckSearchTag": c.Telemetry.CirconusCheckSearchTag,
"CirconusCheckTags": c.Telemetry.CirconusCheckTags,
"CirconusCheckDisplayName": c.Telemetry.CirconusCheckDisplayName,
"CirconusBrokerID": c.Telemetry.CirconusBrokerID,
"CirconusBrokerSelectTag": c.Telemetry.CirconusBrokerSelectTag,
"DogStatsDAddr": c.Telemetry.DogStatsDAddr,
"DogStatsDTags": c.Telemetry.DogStatsDTags,
"PrometheusRetentionTime": c.Telemetry.PrometheusRetentionTime,
"StackdriverProjectID": c.Telemetry.StackdriverProjectID,
"StackdriverLocation": c.Telemetry.StackdriverLocation,
"StackdriverNamespace": c.Telemetry.StackdriverNamespace,
}
result["Telemetry"] = sanitizedTelemetry
}

return &Config{
Listeners: c.Listeners,
Storage: sanitizedStorage,
HAStorage: sanitizedHAStorage,
Seals: sanitizedSeals,

CacheSize: c.CacheSize,
DisableCache: c.DisableCache,
DisableMlock: c.DisableMlock,
DisablePrintableCheck: c.DisablePrintableCheck,

EnableUI: c.EnableUI,

Telemetry: sanitizedTelemetry,

MaxLeaseTTL: c.MaxLeaseTTL,
DefaultLeaseTTL: c.DefaultLeaseTTL,

DefaultMaxRequestDuration: c.DefaultMaxRequestDuration,

ClusterName: c.ClusterName,
ClusterCipherSuites: c.ClusterCipherSuites,

PluginDirectory: c.PluginDirectory,

LogLevel: c.LogLevel,
LogFormat: c.LogFormat,

PidFile: c.PidFile,
EnableRawEndpoint: c.EnableRawEndpoint,

APIAddr: c.APIAddr,
ClusterAddr: c.ClusterAddr,
DisableClustering: c.DisableClustering,

DisablePerformanceStandby: c.DisablePerformanceStandby,

DisableSealWrap: c.DisableSealWrap,

DisableIndexing: c.DisableIndexing,
}
return result
}
109 changes: 64 additions & 45 deletions command/server/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,57 +355,76 @@ func TestConfig_Sanitized(t *testing.T) {
t.Fatalf("err: %s", err)
}

expected := &Config{
Listeners: []*Listener{
&Listener{
Type: "tcp",
Config: map[string]interface{}{
expected := map[string]interface{}{
"APIAddr": "top_level_api_addr",
"CacheSize": 0,
"ClusterAddr": "top_level_cluster_addr",
"ClusterCipherSuites": "",
"ClusterName": "testcluster",
"DefaultLeaseTTL": 36000000000000,
"DefaultMaxRequestDuration": 0,
"DisableCache": true,
"DisableClustering": false,
"DisableIndexing": false,
"DisableMlock": true,
"DisablePerformanceStandby": false,
"DisablePrintableCheck": false,
"DisableSealWrap": true,
"EnableRawEndpoint": true,
"EnableUI": true,
"HAStorage": map[string]interface{}{
"ClusterAddr": "top_level_cluster_addr",
"DisableClustering": true,
"RedirectAddr": "top_level_api_addr",
"Type": "consul"},
"Listeners": []interface{}{
map[string]interface{}{
"Config": map[string]interface{}{
"address": "127.0.0.1:443",
},
"Type": "tcp",
},
},

Storage: &Storage{
Type: "consul",
RedirectAddr: "top_level_api_addr",
ClusterAddr: "top_level_cluster_addr",
},

HAStorage: &Storage{
Type: "consul",
RedirectAddr: "top_level_api_addr",
ClusterAddr: "top_level_cluster_addr",
DisableClustering: true,
},

Telemetry: &Telemetry{
StatsdAddr: "bar",
PrometheusRetentionTime: prometheusDefaultRetentionTime,
},

Seals: []*Seal{
{
Type: "awskms",
Disabled: false,
"LogFormat": "",
"LogLevel": "",
"MaxLeaseTTL": 36000000000000,
"PidFile": "./pidfile",
"PluginDirectory": "",
"Seals": []interface{}{
map[string]interface{}{
"Disabled": false,
"Type": "awskms",
},
},

DisableCache: true,
DisableMlock: true,
EnableUI: true,

EnableRawEndpoint: true,

DisableSealWrap: true,

MaxLeaseTTL: 10 * time.Hour,
DefaultLeaseTTL: 10 * time.Hour,
ClusterName: "testcluster",

PidFile: "./pidfile",

APIAddr: "top_level_api_addr",
ClusterAddr: "top_level_cluster_addr",
"Storage": map[string]interface{}{
"ClusterAddr": "top_level_cluster_addr",
"DisableClustering": false,
"RedirectAddr": "top_level_api_addr",
"Type": "consul",
},
"Telemetry": map[string]interface{}{
"CirconusAPIApp": "",
"CirconusAPIToken": "",
"CirconusAPIURL": "",
"CirconusBrokerID": "",
"CirconusBrokerSelectTag": "",
"CirconusCheckDisplayName": "",
"CirconusCheckForceMetricActivation": "",
"CirconusCheckID": "",
"CirconusCheckInstanceID": "",
"CirconusCheckSearchTag": "",
"CirconusCheckSubmissionURL": "",
"CirconusCheckTags": "",
"CirconusSubmissionInterval": "",
"DisableHostname": false,
"DogStatsDAddr": "",
"DogStatsDTags": []string(nil),
"PrometheusRetentionTime": 86400000000000,
"StackdriverLocation": "",
"StackdriverNamespace": "",
"StackdriverProjectID": "",
"StatsdAddr": "bar",
"StatsiteAddr": ""},
}

sanitizedConfig := config.Sanitized()
Expand Down
2 changes: 1 addition & 1 deletion vault/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -1982,7 +1982,7 @@ func (c *Core) SetConfig(conf *server.Config) {
c.stateLock.Unlock()
}

func (c *Core) SanitizedConfig() *server.Config {
func (c *Core) SanitizedConfig() map[string]interface{} {
return c.rawConfig.Sanitized()
}

Expand Down
13 changes: 1 addition & 12 deletions vault/logical_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"sync"
"time"

"github.com/fatih/structs"
"github.com/hashicorp/vault/physical/raft"

"github.com/hashicorp/errwrap"
Expand Down Expand Up @@ -231,18 +230,8 @@ type SystemBackend struct {
// file(s) provided.
func (b *SystemBackend) handleConfigStateSanitized(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
config := b.Core.SanitizedConfig()

configMap := structs.New(config).Map()

// Remove the raw values from the map
for k := range configMap {
if strings.HasSuffix(k, "Raw") {
delete(configMap, k)
}
}

resp := &logical.Response{
Data: configMap,
Data: config,
}
return resp, nil
}
Expand Down

0 comments on commit 26fdcb5

Please sign in to comment.