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: //lib/python3/dist-packages/awscli/customizations/cloudtrail/__pycache__/subscribe.cpython-310.pyc
o

.&�aa5�@s�ddlZddlZddlZddlmZddlmZddlmZddl	m
Z
e�e�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�L/usr/lib/python3/dist-packages/awscli/customizations/cloudtrail/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)r�required�	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|�||�|�||�dS)Nr)�setup_services�_call)�self�args�parsed_globalsrrr�	_run_main@szCloudTrailSubscribe._run_maincCs�ddd�}|jdur|j|d<|jdur|j|d<t�d�|jjd
i|��|_|jjdi|��|_|jjdi|��|_|jj	j
|_
|jdurL|j|d<|jjd
i|��|_dS)N)�region_name�verifyrrz&Initializing S3, SNS and CloudTrail...�sts�s3�sns�endpoint_url�
cloudtrail)r)r)r)r)
�region�
verify_ssl�LOG�debug�_session�
create_clientrrr�metarrr)rrr�client_argsrrrrGs�






z"CloudTrailSubscribe.setup_servicesc	Cs�|j}|r|��dkrd}n
|��dkrd}ntd��|j}|jrX|j}|jrN|jdurN|jj|j	gd�}|dd	}d
|vrNt
�d�|d
��|d
|_|�
||j|j�n	|sa|jsatd��|jr�z
|�|j|j�}Wnty�|jr�|jj|jd
��wz|�|j	||j|j|�}Wnty�|jr�|jj|jd
�|jr�|jj|dd��wtj�djtj|dd�d��|js�|�|j	�tj�dj||jp�dd��dSdS)z�
        Run the command. Calls various services based on input options and
        outputs the final CloudTrail configuration.
        �trueT�falseFzFYou must pass either true or false to --include-global-service-events.N)�
trailNameList�	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)�include_global_service_events�lower�
ValueError�
s3_use_bucket�
s3_new_bucket�UPDATE�	s3_prefixr�describe_trailsrr r!�format�setup_new_bucket�s3_custom_policy�
sns_new_topic�setup_new_topic�sns_custom_policy�	Exceptionr�
delete_bucket�upsert_cloudtrail_configr�delete_topic�sys�stdout�write�json�dumps�start_cloudtrail)	r�optionsr�gser3�res�
trail_info�topic_result�cloudtrail_configrrrr^sz��

�
����
�
���zCloudTrailSubscribe._callc
CsTz|jjd|j|d�}|d���d�WSty)}ztd|j||��d}~ww)Nzawscloudtrail-policy-)r,�Key�Bodyzutf-8zCUnable to get regional policy template for region %s: %s. Error: %s)r�
get_objectr�read�decoderCr)r�key_name�data�errr�_get_policy�s����zCloudTrailSubscribe._get_policyNc
Cs&tj�dj|d��t|j�}|r|�d�s|d7}|dur!|}n|�t�}|�	d|��	d|�}d|vr=|�	d|p:d�}n|�	d	|pCd�}t
�d
�|��t|j
|�}|r]tdj|d���d|i}|jd
krod|ji}||d<|j
jdi|��}	z|j
j||d�W|	Sty�|j
j|d��w)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}...
)r3�/Nz<BucketName>z<CustomerAccountID>z	<Prefix>/r2z<Prefix>zBucket policy:
{0}zBucket {bucket} already exists.r,z	us-east-1�LocationConstraint�CreateBucketConfiguration)r,�Policyr+r)rGrHrIr=rr�endswithr[�S3_POLICY_TEMPLATE�replacer r!rrrCr�
create_bucket�put_bucket_policyrrD)
rr3r4�
custom_policy�
account_id�policy�
bucket_exists�params�
bucket_configrYrrrr>�sB
�


��

��z$CloudTrailSubscribe.setup_new_bucketc	s6tj�dj�d��t|j�}z	|j��d}Wnty(g}t	�
d�Ynw�fdd�|D�r:tdj�d���|jjj}|durF|}n|�
t�}|�d	|��d
|��d��}|jj�d�}z)|jj|d
d�}|�|dd|�}t	�d�|��|jj|d
d|d�W|Sty�|jj|d
d��w)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}...
��topic�Topicsz$Unable to list topics, continuing...cs&g|]}|d�d�d�kr|�qS)r-�:���)�split)�.0�trkrr�
<listcomp>�s&z7CloudTrailSubscribe.setup_new_topic.<locals>.<listcomp>zTopic {topic} already exists.Nz<Region>z<SNSTopicOwnerAccountId>z<SNSTopicName>��Namer-r.�
Attributesr_zTopic policy:
{0})r-�
AttributeName�AttributeValue)rGrHrIr=rrr�list_topicsrCr �warnr$rr[�SNS_POLICY_TEMPLATErb�create_topic�get_topic_attributes�merge_sns_policyr!�set_topic_attributesrF)	rrlrerf�topicsrrgrQ�
topic_attrrrkrrA�sP
�
��


������z#CloudTrailSubscribe.setup_new_topiccCs2t�|�}t�|�}|d|d7<t�|�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
        �	Statement)rJ�loadsrK)r�left�right�left_parsed�right_parsedrrrr~%s


z$CloudTrailSubscribe.merge_sns_policycCs�tj�d�d|i}|dur||d<|dur||d<|dur"||d<|dur*||d<|js7|jjdi|��n	|jjdi|��|j��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...
ruN�S3BucketNamer*�SnsTopicName�IncludeGlobalServiceEventsr)rGrHrIr:r�create_trail�update_trailr<)rrr3r4rlrNr1rrrrE9s�
z,CloudTrailSubscribe.upsert_cloudtrail_configcCstj�d�|jj|d�S)zE
        Start the CloudTrail service, which begins logging.
        zStarting CloudTrail service...
rt)rGrHrIr�
start_logging)rrrrrrLPsz$CloudTrailSubscribe.start_cloudtrail)N)rr	r
�__doc__�NAME�DESCRIPTION�SYNOPSIS�	ARG_TABLEr:�
_UNDOCUMENTEDrrrr[r>rAr~rErLrrrrr
 sJ
�������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
r�r�r:r�r�rrrrr�Xsr�)rJ�loggingrG�utilsr�awscli.customizations.commandsr�awscli.customizations.utilsr�botocore.exceptionsr�	getLoggerrr rar{rCrr
r�rrrr�<module>s
: