From 135f8f967488f171fcda6c66d7fa35726edaf19c Mon Sep 17 00:00:00 2001 From: Tony Abboud Date: Thu, 12 Jan 2017 12:01:29 -0500 Subject: [PATCH] Add error checking for hostPort range This fix catches the case where there is a single container port and a dynamic host port and will fail out gracefully Example docker-compose.yml snippet: port: ports: - "8091-8093:8091" - "80:8080" Signed-off-by: Tony Abboud --- cli/compose/convert/service.go | 8 +++++--- opts/port.go | 15 +++++++++++---- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/cli/compose/convert/service.go b/cli/compose/convert/service.go index 37f3ece403..a245987c8f 100644 --- a/cli/compose/convert/service.go +++ b/cli/compose/convert/service.go @@ -301,9 +301,11 @@ func convertEndpointSpec(source []string) (*swarm.EndpointSpec, error) { } for port := range ports { - portConfigs = append( - portConfigs, - opts.ConvertPortToPortConfig(port, portBindings)...) + portConfig, err := opts.ConvertPortToPortConfig(port, portBindings) + if err != nil { + return nil, err + } + portConfigs = append(portConfigs, portConfig...) } return &swarm.EndpointSpec{Ports: portConfigs}, nil diff --git a/opts/port.go b/opts/port.go index 020a5d1e1c..531908fb24 100644 --- a/opts/port.go +++ b/opts/port.go @@ -98,7 +98,11 @@ func (p *PortOpt) Set(value string) error { ports, portBindings, _ := nat.ParsePortSpecs([]string{value}) for port := range ports { - portConfigs = append(portConfigs, ConvertPortToPortConfig(port, portBindings)...) + portConfig, err := ConvertPortToPortConfig(port, portBindings) + if err != nil { + return err + } + portConfigs = append(portConfigs, portConfig...) } p.ports = append(p.ports, portConfigs...) } @@ -129,11 +133,14 @@ func (p *PortOpt) Value() []swarm.PortConfig { func ConvertPortToPortConfig( port nat.Port, portBindings map[nat.Port][]nat.PortBinding, -) []swarm.PortConfig { +) ([]swarm.PortConfig, error) { ports := []swarm.PortConfig{} for _, binding := range portBindings[port] { - hostPort, _ := strconv.ParseUint(binding.HostPort, 10, 16) + hostPort, err := strconv.ParseUint(binding.HostPort, 10, 16) + if err != nil && binding.HostPort != "" { + return nil, fmt.Errorf("invalid hostport binding (%s) for port (%s)", binding.HostPort, port.Port()) + } ports = append(ports, swarm.PortConfig{ //TODO Name: ? Protocol: swarm.PortConfigProtocol(strings.ToLower(port.Proto())), @@ -142,5 +149,5 @@ func ConvertPortToPortConfig( PublishMode: swarm.PortConfigPublishModeIngress, }) } - return ports + return ports, nil }