From ee6c3580c95014b404ec9400fdc0575f27e91b80 Mon Sep 17 00:00:00 2001 From: John Howard Date: Sat, 31 Oct 2015 18:49:14 -0700 Subject: [PATCH] Move validation functions Signed-off-by: John Howard --- runconfig/hostconfig_unix.go | 68 ++++++++++++++++++++++++++++++ runconfig/hostconfig_windows.go | 36 +++++++++++++++- runconfig/parse_unix.go | 74 --------------------------------- runconfig/parse_windows.go | 37 ----------------- 4 files changed, 103 insertions(+), 112 deletions(-) delete mode 100644 runconfig/parse_unix.go delete mode 100644 runconfig/parse_windows.go diff --git a/runconfig/hostconfig_unix.go b/runconfig/hostconfig_unix.go index ed2bcf8e3e..7aad5a33f0 100644 --- a/runconfig/hostconfig_unix.go +++ b/runconfig/hostconfig_unix.go @@ -3,6 +3,8 @@ package runconfig import ( + "fmt" + "runtime" "strings" ) @@ -103,3 +105,69 @@ func MergeConfigs(config *Config, hostConfig *HostConfig) *ContainerConfigWrappe "", nil, } } + +// ValidateNetMode ensures that the various combinations of requested +// network settings are valid. +func ValidateNetMode(c *Config, hc *HostConfig) error { + // We may not be passed a host config, such as in the case of docker commit + if hc == nil { + return nil + } + parts := strings.Split(string(hc.NetworkMode), ":") + if parts[0] == "container" { + if len(parts) < 2 || parts[1] == "" { + return fmt.Errorf("--net: invalid net mode: invalid container format container:") + } + } + + if (hc.NetworkMode.IsHost() || hc.NetworkMode.IsContainer()) && c.Hostname != "" { + return ErrConflictNetworkHostname + } + + if hc.NetworkMode.IsHost() && len(hc.Links) > 0 { + return ErrConflictHostNetworkAndLinks + } + + if hc.NetworkMode.IsContainer() && len(hc.Links) > 0 { + return ErrConflictContainerNetworkAndLinks + } + + if hc.NetworkMode.IsUserDefined() && len(hc.Links) > 0 { + return ErrConflictUserDefinedNetworkAndLinks + } + + if (hc.NetworkMode.IsHost() || hc.NetworkMode.IsContainer()) && len(hc.DNS) > 0 { + return ErrConflictNetworkAndDNS + } + + if (hc.NetworkMode.IsContainer() || hc.NetworkMode.IsHost()) && len(hc.ExtraHosts) > 0 { + return ErrConflictNetworkHosts + } + + if (hc.NetworkMode.IsContainer() || hc.NetworkMode.IsHost()) && c.MacAddress != "" { + return ErrConflictContainerNetworkAndMac + } + + if hc.NetworkMode.IsContainer() && (len(hc.PortBindings) > 0 || hc.PublishAllPorts == true) { + return ErrConflictNetworkPublishPorts + } + + if hc.NetworkMode.IsContainer() && len(c.ExposedPorts) > 0 { + return ErrConflictNetworkExposePorts + } + return nil +} + +// ValidateIsolationLevel performs platform specific validation of the +// isolation level in the hostconfig structure. Linux only supports "default" +// which is LXC container isolation +func ValidateIsolationLevel(hc *HostConfig) error { + // We may not be passed a host config, such as in the case of docker commit + if hc == nil { + return nil + } + if !hc.Isolation.IsValid() { + return fmt.Errorf("invalid --isolation: %q - %s only supports 'default'", hc.Isolation, runtime.GOOS) + } + return nil +} diff --git a/runconfig/hostconfig_windows.go b/runconfig/hostconfig_windows.go index 0943800b0b..dbdb1683a0 100644 --- a/runconfig/hostconfig_windows.go +++ b/runconfig/hostconfig_windows.go @@ -1,6 +1,9 @@ package runconfig -import "strings" +import ( + "fmt" + "strings" +) // IsDefault indicates whether container uses the default network stack. func (n NetworkMode) IsDefault() bool { @@ -45,3 +48,34 @@ func MergeConfigs(config *Config, hostConfig *HostConfig) *ContainerConfigWrappe func IsPreDefinedNetwork(network string) bool { return false } + +// ValidateNetMode ensures that the various combinations of requested +// network settings are valid. +func ValidateNetMode(c *Config, hc *HostConfig) error { + // We may not be passed a host config, such as in the case of docker commit + if hc == nil { + return nil + } + parts := strings.Split(string(hc.NetworkMode), ":") + switch mode := parts[0]; mode { + case "default", "none": + default: + return fmt.Errorf("invalid --net: %s", hc.NetworkMode) + } + return nil +} + +// ValidateIsolationLevel performs platform specific validation of the +// isolation level in the hostconfig structure. Windows supports 'default' (or +// blank), and 'hyperv'. These refer to Windows Server Containers and +// Hyper-V Containers respectively. +func ValidateIsolationLevel(hc *HostConfig) error { + // We may not be passed a host config, such as in the case of docker commit + if hc == nil { + return nil + } + if !hc.Isolation.IsValid() { + return fmt.Errorf("invalid --isolation: %q. Windows supports 'default' (Windows Server Container) or 'hyperv' (Hyper-V Container)", hc.Isolation) + } + return nil +} diff --git a/runconfig/parse_unix.go b/runconfig/parse_unix.go deleted file mode 100644 index e4eb827ff9..0000000000 --- a/runconfig/parse_unix.go +++ /dev/null @@ -1,74 +0,0 @@ -// +build !windows - -package runconfig - -import ( - "fmt" - "runtime" - "strings" -) - -// ValidateNetMode ensures that the various combinations of requested -// network settings are valid. -func ValidateNetMode(c *Config, hc *HostConfig) error { - // We may not be passed a host config, such as in the case of docker commit - if hc == nil { - return nil - } - parts := strings.Split(string(hc.NetworkMode), ":") - if parts[0] == "container" { - if len(parts) < 2 || parts[1] == "" { - return fmt.Errorf("--net: invalid net mode: invalid container format container:") - } - } - - if (hc.NetworkMode.IsHost() || hc.NetworkMode.IsContainer()) && c.Hostname != "" { - return ErrConflictNetworkHostname - } - - if hc.NetworkMode.IsHost() && len(hc.Links) > 0 { - return ErrConflictHostNetworkAndLinks - } - - if hc.NetworkMode.IsContainer() && len(hc.Links) > 0 { - return ErrConflictContainerNetworkAndLinks - } - - if hc.NetworkMode.IsUserDefined() && len(hc.Links) > 0 { - return ErrConflictUserDefinedNetworkAndLinks - } - - if (hc.NetworkMode.IsHost() || hc.NetworkMode.IsContainer()) && len(hc.DNS) > 0 { - return ErrConflictNetworkAndDNS - } - - if (hc.NetworkMode.IsContainer() || hc.NetworkMode.IsHost()) && len(hc.ExtraHosts) > 0 { - return ErrConflictNetworkHosts - } - - if (hc.NetworkMode.IsContainer() || hc.NetworkMode.IsHost()) && c.MacAddress != "" { - return ErrConflictContainerNetworkAndMac - } - - if hc.NetworkMode.IsContainer() && (len(hc.PortBindings) > 0 || hc.PublishAllPorts == true) { - return ErrConflictNetworkPublishPorts - } - - if hc.NetworkMode.IsContainer() && len(c.ExposedPorts) > 0 { - return ErrConflictNetworkExposePorts - } - return nil -} - -// ValidateIsolationLevel performs platform specific validation of the -// isolation level in the hostconfig structure. Linux only supports "default". -func ValidateIsolationLevel(hc *HostConfig) error { - // We may not be passed a host config, such as in the case of docker commit - if hc == nil { - return nil - } - if !hc.Isolation.IsValid() { - return fmt.Errorf("invalid --isolation: %q - %s only supports 'default'", hc.Isolation, runtime.GOOS) - } - return nil -} diff --git a/runconfig/parse_windows.go b/runconfig/parse_windows.go deleted file mode 100644 index d9c010c7d3..0000000000 --- a/runconfig/parse_windows.go +++ /dev/null @@ -1,37 +0,0 @@ -package runconfig - -import ( - "fmt" - "strings" -) - -// ValidateNetMode ensures that the various combinations of requested -// network settings are valid. -func ValidateNetMode(c *Config, hc *HostConfig) error { - // We may not be passed a host config, such as in the case of docker commit - if hc == nil { - return nil - } - parts := strings.Split(string(hc.NetworkMode), ":") - switch mode := parts[0]; mode { - case "default", "none": - default: - return fmt.Errorf("invalid --net: %s", hc.NetworkMode) - } - return nil -} - -// ValidateIsolationLevel performs platform specific validation of the -// isolation level in the hostconfig structure. Windows supports 'default' (or -// blank), and 'hyperv'. These refer to Windows Server Containers and -// Hyper-V Containers respectively. -func ValidateIsolationLevel(hc *HostConfig) error { - // We may not be passed a host config, such as in the case of docker commit - if hc == nil { - return nil - } - if !hc.Isolation.IsValid() { - return fmt.Errorf("invalid --isolation: %q. Windows supports 'default' (Windows Server Container) or 'hyperv' (Hyper-V Container)", hc.Isolation) - } - return nil -}