a >h@s6ddlZddlZddlZddlmZGdddZdS)N)Pathc@s|eZdZdZgZgZdZdZdZddZ ddZ dd Z d d Z d d Z ddZddZddZddZddZddZdS)SoSMapzStandardized way to store items with their obfuscated counterparts. Each type of sanitization that SoSCleaner supports should have a corresponding SoSMap() object, to allow for easy retrieval of obfuscated items. TFcCsJi|_t|_g|_|jj|_||_t j |jd|j|_ | dS)NZ cleaner_cache)datasetset _regexes_madecompiled_regexes __class____name__lowerZcnameworkdirospathjoin cache_dir load_entries)selfr rA/usr/lib/python3.9/site-packages/sos/cleaner/mappings/__init__.py__init__ s zSoSMap.__init__cCs"t|jjddd|ddS)ai Load cached entries from the disk. This method must be called when we initialize a Map instance and whenever we want to retrieve self.dataset (e.g. to store default_mapping file). The later is essential since a concurrent Map can add more objects to the cache, so we need to update self.dataset up to date. Keep in mind that size of self.dataset is usually bigger than number of files in the corresponding cleaner's directory: directory contains just whole items (e.g. IP addresses) while dataset contains more derived objects (e.g. subnets). T)parentsexist_okN)rrmkdirload_new_entries_from_dir)rrrrr,s zSoSMap.load_entriescCsX|r.||jvs.||jvs.|jr2t|dkr2dS|jD]}t||tjr8dSq8dS)zSome items need to be completely ignored, for example link-local or loopback addresses should not be obfuscated TF) skip_keysrvaluesignore_short_itemslenignore_matchesrematchI)ritemskiprrr ignore_item<s  zSoSMap.ignore_itemcCsFz|||j|<Wnty0||j|<Yn0|jrB||dS)N) sanitize_itemr Exceptioncompile_regexesadd_regex_itemrr#rrradd_sanitised_item_to_datasetHs  z$SoSMap.add_sanitised_item_to_datasetcCsd}|dkrtj|j|}tj|rd}t|ddd}|}Wdn1sZ0Y|j|ds|| ||d7}tj|j|}q|d8}|d7}qdS)Nrrutf-8encodingFr) r r rrisfileopenreadrgetr+)rcounterZ no_files_cntfnamefr#rrrrPs & z SoSMap.load_new_entries_from_dircCs||r|Sd}|j|ds|sltj|jd}t|jddd}||Wdn1sb0Yz8t |jd}t |jt j |j|||Wqty||Yq0q|j|S)zAdd a particular item to the map, generating an obfuscated pair for it. Positional arguments: :param item: The plaintext object to obfuscate NF)dirwr.r/r)r%rr4tempfileZNamedTemporaryFilerr2namewriterr linkr rr+FileExistsErrorr)rr#Ztmpfiler7r5rrraddfs" ( z SoSMap.addcCsR||rdS||jvrN|j||j|||f|jjdddddS)zAdd an item to the regexes dict and then re-sort the list that the parsers will use during parse_line() :param item: The unobfuscated item to generate a regex for :type item: ``str`` NcSs t|dS)Nr)r)xrrrz'SoSMap.add_regex_item..T)keyreverse)r%rr?rappendget_regex_resultsortr*rrrr)s    zSoSMap.add_regex_itemcCs2|jrdt|d}n t|}t|tjS)a9Generate the object/value that is used by the parser when iterating over pre-generated regexes during parse_line(). For most parsers this will simply be a ``re.Pattern()`` object, but for more complex parsers this can be overridden to provide a different object, e.g. a tuple, for that parer's specific iteration needs. :param item: The unobfuscated string to generate the regex for :type item: ``str`` :returns: A compiled regex pattern for the item :rtype: ``re.Pattern`` z (? s