
    ([QhX)                         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                 J   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                  s7| j                   d   j                  | j                   d         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sftpr   z=There was a problem while setting the remote upload target:  )
r   r'   r)   upload_protocolr(   prompt_for_case_id	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X&99LL/1((( ""#K=11  	KK**+. 	s)   C1 1C1 )'C1 AC1 1	D":DD"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_tokenr9   r$   r%   get_upload_url_stringrequestsr   r6   r;   r   )r   archiverC   rA   RHELAuths        r   _upload_https_postz#RHELUploadTarget._upload_https_postd   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)r6   
startswithr3   r   s    r   rG   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)r6   rS   r3   r(   _get_obfuscated_upload_urlr)   r   s    r   rK   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_namerF   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}~Gd}~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
   )rB   rD      usernametokenz$DEBUG: auth attempt failed (status: z): zUUnable to retrieve Red Hat auth token using provided credentials. Will try anonymous.isAnonymousT)datarD   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)r(   rF   r6   r   upload_sftpREQUESTS_LOADEDr2   r9   r   rH   rI   rJ   strr$   r%   rK   r3   r;   rL   r   status_codejsonloadstextdebugerrordumpsr&   )r   re   rf   _token_userrN   r5   urlretrB   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&EF&CCKLLe   %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. 
)
rX   rY   getsize_max_size_requestr$   warningr&   r   r(   r)   )r   rM   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)r6   rS   r3   r}   r   upload_archiver2   r)   r$   ro   r&   r(   )r   rM   uploadedr5   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)NN)__name__
__module____qualname__rH   rI   upload_target_nameupload_target_idr   r3   r(   _upload_url_upload_methodr9   rz   r   r   r*   r-   r6   r;   rO   rG   rK   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
@
IMV0 r   r
   )rX   rk   sos.upload.targetsr   sos.utilitiesr   r   sos.policies.authr   sos.policies.distros.redhatr   sosr   r&   rL   rh   ImportErrorr
    r   r   <module>r      sM    
  + 8 6 2 O
j| j	  Os   < AA