| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
--- |
| 9 |
module: cloudtrail_info |
| 10 |
version_added: 5.0.0 |
| 11 |
short_description: Gather information about trails in AWS Cloud Trail |
| 12 |
description: |
| 13 |
- Gather information about trails in AWS CloudTrail. |
| 14 |
author: "Gomathi Selvi Srinivasan (@GomathiselviS)" |
| 15 |
options: |
| 16 |
trail_names: |
| 17 |
type: list |
| 18 |
elements: str |
| 19 |
default: [] |
| 20 |
description: |
| 21 |
- Specifies a list of trail names, trail ARNs, or both, of the trails to describe. |
| 22 |
- If an empty list is specified, information for the trail in the current region is returned. |
| 23 |
include_shadow_trails: |
| 24 |
type: bool |
| 25 |
default: true |
| 26 |
description: Specifies whether to include shadow trails in the response. |
| 27 |
extends_documentation_fragment: |
| 28 |
- amazon.aws.common.modules |
| 29 |
- amazon.aws.region.modules |
| 30 |
- amazon.aws.boto3 |
| 31 |
""" |
| 32 |
|
| 33 |
EXAMPLES = r""" |
| 34 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 35 |
|
| 36 |
# Gather information about all trails |
| 37 |
- amazon.aws.cloudtrail_info: |
| 38 |
|
| 39 |
# Gather information about a particular trail |
| 40 |
- amazon.aws.cloudtrail_info: |
| 41 |
trail_names: |
| 42 |
- arn:aws:cloudtrail:us-east-2:123456789012:trail/MyTrail |
| 43 |
""" |
| 44 |
|
| 45 |
RETURN = r""" |
| 46 |
trail_list: |
| 47 |
description: List of trail objects. Each element consists of a dict with all the information related to that cloudtrail. |
| 48 |
type: list |
| 49 |
elements: dict |
| 50 |
returned: always |
| 51 |
contains: |
| 52 |
name: |
| 53 |
description: Name of the trail. |
| 54 |
type: str |
| 55 |
sample: "MyTrail" |
| 56 |
s3_bucket_name: |
| 57 |
description: Name of the Amazon S3 bucket into which CloudTrail delivers the trail files. |
| 58 |
type: str |
| 59 |
sample: "aws-cloudtrail-logs-xxxx" |
| 60 |
s3_key_prefix: |
| 61 |
description: Amazon S3 key prefix that comes after the name of the bucket that is designated for log file delivery. |
| 62 |
type: str |
| 63 |
sample: "xxxx" |
| 64 |
sns_topic_arn: |
| 65 |
description: ARN of the Amazon SNS topic that CloudTrail uses to send notifications when log files are delivered. |
| 66 |
type: str |
| 67 |
sample: "arn:aws:sns:us-east-2:123456789012:MyTopic" |
| 68 |
include_global_service_events: |
| 69 |
description: If True, AWS API calls from AWS global services such as IAM are included. |
| 70 |
type: bool |
| 71 |
sample: true |
| 72 |
is_multi_region_trail: |
| 73 |
description: Specifies whether the trail exists only in one region or exists in all regions. |
| 74 |
type: bool |
| 75 |
sample: true |
| 76 |
home_region: |
| 77 |
description: The region in which the trail was created. |
| 78 |
type: str |
| 79 |
sample: "us-east-1" |
| 80 |
trail_arn: |
| 81 |
description: Specifies the ARN of the trail. |
| 82 |
type: str |
| 83 |
sample: "arn:aws:cloudtrail:us-east-2:123456789012:trail/MyTrail" |
| 84 |
log_file_validation_enabled: |
| 85 |
description: Specifies whether log file validation is enabled. |
| 86 |
type: bool |
| 87 |
sample: true |
| 88 |
cloud_watch_logs_log_group_arn: |
| 89 |
description: Specifies an ARN, that represents the log group to which CloudTrail logs will be delivered. |
| 90 |
type: str |
| 91 |
sample: "arn:aws:sns:us-east-2:123456789012:Mylog" |
| 92 |
cloud_watch_logs_role_arn: |
| 93 |
description: Specifies the role for the CloudWatch Logs endpoint to assume to write to a user's log group. |
| 94 |
type: str |
| 95 |
sample: "arn:aws:sns:us-east-2:123456789012:Mylog" |
| 96 |
kms_key_id: |
| 97 |
description: Specifies the KMS key ID that encrypts the logs delivered by CloudTrail. |
| 98 |
type: str |
| 99 |
sample: "arn:aws:kms:us-east-2:123456789012:key/12345678-1234-1234-1234-123456789012" |
| 100 |
has_custom_event_selectors: |
| 101 |
description: Specifies if the trail has custom event selectors. |
| 102 |
type: bool |
| 103 |
sample: true |
| 104 |
has_insight_selectors: |
| 105 |
description: Specifies whether a trail has insight types specified in an InsightSelector list. |
| 106 |
type: bool |
| 107 |
sample: true |
| 108 |
is_organization_trail: |
| 109 |
description: Specifies whether the trail is an organization trail. |
| 110 |
type: bool |
| 111 |
sample: true |
| 112 |
is_logging: |
| 113 |
description: Whether the CloudTrail is currently logging AWS API calls. |
| 114 |
type: bool |
| 115 |
sample: true |
| 116 |
latest_delivery_error: |
| 117 |
description: Displays any Amazon S3 error that CloudTrail encountered when attempting to deliver log files to the designated bucket. |
| 118 |
type: str |
| 119 |
latest_notification_attempt_time: |
| 120 |
description: Specifies the date and time that CloudTrail last attempt to deliver a notification. |
| 121 |
type: str |
| 122 |
latest_notification_attempt_succeeded: |
| 123 |
description: Specifies the date and time that CloudTrail last successful attempt to deliver a notification. |
| 124 |
type: str |
| 125 |
latest_notification_error: |
| 126 |
description: Displays any Amazon SNS error that CloudTrail encountered when attempting to send a notification. |
| 127 |
type: str |
| 128 |
latest_delivery_attempt_succeeded: |
| 129 |
description: Specifies the date and time that CloudTrail last successful attempt to deliver log files to an account's Amazon S3 bucket. |
| 130 |
type: str |
| 131 |
latest_delivery_attempt_time: |
| 132 |
description: Specifies the date and time that CloudTrail last attempt to deliver log files to an account's Amazon S3 bucket. |
| 133 |
type: str |
| 134 |
latest_delivery_time: |
| 135 |
description: Specifies the date and time that CloudTrail last delivered log files to an account's Amazon S3 bucket. |
| 136 |
type: str |
| 137 |
start_logging_time: |
| 138 |
description: Specifies the most recent date and time when CloudTrail started recording API calls for an AWS account. |
| 139 |
type: str |
| 140 |
stop_logging_time: |
| 141 |
description: Specifies the most recent date and time when CloudTrail stopped recording API calls for an AWS account. |
| 142 |
type: str |
| 143 |
latest_cloud_watch_logs_delivery_error: |
| 144 |
description: Displays any CloudWatch Logs error that CloudTrail encountered when attempting to deliver logs to CloudWatch Logs. |
| 145 |
type: str |
| 146 |
latest_cloud_watch_logs_delivery_time: |
| 147 |
description: Displays the most recent date and time when CloudTrail delivered logs to CloudWatch Logs. |
| 148 |
type: str |
| 149 |
latest_digest_delivery_time: |
| 150 |
description: Specifies the date and time that CloudTrail last delivered a digest file to an account's Amazon S3 bucket. |
| 151 |
type: str |
| 152 |
latest_digest_delivery_error: |
| 153 |
description: Displays any Amazon S3 error that CloudTrail encountered when attempting to deliver a digest file to the designated bucket. |
| 154 |
type: str |
| 155 |
resource_id: |
| 156 |
description: Specifies the ARN of the resource. |
| 157 |
type: str |
| 158 |
tags: |
| 159 |
description: Any tags assigned to the cloudtrail. |
| 160 |
type: dict |
| 161 |
returned: always |
| 162 |
sample: "{ 'my_tag_key': 'my_tag_value' }" |
| 163 |
""" |
| 164 |
|
| 165 |
try: |
| 166 |
import botocore |
| 167 |
except ImportError: |
| 168 |
pass # Handled by AnsibleAWSModule |
| 169 |
|
| 170 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 171 |
|
| 172 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 173 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 174 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 175 |
|
| 176 |
|
| 177 |
def get_trails(connection, module): |
| 178 |
all_trails = [] |
| 179 |
try: |
| 180 |
result = connection.get_paginator("list_trails") |
| 181 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 182 |
module.fail_json_aws(e, msg="Failed to get the trails.") |
| 183 |
for trail in result.paginate(): |
| 184 |
all_trails.extend(list_cloud_trails(trail)) |
| 185 |
return all_trails |
| 186 |
|
| 187 |
|
| 188 |
def list_cloud_trails(trail_dict): |
| 189 |
return [x["TrailARN"] for x in trail_dict["Trails"]] |
| 190 |
|
| 191 |
|
| 192 |
def get_trail_detail(connection, module): |
| 193 |
output = {} |
| 194 |
trail_name_list = module.params.get("trail_names") |
| 195 |
include_shadow_trails = module.params.get("include_shadow_trails") |
| 196 |
if not trail_name_list: |
| 197 |
trail_name_list = get_trails(connection, module) |
| 198 |
try: |
| 199 |
result = connection.describe_trails( |
| 200 |
trailNameList=trail_name_list, includeShadowTrails=include_shadow_trails, aws_retry=True |
| 201 |
) |
| 202 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 203 |
module.fail_json_aws(e, msg="Failed to get the trails.") |
| 204 |
# Turn the boto3 result in to ansible_friendly_snaked_names |
| 205 |
snaked_cloud_trail = [] |
| 206 |
for cloud_trail in result["trailList"]: |
| 207 |
try: |
| 208 |
status_dict = connection.get_trail_status(Name=cloud_trail["TrailARN"], aws_retry=True) |
| 209 |
cloud_trail.update(status_dict) |
| 210 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 211 |
module.fail_json_aws(e, msg="Failed to get the trail status") |
| 212 |
try: |
| 213 |
tag_list = connection.list_tags(ResourceIdList=[cloud_trail["TrailARN"]]) |
| 214 |
for tag_dict in tag_list["ResourceTagList"]: |
| 215 |
cloud_trail.update(tag_dict) |
| 216 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 217 |
module.warn(f"Failed to get the trail tags - {e}") |
| 218 |
snaked_cloud_trail.append(camel_dict_to_snake_dict(cloud_trail)) |
| 219 |
|
| 220 |
# Turn the boto3 result in to ansible friendly tag dictionary |
| 221 |
for tr in snaked_cloud_trail: |
| 222 |
if "tags_list" in tr: |
| 223 |
tr["tags"] = boto3_tag_list_to_ansible_dict(tr["tags_list"], "key", "value") |
| 224 |
del tr["tags_list"] |
| 225 |
if "response_metadata" in tr: |
| 226 |
del tr["response_metadata"] |
| 227 |
output["trail_list"] = snaked_cloud_trail |
| 228 |
return output |
| 229 |
|
| 230 |
|
| 231 |
def main(): |
| 232 |
argument_spec = dict( |
| 233 |
trail_names=dict(type="list", elements="str", default=[]), |
| 234 |
include_shadow_trails=dict(type="bool", default=True), |
| 235 |
) |
| 236 |
|
| 237 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 238 |
|
| 239 |
try: |
| 240 |
connection = module.client("cloudtrail", retry_decorator=AWSRetry.jittered_backoff()) |
| 241 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 242 |
module.fail_json_aws(e, msg="Failed to connect to AWS") |
| 243 |
result = get_trail_detail(connection, module) |
| 244 |
module.exit_json(**result) |
| 245 |
|
| 246 |
|
| 247 |
if __name__ == "__main__": |
| 248 |
main() |