
    G%@i))                         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 	 d dlZdZ G d	 d
e      Zy# e$ r dZY w xY w)    N)UploadTarget)convert_bytesTIMEOUT_DEFAULT)DeviceAuthorizationClass)
RHELPolicy)_sosTFc                        e Zd ZdZdZdZdZd fd	ZdZdZ	e	Z
d	ZdZd
Zd Z fdZd Zd Zd Zd ZddZd Zd Zd Zd fd	Zd Z fdZ x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                 *    t         |   |||       y )N)parserargscmdline)super__init__)selfr   r   r   	__class__s       ;/usr/lib/python3/dist-packages/sos/upload/targets/redhat.pyr   zRHELUploadTarget.__init__!   s    T7C    zhttps://api.access.redhat.comzsftp://sftp.access.redhat.composti   @c                 <    t        | j                  d   t              S )z.Return true if we are running in a RHEL systempolicy)
isinstancecommonsr   r   s    r   check_distributionz#RHELUploadTarget.check_distribution-   s    $,,x0*==r   c                 `    t         |   |       | j                  d   j                  | _        y )Ncmdlineopts)r   pre_workr   upload_directory)r   hook_commonsr   s     r   r   zRHELUploadTarget.pre_work1   s'    & $] ; L Lr   c                 
   | j                   d   j                  r$| j                  j                  t	        d             | j                   d   j
                  s-| j                  | _        | j                  j                  d       y y )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   s    r   prompt_for_upload_userz'RHELUploadTarget.prompt_for_upload_user7   sl    <<&22KK 5 6 ||M*22"//DOKKEF 3r   c                     | j                   d   j                  r%| j                  j                  t	        d             y y )Nr   zVThe option --upload-pass has been deprecated in favour of device authorization in RHEL)r   upload_passr$   r%   r&   r   s    r   prompt_for_upload_passwordz+RHELUploadTarget.prompt_for_upload_passwordB   s8    <<&22KK 5 6 3r   c                    d| j                   d   j                   d}	 | j                  r| j                  S | j                   d   j                  r| j                   d   j                  S | j                   d   j                  dk(  r| j                  S | j                   d   j                  s| j                  S 	 | j                   | S # t
        $ r(}| j                  j                  d|        Y d }~<d }~ww xY w)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es      r   get_upload_urlzRHELUploadTarget.get_upload_urlJ   s    *]3;;<LJ	&||M*55||M2===||M*::fD(((<<.66((( 7 ""#K=11  	KK**+. 	s(   C 1C )'C $C 	C8C33C8c                 (    d| j                    }d|iS )NzBearer Authorization)_device_token)r   str_auths     r   _get_upload_https_authz'RHELUploadTarget._get_upload_https_auth^   s     T//01**r   c                    d|j                   j                  d      d   || j                         fi}t        | j                  | j
                        }|j                         | _        | j                  j                  d| j                                 t        j                  | j                         || j                         |t              S )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_tokenr8   r$   r%   get_upload_url_stringrequestsr   r5   r:   r   )r   archiverB   r@   RHELAuths        r   _upload_https_postz#RHELUploadTarget._upload_https_postb   s     W\\'',R0'--/1
 ,**## &668J 6689; 	<}}T002%%)%@%@%B$*OE 	Er   c                 b    | j                         j                  | j                        rdddS i S )Nfalsezno-cache)	isPrivatezcache-control)r5   
startswithr2   r   s    r   rF   z$RHELUploadTarget._get_upload_headers{   s/     ++D,<,<=!(:FF	r   c                     | j                         j                  | j                        ry| j                         j                  | j                        ry| j	                  | j
                        S )NzRed Hat Customer PortalzRed Hat Secure FTP)r5   rR   r2   r(   _get_obfuscated_upload_urlr)   r   s    r   rJ   z&RHELUploadTarget.get_upload_url_string   sW     ++D,<,<=, ++D,=,=>'..t??r   c                    | j                   j                  d      d   }| j                  d   j                  r| j                  d   j                   d| }| j                  r*t
        j                  j                  | j                  |      }|S )zThe RH SFTP server will only automatically connect file uploads to
        cases if the filename _starts_ with the case number
        r=   r>   r   r&   )upload_archive_namerE   r   r'   r    ospathjoin)r   fnames     r   _get_sftp_upload_namez&RHELUploadTarget._get_sftp_upload_name   sy     ((..s3B7<<&..||M2::;1UGDE  GGLL!6!6>Er   c                    | j                   j                  d      d   | j                         vrt        |          S t
        st        d      d}d}| j                  s6	 t        | j                  | j                        }|j                         | _        | j                  r,| j                  j                  d| j                                 | j                   dz   }d}	| j                  r| j#                         }
t%        j&                  ||
d	
      }	|	j(                  dk(  rFt+        j,                  |	j.                        d   }t+        j,                  |	j.                        d   }n| j                  j1                  d|	j(                   d|	j+                                 | j                  j3                  d       nddi}t%        j&                  |t+        j4                  |      d	      }|j(                  dk(  rRt+        j,                  |j.                        }|d   }|d   }| j                  j                  t7        d| d             n9| j                  j1                  d|j(                   d|j+                                 |r|rt        |   |||      S t        d      # t        $ r3}dt        |      v r| j                  j                  d       Y d}~Hd}~ww xY w)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.r?   z/support/v2/sftp/token
   )rA   rC      usernametokenz$DEBUG: auth attempt failed (status: z): zUUnable to retrieve Red Hat auth token using provided credentials. Will try anonymous.isAnonymousT)datarC   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(   rE   r5   r   upload_sftpREQUESTS_LOADEDr1   r8   r   rG   rH   rI   strr$   r%   rJ   r2   r:   rK   r   status_codejsonloadstextdebugerrordumpsr&   )r   rd   re   rf   _token_userrM   r4   urlretrA   adataanonrespr   s                 r   rg   zRHELUploadTarget.upload_sftp   s   
 ""4(+43F3F3HH7&(( > ? ?
 !!A3..'' &.%>%>%@"KK !!%!;!;!= >@ A !99113G--Wb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&EF05 ' 7 7KLLg   %A.KK$$:	s     J& &	K"/(KK"c                     t         j                  j                  |      }|| j                  k\  rM| j                  j                  t        dt        | j                         d             | j                  | _	        y y )NzLSize of archive is bigger than Red Hat Customer Portal limit for uploads of z  via sos http upload. 
)
rW   rX   getsize_max_size_requestr$   warningr&   r   r(   r)   )r   rL   sizes      r   check_file_too_bigz#RHELUploadTarget.check_file_too_big   sr    www'
 4)))KK *"4#9#9:; <-- . #//DO *r   c           	         	 | j                         j                  | j                        r| j                  |       t        |   |      }|S # t        $ r}d}| j                  j                  | j                        s | j                  j                  t        d| d| j                                | j                  | _        t        |   |      }Y d}~|S d}~ww xY 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)r5   rR   r2   r}   r   upload_archiver1   r)   r$   ro   r&   r(   )r   rL   uploadedr4   r   s       r   r   zRHELUploadTarget.upload_archive   s    	7""$//0@0@A''0w-g6H   		7H??--d.>.>?KKDCy!2!2 35 6 #//DOw-g6H		7s   A	A 	CA<CC)NNN)T)__name__
__module____qualname__rG   rH   upload_target_nameupload_target_idr   r2   r(   _upload_url_upload_methodr8   rz   r   r   r*   r-   r5   r:   rN   rF   rJ   r[   rg   r}   r   __classcell__)r   s   @r   r
   r
      s    E8N0D 2K2LKNM">M	G2(+E2
@
JMX0 r   r
   )rW   rk   sos.upload.targetsr   sos.utilitiesr   r   sos.policies.authr   sos.policies.distros.redhatr   sosr   r&   rK   rh   ImportErrorr
    r   r   <module>r      sM    
  + 8 6 2 O
i| i	  Os   < AA