o
    O6uf  ã                   @   s&   d dl Z d dlmZ G dd„ dƒZdS )é    N)ÚLockc                   @   sd   e Zd ZdZg Zg ZdZdZdZdd„ Z	dd„ Z
dd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚSoSMapzæStandardized 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.
    TFc                 C   s    i | _ tƒ | _g | _tƒ | _d S )N)ÚdatasetÚsetÚ_regexes_madeÚcompiled_regexesr   Úlock)Úself© r
   ú?/usr/lib/python3/dist-packages/sos/cleaner/mappings/__init__.pyÚ__init__   s   zSoSMap.__init__c                 C   sX   |r|| j v s|| j ¡ v s| jrt|ƒdkrdS | jD ]}t ||tj¡r) dS qdS )z€Some items need to be completely ignored, for example link-local or
        loopback addresses should not be obfuscated
        é   TF)	Ú	skip_keysr   ÚvaluesÚignore_short_itemsÚlenÚignore_matchesÚreÚmatchÚI)r	   ÚitemÚskipr
   r
   r   Úignore_item%   s   ÿ
ÿzSoSMap.ignore_itemc                 C   sb   |   |¡r|S | j� |  |¡| j|< | jr|  |¡ | j| W  d  ƒ S 1 s*w   Y  dS )zºAdd a particular item to the map, generating an obfuscated pair
        for it.

        Positional arguments:

            :param item:        The plaintext object to obfuscate
        N)r   r   Úsanitize_itemr   Úcompile_regexesÚadd_regex_item©r	   r   r
   r
   r   Úadd1   s   

$üz
SoSMap.addc                 C   sV   |   |¡rdS || jvr)| j |¡ | j ||  |¡f¡ | jjdd„ dd� dS dS )zßAdd 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``
        Nc                 S   s   t | d ƒS )Nr   )r   )Úxr
   r
   r   Ú<lambda>V   s    z'SoSMap.add_regex_item.<locals>.<lambda>T)ÚkeyÚreverse)r   r   r   r   ÚappendÚget_regex_resultÚsortr   r
   r
   r   r   A   s   

ôzSoSMap.add_regex_itemc                 C   s2   | j rdt |¡› d�}nt |¡}t |tj¡S )a9  Generate 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
(?=\b|_|-))Úmatch_full_words_onlyr   ÚescapeÚcompiler   r   r
   r
   r   r#   X   s   
zSoSMap.get_regex_resultc                 C   s   |S )zýPerform the obfuscation relevant to the item being added to the map.

        This should be overridden by each type of map that subclasses SoSMap

        Positional arguments:

            :param item:        The plaintext object to obfuscate
        r
   r   r
   r
   r   r   k   s   	zSoSMap.sanitize_itemc                 C   s,   |   |¡r|S || jvr|  |¡S | j| S )z—Retrieve an item's obfuscated counterpart from the map. If the item
        does not yet exist in the map, add it by generating one on the fly
        )r   r   r   r   r
   r
   r   Úgetv   s
   



z
SoSMap.getc                 C   s   | j  |¡ dS )a)  Update the map using information from a previous run to ensure that
        we have consistent obfuscation between reports

        Positional arguments:

            :param map_dict:    A dict of mappings with the form of
                                {clean_entry: 'obfuscated_entry'}
        N)r   Úupdate)r	   Úmap_dictr
   r
   r   Úconf_update€   s   	zSoSMap.conf_updateN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r%   r   r   r   r   r#   r   r(   r+   r
   r
   r
   r   r      s    
r   )r   Ú	threadingr   r   r
   r
   r
   r   Ú<module>   s   
