o
    K&bA                     @   s$  d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZmZm	Z	 d dl
mZmZmZmZmZ dZdZdZdZdZd	Zd
ZdZdZdZdZdZg dZe Zdd Zde de	ej! fddZ"di fdee  de	e  de	ee e f  de fddZ#i fde	ee e f  de fddZ$ddi fdee  de	ee   de	e  de	ee e f  de f
d d!Z%d"e d#e d$e d%ee  d&e ddfd'd(Z&d)d* Z'd+d, Z(	dCd"e d#e d&e ddfd-d.Z)d/e ddfd0d1Z*d2d3 Z+d4d5 Z,d6d7 Z-d8d9 Z.dd:d;d<Z/dee  fd=d>Z0	dDd?e	e  d@e	e  ddfdAdBZ1dS )E    N)DictListOptional)event_logger
exceptionsgpgmessagesutilg      N@z  # ubuntu-advantage-toolszDir::Etc::netrc/zDir::Etc::netrcparts/zDir::State::lists/z$Acquire::http::Proxy "{proxy_url}";
z%Acquire::https::Proxy "{proxy_url}";
z/etc/apt/trusted.gpg.dz/usr/share/keyringsz/usr/lib/apt/methods/httpsz /usr/sbin/update-ca-certificatesz//etc/apt/apt.conf.d/90ubuntu-advantage-aptproxy)g      ?g      @g      $@c              
   C   s  |  d\}}tjdsdS z0t !}tjddd||||tj	|dgt
td W d   W dS 1 s8w   Y  W dS  tjyy } z-|jdkrpt|j }td	|rbtd
| td|rptd| tdd}~w tjy   tdt
|w )a  Validate apt credentials for a PPA.

    @param repo_url: private-ppa url path
    @param username: PPA login username.
    @param password: PPA login password or resource token.

    @raises: UserFacingError for invalid credentials, timeout or unexpected
        errors.
    ://z/usr/lib/apt/apt-helperNzdownload-filez{}://{}:{}@{}/ubuntu/pool/zapt-helper-output)Ztimeoutretry_sleepsd   z401\s+unauthorized|httperror401z'Invalid APT credentials provided for {}zconnection timed outz-Timeout trying to access APT repository at {}z7Unexpected APT error. See /var/log/ubuntu-advantage.logzVCannot validate credentials for APT repo. Timeout after {} seconds trying to reach {}.)splitospathexiststempfileZTemporaryDirectoryr	   subpformatjoinAPT_HELPER_TIMEOUTAPT_RETRIESr   ProcessExecutionErrorZ	exit_codestrstderrlowerresearchUserFacingError
subprocessZTimeoutExpired)repo_urlusernamepasswordZprotocol	repo_pathZtmpder    r$   ./usr/lib/python3/dist-packages/uaclient/apt.pyassert_valid_apt_credentials!   sT   

&
r&   	apt_errorreturnc                 C   s   d}t  }|  dD ]}|r*td|}|r*d| d dd  }|| q|rBtjj	t
|dkr8d	nd
dt|d}|S )aA  Parse apt update errors for invalid apt config in user machine.

    This functions parses apt update errors regarding the presence of
    invalid apt config in the system, for example, a ppa that cannot be
    reached, for example.

    In that scenario, apt will output a message in the following formats:

    The repository 'ppa 404 Release' ...
    Failed to fetch ppa 404 ...

    On some releases, both of these errors will be present in the apt error
    message.

    :param apt_error: The apt error string
    :return: a NamedMessage containing the error message
    N
z2(Failed to fetch |The repository .)(?P<url>[^\s]+)z- urlz/distsr      s )pluralfailed_repos)setstripr   r   r   	groupdictaddr   ZAPT_UPDATE_INVALID_URL_CONFIGr   lenr   sorted)r'   	error_msgr/   lineZpattern_matchZrepo_url_matchr$   r$   r%   (_parse_apt_update_for_invalid_apt_configU   s$   
r8   cmdr6   envc              
   C   s   zt j| dt|d\}}W |S  tjyB } z'dt|jv r"t 	 t|j}|r1tj	|j
d|r5|nt|}t|d}~ww )a  Run an apt command, retrying upon failure APT_RETRIES times.

    :param cmd: List containing the apt command to run, passed to subp.
    :param error_msg: The string to raise as UserFacingError when all retries
       are exhausted in failure.
    :param env: Optional dictionary of environment variables to pass to subp.

    :return: stdout from successful run of the apt command.
    :raise UserFacingError: on issues running apt-cache policy.
    T)Zcapturer   r:   z%Could not get lock /var/lib/dpkg/lock)r6   N)r	   r   r   r   r   r   r   APTProcessConflictErrorr8   APTInvalidRepoErrormsgr   )r9   r6   r:   out_errr#   Zrepo_error_msgr=   r$   r$   r%   run_apt_command   s$   

r@   c              
   C   s   zt ddg| d}W |S  tjy   t  tjy* } ztj|jdd }~w tjyF } ztjtj	jd |j tj	j
dd }~ww )Napt-getupdate)r9   r:   )repo_msgr)   )r=   Zmsg_code)r@   r   r;   ZAPTUpdateProcessConflictErrorr<   ZAPTUpdateInvalidRepoErrorr=   r   r   ZAPT_UPDATE_FAILEDname)r:   r>   r#   r$   r$   r%   run_apt_update_command   s   rE   packagesapt_optionsc              
   C   sr   |d u rg }zt g d| |  ||d}W |S  tjy$   tj|d tjy8 } ztj|j|dd }~ww )N)rA   installz--assume-yes)r9   r6   r:   )
header_msg)rC   rI   )r@   r   r;   ZAPTInstallProcessConflictErrorr<   ZAPTInstallInvalidRepoErrorr=   )rF   rG   r6   r:   r>   r#   r$   r$   r%   run_apt_install_command   s*   rJ   repo_filenamer   credentialssuiteskeyring_filec                 C   s0  z	| d\}}W n ty   d}|}Y nw t d }|dr(|dd }t||| d}tdd	gtjj	}	|	
 D ]}
d
||
vrGq=d|
vrLq=d} d}|D ]"}||vrZqSd}d|v rk|sktd|| d}|dj|||d7 }qSt| | t||| tjt|}tjt|}t|| dS )zAdd an authenticated apt repo and credentials to the system.

    @raises: InvalidAPTCredentialsError when the token provided can't access
        the repo PPA.
    :Zbearerseries/NFz	apt-cachepolicyza={}-updatesz	o=Ubuntu,Tr-   z-updatesz?Not enabling apt suite "%s" because "%s-updates" is not enabledz# zQ{maybe_comment}deb {url}/ubuntu {suite} main
# deb-src {url}/ubuntu {suite} main
)maybe_commentr*   suite)r   
ValueErrorr	   get_platform_infoendswithr&   r@   r   ZAPT_POLICY_FAILEDr=   
splitlinesr   loggingdebug
write_fileadd_apt_auth_conf_entryr   r   r   KEYRINGS_DIRAPT_KEYS_DIRr   Zexport_gpg_key)rK   r   rL   rM   rN   r    r!   rP   Zupdates_enabledrS   r7   contentrU   rT   Zsource_keyring_fileZdestination_keyring_filer$   r$   r%   add_auth_apt_repo   sX   
ra   c                 C   s   t  }| d\}}|dr|dd }tj|r!t|}nd}dj|||t	d}d}g }	|
 D ]+}
td	|
}|rZ|d
}||krO|	| d}q4||v rZ|	| d}|	|
 q4|sg|	| |	d tj|d|	dd dS )zBAdd or replace an apt auth line in apt's auth.conf file or conf.d.r
   rQ   NrR   r-   z;machine {repo_path}/ login {login} password {password}{cmt})r"   loginr!   ZcmtFz$machine\s+(?P<repo_url>[.\-\w]+)/?.*r   Tr)     mode)!get_apt_auth_file_from_apt_configr   rX   r   r   r   r	   	load_filer   APT_AUTH_COMMENTrY   r   matchgroupappendr\   r   )r   rb   r!   apt_auth_file	_protocolr"   Zorig_contentZrepo_auth_lineZadded_new_authZ	new_linesr7   Zmachine_matchZmatched_repor$   r$   r%   r]     sB   
	




r]   c                    s   |  d\}}|dr|dd }t }tj|rGt|}dj|d d	 fdd	|
 D }|s=t| dS tj||d
d dS dS )z+Remove a repo from the shared apt auth filer
   rQ   NrR   zmachine {repo_path}/ login)r"   r)   c                    s   g | ]} |vr|qS r$   r$   ).0r7   Zauth_prefixr$   r%   
<listcomp>F  s    z2remove_repo_from_apt_auth_file.<locals>.<listcomp>rc   rd   )r   rX   rf   r   r   r   r	   rg   r   r   rY   unlinkr\   )r   rm   r"   rl   Zapt_authr`   r$   ro   r%   remove_repo_from_apt_auth_file<  s   

rr   c                 C   s2   t |  |rtjt|}t | t| dS )z>Remove an authenticated apt repo and credentials to the systemN)r	   Zdel_filer   r   r   r_   rr   )rK   r   rN   r$   r$   r%   remove_auth_apt_repoN  s
   

rs   filenamec                 C   s6   t j| rt| }|dd}t| | dS dS )z0Uncomment commented deb lines in the given file.z# deb zdeb N)r   r   r   r	   rg   replacer\   )rt   Zfile_contentr$   r$   r%   restore_commented_apt_list_fileY  s
   
rv   c                 C   sP   t  d }|d\}}|dr|dd }dj|||d}t | | dS )z.Add an apt preferences file and pin for a PPA.rP   r
   rQ   NrR   zHPackage: *
Pin: release o={origin}, n={series}
Pin-Priority: {priority}
)originpriorityrP   )r	   rW   r   rX   r   r\   )Zapt_preference_filer   rw   rx   rP   rm   r"   r`   r$   r$   r%   add_ppa_pinninga  s   
ry   c                  C   sV   t dddtg\} }| r| dd d S t dddtg\} }| dd dS )z7Return to patch to the system configured APT auth file.
apt-configshellkey'r+   z90ubuntu-advantagerQ   )r	   r   APT_CONFIG_AUTH_PARTS_DIRr   APT_CONFIG_AUTH_FILErstrip)r>   r?   r$   r$   r%   rf   q  s   

rf   c              
   C   s~   |  d\}}|dr|dd }d}tdddtg\}}|r(| d	d
 }|dd}tttj	
||d| S )zEList any apt files in APT_CONFIG_LISTS_DIR given repo_url and series.r
   rQ   NrR   z/var/lib/apt/listsrz   r{   r|   r}   r+   _z
_dists_{}*)r   rX   r	   r   APT_CONFIG_LISTS_DIRru   r5   globr   r   r   r   )r   rP   rm   r"   Z	lists_dirr>   r?   Zaptlist_filenamer$   r$   r%   find_apt_list_files  s   
r   c                 C   s*   t | |D ]}tj|rt| qdS )z?Remove any apt list files present for this repo_url and series.N)r   r   r   r   rq   )r   rP   r   r$   r$   r%   remove_apt_list_files  s
   
r   )_entitlementsc                 C   s   ddl m} | du rddlm}  | jD ]:}t||sq|jj|jd}|j	j|jd}t
j|r<td| t
| t
j|rMtd| t
| qdS )a  
    Clean apt files written by uaclient

    :param _entitlements:
        The uaclient.entitlements module to use, defaults to
        uaclient.entitlements. (This is only present for testing, because the
        import happens within the function to avoid circular imports.)
    r   )RepoEntitlementN)entitlements)rD   zRemoving apt source file: %sz!Removing apt preferences file: %s)Zuaclient.entitlements.repor   uaclientr   ZENTITLEMENT_CLASSES
issubclassZrepo_list_file_tmplr   rD   Zrepo_pref_file_tmplr   r   r   rZ   inforq   )r   r   Zent_clsZ	repo_fileZ	pref_filer$   r$   r%   clean_apt_files  s    	



r   c                  C   s   t g d\} }|  S )N)z
dpkg-queryz-Wz--showformat=${Package}\n)r	   r   rY   )r>   r   r$   r$   r%   get_installed_packages  s   r   
http_proxyhttps_proxyc                 C   s   | s|rt tjjdd d}| r|tj| d7 }|r$|tj|d7 }|dkr-tj| }|dkr8t	t
 dS tt
| dS )a  
    Writes an apt conf file that configures apt to use the proxies provided as
    args.
    If both args are None, then no apt conf file is written. If this function
    previously wrote a conf file, and was run again with both args as None,
    the existing file is removed.

    :param http_proxy: the url of the http proxy apt should use, or None
    :param https_proxy: the url of the https proxy apt should use, or None
    :return: None
    ZAPT)servicer-   )Z	proxy_urlN)eventr   r   ZSETTING_SERVICE_PROXYr   APT_CONFIG_PROXY_HTTPAPT_CONFIG_PROXY_HTTPSZAPT_PROXY_CONFIG_HEADERr	   Zremove_fileAPT_PROXY_CONF_FILEr\   )r   r   Zapt_proxy_configr$   r$   r%   setup_apt_proxy  s   
r   )N)NN)2r   rZ   r   r   r   r   typingr   r   r   r   r   r   r   r   r	   r   rh   r   r~   r   r   r   r_   r^   ZAPT_METHOD_HTTPS_FILEZCA_CERTIFICATES_FILEr   r   Zget_event_loggerr   r&   r   ZNamedMessager8   r@   rE   rJ   ra   r]   rr   rs   rv   ry   rf   r   r   r   r   r   r$   r$   r$   r%   <module>   s    4
/
"'


?)
