
    ~ g+                         S SK r S SKrS SKJr  S SKJrJr  S SKJr  S SK	J
r
  S SKJr   S SKrSr " S	 S
\5      rg! \ a    Sr Nf = f)    N)UploadTarget)convert_bytesTIMEOUT_DEFAULT)DeviceAuthorizationClass)
RHELPolicy)_sosTFc                      ^  \ rS rSrSrSrSrSrSU 4S jjrSr	S	r
\
rS
rSrSrS rU 4S jrS rS rS rS rS rSS jrS rS rS rSU 4S jjrS rU 4S jrSrU =r$ )RHELUploadTarget   zVhttps://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                 "   > [         TU ]  XUS9  g )N)parserargscmdline)super__init__)selfr   r   r   	__class__s       ;/usr/lib/python3/dist-packages/sos/upload/targets/redhat.pyr   RHELUploadTarget.__init__!   s    7C    zhttps://api.access.redhat.comzsftp://sftp.access.redhat.composti   @c                 <    [        U R                  S   [        5      $ )z.Return true if we are running in a RHEL systempolicy)
isinstancecommonsr   r   s    r   check_distribution#RHELUploadTarget.check_distribution-   s    $,,x0*==r   c                 `   > [         TU ]  U5        U R                  S   R                  U l        g )Ncmdlineopts)r   pre_workr   upload_directory)r   hook_commonsr   s     r   r"   RHELUploadTarget.pre_work1   s'    & $] ; L Lr   c                    U R                   S   R                  (       a  U R                   S   R                  OSnU R                   S   R                  (       d_  U R                   S   R                  (       dA  U(       a  XR                   S   l        O&[	        [        S5      5      U R                   S   l        U R                   S   R                  (       a  U R                   S   R                  U l        U R                  $ )Nr!    zNOptionally, please enter the case id that you are generating this report for: )r   case_idbatchquietinput_)r   caseids     r   prompt_for_case_id#RHELUploadTarget.prompt_for_case_id7   s    LL'// m,4457 	 ||M*00]+116<]+36; 5 67]+3 <<&..<<6>>DL||r   c                    U R                   S   R                  (       a$  U R                  R                  [	        S5      5        U R                   S   R
                  (       d-  U R                  U l        U R                  R                  S5        g g )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infor,   r(   RH_SFTP_HOST
upload_urlr   s    r   prompt_for_upload_user'RHELUploadTarget.prompt_for_upload_userJ   sl    <<&22KK 5 6 ||M*22"//DOKKEF 3r   c                     U R                   S   R                  (       a%  U R                  R                  [	        S5      5        g g )Nr!   zVThe option --upload-pass has been deprecated in favour of device authorization in RHEL)r   upload_passr2   r3   r,   r   s    r   prompt_for_upload_password+RHELUploadTarget.prompt_for_upload_passwordU   s8    <<&22KK 5 6 3r   c                 @   SU R                   S   R                   S3n U R                  (       a  U R                  $ U R                   S   R                  (       a  U R                   S   R                  $ U R                   S   R                  S:X  a  U R                  $ U R                   S   R                  (       d!  U R                  5       (       d  U R                  $ U R                   U 3$ ! [         a(  nU R                  R                  SU 35         S nAN=S nAff = f)Nz/support/v1/cases/r!   z/attachmentssftpz=There was a problem while setting the remote upload target:  )
r   r(   r5   upload_protocolr4   r.   	Exceptionr2   r3   RH_API_HOST)r   rh_case_apies      r   get_upload_urlRHELUploadTarget.get_upload_url]   s   *]3;;<LJ	&||M*55||M2===||M*::fD(((<<.66&&((((( ""#K=11  	KK**+. 	s(   C+ 6C+ 3(C+ >C+ +
D5DDc                 (    SU R                    3nSU0$ )NzBearer Authorization)_device_token)r   str_auths     r   _get_upload_https_auth'RHELUploadTarget._get_upload_https_authr   s     T//01**r   c                    SUR                   R                  S5      S   UU R                  5       40n[        U R                  U R
                  5      nUR                  5       U l        U R                  R                  SU R                  5        35        [        R                  " U R                  5       UU R                  5       U[        S9$ )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 )filesheadersverifytimeout)namesplit_get_upload_headersr   client_identifier_urltoken_endpointget_access_tokenrG   r2   r3   get_upload_url_stringrequestsr   rC   rI   r   )r   archiverR   rP   RHELAuths        r   _upload_https_post#RHELUploadTarget._upload_https_postv   s     W\\'',R0'--/1
 ,**## &668J 6689; 	<}}T002%%)%@%@%B$*OE 	Er   c                 l    U R                  5       R                  U R                  5      (       a  SSS.$ 0 $ )Nfalsezno-cache)	isPrivatezcache-control)rC   
startswithr@   r   s    r   rV   $RHELUploadTarget._get_upload_headers   s2     ++D,<,<==!(:FF	r   c                     U R                  5       R                  U R                  5      (       a  gU R                  5       R                  U R                  5      (       a  gU R	                  U R
                  5      $ )NzRed Hat Customer PortalzRed Hat Secure FTP)rC   rc   r@   r4   _get_obfuscated_upload_urlr5   r   s    r   rZ   &RHELUploadTarget.get_upload_url_string   s]     ++D,<,<==, ++D,=,=>>'..t??r   c                 0   U R                   R                  S5      S   nU R                  S   R                  (       a  U R                  S   R                   SU 3nU R                  (       a*  [
        R                  R                  U R                  U5      nU$ )zwThe RH SFTP server will only automatically connect file uploads to
cases if the filename _starts_ with the case number
rM   rN   r!   r,   )upload_archive_namerU   r   r(   r#   ospathjoin)r   fnames     r   _get_sftp_upload_name&RHELUploadTarget._get_sftp_upload_name   sy     ((..s3B7<<&..||M2::;1UGDE  GGLL!6!6>Er   c                   > U R                   R                  S5      S   U R                  5       ;  a  [        TU ]  5       $ [
        (       d  [        S5      eSnSnU R                  (       d6   [        U R                  U R                  5      nUR                  5       U l        U R                  (       a,  U R                  R                  SU R                  5        35        U R                   S-   nSnU R                  (       a  U R#                  5       n	[$        R&                  " XyS	S
9nUR(                  S:X  aH  [*        R,                  " UR.                  5      S   n[*        R,                  " UR.                  5      S   nGOU R                  R1                  SUR(                   SUR+                  5        35        U R                  R3                  S5        OSS0n
[$        R&                  " U[*        R4                  " U
5      S	S9nUR(                  S:X  aS  [*        R,                  " UR.                  5      nUS   nUS   nU R                  R                  [7        SU S35      5        O9U R                  R1                  SUR(                   SUR+                  5        35        U(       a  U(       a  [        TU ]  XCS9$ [        S5      e! [         a5  nS[        U5      ;   a  U R                  R                  S5         SnAGN[SnAff = f)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.Nzend user deniedz:Device token authorization has been cancelled by the user.rO   z/support/v2/sftp/token
   )rQ   rS      usernametokenz$DEBUG: auth attempt failed (status: z): zUUnable to retrieve Red Hat auth token using provided credentials. Will try anonymous.isAnonymousT)datarS   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passwordz1Could not retrieve valid or anonymous credentials)r4   rU   rC   r   upload_sftpREQUESTS_LOADEDr?   rG   r   rW   rX   rY   strr2   r3   rZ   r@   rI   r[   r   status_codejsonloadstextdebugerrordumpsr,   )r   rx   ry   _token_userr]   rB   urlretrQ   adataanonrespr   s                r   rz   RHELUploadTarget.upload_sftp   s   
 ""4(+43F3F3HH7&(( > ? ?
 !!A3..'' &.%>%>%@"KK !!%!;!;!= >@ A !99113G--bAC#%

388,Z8CHH-g6!!:3??:K3xxzl$ !!7
 #D)E==4::e+<bID3&zz$)),Z(g  eW %M N O
 !!?''(DIIK=: V7&E&CCKLLe   %A.KK$$:	s   , K 
L*K<<Lc                     [         R                  R                  U5      nX R                  :  aM  U R                  R                  [        S[        U R                  5       S35      5        U R                  U l	        g g )NzLSize of archive is bigger than Red Hat Customer Portal limit for uploads of z  via sos http upload. 
)
rj   rk   getsize_max_size_requestr2   warningr,   r   r4   r5   )r   r\   sizes      r   check_file_too_big#RHELUploadTarget.check_file_too_big   sp    www'
 )))KK *"4#9#9:; <-- . #//DO *r   c           	        >  U R                  5       R                  U R                  5      (       a  U R                  U5        [        TU ]  U5      nU$ ! [         a  nSnU R                  R                  U R                  5      (       d  e U R                  R                  [        SU SU R                   35      5        U R                  U l        [        TU ]  U5      n SnAU$ SnAff = f)zoOverride 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)rC   rc   r@   r   r   upload_archiver?   r5   r2   r   r,   r4   )r   r\   uploadedrB   r   s       r   r   RHELUploadTarget.upload_archive  s    	7""$//0@0@AA''0w-g6H   		7H??--d.>.>??KKDCy!2!2 35 6 #//DOw-g6H		7s   AA 
C)BC$$C))rG   r(   r#   r5   )NNN)T)NN)__name__
__module____qualname____firstlineno__rW   rX   upload_target_nameupload_target_idr   r@   r4   _upload_url_upload_methodrG   r   r   r"   r.   r6   r:   rC   rI   r^   rV   rZ   rn   rz   r   r   __static_attributes____classcell__)r   s   @r   r
   r
      s    E8N0D 2K2LKNM">M&	G2*+E2
@
IMV0 r   r
   )rj   r~   sos.upload.targetsr   sos.utilitiesr   r   sos.policies.authr   sos.policies.distros.redhatr   sosr   r,   r[   r{   ImportErrorr
    r   r   <module>r      sM    
  + 8 6 2 O
|| |	  Os   < AA