HEX
Server: Apache
System: Linux 185.122.168.184.host.secureserver.net 5.14.0-570.60.1.el9_6.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Nov 5 05:00:59 EST 2025 x86_64
User: barbeatleanalyti (1024)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: //lib/python3.9/site-packages/sos/upload/targets/__pycache__/redhat.cpython-39.pyc
a

�:�h�(�@s�ddlZddlZddlmZddlmZmZddlmZddl	m
Z
ddlmZ
zddlZdZWneyvdZYn0Gd	d
�d
e�ZdS)�N)�UploadTarget)�
convert_bytes�TIMEOUT_DEFAULT)�DeviceAuthorizationClass)�
RHELPolicy)�_sosTFcs�eZdZdZdZdZdZd'�fdd�	ZdZd	Z	e	Z
d
ZdZdZ
dd
�Z�fdd�Zdd�Zdd�Zdd�Zdd�Zd(dd�Zdd�Zdd�Zdd �Zd)�fd!d"�	Zd#d$�Z�fd%d&�Z�ZS)*�RHELUploadTargetzVhttps://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/auth/devicezPhttps://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/tokenzRed Hat Upload TargetZredhatNcst�j|||d�dS)N)�parser�args�cmdline)�super�__init__)�selfr	r
r��	__class__��=/usr/lib/python3.9/site-packages/sos/upload/targets/redhat.pyr
!szRHELUploadTarget.__init__zhttps://api.access.redhat.comzsftp://sftp.access.redhat.com�posti@cCst|jdt�S)z.Return true if we are running in a RHEL systemZpolicy)�
isinstance�commonsr�rrrr�check_distribution-sz#RHELUploadTarget.check_distributioncst��|�|jdj|_dS)N�cmdlineopts)r�pre_workr�upload_directory)rZhook_commonsrrrr1szRHELUploadTarget.pre_workcCs@|jdjr|j�td��|jdjs<|j|_|j�d�dS)NrzVThe option --upload-user has been deprecated in favour of device authorization in RHELz&No case id provided, uploading to SFTP)rZupload_user�ui_log�info�_�case_id�RH_SFTP_HOST�
upload_urlrrrr�prompt_for_upload_user7s�z'RHELUploadTarget.prompt_for_upload_usercCs |jdjr|j�td��dS)NrzVThe option --upload-pass has been deprecated in favour of device authorization in RHEL)rZupload_passrrrrrrr�prompt_for_upload_passwordBs�z+RHELUploadTarget.prompt_for_upload_passwordc
Cs�d|jdj�d�}zX|jr$|jWS|jdjr>|jdjWS|jdjdkrV|jWS|jdjsj|jWSWn6ty�}z|j�d|���WYd}~n
d}~00|j�|��S)Nz/support/v1/cases/rz/attachments�sftpz=There was a problem while setting the remote upload target:  )	rrr Zupload_protocolr�	Exceptionrr�RH_API_HOST)rZrh_case_api�errr�get_upload_urlJs&
���zRHELUploadTarget.get_upload_urlcCsd|j��}d|iS)NzBearer Z
Authorization)�
_device_token)rZstr_authrrr�_get_upload_https_auth^sz'RHELUploadTarget._get_upload_https_authTcCshd|j�d�d||��fi}t|j|j�}|��|_|j�	d|�
����tj|�
�||��|td�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_tokenr(rr�get_upload_url_string�requestsrr'r)r)r�archiver0r.�RHELAuthrrr�_upload_https_postbs ���
��z#RHELUploadTarget._upload_https_postcCs|���|j�rddd�SiS)NZfalsezno-cache)Z	isPrivatez
cache-control)r'�
startswithr%rrrrr4{s
z$RHELUploadTarget._get_upload_headerscCs4|���|j�rdS|���|j�r(dS|�|j�S)NzRed Hat Customer PortalzRed Hat Secure FTP)r'r=r%rZ_get_obfuscated_upload_urlr rrrrr8�s
z&RHELUploadTarget.get_upload_url_stringcCsL|j�d�d}|jdjr2|jdj�d|��}|jrHtj�|j|�}|S)z�The RH SFTP server will only automatically connect file uploads to
        cases if the filename _starts_ with the case number
        r+r,rr)Zupload_archive_namer3rrr�os�path�join)r�fnamerrr�_get_sftp_upload_name�sz&RHELUploadTarget._get_sftp_upload_namec

s�|j�d�d|��vr"t���Sts.td��d}d}|js�zt|j	|j
�}Wn<ty�}z$dt|�vrv|j�
d�WYd}~nd}~00|��|_|jr�|j�
d|�����|jd}d}|j�r:|��}	tj||	d	d
�}|jdk�rt�|j�d}t�|j�d
}n*|j�d|j�d|�����|j�d�n~ddi}
tj|t�|
�d	d�}|jdk�r�t�|j�}|d}|d
}|j�
td|�d���n|j�d|j�d|�����|�r�|�r�t�j||d�Std��dS)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.Nzend user deniedz:Device token authorization has been cancelled by the user.r-z/support/v2/sftp/token�
)r/r1���username�tokenz$DEBUG: auth attempt failed (status: z): zUUnable to retrieve Red Hat auth token using provided credentials. Will try anonymous.ZisAnonymousT)�datar1zUser z^ used for anonymous upload. Please inform your support engineer so they may retrieve the data.z)DEBUG: anonymous request failed (status: )�user�passwordz1Could not retrieve valid or anonymous credentials)rr3r'r�upload_sftp�REQUESTS_LOADEDr$r(rr5r6�strrrr7r8r%r)r9rZstatus_code�json�loads�text�debug�error�dumpsr)
rrIrJZ_token�_userr;r&�url�retr/ZadataZanonZresprrrrK�sr
��
�

�������zRHELUploadTarget.upload_sftpcCs@tj�|�}||jkr<|j�tdt|j��d���|j|_	dS)NzLSize of archive is bigger than Red Hat Customer Portal limit for uploads of z  via sos http upload. 
)
r>r?�getsize�_max_size_requestrZwarningrrrr )rr:�sizerrr�check_file_too_big�s
��z#RHELUploadTarget.check_file_too_bigc
s�z*|���|j�r|�|�t��|�}Wnjty�}zRd}|j�|j�sN�|j�	t
d|�d|j����|j|_t��|�}WYd}~n
d}~00|S)zOverride 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'r=r%rZr�upload_archiver$r rrRrr)rr:Zuploadedr&rrrr[�s$
���"zRHELUploadTarget.upload_archive)NNN)T)NN)�__name__�
__module__�__qualname__r5r6Zupload_target_nameZupload_target_idr
r%rZ_upload_urlZ_upload_methodr(rXrrr!r"r'r)r<r4r8rBrKrZr[�
__classcell__rrrrrs0

Kr)r>rNZsos.upload.targetsrZ
sos.utilitiesrrZsos.policies.authrZsos.policies.distros.redhatrZsosrrr9rL�ImportErrorrrrrr�<module>	s