Skip to content

Commit b7595ee

Browse files
authored
nat: T6371: fix op mode display of configured ports when comma separated list of ports/ranges exists
Before: Issuing the op mode command "show nat source rules" will throw an exception if the user has configured NAT rules using a list of ports as a comma-separated list (e.g. '!22,telnet,http,123,1001-1005'). Also there was no handling for the "!" rule and so '!53' would display as '53'. With this PR: Introduced iteration to capture all configured ports and append to the appropriate string for display to the user as well as handling of '!' if present in user's configuration.
1 parent a234384 commit b7595ee

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

src/op_mode/nat.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,23 @@ def _get_raw_translation(direction, family, address=None):
9999

100100

101101
def _get_formatted_output_rules(data, direction, family):
102+
def _get_ports_for_output(my_dict):
103+
# Get and insert all configured ports or port ranges into output string
104+
for index, port in enumerate(my_dict['set']):
105+
if 'range' in str(my_dict['set'][index]):
106+
output = my_dict['set'][index]['range']
107+
output = '-'.join(map(str, output))
108+
else:
109+
output = str(port)
110+
if index == 0:
111+
output = str(output)
112+
else:
113+
output = ','.join([output,output])
114+
# Handle case where configured ports are a negated list
115+
if my_dict['op'] == '!=':
116+
output = '!' + output
117+
return(output)
118+
102119
# Add default values before loop
103120
sport, dport, proto = 'any', 'any', 'any'
104121
saddr = '::/0' if family == 'inet6' else '0.0.0.0/0'
@@ -126,21 +143,9 @@ def _get_formatted_output_rules(data, direction, family):
126143
elif my_dict['field'] == 'daddr':
127144
daddr = f'{op}{my_dict["prefix"]["addr"]}/{my_dict["prefix"]["len"]}'
128145
elif my_dict['field'] == 'sport':
129-
# Port range or single port
130-
if jmespath.search('set[*].range', my_dict):
131-
sport = my_dict['set'][0]['range']
132-
sport = '-'.join(map(str, sport))
133-
else:
134-
sport = my_dict.get('set')
135-
sport = ','.join(map(str, sport))
146+
sport = _get_ports_for_output(my_dict)
136147
elif my_dict['field'] == 'dport':
137-
# Port range or single port
138-
if jmespath.search('set[*].range', my_dict):
139-
dport = my_dict["set"][0]["range"]
140-
dport = '-'.join(map(str, dport))
141-
else:
142-
dport = my_dict.get('set')
143-
dport = ','.join(map(str, dport))
148+
dport = _get_ports_for_output(my_dict)
144149
else:
145150
field = jmespath.search('left.payload.field', match)
146151
if field == 'saddr':

0 commit comments

Comments
 (0)