Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/awscli/customizations/cloudtrail/utils.py
1567 views
1
# Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License"). You
4
# may not use this file except in compliance with the License. A copy of
5
# the License is located at
6
#
7
# http://aws.amazon.com/apache2.0/
8
#
9
# or in the "license" file accompanying this file. This file is
10
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
# ANY KIND, either express or implied. See the License for the specific
12
# language governing permissions and limitations under the License.
13
14
15
def get_account_id_from_arn(trail_arn):
16
"""Gets the account ID portion of an ARN"""
17
return trail_arn.split(':')[4]
18
19
20
def get_account_id(sts_client):
21
"""Retrieve the AWS account ID for the authenticated user or role"""
22
response = sts_client.get_caller_identity()
23
return response['Account']
24
25
26
def get_trail_by_arn(cloudtrail_client, trail_arn):
27
"""Gets trail information based on the trail's ARN"""
28
trails = cloudtrail_client.describe_trails()['trailList']
29
for trail in trails:
30
if trail.get('TrailARN', None) == trail_arn:
31
return trail
32
raise ValueError('A trail could not be found for %s' % trail_arn)
33
34