HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: //lib/python3.6/site-packages/awscli/customizations/cloudtrail/__pycache__/subscribe.cpython-36.pyc
3

�T_a5�@s�ddlZddlZddlZddlmZddlmZddlmZddl	m
Z
eje�Z
dZdZGd	d
�d
e�ZGdd�de�ZGd
d�de�ZdS)�N�)�get_account_id)�BasicCommand)�s3_bucket_exists)�ClientErrorz6policy/S3/AWSCloudTrail-S3BucketPolicy-2014-12-17.jsonz7policy/SNS/AWSCloudTrail-SnsTopicPolicy-2014-12-17.jsonc@seZdZdS)�CloudTrailErrorN)�__name__�
__module__�__qualname__�rr�/usr/lib/python3.6/subscribe.pyrsrc
@s�eZdZdZdZdZdZdddd�d	d
d�dd
d�ddd�ddd�ddd�ddd�ddd�gZdZdZ	dd�Z
dd�Zdd�Zdd �Z
d,d"d#�Zd-d$d%�Zd&d'�Zd(d)�Zd*d+�Zd!S).�CloudTrailSubscribez�
    Subscribe/update a user account to CloudTrail, creating the required S3 bucket,
    the optional SNS topic, and starting the CloudTrail monitoring and logging.
    zcreate-subscriptionz�Creates and configures the AWS resources necessary to use CloudTrail, creates a trail using those resources, and turns on logging.znaws cloudtrail create-subscription (--s3-use-bucket|--s3-new-bucket) bucket-name [--sns-new-topic topic-name]
�nameTzCloudtrail name)rZrequired�	help_textz
s3-new-bucketz%Create a new S3 bucket with this name)rrz
s3-use-bucketz(Use an existing S3 bucket with this namez	s3-prefixzS3 object prefixz
sns-new-topicz%Create a new SNS topic with this namezinclude-global-service-eventsz(Whether to include global service eventszs3-custom-policyz Custom S3 policy template or URLzsns-custom-policyz!Custom SNS policy template or URLFcCs|j||�|j||�dS)Nr)�setup_services�_call)�self�args�parsed_globalsrrr�	_run_main@szCloudTrailSubscribe._run_maincCs�ddd�}|jdk	r|j|d<|jdk	r2|j|d<tjd�|jjd
|�|_|jjd|�|_|jjd|�|_|jj	j
|_
|jdk	r�|j|d<|jjd
|�|_dS)N)�region_name�verifyrrz&Initializing S3, SNS and CloudTrail...�sts�s3�sns�endpoint_url�
cloudtrail)r)r)r)r)
�regionZ
verify_ssl�LOG�debugZ_sessionZ
create_clientrrr�metarrr)rrrZclient_argsrrrrGs






z"CloudTrailSubscribe.setup_servicesc	Cs�|j}|r6|j�dkrd}n|j�dkr.d}ntd��|j}|jr�|j}|jr�|jdkr�|jj|j	gd�}|dd	}d
|kr�t
jdj|d
��|d
|_|j
||j|j�n|r�|jr�td��|j�ry|j|j|j�}Wn0tk
�r|j�r|jj|jd
��YnXy|j|j	||j|j|�}WnJtk
�rz|j�rZ|jj|jd
�|j�rt|jj|dd��YnXtjjdjtj|dd�d��|j�s�|j|j	�tjjdj||j�p�dd��dS)z�
        Run the command. Calls various services based on input options and
        outputs the final CloudTrail configuration.
        �trueTZfalseFzFYou must pass either true or false to --include-global-service-events.N)Z
trailNameListZ	trailListr�S3KeyPrefixzSetting S3 prefix to {0}zBYou must pass either --s3-use-bucket or --s3-new-bucket to create.)�Bucket�TopicArn)r$z#CloudTrail configuration:
{config}
�)�indent)�configz,Logs will be delivered to {bucket}:{prefix}
�)�bucket�prefix)Zinclude_global_service_events�lower�
ValueErrorZ
s3_use_bucketZ
s3_new_bucket�UPDATEZ	s3_prefixr�describe_trailsrrr�format�setup_new_bucketZs3_custom_policyZ
sns_new_topic�setup_new_topicZsns_custom_policy�	Exceptionr�
delete_bucket�upsert_cloudtrail_configr�delete_topic�sys�stdout�write�json�dumps�start_cloudtrail)	rZoptionsr�gser)�resZ
trail_info�topic_resultZcloudtrail_configrrrr^sd




zCloudTrailSubscribe._callcCs`y(|jjd|j|d�}|dj�jd�Stk
rZ}ztd|j||��WYdd}~XnXdS)Nzawscloudtrail-policy-)r#ZKeyZBodyzutf-8zCUnable to get regional policy template for region %s: %s. Error: %s)rZ
get_objectr�read�decoder2r)rZkey_name�data�errr�_get_policy�szCloudTrailSubscribe._get_policyNc
Cs.tjjdj|d��t|j�}|r6|jd�r6|d7}|dk	rD|}n
|jt�}|j	d|�j	d|�}d|kr||j	d|pvd�}n|j	d	|p�d�}t
jd
j|��t|j
|�}|r�tdj|d���d|i}|jd
kr�d|ji}||d<|j
jf|�}	y|j
j||d�Wn&tk
�r(|j
j|d��YnX|	S)zx
        Creates a new S3 bucket with an appropriate policy to let CloudTrail
        write to the prefix path.
        z%Setting up new S3 bucket {bucket}...
)r)�/Nz<BucketName>z<CustomerAccountID>z	<Prefix>/r(z<Prefix>zBucket policy:
{0}zBucket {bucket} already exists.r#z	us-east-1ZLocationConstraintZCreateBucketConfiguration)r#�Policy)r#)r6r7r8r/rr�endswithrC�S3_POLICY_TEMPLATE�replacerrrrr2rZ
create_bucketZput_bucket_policyrr3)
rr)r*�
custom_policy�
account_id�policyZ
bucket_existsZparamsZ
bucket_configrArrrr0�s:




z$CloudTrailSubscribe.setup_new_bucketc	sBtjjdj�d��t|j�}y|jj�d}Wn"tk
rRg}t	j
d�YnX�fdd�|D�rvtdj�d���|jjj}|dk	r�|}n
|j
t�}|jd	|�jd
|�jd��}|jj�d�}yP|jj|d
d�}|j|dd|�}t	jdj|��|jj|d
d|d�Wn*tk
�r<|jj|d
d��YnX|S)zz
        Creates a new SNS topic with an appropriate policy to let CloudTrail
        post messages to the topic.
        z$Setting up new SNS topic {topic}...
)�topicZTopicsz$Unable to list topics, continuing...cs&g|]}|djd�d�kr|�qS)r$�:r���)�split)�.0�t)rLrr�
<listcomp>�sz7CloudTrailSubscribe.setup_new_topic.<locals>.<listcomp>zTopic {topic} already exists.Nz<Region>z<SNSTopicOwnerAccountId>z<SNSTopicName>)�Namer$)r$Z
AttributesrEzTopic policy:
{0})r$Z
AttributeNameZAttributeValue)r6r7r8r/rrrZlist_topicsr2r�warnr rrC�SNS_POLICY_TEMPLATErHZcreate_topicZget_topic_attributes�merge_sns_policyrZset_topic_attributesr5)	rrLrIrJZtopicsrrKr>Z
topic_attrr)rLrr1�s@



z#CloudTrailSubscribe.setup_new_topiccCs2tj|�}tj|�}|d|d7<tj|�S)a�
        Merge two SNS topic policy documents. The id information from
        ``left`` is used in the final document, and the statements
        from ``right`` are merged into ``left``.

        http://docs.aws.amazon.com/sns/latest/dg/BasicStructure.html

        :type left: string
        :param left: First policy JSON document
        :type right: string
        :param right: Second policy JSON document
        :rtype: string
        :return: Merged policy JSON
        Z	Statement)r9�loadsr:)r�left�rightZleft_parsedZright_parsedrrrrV%s

z$CloudTrailSubscribe.merge_sns_policycCs�tjjd�d|i}|dk	r$||d<|dk	r4||d<|dk	rD||d<|dk	rT||d<|jsj|jjf|�n|jjf|�|jj�S)z�
        Either create or update the CloudTrail configuration depending on
        whether this command is a create or update command.
        z.Creating/updating CloudTrail configuration...
rSNZS3BucketNamer"ZSnsTopicNameZIncludeGlobalServiceEvents)r6r7r8r-rZcreate_trailZupdate_trailr.)rrr)r*rLr<r'rrrr49sz,CloudTrailSubscribe.upsert_cloudtrail_configcCstjjd�|jj|d�S)zE
        Start the CloudTrail service, which begins logging.
        zStarting CloudTrail service...
)rS)r6r7r8rZ
start_logging)rrrrrr;Psz$CloudTrailSubscribe.start_cloudtrail)N)N)rr	r
�__doc__�NAME�DESCRIPTION�SYNOPSISZ	ARG_TABLEr-Z
_UNDOCUMENTEDrrrrCr0r1rVr4r;rrrrr
 s:

M
4
;r
c@s eZdZdZdZdZdZdZdS)�CloudTrailUpdatezF
    Like subscribe above, but the update version of the command.
    zupdate-subscriptionTzlUpdates any of the trail configuration settings, and creates and configures any new AWS resources specified.zpaws cloudtrail update-subscription [(--s3-use-bucket|--s3-new-bucket) bucket-name] [--sns-new-topic topic-name]
N)rr	r
rZr[r-r\r]rrrrr^Xs
r^)r9Zloggingr6ZutilsrZawscli.customizations.commandsrZawscli.customizations.utilsrZbotocore.exceptionsrZ	getLoggerrrrGrUr2rr
r^rrrr�<module>
s
: