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:
		| @@ -375,7 +375,7 @@ def get_list_osd(zkhandler, limit, is_fuzzy=True): | ||||
|     for osd in full_osd_list: | ||||
|         if limit: | ||||
|             try: | ||||
|                 if re.match(limit, osd): | ||||
|                 if re.fullmatch(limit, osd): | ||||
|                     osd_list.append(getOSDInformation(zkhandler, osd)) | ||||
|             except Exception as 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): | ||||
|     full_pool_list = zkhandler.children("base.pool") | ||||
|  | ||||
|     if limit: | ||||
|         if not is_fuzzy: | ||||
|             limit = "^" + limit + "$" | ||||
|     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 + ".*" | ||||
|  | ||||
|     get_pool_info = dict() | ||||
|     for pool in full_pool_list: | ||||
|         is_limit_match = False | ||||
|         if limit: | ||||
|             try: | ||||
|                 if re.match(limit, pool): | ||||
|                 if re.fullmatch(limit, pool): | ||||
|                     is_limit_match = True | ||||
|             except Exception as 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) | ||||
|  | ||||
|     if limit: | ||||
|         if not is_fuzzy: | ||||
|             limit = "^" + limit + "$" | ||||
|         else: | ||||
|             # Implicitly assume fuzzy limits | ||||
|             if not re.match(r"\^.*", limit): | ||||
|                 limit = ".*" + limit | ||||
|             if not re.match(r".*\$", limit): | ||||
|                 limit = limit + ".*" | ||||
|     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 + ".*" | ||||
|  | ||||
|     get_volume_info = dict() | ||||
|     for volume in full_volume_list: | ||||
| @@ -867,7 +867,7 @@ def get_list_volume(zkhandler, pool, limit, is_fuzzy=True): | ||||
|         if limit: | ||||
|             # Try to match the limit against the volume name | ||||
|             try: | ||||
|                 if re.match(limit, volume_name): | ||||
|                 if re.fullmatch(limit, volume_name): | ||||
|                     is_limit_match = True | ||||
|             except Exception as 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("/") | ||||
|         if limit: | ||||
|             try: | ||||
|                 if re.match(limit, snapshot_name): | ||||
|                 if re.fullmatch(limit, snapshot_name): | ||||
|                     snapshot_list.append( | ||||
|                         { | ||||
|                             "pool": pool_name, | ||||
|   | ||||
| @@ -665,16 +665,20 @@ def get_list(zkhandler, limit, is_fuzzy=True): | ||||
|     net_list = [] | ||||
|     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: | ||||
|         description = zkhandler.read(("network", net)) | ||||
|         if limit: | ||||
|             try: | ||||
|                 if not is_fuzzy: | ||||
|                     limit = "^" + limit + "$" | ||||
|  | ||||
|                 if re.match(limit, net): | ||||
|                 if re.fullmatch(limit, net): | ||||
|                     net_list.append(getNetworkInformation(zkhandler, net)) | ||||
|                 if re.match(limit, description): | ||||
|                 if re.fullmatch(limit, description): | ||||
|                     net_list.append(getNetworkInformation(zkhandler, net)) | ||||
|             except Exception as 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 += getNetworkDHCPLeases(zkhandler, net_vni) | ||||
|  | ||||
|     if limit: | ||||
|         try: | ||||
|             if not is_fuzzy: | ||||
|                 limit = "^" + limit + "$" | ||||
|  | ||||
|             # Implcitly assume fuzzy limits | ||||
|             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) | ||||
|     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 lease in full_dhcp_list: | ||||
|         valid_lease = False | ||||
|         if limit: | ||||
|             if re.match(limit, lease): | ||||
|             if re.fullmatch(limit, lease): | ||||
|                 valid_lease = True | ||||
|             if re.match(limit, lease): | ||||
|             if re.fullmatch(limit, lease): | ||||
|                 valid_lease = True | ||||
|         else: | ||||
|             valid_lease = True | ||||
| @@ -748,23 +746,17 @@ def get_list_acl(zkhandler, network, limit, direction, is_fuzzy=True): | ||||
|     acl_list = [] | ||||
|     full_acl_list = getNetworkACLs(zkhandler, net_vni, direction) | ||||
|  | ||||
|     if limit: | ||||
|         try: | ||||
|             if not is_fuzzy: | ||||
|                 limit = "^" + limit + "$" | ||||
|  | ||||
|             # Implcitly assume fuzzy limits | ||||
|             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) | ||||
|     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 acl in full_acl_list: | ||||
|         valid_acl = False | ||||
|         if limit: | ||||
|             if re.match(limit, acl["description"]): | ||||
|             if re.fullmatch(limit, acl["description"]): | ||||
|                 valid_acl = True | ||||
|         else: | ||||
|             valid_acl = True | ||||
|   | ||||
| @@ -237,13 +237,17 @@ def get_list( | ||||
|     node_list = [] | ||||
|     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: | ||||
|         if limit: | ||||
|             try: | ||||
|                 if not is_fuzzy: | ||||
|                     limit = "^" + limit + "$" | ||||
|  | ||||
|                 if re.match(limit, node): | ||||
|                 if re.fullmatch(limit, node): | ||||
|                     node_list.append(getNodeInformation(zkhandler, node)) | ||||
|             except Exception as e: | ||||
|                 return False, "Regex Error: {}".format(e) | ||||
|   | ||||
| @@ -1227,9 +1227,9 @@ def get_list(zkhandler, node, state, tag, limit, is_fuzzy=True, negate=False): | ||||
|         if limit: | ||||
|             # Try to match the limit against the UUID (if applicable) and name | ||||
|             try: | ||||
|                 if is_limit_uuid and re.match(limit, vm): | ||||
|                 if is_limit_uuid and re.fullmatch(limit, vm): | ||||
|                     is_limit_match = True | ||||
|                 if re.match(limit, name): | ||||
|                 if re.fullmatch(limit, name): | ||||
|                     is_limit_match = True | ||||
|             except Exception as e: | ||||
|                 return False, "Regex Error: {}".format(e) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user