o
    Û1jt)  ã                   @   sŒ   d dl Z d dlZd dlmZ d dlmZmZ d dlmZ d dl	m
Z
 d dlmZ zd dlZdZW n ey;   dZY nw G d	d
„ d
eƒZdS )é    N)ÚUploadTarget)Úconvert_bytesÚTIMEOUT_DEFAULT)ÚDeviceAuthorizationClass)Ú
RHELPolicy)Ú_sosTFc                       s¾   e Zd ZdZdZdZdZd'‡ fdd„	ZdZd	Z	e	Z
d
ZdZdZdd„ Z‡ fdd„Zdd„ Zdd„ Zdd„ Zdd„ Zd(dd„Zdd„ Zdd„ Zdd „ Zd'‡ fd!d"„	Zd#d$„ Z‡ fd%d&„Z‡  ZS ))ÚRHELUploadTargetzVhttps://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/auth/devicezPhttps://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/tokenzRed Hat Upload TargetÚredhatNc                    s   t ƒ j|||d� d S )N)ÚparserÚargsÚcmdline)ÚsuperÚ__init__)Úselfr
   r   r   ©Ú	__class__© ú;/usr/lib/python3/dist-packages/sos/upload/targets/redhat.pyr   !   s   zRHELUploadTarget.__init__zhttps://api.access.redhat.comzsftp://sftp.access.redhat.comÚposti   @c                 C   s   t | jd tƒS )z.Return true if we are running in a RHEL systemÚpolicy)Ú
isinstanceÚcommonsr   ©r   r   r   r   Úcheck_distribution-   s   z#RHELUploadTarget.check_distributionc                    s   t ƒ  |¡ | jd j| _d S )NÚcmdlineopts)r   Úpre_workr   Úupload_directory)r   Úhook_commonsr   r   r   r   1   s   zRHELUploadTarget.pre_workc                 C   sD   | j d jr| j tdƒ¡ | j d js | j| _| j d¡ d S d S )Nr   zVThe option --upload-user has been deprecated in favour of device authorization in RHELz&No case id provided, uploading to SFTP)r   Úupload_userÚui_logÚinfoÚ_Úcase_idÚRH_SFTP_HOSTÚ
upload_urlr   r   r   r   Úprompt_for_upload_user7   s   ÿýz'RHELUploadTarget.prompt_for_upload_userc                 C   s$   | j d jr| j tdƒ¡ d S d S )Nr   zVThe option --upload-pass has been deprecated in favour of device authorization in RHEL)r   Úupload_passr   r    r!   r   r   r   r   Úprompt_for_upload_passwordB   s
   ÿÿz+RHELUploadTarget.prompt_for_upload_passwordc              
   C   s²   d| j d j› d�}z,| jr| jW S | j d jr| j d jW S | j d jdkr+| jW S | j d js5| jW S W n tyQ } z| j d|› �¡ W Y d }~nd }~ww | j› |› �S )Nz/support/v1/cases/r   z/attachmentsÚsftpz=There was a problem while setting the remote upload target:  )	r   r"   r$   Úupload_protocolr#   Ú	Exceptionr   r    ÚRH_API_HOST)r   Úrh_case_apiÚer   r   r   Úget_upload_urlJ   s,   
ÿÿÿÿ€ÿzRHELUploadTarget.get_upload_urlc                 C   s   d| j › �}d|iS )NzBearer ÚAuthorization)Ú_device_token)r   Ústr_authr   r   r   Ú_get_upload_https_auth^   s   z'RHELUploadTarget._get_upload_https_authTc                 C   sh   d|j  d¡d ||  ¡ fi}t| j| jƒ}| ¡ | _| j 	d|  
¡ › �¡ tj|  ¡ ||  ¡ |td�S )zÔIf upload_https() needs to use requests.post(), use this method.

        Policies should override this method instead of the base upload_https()

        :param archive:     The open archive file object
        Úfileú/éÿÿÿÿú/Device authorized correctly. Uploading file to )ÚfilesÚheadersÚverifyÚtimeout)ÚnameÚsplitÚ_get_upload_headersr   Úclient_identifier_urlÚtoken_endpointÚget_access_tokenr0   r   r    Úget_upload_url_stringÚrequestsr   r.   r2   r   )r   Úarchiver9   r7   ÚRHELAuthr   r   r   Ú_upload_https_postb   s    ÿÿþ
ÿþz#RHELUploadTarget._upload_https_postc                 C   s   |   ¡  | j¡rdddœS i S )NÚfalsezno-cache)Ú	isPrivatezcache-control)r.   Ú
startswithr+   r   r   r   r   r=   {   s   
z$RHELUploadTarget._get_upload_headersc                 C   s4   |   ¡  | j¡r
dS |   ¡  | j¡rdS |  | j¡S )NzRed Hat Customer PortalzRed Hat Secure FTP)r.   rH   r+   r#   Ú_get_obfuscated_upload_urlr$   r   r   r   r   rA   €   s
   z&RHELUploadTarget.get_upload_url_stringc                 C   sL   | j  d¡d }| jd jr| jd j› d|› �}| jr$tj | j|¡}|S )z‡The RH SFTP server will only automatically connect file uploads to
        cases if the filename _starts_ with the case number
        r4   r5   r   r!   )Úupload_archive_namer<   r   r"   r   ÚosÚpathÚjoin)r   Úfnamer   r   r   Ú_get_sftp_upload_name‡   s   z&RHELUploadTarget._get_sftp_upload_namec              
      sâ  | j  d¡d |  ¡ vrtƒ  ¡ S tstdƒ‚d}d}d}| jsMz	t| j	| j
ƒ}W n tyG } zdt|ƒv r=| j d¡ W Y d}~n
d}~ww | ¡ | _| jr[| j d|  ¡ › �¡ | jd }	d}
| jr¢|  ¡ }tj|	|d	d
�}
|
jdkrŒt |
j¡d }t |
j¡d }d|› �}nT| j d|
j› d|
 ¡ › �¡ | j d¡ n>ddi}tj|	t |¡d	d�}|jdkrÑt |j¡}|d }|d }| j td|› d�ƒ¡ n| j d|j› d| ¡ › �¡ |rí|rítƒ j|||d�S tdƒ‚)z²Override the base upload_sftp to allow for setting an on-demand
        generated anonymous login for the RH SFTP server if a username and
        password are not given
        z//é   zPpython3-requests is not installed and is required for obtaining SFTP auth token.Nzend user deniedz:Device token authorization has been cancelled by the user.r6   z/support/v2/sftp/tokené
   )r8   r:   éÈ   ÚusernameÚtokenz/users/z$DEBUG: auth attempt failed (status: z): zUUnable to retrieve Red Hat auth token using provided credentials. Will try anonymous.ÚisAnonymousT)Údatar:   zUser z^ used for anonymous upload. Please inform your support engineer so they may retrieve the data.z)DEBUG: anonymous request failed (status: )ÚuserÚpasswordÚuser_dirz1Could not retrieve valid or anonymous credentials)r#   r<   r.   r   Úupload_sftpÚREQUESTS_LOADEDr*   r0   r   r>   r?   Ústrr   r    r@   rA   r+   r2   rB   r   Ústatus_codeÚjsonÚloadsÚtextÚdebugÚerrorÚdumpsr!   )r   rW   rX   rY   Ú_tokenÚ_userÚ	_user_dirrD   r-   ÚurlÚretr8   ÚadataÚanonÚrespr   r   r   rZ   ”   s~   
þÿ€ü
	ÿ


ÿÿÿ
ÿÿÿÿ
ÿzRHELUploadTarget.upload_sftpc                 C   sD   t j |¡}|| jkr | j tdt| jƒ› d�ƒ¡ | j| _	d S d S )NzLSize of archive is bigger than Red Hat Customer Portal limit for uploads of z  via sos http upload. 
)
rK   rL   ÚgetsizeÚ_max_size_requestr   Úwarningr!   r   r#   r$   )r   rC   Úsizer   r   r   Úcheck_file_too_bigâ   s   
þÿùz#RHELUploadTarget.check_file_too_bigc              
      sš   z|   ¡  | j¡r|  |¡ tƒ  |¡}W |S  tyL } z*d}| j | j¡s(‚ | j 	t
d|› d| j› �ƒ¡ | j| _tƒ  |¡}W Y d}~|S d}~ww )zOverride the base upload_archive to provide for automatic failover
        from RHCP failures to the public RH dropbox
        Fz0Upload to Red Hat Customer Portal failed due to z	. Trying N)r.   rH   r+   rp   r   Úupload_archiver*   r$   r   rb   r!   r#   )r   rC   Úuploadedr-   r   r   r   rq   ñ   s*   
öÿÿÿ€özRHELUploadTarget.upload_archive)NNN)T)Ú__name__Ú
__module__Ú__qualname__r>   r?   Úupload_target_nameÚupload_target_idr   r+   r#   Ú_upload_urlÚ_upload_methodr0   rm   r   r   r%   r'   r.   r2   rE   r=   rA   rO   rZ   rp   rq   Ú__classcell__r   r   r   r   r      s2    
Nr   )rK   r^   Úsos.upload.targetsr   Úsos.utilitiesr   r   Úsos.policies.authr   Úsos.policies.distros.redhatr   Úsosr   r!   rB   r[   ÚImportErrorr   r   r   r   r   Ú<module>   s   ÿ