Skip to content

Commit

Permalink
Add default (empty) HostConfig if it is nil when validating Container…
Browse files Browse the repository at this point in the history
…CreateConfig. This is needed when the client request is sent via docker rest api call without specifying HostConfig in the form data. See examples in vmware#2557. This fixes vmware#2557 and vmware#3709.
  • Loading branch information
chengwang86 committed Feb 3, 2017
1 parent 5f37ad5 commit 86a8ab9
Showing 1 changed file with 32 additions and 29 deletions.
61 changes: 32 additions & 29 deletions lib/apiservers/engine/backends/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,10 @@ func setPathFromImageConfig(config, imageConfig *containertypes.Config) {
func validateCreateConfig(config *types.ContainerCreateConfig) error {
defer trace.End(trace.Begin("Container.validateCreateConfig"))

if config.HostConfig == nil {
config.HostConfig = &containertypes.HostConfig{}
}

// process cpucount here
var cpuCount int64 = DefaultCPUs

Expand Down Expand Up @@ -1388,9 +1392,6 @@ func validateCreateConfig(config *types.ContainerCreateConfig) error {
config.HostConfig.Memory = memoryMB
log.Infof("Container memory: %d MB", config.HostConfig.Memory)

if config.HostConfig == nil || config.Config == nil {
return BadRequestError("invalid config")
}

if config.NetworkingConfig == nil {
config.NetworkingConfig = &dnetwork.NetworkingConfig{}
Expand All @@ -1405,41 +1406,43 @@ func validateCreateConfig(config *types.ContainerCreateConfig) error {
}

// validate port bindings
if config.HostConfig != nil {
var ips []string
if addrs, err := publicIPv4Addrs(); err != nil {
log.Warnf("could not get address for public interface: %s", err)
} else {
ips = make([]string, len(addrs))
for i := range addrs {
ips[i] = addrs[i].IP.String()
}
var ips []string
if addrs, err := publicIPv4Addrs(); err != nil {
log.Warnf("could not get address for public interface: %s", err)
} else {
ips = make([]string, len(addrs))
for i := range addrs {
ips[i] = addrs[i].IP.String()
}
}

for _, pbs := range config.HostConfig.PortBindings {
for _, pb := range pbs {
if pb.HostIP != "" && pb.HostIP != "0.0.0.0" {
// check if specified host ip equals any of the addresses on the "client" interface
found := false
for _, i := range ips {
if i == pb.HostIP {
found = true
break
}
}
if !found {
return InternalServerError("host IP for port bindings is only supported for 0.0.0.0 and the public interface IP address")
for _, pbs := range config.HostConfig.PortBindings {
for _, pb := range pbs {
if pb.HostIP != "" && pb.HostIP != "0.0.0.0" {
// check if specified host ip equals any of the addresses on the "client" interface
found := false
for _, i := range ips {
if i == pb.HostIP {
found = true
break
}
}

start, end, _ := nat.ParsePortRangeToInt(pb.HostPort)
if start != end {
return InternalServerError("host port ranges are not supported for port bindings")
if !found {
return InternalServerError("host IP for port bindings is only supported for 0.0.0.0 and the public interface IP address")
}
}

start, end, _ := nat.ParsePortRangeToInt(pb.HostPort)
if start != end {
return InternalServerError("host port ranges are not supported for port bindings")
}
}
}

if config.Config == nil {
return BadRequestError("invalid config")
}

// TODO(jzt): users other than root are not currently supported
// We should check for USER in config.Config.Env once we support Dockerfiles.
if config.Config.User != "" && config.Config.User != "root" {
Expand Down

0 comments on commit 86a8ab9

Please sign in to comment.