Standardize fuzzy matching and use fullmatch

Solves two problems:

1. How match fuzziness was used was very inconsistent; make them all the
same, i.e. "if is_fuzzy and limit, apply .* to both sides".

2. Use re.fullmatch instead of re.match to ensure exact matching of the
regex to the value. Without fuzziness, this would sometimes cause
inconsistent behavior, for instance if a limit was non-fuzzy "vm",
expecting to match the actual "vm", but also matching "vm1" too.
This commit is contained in:
Joshua Boniface 2021-12-06 16:35:29 -05:00
parent 2461941421
commit c776aba8b3
4 changed files with 50 additions and 54 deletions

View File

@ -375,7 +375,7 @@ def get_list_osd(zkhandler, limit, is_fuzzy=True):
for osd in full_osd_list: for osd in full_osd_list:
if limit: if limit:
try: try:
if re.match(limit, osd): if re.fullmatch(limit, osd):
osd_list.append(getOSDInformation(zkhandler, osd)) osd_list.append(getOSDInformation(zkhandler, osd))
except Exception as e: except Exception as e:
return False, "Regex Error: {}".format(e) return False, "Regex Error: {}".format(e)
@ -496,16 +496,19 @@ def remove_pool(zkhandler, name):
def get_list_pool(zkhandler, limit, is_fuzzy=True): def get_list_pool(zkhandler, limit, is_fuzzy=True):
full_pool_list = zkhandler.children("base.pool") full_pool_list = zkhandler.children("base.pool")
if limit: if is_fuzzy and limit:
if not is_fuzzy: # Implicitly assume fuzzy limits
limit = "^" + limit + "$" if not re.match(r"\^.*", limit):
limit = ".*" + limit
if not re.match(r".*\$", limit):
limit = limit + ".*"
get_pool_info = dict() get_pool_info = dict()
for pool in full_pool_list: for pool in full_pool_list:
is_limit_match = False is_limit_match = False
if limit: if limit:
try: try:
if re.match(limit, pool): if re.fullmatch(limit, pool):
is_limit_match = True is_limit_match = True
except Exception as e: except Exception as e:
return False, "Regex Error: {}".format(e) return False, "Regex Error: {}".format(e)
@ -848,15 +851,12 @@ def get_list_volume(zkhandler, pool, limit, is_fuzzy=True):
full_volume_list = getCephVolumes(zkhandler, pool) full_volume_list = getCephVolumes(zkhandler, pool)
if limit: if is_fuzzy and limit:
if not is_fuzzy: # Implicitly assume fuzzy limits
limit = "^" + limit + "$" if not re.match(r"\^.*", limit):
else: limit = ".*" + limit
# Implicitly assume fuzzy limits if not re.match(r".*\$", limit):
if not re.match(r"\^.*", limit): limit = limit + ".*"
limit = ".*" + limit
if not re.match(r".*\$", limit):
limit = limit + ".*"
get_volume_info = dict() get_volume_info = dict()
for volume in full_volume_list: for volume in full_volume_list:
@ -867,7 +867,7 @@ def get_list_volume(zkhandler, pool, limit, is_fuzzy=True):
if limit: if limit:
# Try to match the limit against the volume name # Try to match the limit against the volume name
try: try:
if re.match(limit, volume_name): if re.fullmatch(limit, volume_name):
is_limit_match = True is_limit_match = True
except Exception as e: except Exception as e:
return False, "Regex Error: {}".format(e) return False, "Regex Error: {}".format(e)
@ -1073,7 +1073,7 @@ def get_list_snapshot(zkhandler, pool, volume, limit, is_fuzzy=True):
pool_name, volume_name = volume.split("/") pool_name, volume_name = volume.split("/")
if limit: if limit:
try: try:
if re.match(limit, snapshot_name): if re.fullmatch(limit, snapshot_name):
snapshot_list.append( snapshot_list.append(
{ {
"pool": pool_name, "pool": pool_name,

View File

@ -665,16 +665,20 @@ def get_list(zkhandler, limit, is_fuzzy=True):
net_list = [] net_list = []
full_net_list = zkhandler.children("base.network") full_net_list = zkhandler.children("base.network")
if is_fuzzy and limit:
# Implicitly assume fuzzy limits
if not re.match(r"\^.*", limit):
limit = ".*" + limit
if not re.match(r".*\$", limit):
limit = limit + ".*"
for net in full_net_list: for net in full_net_list:
description = zkhandler.read(("network", net)) description = zkhandler.read(("network", net))
if limit: if limit:
try: try:
if not is_fuzzy: if re.fullmatch(limit, net):
limit = "^" + limit + "$"
if re.match(limit, net):
net_list.append(getNetworkInformation(zkhandler, net)) net_list.append(getNetworkInformation(zkhandler, net))
if re.match(limit, description): if re.fullmatch(limit, description):
net_list.append(getNetworkInformation(zkhandler, net)) net_list.append(getNetworkInformation(zkhandler, net))
except Exception as e: except Exception as e:
return False, "Regex Error: {}".format(e) return False, "Regex Error: {}".format(e)
@ -700,25 +704,19 @@ def get_list_dhcp(zkhandler, network, limit, only_static=False, is_fuzzy=True):
full_dhcp_list = getNetworkDHCPReservations(zkhandler, net_vni) full_dhcp_list = getNetworkDHCPReservations(zkhandler, net_vni)
full_dhcp_list += getNetworkDHCPLeases(zkhandler, net_vni) full_dhcp_list += getNetworkDHCPLeases(zkhandler, net_vni)
if limit: if is_fuzzy and limit:
try: # Implicitly assume fuzzy limits
if not is_fuzzy: if not re.match(r"\^.*", limit):
limit = "^" + limit + "$" limit = ".*" + limit
if not re.match(r".*\$", limit):
# Implcitly assume fuzzy limits limit = limit + ".*"
if not re.match(r"\^.*", limit):
limit = ".*" + limit
if not re.match(r".*\$", limit):
limit = limit + ".*"
except Exception as e:
return False, "Regex Error: {}".format(e)
for lease in full_dhcp_list: for lease in full_dhcp_list:
valid_lease = False valid_lease = False
if limit: if limit:
if re.match(limit, lease): if re.fullmatch(limit, lease):
valid_lease = True valid_lease = True
if re.match(limit, lease): if re.fullmatch(limit, lease):
valid_lease = True valid_lease = True
else: else:
valid_lease = True valid_lease = True
@ -748,23 +746,17 @@ def get_list_acl(zkhandler, network, limit, direction, is_fuzzy=True):
acl_list = [] acl_list = []
full_acl_list = getNetworkACLs(zkhandler, net_vni, direction) full_acl_list = getNetworkACLs(zkhandler, net_vni, direction)
if limit: if is_fuzzy and limit:
try: # Implicitly assume fuzzy limits
if not is_fuzzy: if not re.match(r"\^.*", limit):
limit = "^" + limit + "$" limit = ".*" + limit
if not re.match(r".*\$", limit):
# Implcitly assume fuzzy limits limit = limit + ".*"
if not re.match(r"\^.*", limit):
limit = ".*" + limit
if not re.match(r".*\$", limit):
limit = limit + ".*"
except Exception as e:
return False, "Regex Error: {}".format(e)
for acl in full_acl_list: for acl in full_acl_list:
valid_acl = False valid_acl = False
if limit: if limit:
if re.match(limit, acl["description"]): if re.fullmatch(limit, acl["description"]):
valid_acl = True valid_acl = True
else: else:
valid_acl = True valid_acl = True

View File

@ -237,13 +237,17 @@ def get_list(
node_list = [] node_list = []
full_node_list = zkhandler.children("base.node") full_node_list = zkhandler.children("base.node")
if is_fuzzy and limit:
# Implicitly assume fuzzy limits
if not re.match(r"\^.*", limit):
limit = ".*" + limit
if not re.match(r".*\$", limit):
limit = limit + ".*"
for node in full_node_list: for node in full_node_list:
if limit: if limit:
try: try:
if not is_fuzzy: if re.fullmatch(limit, node):
limit = "^" + limit + "$"
if re.match(limit, node):
node_list.append(getNodeInformation(zkhandler, node)) node_list.append(getNodeInformation(zkhandler, node))
except Exception as e: except Exception as e:
return False, "Regex Error: {}".format(e) return False, "Regex Error: {}".format(e)

View File

@ -1227,9 +1227,9 @@ def get_list(zkhandler, node, state, tag, limit, is_fuzzy=True, negate=False):
if limit: if limit:
# Try to match the limit against the UUID (if applicable) and name # Try to match the limit against the UUID (if applicable) and name
try: try:
if is_limit_uuid and re.match(limit, vm): if is_limit_uuid and re.fullmatch(limit, vm):
is_limit_match = True is_limit_match = True
if re.match(limit, name): if re.fullmatch(limit, name):
is_limit_match = True is_limit_match = True
except Exception as e: except Exception as e:
return False, "Regex Error: {}".format(e) return False, "Regex Error: {}".format(e)