daemon/config: validate network-diagnostic-port

with this patch:

    dockerd --network-diagnostic-port -1 --validate
    unable to configure the Docker daemon with file /etc/docker/daemon.json: merged configuration validation from file and command line flags failed: invalid network-diagnostic-port (-1): value must be between 0 and 65535

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-01-19 19:15:15 +01:00
parent 370c7a30e2
commit 6b14bdb7c7
3 changed files with 22 additions and 2 deletions

View File

@@ -734,6 +734,9 @@ func Validate(config *Config) error {
if config.MaxDownloadAttempts < 0 {
return errors.Errorf("invalid max download attempts: %d", config.MaxDownloadAttempts)
}
if config.NetworkDiagnosticPort < 0 || config.NetworkDiagnosticPort > 65535 {
return errors.Errorf("invalid network-diagnostic-port (%d): value must be between 0 and 65535", config.NetworkDiagnosticPort)
}
if _, err := ParseGenericResources(config.NodeGenericResources); err != nil {
return err

View File

@@ -318,6 +318,24 @@ func TestValidateConfigurationErrors(t *testing.T) {
expectedErr: "invalid max download attempts: 0",
},
*/
{
name: "negative network-diagnostic-port",
config: &Config{
CommonConfig: CommonConfig{
NetworkDiagnosticPort: -1,
},
},
expectedErr: "invalid network-diagnostic-port (-1): value must be between 0 and 65535",
},
{
name: "network-diagnostic-port out of range",
config: &Config{
CommonConfig: CommonConfig{
NetworkDiagnosticPort: 65536,
},
},
expectedErr: "invalid network-diagnostic-port (65536): value must be between 0 and 65535",
},
{
name: "generic resource without =",
config: &Config{

View File

@@ -266,8 +266,7 @@ func (daemon *Daemon) reloadLiveRestore(txn *reloadTxn, newCfg *configStore, con
// reloadNetworkDiagnosticPort updates the network controller starting the diagnostic if the config is valid
func (daemon *Daemon) reloadNetworkDiagnosticPort(txn *reloadTxn, newCfg *configStore, conf *config.Config, attributes map[string]string) error {
txn.OnCommit(func() error {
if conf == nil || daemon.netController == nil || !conf.IsValueSet("network-diagnostic-port") ||
conf.NetworkDiagnosticPort < 1 || conf.NetworkDiagnosticPort > 65535 {
if conf == nil || daemon.netController == nil || !conf.IsValueSet("network-diagnostic-port") || conf.NetworkDiagnosticPort == 0 {
// If there is no config make sure that the diagnostic is off
if daemon.netController != nil {
daemon.netController.StopDiagnostic()