Explorar o código

daemon/config: Validate(): validate log-level

Log-level validation was previously performed when configuring the daemon-logs;
this moves the validation to config.Validate() so that we can catch invalid
settings when running dockerd --validate.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn %!s(int64=3) %!d(string=hai) anos
pai
achega
390c7d6871
Modificáronse 4 ficheiros con 34 adicións e 18 borrados
  1. 6 10
      cmd/dockerd/daemon.go
  2. 4 8
      cmd/dockerd/daemon_test.go
  3. 7 0
      daemon/config/config.go
  4. 17 0
      daemon/config/config_test.go

+ 6 - 10
cmd/dockerd/daemon.go

@@ -91,10 +91,7 @@ func (cli *DaemonCli) start(opts *daemonOptions) (err error) {
 	}
 	}
 
 
 	configureProxyEnv(cli.Config)
 	configureProxyEnv(cli.Config)
-
-	if err := configureDaemonLogs(cli.Config); err != nil {
-		return err
-	}
+	configureDaemonLogs(cli.Config)
 
 
 	logrus.Info("Starting up")
 	logrus.Info("Starting up")
 
 
@@ -764,14 +761,14 @@ func systemContainerdRunning(honorXDG bool) (string, bool, error) {
 	return addr, err == nil, nil
 	return addr, err == nil, nil
 }
 }
 
 
-// configureDaemonLogs sets the logrus logging level and formatting
-func configureDaemonLogs(conf *config.Config) error {
+// configureDaemonLogs sets the logrus logging level and formatting. It expects
+// the passed configuration to already be validated, and ignores invalid options.
+func configureDaemonLogs(conf *config.Config) {
 	if conf.LogLevel != "" {
 	if conf.LogLevel != "" {
 		lvl, err := logrus.ParseLevel(conf.LogLevel)
 		lvl, err := logrus.ParseLevel(conf.LogLevel)
-		if err != nil {
-			return fmt.Errorf("unable to parse logging level: %s", conf.LogLevel)
+		if err == nil {
+			logrus.SetLevel(lvl)
 		}
 		}
-		logrus.SetLevel(lvl)
 	} else {
 	} else {
 		logrus.SetLevel(logrus.InfoLevel)
 		logrus.SetLevel(logrus.InfoLevel)
 	}
 	}
@@ -780,7 +777,6 @@ func configureDaemonLogs(conf *config.Config) error {
 		DisableColors:   conf.RawLogs,
 		DisableColors:   conf.RawLogs,
 		FullTimestamp:   true,
 		FullTimestamp:   true,
 	})
 	})
-	return nil
 }
 }
 
 
 func configureProxyEnv(conf *config.Config) {
 func configureProxyEnv(conf *config.Config) {

+ 4 - 8
cmd/dockerd/daemon_test.go

@@ -186,19 +186,15 @@ func TestLoadDaemonConfigWithRegistryOptions(t *testing.T) {
 
 
 func TestConfigureDaemonLogs(t *testing.T) {
 func TestConfigureDaemonLogs(t *testing.T) {
 	conf := &config.Config{}
 	conf := &config.Config{}
-	err := configureDaemonLogs(conf)
-	assert.NilError(t, err)
+	configureDaemonLogs(conf)
 	assert.Check(t, is.Equal(logrus.InfoLevel, logrus.GetLevel()))
 	assert.Check(t, is.Equal(logrus.InfoLevel, logrus.GetLevel()))
 
 
 	conf.LogLevel = "warn"
 	conf.LogLevel = "warn"
-	err = configureDaemonLogs(conf)
-	assert.NilError(t, err)
+	configureDaemonLogs(conf)
 	assert.Check(t, is.Equal(logrus.WarnLevel, logrus.GetLevel()))
 	assert.Check(t, is.Equal(logrus.WarnLevel, logrus.GetLevel()))
 
 
+	// log level should not be changed when passing an invalid value
 	conf.LogLevel = "foobar"
 	conf.LogLevel = "foobar"
-	err = configureDaemonLogs(conf)
-	assert.Error(t, err, "unable to parse logging level: foobar")
-
-	// log level should not be changed after a failure
+	configureDaemonLogs(conf)
 	assert.Check(t, is.Equal(logrus.WarnLevel, logrus.GetLevel()))
 	assert.Check(t, is.Equal(logrus.WarnLevel, logrus.GetLevel()))
 }
 }

+ 7 - 0
daemon/config/config.go

@@ -558,6 +558,13 @@ func findConfigurationConflicts(config map[string]interface{}, flags *pflag.Flag
 // such as config.DNS, config.Labels, config.DNSSearch,
 // such as config.DNS, config.Labels, config.DNSSearch,
 // as well as config.MaxConcurrentDownloads, config.MaxConcurrentUploads and config.MaxDownloadAttempts.
 // as well as config.MaxConcurrentDownloads, config.MaxConcurrentUploads and config.MaxDownloadAttempts.
 func Validate(config *Config) error {
 func Validate(config *Config) error {
+	// validate log-level
+	if config.LogLevel != "" {
+		if _, err := logrus.ParseLevel(config.LogLevel); err != nil {
+			return fmt.Errorf("invalid logging level: %s", config.LogLevel)
+		}
+	}
+
 	// validate DNS
 	// validate DNS
 	for _, dns := range config.DNS {
 	for _, dns := range config.DNS {
 		if _, err := opts.ValidateIPAddress(dns); err != nil {
 		if _, err := opts.ValidateIPAddress(dns); err != nil {

+ 17 - 0
daemon/config/config_test.go

@@ -345,6 +345,15 @@ func TestValidateConfigurationErrors(t *testing.T) {
 			},
 			},
 			expectedErr: "invalid bind address (127.0.0.1:2375/path): should not contain a path element",
 			expectedErr: "invalid bind address (127.0.0.1:2375/path): should not contain a path element",
 		},
 		},
+		{
+			name: "with invalid log-level",
+			config: &Config{
+				CommonConfig: CommonConfig{
+					LogLevel: "foobar",
+				},
+			},
+			expectedErr: "invalid logging level: foobar",
+		},
 	}
 	}
 	for _, tc := range testCases {
 	for _, tc := range testCases {
 		t.Run(tc.name, func(t *testing.T) {
 		t.Run(tc.name, func(t *testing.T) {
@@ -437,6 +446,14 @@ func TestValidateConfiguration(t *testing.T) {
 				},
 				},
 			},
 			},
 		},
 		},
+		{
+			name: "with log-level warn",
+			config: &Config{
+				CommonConfig: CommonConfig{
+					LogLevel: "warn",
+				},
+			},
+		},
 	}
 	}
 	for _, tc := range testCases {
 	for _, tc := range testCases {
 		t.Run(tc.name, func(t *testing.T) {
 		t.Run(tc.name, func(t *testing.T) {