a D=]i(@sddlZddlZddlmZddlmZmZddlmZddl m Z ddl m Z zddlZdZWneyvdZYn0Gd d d eZdS) N) UploadTarget) convert_bytesTIMEOUT_DEFAULT)DeviceAuthorizationClass) RHELPolicy)_sosTFcseZdZdZdZdZdZd'fdd ZdZd Z e Z d Z dZ d Z d d ZfddZddZddZddZddZd(ddZddZddZdd Zd)fd!d" Zd#d$Zfd%d&ZZS)*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 TargetZredhatNcstj|||ddS)N)parserargscmdline)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.composti@cCst|jdtS)z.Return true if we are running in a RHEL systemZpolicy) isinstancecommonsrrrrrcheck_distribution-sz#RHELUploadTarget.check_distributioncst||jdj|_dS)N cmdlineopts)r pre_workrupload_directory)rZ hook_commonsrrrr1s zRHELUploadTarget.pre_workcCs@|jdjr|jtd|jdjs<|j|_|jddS)NrzVThe option --upload-user has been deprecated in favour of device authorization in RHELz&No case id provided, uploading to SFTP)rZ upload_userui_loginfo_case_id RH_SFTP_HOST upload_urlrrrrprompt_for_upload_user7s  z'RHELUploadTarget.prompt_for_upload_usercCs |jdjr|jtddS)NrzVThe option --upload-pass has been deprecated in favour of device authorization in RHEL)rZ upload_passrrrrrrrprompt_for_upload_passwordBs z+RHELUploadTarget.prompt_for_upload_passwordc Csd|jdjd}zX|jr$|jWS|jdjr>|jdjWS|jdjdkrV|jWS|jdjsj|jWSWn6ty}z|jd|WYd}~n d}~00|j|S)Nz/support/v1/cases/rz /attachmentssftpz=There was a problem while setting the remote upload target: ) rrr Zupload_protocolr Exceptionrr RH_API_HOST)rZ rh_case_apierrrget_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|jdd||fi}t|j|j}||_|j d| t j | |||tdS)zIf 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 )filesheadersverifytimeout)namesplit_get_upload_headersrclient_identifier_urltoken_endpointget_access_tokenr(rrget_upload_url_stringrequestsrr'r)r)rarchiver0r.RHELAuthrrr_upload_https_postbs   z#RHELUploadTarget._upload_https_postcCs||jrdddSiS)NZfalsezno-cache)Z isPrivatez cache-control)r' startswithr%rrrrr4{s z$RHELUploadTarget._get_upload_headerscCs4||jrdS||jr(dS||jS)NzRed Hat Customer PortalzRed Hat Secure FTP)r'r=r%rZ_get_obfuscated_upload_urlr rrrrr8s z&RHELUploadTarget.get_upload_url_stringcCsL|jdd}|jdjr2|jdjd|}|jrHtj|j|}|S)zThe RH SFTP server will only automatically connect file uploads to cases if the filename _starts_ with the case number r+r,rr)Zupload_archive_namer3rrrospathjoin)rfnamerrr_get_sftp_upload_names  z&RHELUploadTarget._get_sftp_upload_namec s|jdd|vr"tSts.tdd}d}|jszt|j |j }Wn<ty}z$dt |vrv|j dWYd}~nd}~00||_|jr|j d||jd}d}|jr:|} tj|| d d }|jd krt|jd }t|jd }n*|j d|jd||j dn~ddi} tj|t| d d} | jd krt| j} | d }| d }|j td|dn|j d| jd| |r|rtj||dStddS)zOverride 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/r1usernametokenz$DEBUG: auth attempt failed (status: z): zUUnable to retrieve Red Hat auth token using provided credentials. Will try anonymous.Z isAnonymousT)datar1zUser z^ used for anonymous upload. Please inform your support engineer so they may retrieve the data.z)DEBUG: anonymous request failed (status: )userpasswordz1Could not retrieve valid or anonymous credentials)rr3r'r upload_sftpREQUESTS_LOADEDr$r(rr5r6strrrr7r8r%r)r9rZ status_codejsonloadstextdebugerrordumpsr) rrIrJZ_token_userr;r&urlretr/ZadataZanonZresprrrrKsr         zRHELUploadTarget.upload_sftpcCs@tj|}||jkr<|jtdt|jd|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:sizerrrcheck_file_too_bigs  z#RHELUploadTarget.check_file_too_bigc sz*||jr||t|}Wnjty}zRd}|j|jsN|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 s