HEX
Server: Apache
System: Linux pdx1-shared-a1-38 6.6.104-grsec-jammy+ #3 SMP Tue Sep 16 00:28:11 UTC 2025 x86_64
User: mmickelson (3396398)
PHP: 8.1.31
Disabled: NONE
Upload Files
File: //usr/lib/python3/dist-packages/sos/upload/targets/__pycache__/__init__.cpython-310.pyc
o

([Qh�p�@s�ddlZddlZddlZddlmZddlmZddlmZm	Z	zddl
Z
dZWney3dZYnwzddl
Z
dZWneyGdZYnwGdd�d�ZdS)	�N)�getpass)�_sos)�
is_executable�TIMEOUT_DEFAULTTFc@seZdZdZdZdZdZdZdZdZ	dZ
dZdZdZ
dZdZdZdZdZdZdZdZdZdZdZdZdZdZdZid	d�d
d�dd�d
g�dd�dd�dd�dd�dd�dd�dd�dd�dd�dd�dd�dd�dd�ddi�Zdddd�Zdd �Zd!d"�Z e!d#d$��Z"d%d&�Z#d'd(�Z$d)d*�Z%d+d,�Z&d-d.�Z'd/d0�Z(d1d2�Z)d3d4�Z*d5d6�Z+d7d8�Z,d9d:�Z-ded;d<�Z.d=d>�Z/d?d@�Z0dAdB�Z1dCdD�Z2dEdF�Z3dGdH�Z4dIdJ�Z5dKdL�Z6dMdN�Z7dOdP�Z8dQdR�Z9dedSdT�Z:dUdV�Z;dfdXdY�Z<dZd[�Z=dfd\d]�Z>d^d_�Z?dgd`da�Z@		dhdbdc�ZAdS)i�UploadTargetz�
    This class is designed to upload files to a distribution
    defined location. These files can be either sos reports,
    sos collections, or other kind of files like: vmcores,
    application cores, logs, etc.

    z�
            Upload a file (can be an sos report, a must-gather, or others) to
             a distribution defined remote location
            zGeneric Upload�genericN�/zhttps://s3.amazonaws.com��upload_file�case_id�low_priorityF�profiles�
upload_url�upload_directory�upload_user�upload_pass�
upload_method�auto�upload_no_ssl_verify�upload_protocol�upload_s3_endpoint�upload_s3_region�upload_s3_bucket�upload_s3_access_key�upload_s3_secret_key�upload_s3_object_prefix�
upload_targetcCs"t�d�|_||_||_||_dS)N�sos_ui)�logging�	getLogger�ui_log�parser�cmdline�args)�selfr!r#r"�r%�=/usr/lib/python3/dist-packages/sos/upload/targets/__init__.py�__init__as
zUploadTarget.__init__cCsdS)aSThis should be overridden by upload targets

        This is called by sos upload on each target type that exists, and
        is meant to return True when the upload target matches a criteria
        that indicates that is the local upload target that should be used.

        Only the first upload target to determine a match is selectedFr%�r$r%r%r&�check_distributionhszUploadTarget.check_distributioncCs|jS)N)�upload_target_idr(r%r%r&�
get_target_idrszUploadTarget.get_target_idcCs|jr|jS|j��S)z-Returns the upload target's name as a string.)�upload_target_name�__name__�lower)�clsr%r%r&�nameus
zUploadTarget.namecCs*|jd|jd|jdj|jdjd�S)N�cmdlineopts�policy)r1r2rr)�hook_commonsrrr(r%r%r&�get_commons|s
�zUploadTarget.get_commonscCs
||_dS)zMSet common host data for the Upload targets
            to reference
        N)�commons)r$r5r%r%r&�set_commons�s
zUploadTarget.set_commonscCs�||_|��|_|jd}|jd}|jr|��|j|_|j|_|j|_|j|_	d|_
|j|_|j|_|j
|_
|j|_|j|_|j|_|jsy|js{|��r\|jdks\|��|��n|jdkrq|��|��|��|��|j�d�dSdSdS)Nr1r2r	�s3)r3r4r5r�_configure_low_priorityrrrr�upload_password�upload_archive_namerrrrrr�batch�quiet�get_upload_urlr�prompt_for_upload_user�prompt_for_upload_password�prompt_for_upload_s3_bucket�prompt_for_upload_s3_endpoint�prompt_for_upload_s3_access_key�prompt_for_upload_s3_secret_keyr �info)r$r3�cmdline_optsr2r%r%r&�pre_work�s>


�


�zUploadTarget.pre_workcCs8|��sd|���d|���d�}tt|��|_dSdS)zuShould be overridden by targets to determine if an access key needs
        to be provided for upload or not
        z0Please provide the upload access key for bucket � via endpoint �: N)�get_upload_s3_access_key�get_upload_s3_bucket�get_upload_s3_endpoint�input�_r�r$�msgr%r%r&rB�s����z,UploadTarget.prompt_for_upload_s3_access_keycCs4|��sd|���d|���d�}t|�|_dSdS)ztShould be overridden by targets to determine if a secret key needs
        to be provided for upload or not
        z0Please provide the upload secret key for bucket rGrHN)�get_upload_s3_secret_keyrJrKrrrNr%r%r&rC�s����z,UploadTarget.prompt_for_upload_s3_secret_keycCsL|js#|jr|j�d�r|jdd�|_|jSttd��}|�d�|_|jS)zpShould be overridden by targets to determine if a bucket needs to
        be provided for upload or not
        �s3://�Nz"Please provide the upload bucket: r)rr�
startswithrLrM�strip)r$�
user_inputr%r%r&r@�s�z(UploadTarget.prompt_for_upload_s3_bucketcCs>|j}|jsd|���d|�d�}tt|��}|p||_|jS)zsShould be overridden by targets to determine if an endpoint needs
        to be provided for upload or not
        z.Please provide the upload endpoint for bucket z (default: z): )�_upload_s3_endpointrrJrLrM)r$�default_endpointrOrUr%r%r&rA�s���
z*UploadTarget.prompt_for_upload_s3_endpointcCs.|��sd|���d�}tt|��|_dSdS)zcShould be overridden by targets to determine if a user needs to
        be provided or not
        zPlease provide upload user for rHN)�get_upload_userr=rLrMrrNr%r%r&r>�s�z#UploadTarget.prompt_for_upload_usercCs<|��s|��|jkrd|���d�}t|�|_dSdSdS)zrShould be overridden by targets to determine if a password needs to
        be provided for upload or not
        z'Please provide the upload password for rHN)�get_upload_passwordrX�_upload_userrr9rNr%r%r&r?�s���z'UploadTarget.prompt_for_upload_passwordcCsL||_|js|��|_|jstd��|��}|j�td|������|�S)a	
        Entry point for sos attempts to upload the generated archive to a
        target or user specified location.

        Currently there is support for HTTPS, SFTP, and FTP. HTTPS uploads are
        preferred for target-defined defaults.

        Targets that need to override uploading methods should override the
        respective upload_https(), upload_sftp(), and/or upload_ftp() methods
        and should NOT override this method.

        :param archive: The archive filepath to use for upload
        :type archive: ``str``

        In order to enable this for a target, that target needs to implement
        the following:

        Required Class Attrs

        :_upload_url:     The default location to use. Note these MUST include
                          protocol header
        :_upload_user:    Default username, if any else None
        :_upload_password: Default password, if any else None

        The following Class Attrs may optionally be overidden by the Target

        :_upload_directory:     Default FTP server directory, if any


        The following methods may be overridden by ``Target`` as needed

        `prompt_for_upload_user()`
            Determines if sos should prompt for a username or not.

        `get_upload_user()`
            Determines if the default or a different username should be used

        `get_upload_https_auth()`
            Format authentication data for HTTPS uploads

        `get_upload_url_string()`
            Print a more human-friendly string than vendor URLs
        zBNo upload destination provided by upload target or by --upload-urlzAttempting upload to )	r:rr=�	Exception�_determine_upload_typer rDrM�get_upload_url_string)r$�archive�upload_funcr%r%r&�upload_archive�s,
�zUploadTarget.upload_archivecCsv|j|j|j|jd�}|jdj|vr||jdjSd|jvr$td��|j�d�\}}||vr7td|����||S)aBased on the url provided, determine what type of upload to attempt.

        Note that this requires users to provide a FQDN address, such as
        https://myvendor.com/api or ftp://myvendor.com instead of
        myvendor.com/api or myvendor.com
        )�ftp�sftp�httpsr7r1z://z#Must provide protocol in upload URLz&Unsupported or unrecognized protocol: )	�
upload_ftp�upload_sftp�upload_https�	upload_s3r5rrr[�split)r$�prots�protrMr%r%r&r\3s�
z#UploadTarget._determine_upload_typecCs&|s|��}|s|��}tj�||�S)ahFormats the user/password credentials using basic auth

        :param user: The username for upload
        :type user: ``str``

        :param password: Password for `user` to use for upload
        :type password: ``str``

        :returns: The user/password auth suitable for use in requests calls
        :rtype: ``requests.auth.HTTPBasicAuth()``
        )rXrY�requests�auth�
HTTPBasicAuth)r$�user�passwordr%r%r&�get_upload_https_authIs
z"UploadTarget.get_upload_https_authcC�t�dd�p|jp|jS)z�Helper function to determine if we should use the target default
        upload access key or one provided by the user

        :returns: The access_key to use for upload
        :rtype: ``str``
        �SOSUPLOADS3ACCESSKEYN)�os�getenvr�_upload_s3_access_keyr(r%r%r&rI\�
��z%UploadTarget.get_upload_s3_access_keycCs|js|��|jS)z�Helper function to determine if we should use the target default
        upload endpoint or one provided by the user

        :returns: The S3 Endpoint to use for upload
        :rtype: ``str``
        )rrAr(r%r%r&rKgsz#UploadTarget.get_upload_s3_endpointcC�|jp|jS)z�Helper function to determine if we should use the target default
        upload region or one provided by the user

        :returns: The S3 region to use for upload
        :rtype: ``str``
        )r�_upload_s3_regionr(r%r%r&�get_upload_s3_regionr�z!UploadTarget.get_upload_s3_regioncCsb|jr$|j�d�r$|jdd��dd�}|d|_t|�dkr$|d|_|js+|��|jp0|jS)z�Helper function to determine if we should use the target default
        upload bucket or one provided by the user

        :returns: The S3 bucket to use for upload
        :rtype: ``str``
        rQrRNr�r)rrSrhr�lenrr@�_upload_s3_bucket)r$�bucket_and_prefixr%r%r&rJ{s

z!UploadTarget.get_upload_s3_bucketcCrw)z�Helper function to determine if we should use the target default
        upload object prefix or one provided by the user

        :returns: The S3 object prefix to use for upload
        :rtype: ``str``
        )r�_upload_s3_object_prefixr(r%r%r&�get_upload_s3_object_prefix�rzz(UploadTarget.get_upload_s3_object_prefixcCrq)z�Helper function to determine if we should use the target default
        upload secret key or one provided by the user

        :returns: The S3 secret key to use for upload
        :rtype: ``str``
        �SOSUPLOADS3SECRETKEYN)rsrtr�_upload_s3_secret_keyr(r%r%r&rP�rvz%UploadTarget.get_upload_s3_secret_keycCsF|js|jr|jr|jr|��}|��}d|�d|��|_|jp"|jS)z�Helper function to determine if we should use the target default
        upload url or one provided by the user

        :returns: The URL to use for upload
        :rtype: ``str``
        rQr)rrrrrJr��_upload_url)r$�bucket�prefixr%r%r&r=�s���zUploadTarget.get_upload_urlcCsd}t�|d|�}|S)Nz([^:]+://[^:]+:)([^@]+)(@.+)z\1********\3)�re�sub)r$�url�pattern�obfuscated_urlr%r%r&�_get_obfuscated_upload_url�sz'UploadTarget._get_obfuscated_upload_urlcCs|�|���S)z�Used by upload targets to potentially change the string used to
        report upload location from the URL to a more human-friendly string
        )r�r=r(r%r%r&r]�sz"UploadTarget.get_upload_url_stringcCrq)z�Helper function to determine if we should use the target default
        upload user or one provided by the user

        :returns: The username to use for upload
        :rtype: ``str``
        �
SOSUPLOADUSERN)rsrtrrZr(r%r%r&rX�rvzUploadTarget.get_upload_usercCrq)a_Helper function to determine if we should use the target default
        upload password or one provided by the user

        A user provided password, either via option or the 'SOSUPLOADPASSWORD'
        environment variable will have precendent over any target value

        :returns: The password to use for upload
        :rtype: ``str``
        �SOSUPLOADPASSWORDN)rsrtr9�_upload_passwordr(r%r%r&rY�s

��z UploadTarget.get_upload_passwordc
Cstd�std��zddl}Wnty}ztd�|�d}~wwd}|s(|��}|s.|��}|���dd�}d	|�d
|��}|j|dd�}d
dd|j	|j
g}	|j|	dd�}
|
dkr\d}nQ|
dkr�|�|�d
d|j	|j
g}|j|dd�dk}|s�|�
�td|������n'|
dkr�td|���d���|
dkr�td|������|
dkr�td|j����|s�|�
�td|������d|j�d |����}|�|�d!|j	|j
d"g}
|j|
d#d�}|dkr�|�d$�dS|dkr�td%��|dkr�td&|j����|dk�rtd'��td(|j����))a�Attempts to upload the archive to an SFTP location.

        Due to the lack of well maintained, secure, and generally widespread
        python libraries for SFTP, sos will shell-out to the system's local ssh
        installation in order to handle these uploads.

        Do not override this method with one that uses python-paramiko, as the
        upstream sos team will reject any PR that includes that dependency.
        rbzSFTP is not locally supportedrNzFSFTP upload requires python3-pexpect, which is not currently installedFzsftp://r	z sftp -oStrictHostKeyChecking=no �@zutf-8)�encodingzsftp>z	password:zConnection refused���timeoutTr{zPermission denied�
z#Incorrect username or password for �zConnection refused by z. Incorrect port?�z!Timeout hit trying to connect to �z,Unexpected error trying to connect to sftp: zUnable to connect via SFTP to zput � z100%zNo such file or directory��byezTimeout expired while uploadingzUnknown error during upload: z&Unable to write archive to destinationz!Unexpected response from server: )rr[�pexpect�ImportErrorrXrYr=�replace�spawn�TIMEOUT�EOF�expect�sendline�closer]�beforer:�_get_sftp_upload_name)r$rnror��err�sftp_connected�sftp_url�sftp_cmd�ret�sftp_expects�idx�pass_expects�put_cmd�put_expects�put_successr%r%r&re�s�����
���
����
�
�

zUploadTarget.upload_sftpcCs*|j�d�d}|jrtj�|j|�}|S)a$If a specific file name pattern is required by the SFTP server,
        override this method in the relevant Upload Target. Otherwise the
        archive's name on disk will be used

        :returns:       Filename as it will exist on the SFTP server
        :rtype:         ``str``
        r���)r:rhrrs�path�join)r$�fnamer%r%r&r�7sz"UploadTarget._get_sftp_upload_nameTcCstj|��||��|td�S)z�If upload_https() needs to use requests.put(), use this method.

        Targets should override this method instead of the base upload_https()

        :param archive:     The open archive file object
        )�datarl�verifyr�)rk�putr=rpr)r$r^r�r%r%r&�_upload_https_putDs�zUploadTarget._upload_https_putcCsiS)zJDefine any needed headers to be passed with the POST request here
        r%r(r%r%r&�_get_upload_headersOsz UploadTarget._get_upload_headerscCs:d|j�d�d||��fi}tj|��||��|td�S)z�If upload_https() needs to use requests.post(), use this method.

        Targets should override this method instead of the base upload_https()

        :param archive:     The open archive file object
        �filerr�)�filesrlr�r�)r0rhr�rk�postr=rpr)r$r^r�r�r%r%r&�_upload_https_postTs���zUploadTarget._upload_https_postcCs�tstd��t|jd��O}|jdjdkr|j}n|jdj}|jdjdu}|dkr2|�||�}n|�	||�}|j
dvrR|j
dkrFtd	��td
|j
�d|j����	Wd�d
S1s^wYdS)z�Attempts to upload the archive to an HTTPS location.

        :returns: ``True`` if upload is successful
        :rtype: ``bool``

        :raises: ``Exception`` if upload was unsuccessful
        z7Unable to upload due to missing python requests library�rbr1rFr�)����i�z/Authentication failed: invalid user credentialszPOST request returned rHNT)�REQUESTS_LOADEDr[�openr:r5r�_upload_methodrr�r��status_code�reason)r$�arc�methodr��rr%r%r&rfcs(

��$�zUploadTarget.upload_httpsc
Cs�ddl}ddl}|s|��}|durtd��|�dd�}|s"|��}|s(|��}|s0|jp/|j}z|j	|||dd�}|s@td��|�
|�Wnm|jy[}ztd	|���|�d}~w|jyo}ztd
|���|�d}~w|j
y�}z8t|���d}	|	dkr�td|�d
��|�|	dkr�td|�d
��|�|	dkr�td|���|�tdt|����|�d}~wwt|jd��}
|�d|j�d�d��|
�Wd�n1s�wY|��dS)aTAttempts to upload the archive to either the target defined or user
        provided FTP location.

        :param url: The URL to upload to
        :type url: ``str``

        :param directory: The directory on the FTP server to write to
        :type directory: ``str`` or ``None``

        :param user: The user to authenticate with
        :type user: ``str``

        :param password: The password to use for `user`
        :type password: ``str``

        :returns: ``True`` if upload is successful
        :rtype: ``bool``

        :raises: ``Exception`` if upload in unsuccessful
        rNzPno FTP server specified by upload target, use --upload-url to specify a locationzftp://r	r�r�z3connection failed, did you set a user and password?z timeout hit while connecting to zunable to connect to �503zcould not login as '�'�530zinvalid password for user '�550z"could not set upload directory to z#error trying to establish session: r�zSTOR rr�T)�ftplib�socketr=r[r�rXrYr�_upload_directory�FTP�cwdr��gaierror�
error_perm�strrhr�r:�
storbinary�quit)r$r��	directoryrnror�r��sessionr��errno�_arcfiler%r%r&rd�s`���������zUploadTarget.upload_ftpc

Cststd��|s|��}|s|��}|s|���d�}|sB|��}|dkr0|�d�r0|dd�}|dkrB|�d�sB|r@|�d�nd}|sH|�	�}|sN|�
�}tjd||||d�}z||j
�d�d}|�|j
||�Wd	Sty�}	z
td
t|	����|	�d}	~	ww)a�Attempts to upload the archive to an S3 bucket.

        :param endpoint: The S3 endpoint to upload to
        :type endpoint: str

        :param region: The S3 region to upload to
        :type region: str

        :param bucket: The name of the S3 bucket to upload to
        :type bucket: str

        :param prefix: The prefix for the S3 object/key
        :type prefix: str

        :param access_key: The access key for the S3 bucket
        :type access_key: str

        :param secret_key: The secret key for the S3 bucket
        :type secret_key: str

        :returns: True if upload is successful
        :rtype: bool

        :raises: Exception if upload is unsuccessful
        z4Unable to upload due to missing python boto3 libraryrr	r{Nr7)�endpoint_url�region_name�aws_access_key_id�aws_secret_access_keyr�TzFailed to upload to S3: )�BOTO3_LOADEDr[rKryrJrTr�rS�endswithrIrP�boto3�clientr:rhr
r�)
r$�endpoint�regionr�r��
access_key�
secret_key�	s3_client�key�er%r%r&rg�sB����zUploadTarget.upload_s3)NNN)NN)T)NNNN)NNNNNN)Br-�
__module__�__qualname__�__doc__�descr,r*�_upload_filer�r�rZr�r�rVr}rur�rxrrrr9rrrrrrr�arg_defaultsr'r)r+�classmethodr0r4r6rFrBrCr@rAr>r?r`r\rprIrKryrJr�rPr=r�r]rXrYrer�r�r�r�rfrdrgr%r%r%r&r s�
��������	�
���
������


	*

8
		
c



F�r)rsr�rr�sosrrM�
sos.utilitiesrrrkr�r�r�r�rr%r%r%r&�<module>s&
��