Skip to content

Commit

Permalink
[pfsense_nat_port_forward] Handle non-TCP/UDP protocols
Browse files Browse the repository at this point in the history
Fixes #28
  • Loading branch information
opoplawski committed Jan 21, 2023
1 parent 80caba7 commit 624684a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
19 changes: 14 additions & 5 deletions plugins/module_utils/nat_port_forward.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

# Copyright: (c) 2019, Frederic Bor <frederic.bor@wanadoo.fr>
# Copyright: (c) 2023, Orion Poplwski <orion@nwra.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
Expand Down Expand Up @@ -120,10 +121,16 @@ def _parse_target_address(self, obj):
else:
self.module.fail_json(msg='"%s" is not a valid redirect target IP address or host alias.' % (param))

if ports is not None and self.pfsense.is_port_or_alias(ports):
obj['local-port'] = ports
else:
self.module.fail_json(msg='"{0}" is not a valid redirect target port. It must be a port alias or integer between 1 and 65535.'.format(ports))
if ports is None and self.params['protocol'] in ["tcp", "udp", "tcp/udp"]:
self.module.fail_json(msg='Must specify a target port with protocol "{0}".'.format(self.params['protocol']))

if ports is not None:
if self.params['protocol'] not in ["tcp", "udp", "tcp/udp"]:
self.module.fail_json(msg='Cannot specify a target port with protocol "{0}".'.format(self.params['protocol']))
elif self.pfsense.is_port_or_alias(ports):
obj['local-port'] = ports
else:
self.module.fail_json(msg='"{0}" is not a valid redirect target port. It must be a port alias or integer between 1 and 65535.'.format(ports))

def _validate_params(self):
""" do some extra checks on input parameters """
Expand Down Expand Up @@ -436,7 +443,9 @@ def _obj_to_log_fields(self, rule):
res = {}
res['source'] = self._obj_address_to_log_field(rule, 'source')
res['destination'] = self._obj_address_to_log_field(rule, 'destination')
res['target'] = rule['target'] + ':' + rule['local-port']
res['target'] = rule['target']
if 'local-port' in rule:
res['target'] += ':' + rule['local-port']
res['interface'] = self.pfsense.get_interface_display_name(rule['interface'])

return res
21 changes: 21 additions & 0 deletions tests/unit/plugins/modules/test_pfsense_nat_port_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,27 @@ def test_nat_port_forward_create_before(self):
)
self.do_module_test(obj, command=command, target_idx=1)

def test_nat_port_forward_create_icmp(self):
""" test """
obj = dict(descr='test_pf', interface='wan', protocol='icmp', source='any', destination='1.2.3.4', target='2.3.4.5', associated_rule='associated')
command = [
"create rule 'NAT test_pf' on 'wan', source='any', destination='2.3.4.5', protocol='icmp'",
"create nat_port_forward 'test_pf', interface='wan', protocol='icmp', source='any', destination='1.2.3.4', target='2.3.4.5'"
]
self.do_module_test(obj, command=command, target_idx=3)

def test_nat_port_forward_create_tcp_fail_no_port(self):
""" test """
obj = dict(descr='test_pf', interface='wan', source='any', destination='1.2.3.4', target='2.3.4.5', associated_rule='associated')
msg = 'Must specify a target port with protocol "tcp".'
self.do_module_test(obj, failed=True, msg=msg)

def test_nat_port_forward_create_icmp_fail_port(self):
""" test """
obj = dict(descr='test_pf', interface='wan', protocol='icmp', source='any', destination='1.2.3.4', target='2.3.4.5:443', associated_rule='associated')
msg = 'Cannot specify a target port with protocol "icmp".'
self.do_module_test(obj, failed=True, msg=msg)

def test_nat_port_forward_update_noop(self):
""" test """
obj = dict(descr='one', interface='wan', source='any', destination='IP:wan:22022', target='10.255.1.20:22', associated_rule='none')
Expand Down

0 comments on commit 624684a

Please sign in to comment.