o
    Û1jV$  ã                   @   s(   d dl Z d dlmZ G dd„ deƒZdS )é    N)ÚSoSMapc                       s˜   e Zd ZdZg d¢ZddgZdZdZdZdZ	dZ
i Zi Z‡ fdd	„Zd
d„ Zdd„ Zdd„ Zdd„ Z‡ fdd„Zdd„ Zdd„ Zdd„ Zdd„ Z‡  ZS )ÚSoSHostnameMapaL  Mapping store for hostnames and domain names

    Hostnames are obfuscated using an incrementing counter based on the total
    number of hosts matched regardless of domain name.

    Domain names are obfuscated based on the host's hostname, plus any user
    defined domains passed in by the `--domains` options.

    Domains are obfuscated as whole units, meaning the domains 'example.com'
    and 'host.foo.example.com' will be separately obfuscated with no relation
    for example as 'obfuscatedomdain1.com' and 'obfuscatedomain2.com'.

    Top-level domains are left untouched.
    )Ú	localhostz.*localdomain.*z^com..*ÚwwwÚapi)
z.yamlz.ymlz.crtz.keyz.pemz.logz.repoz.rulesz.confz.cfgTr   c                    s    d|v r
|  dd¡}tƒ  |¡S )z³Override the base get_regex_result() to provide a regex that, if
        this is an FQDN or a straight domain, will include an underscore
        formatted regex as well.
        Ú.z(\.|_))ÚreplaceÚsuperÚget_regex_result©ÚselfÚitem©Ú	__class__© úC/usr/lib/python3/dist-packages/sos/cleaner/mappings/hostname_map.pyr
   5   s   zSoSHostnameMap.get_regex_resultc                 C   s   d|› d�S )Nz(?<![a-z0-9])(?:z)(?![a-z0-9])r   r   r   r   r   Úget_regex_fullword>   s   z!SoSHostnameMap.get_regex_fullwordc                 C   s8   d|v rdd„ |  d¡D ƒ}d |¡}|S t |¡}|S )z³Override the base get_regex_escape() to provide a regex that, if
        this is an FQDN or a straight domain, will include an underscore
        formatted regex as well.
        r   c                 S   s   g | ]}t  |¡‘qS r   )ÚreÚescape)Ú.0Úpr   r   r   Ú
<listcomp>I   s    z3SoSHostnameMap.get_regex_escape.<locals>.<listcomp>z(?:\.|_))ÚsplitÚjoinr   r   )r   r   Úpartsr   r   r   Úget_regex_escapeB   s   

ÿzSoSHostnameMap.get_regex_escapec                 C   sš   zt | j ¡ dd�d  d¡d }t|ƒd | _W n	 ty"   Y nw zt | j ¡ dd�d  d¡}t|d  d¡d ƒd | _W dS  tyL   Y dS w )	z�Set the initial counter for host and domain obfuscation numbers
        based on what is already present in the mapping.
        T)Úreverser   Úhosté   Údomainr   N)	ÚsortedÚhostsÚvaluesr   ÚintÚ
host_countÚ
IndexErrorÚ_domainsÚdomain_count)r   ÚhÚdr   r   r   Úset_initial_countsO   s    þ"þz!SoSHostnameMap.set_initial_countsc                    sj   || j v rdS | d¡}d | d¡dd… ¡‰ t|ƒdkr%|d | jv S t‡ fdd„| j D ƒƒr3dS dS )	zkCheck if a potential domain is in one of the domains we've loaded
        and should be obfuscated
        Tr   r   éÿÿÿÿr   c                 3   s   � | ]}ˆ   |¡V  qd S ©N)Úendswith)r   Ú_d©Úno_tldr   r   Ú	<genexpr>n   s   € z?SoSHostnameMap.domain_name_in_loaded_domains.<locals>.<genexpr>F)r&   r   r   Úlenr!   Úany)r   r   r   r   r/   r   Údomain_name_in_loaded_domainsc   s   

z,SoSHostnameMap.domain_name_in_loaded_domainsc              	      sÊ  d}d}d }|  d¡r||d 7 }|dd … }|  d¡s| d¡r2||d 7 }|dd… }| d¡s!|| jv r<| j| S |  | ¡ ¡sKd |||g¡S | | j¡rdd| d¡d  }| |d¡}||7 }|| jvr×t	| j 
¡ dtd�D ]c}d	}| |¡}| d¡}	|	d rŽ|	d | jv rŽd}t|ƒdks˜|d s™qs|s¯|d  d¡s§| |¡r¯tƒ  |¡} n(| |d ¡rÖz| |d ¡d }
|d tƒ  |
¡ }W  n
 tyÕ   Y qsw qs|sßtƒ  |¡}|| | S )
NÚ )r   Ú_r   r   r+   r   T)r   ÚkeyF)Ú
startswithr-   Údatasetr4   Úlowerr   Ú
strip_extsr   r   r    Úkeysr2   r!   r	   ÚgetÚ	Exception)r   r   ÚprefixÚsuffixÚfinalÚextÚ	_existingÚ_host_substrÚ_testÚ_hÚitmr   r   r   r=   s   s^   

þ

þ



ÿ

ÿþùzSoSHostnameMap.getc           	      C   sà   |  d¡}t|ƒdkr|  |d  ¡ ¡S t|ƒdkr.|  |¡}tdd„ |D ƒƒr,| ¡ }|S t|ƒdkrn|d }|dd … }t|ƒdkrL|  | ¡ ¡}nd}|  |¡}|| j|< d ||g¡}tdd„ |D ƒƒrl| ¡ }|S d S )	Nr   r   r   é   c                 s   ó   � | ]}|  ¡ V  qd S r,   ©Úisupper©r   r(   r   r   r   r1   ¸   ó   € z/SoSHostnameMap.sanitize_item.<locals>.<genexpr>Úunknownc                 s   rI   r,   rJ   rL   r   r   r   r1   Ë   rM   )	r   r2   Úsanitize_short_namer:   Úsanitize_domainÚallÚupperr9   r   )	r   r   r   ÚdnameÚhostnamer   Úob_hostnameÚ	ob_domainÚ_fqdnr   r   r   Úsanitize_item°   s*   



zSoSHostnameMap.sanitize_itemc                 C   s^   |r|| j v r	|S || jvr*d| j› �}|| j|< |  jd7  _|| j|< |  |¡ | j| S )z„Obfuscate the short name of the host with an incremented counter
        based on the total number of obfuscated host names
        r   r   )Ú	skip_keysr9   r$   r!   Úadd_regex_item)r   rT   Úob_hostr   r   r   rO   Ð   s   




z"SoSHostnameMap.sanitize_short_namec                 C   sz   | j D ]}t |d |¡¡rd |¡  S q|d  ¡ }d |dd… ¡ ¡ }|  |¡}d ||g¡}|| jd |¡< |S )zeObfuscate the domainname, broken out into subdomains. Top-level
        domains are ignored.
        r   r+   r   )Úignore_matchesr   Úmatchr   r:   Ú_new_obfuscated_domainr9   )r   r   Ú_skipÚ
top_domainrS   rV   r   r   r   rP   Þ   s   
ÿ
zSoSHostnameMap.sanitize_domainc                 C   s4   || j vrd| j› �| j |< |  jd7  _| j | S )zDGenerate an obfuscated domain for each subdomain name given
        Úobfuscateddomainr   )r&   r'   )r   rS   r   r   r   r^   í   s   

z%SoSHostnameMap._new_obfuscated_domain)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r\   rY   r;   Úignore_short_itemsÚmatch_full_words_onlyr$   r'   r&   r!   r
   r   r   r*   r4   r=   rX   rO   rP   r^   Ú__classcell__r   r   r   r   r      s.    þ	= r   )r   Úsos.cleaner.mappingsr   r   r   r   r   r   Ú<module>   s   
