Skip to content

Commit

Permalink
Issue:3903841:PDR plugin: adding warnings and protection (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
vg12345 authored May 21, 2024
1 parent 7a7c3f4 commit 4a16b07
Showing 1 changed file with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,16 @@ def exclude_ports(self):
response = ""
for pair in pairs:
if pair:
port_name = pair[0]
port_name = self.fix_port_name(pair[0])
ttl = 0 if len(pair) == 1 else int(pair[1])
self.isolation_mgr.exclude_list.add(port_name, ttl)
if ttl == 0:
response += f"Port {port_name} added to exclude list forever{EOL}"
response += f"Port {port_name} added to exclude list forever"
else:
response += f"Port {port_name} added to exclude list for {ttl} seconds{EOL}"
response += f"Port {port_name} added to exclude list for {ttl} seconds"

response += self.get_port_warning(port_name) + EOL

return response, HTTPStatus.OK


Expand All @@ -98,10 +100,13 @@ def include_ports(self):

response = ""
for port_name in port_names:
port_name = self.fix_port_name(port_name)
if self.isolation_mgr.exclude_list.remove(port_name):
response += f"Port {port_name} removed from exclude list{EOL}"
response += f"Port {port_name} removed from exclude list"
else:
response += f"Port {port_name} is not in exclude list{EOL}"
response += f"Port {port_name} is not in exclude list"

response += self.get_port_warning(port_name) + EOL

return response, HTTPStatus.OK

Expand All @@ -116,3 +121,27 @@ def get_request_data(self):
else:
# Attempt to load plain data text as JSON
return json.loads(request.get_data(as_text=True))


def fix_port_name(self, port_name):
"""
Try to fix common user mistakes for input port names
Return fixed port name
"""
# Remove '0x' from the beginning
if port_name.startswith('0x'):
port_name = port_name[2:]

# Additional corrections can be added here upon request
return port_name

def get_port_warning(self, port_name):
"""
Return warning text if port does not exist in ports data and update plugin logs
"""
if not self.isolation_mgr.test_mode:
if not self.isolation_mgr.ports_data.get(port_name):
self.isolation_mgr.logger.warning(f"Port {port_name} is not found in ports data")
return " (WARNING: port is not found in ports data)"

return ""

0 comments on commit 4a16b07

Please sign in to comment.