From 7fa9161585c348f0e6432ebd66b423e393f79ad5 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Mon, 6 Feb 2017 14:16:03 +0100 Subject: [PATCH] Make sure we validate simple syntax on service commands We ignored errors for simple syntax in `PortOpt` (missed that in the previous migration of this code). This make sure we don't ignore `nat.Parse` errors. Test has been migrate too (errors are not exactly the same as before though -_-) Signed-off-by: Vincent Demeester --- opts/port.go | 15 ++++++++++++--- opts/port_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/opts/port.go b/opts/port.go index 531908fb24..152683c981 100644 --- a/opts/port.go +++ b/opts/port.go @@ -94,11 +94,20 @@ func (p *PortOpt) Set(value string) error { } else { // short syntax portConfigs := []swarm.PortConfig{} - // We can ignore errors because the format was already validated by ValidatePort - ports, portBindings, _ := nat.ParsePortSpecs([]string{value}) + ports, portBindingMap, err := nat.ParsePortSpecs([]string{value}) + if err != nil { + return err + } + for _, portBindings := range portBindingMap { + for _, portBinding := range portBindings { + if portBinding.HostIP != "" { + return fmt.Errorf("HostIP is not supported.") + } + } + } for port := range ports { - portConfig, err := ConvertPortToPortConfig(port, portBindings) + portConfig, err := ConvertPortToPortConfig(port, portBindingMap) if err != nil { return err } diff --git a/opts/port_test.go b/opts/port_test.go index 67bcf8f1d9..a483d269aa 100644 --- a/opts/port_test.go +++ b/opts/port_test.go @@ -245,6 +245,42 @@ func TestPortOptInvalidComplexSyntax(t *testing.T) { } } +func TestPortOptInvalidSimpleSyntax(t *testing.T) { + testCases := []struct { + value string + expectedError string + }{ + { + value: "9999999", + expectedError: "Invalid containerPort: 9999999", + }, + { + value: "80/xyz", + expectedError: "Invalid proto: xyz", + }, + { + value: "tcp", + expectedError: "Invalid containerPort: tcp", + }, + { + value: "udp", + expectedError: "Invalid containerPort: udp", + }, + { + value: "", + expectedError: "No port specified", + }, + { + value: "1.1.1.1:80:80", + expectedError: "HostIP is not supported", + }, + } + for _, tc := range testCases { + var port PortOpt + assert.Error(t, port.Set(tc.value), tc.expectedError) + } +} + func assertContains(t *testing.T, portConfigs []swarm.PortConfig, expected swarm.PortConfig) { var contains = false for _, portConfig := range portConfigs {