Skip to content

Commit d067ee0

Browse files
author
Capirca Team
committed
Merge pull request #276 from kevinsteves:br13
PiperOrigin-RevId: 376864098
2 parents 48dd5e9 + 7f8e91a commit d067ee0

2 files changed

Lines changed: 54 additions & 2 deletions

File tree

capirca/lib/paloaltofw.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,14 @@ class ServiceMap(object):
141141
def __init__(self):
142142
self.entries = {}
143143

144-
def get_service_name(self, term_name, src_ports, ports, protocol):
144+
def get_service_name(self, term_name, src_ports, ports, protocol, prefix=None):
145145
"""Returns service name based on the provided ports and protocol."""
146146
if (src_ports, ports, protocol) in self.entries:
147147
return self.entries[(src_ports, ports, protocol)]["name"]
148148

149-
service_name = "service-%s-%s" % (term_name, protocol)
149+
if prefix is None:
150+
prefix = "service-"
151+
service_name = "%s%s-%s" % (prefix, term_name, protocol)
150152

151153
if len(service_name) > 63:
152154
raise PaloAltoFWNameTooLongError(
@@ -268,6 +270,21 @@ def pan_ports(ports):
268270
if service_name not in self.options["service"]:
269271
self.options["service"].append(service_name)
270272

273+
elif "tcp" in term.protocol or "udp" in term.protocol:
274+
for p in term.protocol:
275+
if p not in ["tcp", "udp"]:
276+
logging.warning(
277+
"WARNING: Term %s in policy %s>%s contains port-less tcp "
278+
"and/or udp with protocol %s. Move %s to another term.",
279+
term.name, self.options["from_zone"][0],
280+
self.options["to_zone"][0], p, p)
281+
continue
282+
ports = pan_ports([("0", "65535")])
283+
# use prefix "" to avoid service name clash with term named "any"
284+
service_name = service_map.get_service_name("any", (), ports, p, "")
285+
if service_name not in self.options["service"]:
286+
self.options["service"].append(service_name)
287+
271288
if term.protocol:
272289
# Add application "any" to all terms, unless ICMP/ICMPv6
273290
for proto_name in term.protocol:

tests/lib/paloaltofw_test.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,41 @@ def testPanPorts(self):
933933
x = paloalto.config.findtext(PATH_SERVICE + path)
934934
self.assertEqual(x, "123", output)
935935

936+
T = """
937+
protocol:: tcp
938+
"""
939+
940+
pol = policy.ParsePolicy(POL % T, definitions)
941+
paloalto = paloaltofw.PaloAltoFW(pol, EXP_INFO)
942+
output = str(paloalto)
943+
name = "any-tcp"
944+
path = "/entry[@name='%s']/protocol/tcp/port" % name
945+
x = paloalto.config.findtext(PATH_SERVICE + path)
946+
self.assertEqual(x, "0-65535", output)
947+
path = "/entry[@name='%s']/protocol/tcp/source-port" % name
948+
x = paloalto.config.find(PATH_SERVICE + path)
949+
self.assertIsNone(x, output)
950+
951+
T = """
952+
protocol:: tcp udp
953+
"""
954+
955+
pol = policy.ParsePolicy(POL % T, definitions)
956+
paloalto = paloaltofw.PaloAltoFW(pol, EXP_INFO)
957+
output = str(paloalto)
958+
name = "any-tcp"
959+
path = "/entry[@name='%s']/protocol/tcp/port" % name
960+
x = paloalto.config.findtext(PATH_SERVICE + path)
961+
self.assertEqual(x, "0-65535", output)
962+
name = "any-udp"
963+
path = "/entry[@name='%s']/protocol/udp/port" % name
964+
x = paloalto.config.findtext(PATH_SERVICE + path)
965+
self.assertEqual(x, "0-65535", output)
966+
x = paloalto.config.findall(PATH_RULES +
967+
"/entry[@name='rule-1']/service/member")
968+
services = {elem.text for elem in x}
969+
self.assertEqual({"any-tcp", "any-udp"}, services, output)
970+
936971

937972
if __name__ == '__main__':
938973
unittest.main()

0 commit comments

Comments
 (0)