|
| 1 | +""" |
| 2 | +Uncoder IO Commercial Edition License |
| 3 | +----------------------------------------------------------------- |
| 4 | +Copyright (c) 2024 SOC Prime, Inc. |
| 5 | +
|
| 6 | +This file is part of the Uncoder IO Commercial Edition ("CE") and is |
| 7 | +licensed under the Uncoder IO Non-Commercial License (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + https://github.com/UncoderIO/UncoderIO/blob/main/LICENSE |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +----------------------------------------------------------------- |
| 17 | +""" |
| 18 | + |
| 19 | +import re |
| 20 | +from typing import Union |
| 21 | + |
| 22 | +from app.translator.core.models.query_container import RawQueryContainer, TokenizedQueryContainer |
| 23 | +from app.translator.core.parser import PlatformQueryParser |
| 24 | +from app.translator.platforms.base.aql.const import NUM_VALUE_PATTERN, SINGLE_QUOTES_VALUE_PATTERN |
| 25 | +from app.translator.platforms.base.aql.mapping import AQLMappings, aql_mappings |
| 26 | +from app.translator.platforms.base.aql.tokenizer import AQLTokenizer |
| 27 | +from app.translator.tools.utils import get_match_group |
| 28 | + |
| 29 | + |
| 30 | +class AQLQueryParser(PlatformQueryParser): |
| 31 | + tokenizer = AQLTokenizer() |
| 32 | + mappings: AQLMappings = aql_mappings |
| 33 | + |
| 34 | + log_source_functions = ("LOGSOURCENAME", "LOGSOURCEGROUPNAME", "LOGSOURCETYPENAME", "CATEGORYNAME") |
| 35 | + log_source_function_pattern = r"\(?(?P<key>___func_name___\([a-zA-Z]+\))(?:\s+like\s+|\s+ilike\s+|\s*=\s*)'(?P<value>[%a-zA-Z\s]+)'\s*\)?\s+(?:and|or)?\s" # noqa: E501 |
| 36 | + |
| 37 | + log_source_key_types = ("devicetype", "category", "qid", "qideventcategory") |
| 38 | + log_source_pattern = rf"___source_type___(?:\s+like\s+|\s+ilike\s+|\s*=\s*)(?:{SINGLE_QUOTES_VALUE_PATTERN}|{NUM_VALUE_PATTERN})(?:\s+(?:and|or)\s+|\s+)?" # noqa: E501 |
| 39 | + num_value_pattern = r"[0-9]+" |
| 40 | + multi_num_log_source_pattern = ( |
| 41 | + rf"___source_type___\s+in\s+\((?P<value>(?:{num_value_pattern}(?:\s*,\s*)?)+)\)(?:\s+(?:and|or)\s+|\s+)?" |
| 42 | + ) |
| 43 | + str_value_pattern = r"""(?:')(?P<s_q_value>(?:[:a-zA-Z\*0-9=+%#\-\/\\,_".$&^@!\(\)\{\}\s]|'')+)(?:')""" |
| 44 | + multi_str_log_source_pattern = ( |
| 45 | + rf"""___source_type___\s+in\s+\((?P<value>(?:{str_value_pattern}(?:\s*,\s*)?)+)\)(?:\s+(?:and|or)\s+|\s+)?""" |
| 46 | + ) |
| 47 | + |
| 48 | + table_pattern = r"\sFROM\s(?P<table>[a-zA-Z\.\-\*]+)\sWHERE\s" |
| 49 | + |
| 50 | + def __clean_query(self, query: str) -> str: |
| 51 | + for func_name in self.log_source_functions: |
| 52 | + pattern = self.log_source_function_pattern.replace("___func_name___", func_name) |
| 53 | + while search := re.search(pattern, query, flags=re.IGNORECASE): |
| 54 | + pos_start = search.start() |
| 55 | + pos_end = search.end() |
| 56 | + query = query[:pos_start] + query[pos_end:] |
| 57 | + |
| 58 | + return query |
| 59 | + |
| 60 | + @staticmethod |
| 61 | + def __parse_multi_value_log_source( |
| 62 | + match: re.Match, query: str, pattern: str |
| 63 | + ) -> tuple[str, Union[list[str], list[int]]]: |
| 64 | + value = match.group("value") |
| 65 | + pos_start = match.start() |
| 66 | + pos_end = match.end() |
| 67 | + query = query[:pos_start] + query[pos_end:] |
| 68 | + return query, re.findall(pattern, value) |
| 69 | + |
| 70 | + def __parse_log_sources(self, query: str) -> tuple[dict[str, Union[list[str], list[int]]], str]: |
| 71 | + log_sources = {} |
| 72 | + |
| 73 | + if search := re.search(self.table_pattern, query, flags=re.IGNORECASE): |
| 74 | + pos_end = search.end() |
| 75 | + query = query[pos_end:] |
| 76 | + |
| 77 | + for log_source_key in self.log_source_key_types: |
| 78 | + pattern = self.log_source_pattern.replace("___source_type___", log_source_key) |
| 79 | + while search := re.search(pattern, query, flags=re.IGNORECASE): |
| 80 | + num_value = get_match_group(search, group_name="num_value") |
| 81 | + str_value = get_match_group(search, group_name="s_q_value") |
| 82 | + value = num_value and int(num_value) or str_value |
| 83 | + log_sources.setdefault(log_source_key, []).append(value) |
| 84 | + pos_start = search.start() |
| 85 | + pos_end = search.end() |
| 86 | + query = query[:pos_start] + query[pos_end:] |
| 87 | + |
| 88 | + pattern = self.multi_num_log_source_pattern.replace("___source_type___", log_source_key) |
| 89 | + if search := re.search(pattern, query, flags=re.IGNORECASE): |
| 90 | + query, values = self.__parse_multi_value_log_source(search, query, self.num_value_pattern) |
| 91 | + values = [int(v) for v in values] |
| 92 | + log_sources.setdefault(log_source_key, []).extend(values) |
| 93 | + |
| 94 | + pattern = self.multi_str_log_source_pattern.replace("___source_type___", log_source_key) |
| 95 | + if search := re.search(pattern, query, flags=re.IGNORECASE): |
| 96 | + query, values = self.__parse_multi_value_log_source(search, query, self.str_value_pattern) |
| 97 | + log_sources.setdefault(log_source_key, []).extend(values) |
| 98 | + |
| 99 | + return log_sources, query |
| 100 | + |
| 101 | + def _parse_query(self, text: str) -> tuple[str, dict[str, Union[list[str], list[int]]]]: |
| 102 | + query = self.__clean_query(text) |
| 103 | + log_sources, query = self.__parse_log_sources(query) |
| 104 | + return query, log_sources |
| 105 | + |
| 106 | + def parse(self, raw_query_container: RawQueryContainer) -> TokenizedQueryContainer: |
| 107 | + query, log_sources = self._parse_query(raw_query_container.query) |
| 108 | + tokens, source_mappings = self.get_tokens_and_source_mappings(query, log_sources) |
| 109 | + fields_tokens = self.get_fields_tokens(tokens=tokens) |
| 110 | + meta_info = raw_query_container.meta_info |
| 111 | + meta_info.query_fields = fields_tokens |
| 112 | + meta_info.source_mapping_ids = [source_mapping.source_id for source_mapping in source_mappings] |
| 113 | + return TokenizedQueryContainer(tokens=tokens, meta_info=meta_info) |
0 commit comments