mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Make sure flat options are not parsed as config structures.
Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
parent
7abc7b383c
commit
b6766e3063
2 changed files with 58 additions and 4 deletions
|
@ -21,6 +21,15 @@ const (
|
||||||
disableNetworkBridge = "none"
|
disableNetworkBridge = "none"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// flatOptions contains configuration keys
|
||||||
|
// that MUST NOT be parsed as deep structures.
|
||||||
|
// Use this to differentiate these options
|
||||||
|
// with others like the ones in CommonTLSOptions.
|
||||||
|
var flatOptions = map[string]bool{
|
||||||
|
"cluster-store-opts": true,
|
||||||
|
"log-opts": true,
|
||||||
|
}
|
||||||
|
|
||||||
// LogConfig represents the default log configuration.
|
// LogConfig represents the default log configuration.
|
||||||
// It includes json tags to deserialize configuration from a file
|
// It includes json tags to deserialize configuration from a file
|
||||||
// using the same names that the flags in the command line uses.
|
// using the same names that the flags in the command line uses.
|
||||||
|
@ -208,13 +217,14 @@ func getConflictFreeConfiguration(configFile string, flags *flag.FlagSet) (*Conf
|
||||||
func configValuesSet(config map[string]interface{}) map[string]interface{} {
|
func configValuesSet(config map[string]interface{}) map[string]interface{} {
|
||||||
flatten := make(map[string]interface{})
|
flatten := make(map[string]interface{})
|
||||||
for k, v := range config {
|
for k, v := range config {
|
||||||
if m, ok := v.(map[string]interface{}); ok {
|
if m, isMap := v.(map[string]interface{}); isMap && !flatOptions[k] {
|
||||||
for km, vm := range m {
|
for km, vm := range m {
|
||||||
flatten[km] = vm
|
flatten[km] = vm
|
||||||
}
|
}
|
||||||
} else {
|
continue
|
||||||
flatten[k] = v
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
flatten[k] = v
|
||||||
}
|
}
|
||||||
return flatten
|
return flatten
|
||||||
}
|
}
|
||||||
|
|
|
@ -238,7 +238,7 @@ func TestLoadDaemonConfigWithEmbeddedOptions(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if loadedConfig == nil {
|
if loadedConfig == nil {
|
||||||
t.Fatalf("expected configuration %v, got nil", c)
|
t.Fatal("expected configuration, got nil")
|
||||||
}
|
}
|
||||||
if loadedConfig.CommonTLSOptions.CAFile != "/etc/certs/ca.pem" {
|
if loadedConfig.CommonTLSOptions.CAFile != "/etc/certs/ca.pem" {
|
||||||
t.Fatalf("expected CA file path /etc/certs/ca.pem, got %v", loadedConfig.CommonTLSOptions.CAFile)
|
t.Fatalf("expected CA file path /etc/certs/ca.pem, got %v", loadedConfig.CommonTLSOptions.CAFile)
|
||||||
|
@ -247,3 +247,47 @@ func TestLoadDaemonConfigWithEmbeddedOptions(t *testing.T) {
|
||||||
t.Fatalf("expected LogConfig type syslog, got %v", loadedConfig.LogConfig.Type)
|
t.Fatalf("expected LogConfig type syslog, got %v", loadedConfig.LogConfig.Type)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLoadDaemonConfigWithMapOptions(t *testing.T) {
|
||||||
|
c := &daemon.Config{}
|
||||||
|
common := &cli.CommonFlags{}
|
||||||
|
flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
|
||||||
|
|
||||||
|
flags.Var(opts.NewNamedMapOpts("cluster-store-opts", c.ClusterOpts, nil), []string{"-cluster-store-opt"}, "")
|
||||||
|
flags.Var(opts.NewNamedMapOpts("log-opts", c.LogConfig.Config, nil), []string{"-log-opt"}, "")
|
||||||
|
|
||||||
|
f, err := ioutil.TempFile("", "docker-config-")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
configFile := f.Name()
|
||||||
|
f.Write([]byte(`{
|
||||||
|
"cluster-store-opts": {"kv.cacertfile": "/var/lib/docker/discovery_certs/ca.pem"},
|
||||||
|
"log-opts": {"tag": "test"}
|
||||||
|
}`))
|
||||||
|
f.Close()
|
||||||
|
|
||||||
|
loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if loadedConfig == nil {
|
||||||
|
t.Fatal("expected configuration, got nil")
|
||||||
|
}
|
||||||
|
if loadedConfig.ClusterOpts == nil {
|
||||||
|
t.Fatal("expected cluster options, got nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedPath := "/var/lib/docker/discovery_certs/ca.pem"
|
||||||
|
if caPath := loadedConfig.ClusterOpts["kv.cacertfile"]; caPath != expectedPath {
|
||||||
|
t.Fatalf("expected %s, got %s", expectedPath, caPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
if loadedConfig.LogConfig.Config == nil {
|
||||||
|
t.Fatal("expected log config options, got nil")
|
||||||
|
}
|
||||||
|
if tag := loadedConfig.LogConfig.Config["tag"]; tag != "test" {
|
||||||
|
t.Fatalf("expected log tag `test`, got %s", tag)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue