|
| 1 | +import re |
| 2 | + |
| 3 | + |
| 4 | +class RegexConfigHelper: |
| 5 | + |
| 6 | + def __init__(self): |
| 7 | + pass |
| 8 | + |
| 9 | + @classmethod |
| 10 | + def prepare_config_mapping(cls, event): |
| 11 | + """ |
| 12 | + Function to prepare config mapping |
| 13 | + Args: |
| 14 | + event: Event to be logged |
| 15 | + Return: |
| 16 | + regex_config: Regex config mapping |
| 17 | + """ |
| 18 | + regex_config = {} |
| 19 | + |
| 20 | + # Config mapping for request.verb |
| 21 | + if event.request.verb: |
| 22 | + regex_config["request.verb"] = event.request.verb |
| 23 | + |
| 24 | + # Config mapping for request.uri |
| 25 | + if event.request.uri: |
| 26 | + extracted = re.match(r"http[s]*://[^/]+(/[^?]+)", event.request.uri) |
| 27 | + if extracted is not None: |
| 28 | + route_mapping = extracted.group(1) |
| 29 | + else: |
| 30 | + route_mapping = '/' |
| 31 | + regex_config["request.route"] = route_mapping |
| 32 | + |
| 33 | + # Config mapping for request.ip_address |
| 34 | + if event.request.ip_address: |
| 35 | + regex_config["request.ip_address"] = event.request.ip_address |
| 36 | + |
| 37 | + # Config mapping for response.status |
| 38 | + if event.response.status: |
| 39 | + regex_config["response.status"] = event.response.status |
| 40 | + |
| 41 | + return regex_config |
| 42 | + |
| 43 | + @classmethod |
| 44 | + def regex_match(cls, event_value, condition_value): |
| 45 | + """ |
| 46 | + Function to perform the regex matching with event value and condition value |
| 47 | + Args: |
| 48 | + event_value: Value associated with event (request) |
| 49 | + condition_value: Value associated with the regex config condition |
| 50 | + Return: |
| 51 | + regex_matched: Regex matched value to determine if the regex match was successful |
| 52 | + """ |
| 53 | + extracted = re.search(condition_value, event_value) |
| 54 | + if extracted is not None: |
| 55 | + return extracted.group(0) |
| 56 | + |
| 57 | + def fetch_sample_rate_on_regex_match(self, regex_configs, config_mapping): |
| 58 | + """ |
| 59 | + Function to fetch the sample rate and determine if request needs to be block or not |
| 60 | + Args: |
| 61 | + regex_configs: Regex configs |
| 62 | + config_mapping: Config associated with the request |
| 63 | + Return: |
| 64 | + sample_rate: Sample rate |
| 65 | + """ |
| 66 | + # Iterate through the list of regex configs |
| 67 | + for regex_rule in regex_configs: |
| 68 | + # Fetch the sample rate |
| 69 | + sample_rate = regex_rule["sample_rate"] |
| 70 | + # Fetch the conditions |
| 71 | + conditions = regex_rule["conditions"] |
| 72 | + # Bool flag to determine if the regex conditions are matched |
| 73 | + regex_matched = None |
| 74 | + # Create a table to hold the conditions mapping (path and value) |
| 75 | + condition_table = {} |
| 76 | + # Iterate through the regex rule conditions and map the path and value |
| 77 | + for condition in conditions: |
| 78 | + # Add condition path -> value to the condition table |
| 79 | + condition_table[condition["path"]] = condition["value"] |
| 80 | + # Iterate through conditions table and perform `and` operation between each conditions |
| 81 | + for path, values in condition_table.items(): |
| 82 | + # Check if the path exists in the request config mapping |
| 83 | + if config_mapping[path]: |
| 84 | + # Fetch the value of the path in request config mapping |
| 85 | + event_data = config_mapping[path] |
| 86 | + # Perform regex matching with event value |
| 87 | + regex_matched = self.regex_match(event_data, values) |
| 88 | + else: |
| 89 | + # Path does not exists in request config mapping, so no need to match regex condition rule |
| 90 | + regex_matched = False |
| 91 | + # If one of the rule does not match, skip the condition & avoid matching other rules for the same condition |
| 92 | + if not regex_matched: |
| 93 | + break |
| 94 | + # If regex conditions matched, return sample rate |
| 95 | + if regex_matched: |
| 96 | + return sample_rate |
| 97 | + # If regex conditions are not matched, return sample rate as None and will use default sample rate |
| 98 | + return None |
0 commit comments