%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python2.7/site-packages/salt/utils/
Upload File :
Create Path :
Current File : //lib/python2.7/site-packages/salt/utils/aws.pyo

�
���^c@@sJdZddlmZmZmZddlZddlZddlZddlmZddl	Z	ddl
Z
ddlZddlZ
ddlZddlZddlmZddlZ
ddljjZddlmZyddlZeZWnek
reZnXddlm Z m!Z!m"Z"ddl#m$Z$m%Z%ej&e'�Z(d	Z)d
Z*ddd
dddgZ+dZ,dZ-dZ.da/da0da1da2da3iZ4d�Z5d�Z6d�Z7dd�Z9e*ddddddddd�	Z:d�Z;d�Z<ddddeedddddd�Z=d �Z>ddd!�Z?dS("u�
Connection library for AWS

.. versionadded:: 2015.5.0

This is a base library used by a number of AWS services.

:depends: requests
i(tabsolute_importtprint_functiontunicode_literalsN(tdatetime(tsix(tElementTree(tmaptrangetzip(t	urlencodeturlparseu	us-east-1u
2014-10-01uRequestLimitExceededuInsufficientInstanceCapacityu
InternalErroruUnavailableuInsufficientAddressCapacityu$InsufficientReservedInstanceCapacitygffffff@iuuse-instance-role-credentialsucC@s!tjtjdd|��dS(uz
    backoff an exponential amount of time to throttle requests
    during "API Rate Exceeded" failures as suggested by the AWS documentation here:
    https://docs.aws.amazon.com/AWSEC2/latest/APIReference/query-api-troubleshooting.html
    and also here:
    https://docs.aws.amazon.com/general/latest/gr/api-retries.html
    Failure to implement this approach results in a failure rate of >30% when using salt-cloud with
    "--parallel" when creating 50 or more instances with a fixed delay of 2 seconds.
    A failure rate of >10% is observed when using the salt-api with an asynchronous client
    specified (runner_async).
    iiN(ttimetsleeptrandomtuniform(tattempts((s2/usr/lib/python2.7/site-packages/salt/utils/aws.pytsleep_exponential_backoffBscC@sd}|dtks&|dtkr�tdkritj�}|jd�}|tkritttfSny9tj	ddidd6dt
�}|j�|j}Wn5tj
jtj
jfk
r�|d|ddfSXy9tj	d	j|�didd6dt
�}|j�Wn5tj
jtj
jfk
rJ|d|ddfSX|j�}|d
a|da|da|d
atttf}n|d|ddf}|j	d�dk	r|j�}|jdd�tjd|j	d��t|d|j	d�dd�}n|S(uB
    Return the credentials for AWS signing.  This could be just the id and key
    specified in the provider configuration, or if the id or key is set to the
    literal string 'use-instance-role-credentials' creds will pull the instance
    role credentials from the meta data, cache them, and provide them instead.
    uidukeyuu%Y-%m-%dT%H:%M:%SZuAhttp://169.254.169.254/latest/meta-data/iam/security-credentials/tproxiesuhttpttimeoutuDhttp://169.254.169.254/latest/meta-data/iam/security-credentials/{0}uAccessKeyIduSecretAccessKeyuTokenu
Expirationurole_arnuAssuming the role: %strole_arntlocationu	us-east-1(N(t
IROLE_CODEt__Expiration__Rtutcnowtstrftimet__AccessKeyId__t__SecretAccessKey__t	__Token__trequeststgettAWS_METADATA_TIMEOUTtraise_for_statusttextt
exceptionst	HTTPErrortConnectionErrortformattjsontNonetcopytpoptlogtinfot
assumed_creds(tprovidertret_credentialsttimenowt	timestamptresulttroletdatatprovider_shadow((s2/usr/lib/python2.7/site-packages/salt/utils/aws.pytcredsQsF
 





$cC@sLtj�}|jd�}t|�\}}}	|j�}
||
d<d|
d<d|
d<dj|�|
d<||
d	<t|
j��}ttt	|
j
|���}ttt||���}
d
j|j
d�|j
d�|
j
d��}tj||tj�}tj|j��}|j�|
d<|	d
krH|	|
d<n|
S(u�
    Sign a query against AWS services using Signature Version 2 Signing
    Process. This is documented at:

    http://docs.aws.amazon.com/general/latest/gr/signature-version-2.html
    u%Y-%m-%dT%H:%M:%SZuAWSAccessKeyIdu2uSignatureVersionu
HmacSHA256uSignatureMethodu{0}u	TimestampuVersionu
{0}
{1}
/
{2}uutf-8u	Signatureuu
SecurityToken(RRRR4R'R$tsortedtkeystlistRRR	Rtencodethmactnewthashlibtsha256tbinasciit
b2a_base64tdigesttstrip(tmethodtendpointtparamsR,taws_api_versionR.R/t
access_key_idtsecret_access_keyttokentparams_with_headersR6tvaluestquerystringt	canonicalthashedtsig((s2/usr/lib/python2.7/site-packages/salt/utils/aws.pytsig2�s,




cC@s�tjd�}tjtj�j��}x>tj�D]0\}}|d|dkr7tj	|�q7q7W|tkr�t|}|d|d|dfSd}|j
dtjj
idd	6�d
�d
d!}	tdd
di|d6dd6|	d6|d6dd6dd6d|ddddd|ddd|d d!�\}
}d"|
d#<tjd|d$|
ddd%t�}|jd&kr�tjd'|j�n|j�|j�}
|
d(d)d*}|t|<|d|d|dfS(+Nu[^a-z0-9A-Z+=,.@-]u
ExpirationixuAccessKeyIduSecretAccessKeyuSessionTokenu
2011-06-15uuroot_dirii?uGETusts.amazonaws.comRCuVersionu
AssumeRoleuActionuRoleSessionNameuRoleArnud{"Version":"2012-10-17","Statement":[{"Sid":"Stmt1", "Effect":"Allow","Action":"*","Resource":"*"}]}uPolicyu3600uDurationSecondsRDR2turiu/t	prov_dicttproductustsRt
requesturluhttps://sts.amazonaws.com/uapplication/jsonuAccepttheaderstverifyi�uAssumeRole response: %suAssumeRoleResponseuAssumeRoleResultuCredentials(tretcompileRtmktimeRRt	timetuplet__AssumeCache__titemstdeletetsubtsalttconfigtget_idR&tsig4RtrequesttTruetstatus_codeR)R*tcontentRR%(RPRRtvalid_session_name_retnowtkeyR4tctversiontsession_nameRSRRR0trespR2((s2/usr/lib/python2.7/site-packages/salt/utils/aws.pyR+�sL
0

	

uec2u/c
%C@stj�}
|dkr0t|�\}}}nt||d|�\}}}|dkrft�}n|dkr{t}n|j�}|dkr�||d<nt|j	��}t
t|j|��}t
t
t||���jdd�}|
jd�}|
jd�}i}t|
t�r3|
j�}n|sQtjjj|	�}n||d	<||d
<||d<g}g}|dkr�||d
<nxbt|j	�dtjj�D]B}|j�}|jdj|||j���|j|�q�Wdj|�d}dj|�}d}dj||||||f�}dj|||df�} dj||| tjjj|�f�}!t||||�}"tj |"|!j!d�t"j#�j$�}#dj||| ||#�}$|$|d<dj||�}||fS(u_
    Sign a query against AWS services using Signature Version 4 Signing
    Process. This is documented at:

    http://docs.aws.amazon.com/general/latest/gr/sigv4_signing.html
    http://docs.aws.amazon.com/general/latest/gr/sigv4-signed-request-examples.html
    http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
    Rus3ussmuVersionu+u%20u%Y%m%dT%H%M%SZu%Y%m%du
X-Amz-dateuhostux-amz-content-sha256uuX-Amz-security-tokenRgu{0}:{1}u
u;uAWS4-HMAC-SHA256u/uaws4_requestuutf-8u8{0} Credential={1}/{2}, SignedHeaders={3}, Signature={4}u
Authorizationu{0}?{1}N(us3ussm(%RRR&R4R+tget_region_from_metadatatDEFAULT_LOCATIONR'R5R6R7RRR	RtreplaceRt
isinstancetdictR]tutilst	hashutilst
sha256_digestRt	text_typetlowertappendR$R@tjoint_sig_keyR9R:R8R;R<t	hexdigest(%RARBRCRPRDRRQRORRR2RSRtpayload_hashR.RERFRGRHR6RIRJtamzdatet	datestamptnew_headersta_canonical_headersta_signed_headerstheadertlower_headertcanonical_headerstsigned_headerst	algorithmtcanonical_requesttcredential_scopetstring_to_signtsigning_keyt	signaturetauthorization_header((s2/usr/lib/python2.7/site-packages/salt/utils/aws.pyR`�s�	
'



%#		
cC@s%tj||jd�tj�j�S(u�
    Key derivation functions. See:

    http://docs.aws.amazon.com/general/latest/gr/signature-v4-examples.html#signature-v4-examples-python
    uutf-8(R9R:R8R;R<R?(Rgtmsg((s2/usr/lib/python2.7/site-packages/salt/utils/aws.pyt_signZscC@setd|jd�|�}|rCt||�}t||�}nt||�}t|d�}|S(u�
    Get a signature key. See:

    http://docs.aws.amazon.com/general/latest/gr/signature-v4-examples.html#signature-v4-examples-python
    uAWS4uutf-8uaws4_request(R�R8(Rgt
date_stampt
regionNametserviceNametkDatetkRegiontkServicetkSigning((s2/usr/lib/python2.7/site-packages/salt/utils/aws.pyRxcsu2c 
C@s�|d$kri}n|d$kr*i}n|jdd$|	f�}|jdi�}|d$kr�|j|di�j|	i�}
|
r�tt|
j���d}|j||	�}q�n|j|i�j|	i�}
|
jdd�}|st||
�}n|d$kr�|sI|
jddj|	||��}d	j|�}q�t|�j}|d
kr�dj|�}tj	|�|t
kr�i|d6|fSi|d6Sntjd
|�d}|
jd|
jdj|	�t��}|
jdd$�sY|j|i�jdi�jdi�|
d<|j|i�jdi�jdi�|
d<n|
dkr�t
||||
|||	d|�\}}i}nt||||
|�}i}d}x�|tkr@tjd|�tjd|�yPtj|d|d|�}tjd|j�tjd|j�|j�PWq�tjjk
r<}tj|jj�}tj|�}|jdi�jdi�jdd
�}|tkr�|r�|tkr�|d7}tj	d|jj|||�t|�q�ntj	d |jj||�|t
kr1i|d6|fSi|d6SXq�Wtj	d |jj||�|t
krzi|d6|fSi|d6Stj|j�}|d}|t
kr�|}n|r<tj d%kr�t!|j"��}nt!|�}xMt#d|�D]9}||j$j%d#�}|d|kr�||}q�q�Wng}x$|D]}|j&tj|��qIW|t
kr||fS|S(&u�
    Perform a query against AWS services using Signature Version 2 Signing
    Process. This is documented at:

    http://docs.aws.amazon.com/general/latest/gr/signature-version-2.html

    Regions and endpoints are documented at:

    http://docs.aws.amazon.com/general/latest/gr/rande.html

    Default ``product`` is ``ec2``. Valid ``product`` names are:

    .. code-block: yaml

        - autoscaling (Auto Scaling)
        - cloudformation (CloudFormation)
        - ec2 (Elastic Compute Cloud)
        - elasticache (ElastiCache)
        - elasticbeanstalk (Elastic BeanStalk)
        - elasticloadbalancing (Elastic Load Balancing)
        - elasticmapreduce (Elastic MapReduce)
        - iam (Identity and Access Management)
        - importexport (Import/Export)
        - monitoring (CloudWatch)
        - rds (Relational Database Service)
        - simpledb (SimpleDB)
        - sns (Simple Notification Service)
        - sqs (Simple Queue Service)
    ufunctionu	providersiiuservice_urlu
amazonaws.comuendpointu{0}.{1}.{2}uhttps://{0}/uurCould not find a valid endpoint in the requesturl: {0}. Looking for something like https://some.aws.endpoint/?argsuerroruUsing AWS endpoint: %suGETuaws_api_versionu{0}_api_versionuiduec2ukeyu4RRuAWS Request: %suAWS Request Parameters: %sRSRCuAWS Response Status Code: %suAWS Response Text: %suErrorsuErroruCodeuFAWS Response Status Code and Error: [%s %s] %s; Attempts remaining: %su.AWS Response Status Code and Error: [%s %s] %siiu}N(ii('R&RR7R6tget_locationR$R
tnetlocR)terrorRbtdebugtDEFAULT_AWS_API_VERSIONR`RNtAWS_MAX_RETRIESttraceRRcR RR!R"tETt
fromstringtresponseRdtxmltto_dicttAWS_RETRY_CODESRtsystversion_infotlentgetchildrenRttagtsplitRv( RCtsetnameRRRt
return_urltreturn_roottoptsR,RBRQtsigvertfunctiont	providersRPtdrivertservice_urltendpoint_errRARDRSRHRR0texctrootR2terr_codeRZtchildren_lentitemtcompstret((s2/usr/lib/python2.7/site-packages/salt/utils/aws.pytqueryss� 		"	
	.1'	

*


	

cC@s�tdkrtjd�dStdkr-tSy&tjddidd6dt�}Wn1tjjk
r�tj	dd	t
�dadSXy|j�d
}|atSWn%tt
fk
r�tj	d�dSXdS(
uh
    Try to get region from instance identity document and cache it

    .. versionadded:: 2015.5.6
    udo-not-get-from-metadatauDPreviously failed to get AWS region from metadata. Not trying again.uu@http://169.254.169.254/latest/dynamic/instance-identity/documentRuhttpRu0Failed to get AWS region from instance metadata.texc_infouregionu-Failed to decode JSON from instance metadata.N(t__Location__R)R�R&RRRR!tRequestExceptiontwarningRbR%t
ValueErrortKeyError(R0tregion((s2/usr/lib/python2.7/site-packages/salt/utils/aws.pyRls*

cC@s|dkri}n|jd�}|dkrN|dk	rN|jd�}n|dkrft�}n|dkr{t}n|S(u�
    Return the region to use, in this order:
        opts['location']
        provider['location']
        get_region_from_metadata()
        DEFAULT_LOCATION
    ulocationN(R&RRlRm(R�R,R�((s2/usr/lib/python2.7/site-packages/salt/utils/aws.pyR�Fs		(@t__doc__t
__future__RRRR�RR=RR;R9tloggingtsalt.configR]RUR
tsalt.extRtsalt.utils.hashutilstsalt.utils.xmlutilRqtxmlutilR�tsalt._compatRR�RRbtHAS_REQUESTStImportErrortFalsetsalt.ext.six.movesRRRtsalt.ext.six.moves.urllib.parseR	R
t	getLoggert__name__R)RmR�R�RR�RRRRRR�RYRR4RNR&R+R`R�RxR�RlR�(((s2/usr/lib/python2.7/site-packages/salt/utils/aws.pyt<module>
sl


			?	(4l			�	'

Zerion Mini Shell 1.0