浏览代码

daemon/config: Validate(): validate hosts

The config.Validate() function did not validate hosts that were configured in
the daemon.json configuration file, resulting in `--validate` to pass, but the
daemon failing to start.

before this patch:

    echo '{"hosts":["127.0.0.1:2375/path"]}' > /etc/docker/daemon.json

    dockerd --validate
    configuration OK

    dockerd
    INFO[2022-04-03T11:42:22.162366200Z] Starting up
    failed to load listeners: error parsing -H 127.0.0.1:2375/path: invalid bind address (127.0.0.1:2375/path): should not contain a path element

with this patch:

    echo '{"hosts":["127.0.0.1:2375/path"]}' > /etc/docker/daemon.json

    dockerd --validate
    unable to configure the Docker daemon with file /etc/docker/daemon.json: configuration validation from file failed: invalid bind address (127.0.0.1:2375/path): should not contain a path element

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn 3 年之前
父节点
当前提交
a35b4ac54a
共有 2 个文件被更改,包括 23 次插入0 次删除
  1. 6 0
      daemon/config/config.go
  2. 17 0
      daemon/config/config_test.go

+ 6 - 0
daemon/config/config.go

@@ -601,6 +601,12 @@ func Validate(config *Config) error {
 		}
 	}
 
+	for _, h := range config.Hosts {
+		if _, err := opts.ValidateHost(h); err != nil {
+			return err
+		}
+	}
+
 	// validate platform-specific settings
 	return config.ValidatePlatformConfig()
 }

+ 17 - 0
daemon/config/config_test.go

@@ -336,6 +336,15 @@ func TestValidateConfigurationErrors(t *testing.T) {
 			},
 			expectedErr: "could not parse GenericResource: mixed discrete and named resources in expression 'foo=[bar 1]'",
 		},
+		{
+			name: "with invalid hosts",
+			config: &Config{
+				CommonConfig: CommonConfig{
+					Hosts: []string{"127.0.0.1:2375/path"},
+				},
+			},
+			expectedErr: "invalid bind address (127.0.0.1:2375/path): should not contain a path element",
+		},
 	}
 	for _, tc := range testCases {
 		t.Run(tc.name, func(t *testing.T) {
@@ -420,6 +429,14 @@ func TestValidateConfiguration(t *testing.T) {
 				},
 			},
 		},
+		{
+			name: "with hosts",
+			config: &Config{
+				CommonConfig: CommonConfig{
+					Hosts: []string{"tcp://127.0.0.1:2375"},
+				},
+			},
+		},
 	}
 	for _, tc := range testCases {
 		t.Run(tc.name, func(t *testing.T) {