3 'j( @sddlZddlZddlmZddlmZmZddlmZddl m Z ddl m Z yddlZdZWnek rxdZYnXGd 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.6/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) isinstancecommonsr)rrrrcheck_distribution-sz#RHELUploadTarget.check_distributioncstj||jdj|_dS)N cmdlineopts)r pre_workrupload_directory)rZ hook_commons)rrrr1s zRHELUploadTarget.pre_workcCs@|jdjr|jjtd|jdjs<|j|_|jjddS)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_url)rrrrprompt_for_upload_user7s    z'RHELUploadTarget.prompt_for_upload_usercCs |jdjr|jjtddS)NrzVThe option --upload-pass has been deprecated in favour of device authorization in RHEL)rZ upload_passrrr)rrrrprompt_for_upload_passwordBs z+RHELUploadTarget.prompt_for_upload_passwordcCsd|jdjd}yP|jr"|jS|jdjr:|jdjS|jdjdkrP|jS|jdjsb|jSWn4tk r}z|jjd|WYdd}~XnX|j|S)Nz/support/v1/cases/rz /attachmentsZsftpz=There was a problem while setting the remote upload target: ) rrrZupload_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|jjdd||jfi}t|j|j}|j|_|jj d|j t j |j ||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/z/Device authorized correctly. Uploading file to )filesheadersverifytimeout)namesplit_get_upload_headersrclient_identifier_urltoken_endpointget_access_tokenr%rrget_upload_url_stringrequestsrr$r&r)rarchiver,r*RHELAuthrrr_upload_https_postbs   z#RHELUploadTarget._upload_https_postcCs|jj|jrdddSiS)NZfalsezno-cache)Z isPrivatez cache-control)r$ startswithr")rrrrr1{s z$RHELUploadTarget._get_upload_headerscCs4|jj|jrdS|jj|jr(dS|j|jS)NzRed Hat Customer PortalzRed Hat Secure FTP)r$r:r"rZ_get_obfuscated_upload_urlr)rrrrr5s z&RHELUploadTarget.get_upload_url_stringcCsL|jjdd}|jdjr2|jdjd|}|jrHtjj|j|}|S)zThe RH SFTP server will only automatically connect file uploads to cases if the filename _starts_ with the case number r(r)rrr.)Zupload_archive_namer0rrrospathjoin)rZfnamerrr_get_sftp_upload_names  z&RHELUploadTarget._get_sftp_upload_namec s|jjdd|jkr"tjSts.tdd}d}|jsyt|j |j }Wn:tk r}zdt |krx|j j dWYdd}~Xn X|j|_|jr|j j d|j|jd}d}|jr8|j} tj|| d d }|jd kr tj|jd }tj|jd }n*|j jd|jd|j|j jdn~ddi} tj|tj| d d} | jd krtj| j} | d }| d }|j j td|dn|j jd| jd| j|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//r)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.z/Device authorized correctly. Uploading file to z/support/v2/sftp/token )r+r-Zusernametokenz$DEBUG: auth attempt failed (status: z): zUUnable to retrieve Red Hat auth token using provided credentials. Will try anonymous.Z isAnonymousT)datar-zUser 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)rr0r$r upload_sftpREQUESTS_LOADEDr!r%rr2r3strrrr4r5r"r&r6rZ status_codejsonloadstextdebugerrordumpsr) rrCrDZ_tokenZ_userr8r#Zurlretr+ZadataZanonZresp)rrrrEsV         zRHELUploadTarget.upload_sftpcCs@tjj|}||jkr<|jjtdt|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)rr7sizerrrcheck_file_too_bigs   z#RHELUploadTarget.check_file_too_bigcsy*|jj|jr|j|tj|}Wnhtk r}zLd}|jj|jsP|jj t d|d|j |j |_tj|}WYdd}~XnX|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"rRr upload_archiver!rrrLrr)rr7Zuploadedr#)rrrrSs zRHELUploadTarget.upload_archive)NNN)T)NN)__name__ __module__ __qualname__r2r3Zupload_target_nameZupload_target_idr r"rZ _upload_urlZ_upload_methodr%rPrrrr r$r&r9r1r5r>rErRrS __classcell__rr)rrrs0    Kr)r;rHZsos.upload.targetsrZ sos.utilitiesrrZsos.policies.authrZsos.policies.distros.redhatrZsosrrr6rF ImportErrorrrrrr s