Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions docker/utils/ports/ports.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


def add_port_mapping(port_bindings, internal_port, external):
if internal_port in port_bindings:
port_bindings[internal_port].append(external)
Expand Down Expand Up @@ -33,9 +32,8 @@ def to_port_range(port):
if "/" in port:
parts = port.split("/")
if len(parts) != 2:
raise ValueError('Invalid port "%s", should be '
'[[remote_ip:]remote_port[-remote_port]:]'
'port[/protocol]' % port)
_raise_invalid_port(port)

port, protocol = parts
protocol = "/" + protocol

Expand All @@ -52,11 +50,17 @@ def to_port_range(port):
'port or startport-endport' % port)


def _raise_invalid_port(port):
raise ValueError('Invalid port "%s", should be '
'[[remote_ip:]remote_port[-remote_port]:]'
'port[/protocol]' % port)


def split_port(port):
parts = str(port).split(':')

if not 1 <= len(parts) <= 3:
raise ValueError('Invalid port "%s", should be '
'[[remote_ip:]remote_port:]port[/protocol]' % port)
_raise_invalid_port(port)

if len(parts) == 1:
internal_port, = parts
Expand All @@ -66,6 +70,10 @@ def split_port(port):

internal_range = to_port_range(internal_port)
external_range = to_port_range(external_port)

if internal_range is None or external_range is None:
_raise_invalid_port(port)

if len(internal_range) != len(external_range):
raise ValueError('Port ranges don\'t match in length')

Expand Down
8 changes: 8 additions & 0 deletions tests/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,14 @@ def test_port_and_range_invalid(self):
self.assertRaises(ValueError,
lambda: split_port("0.0.0.0:1000:2000-2002/tcp"))

def test_port_only_with_colon(self):
self.assertRaises(ValueError,
lambda: split_port(":80"))

def test_host_only_with_colon(self):
self.assertRaises(ValueError,
lambda: split_port("localhost:"))

def test_build_port_bindings_with_one_port(self):
port_bindings = build_port_bindings(["127.0.0.1:1000:1000"])
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000")])
Expand Down