| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2018 Dennis Conrad for Sainsbury's |
| 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: inspector_target |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Create, Update and Delete Amazon Inspector Assessment Targets |
| 12 |
description: |
| 13 |
- Creates, updates, or deletes Amazon Inspector Assessment Targets and manages |
| 14 |
the required Resource Groups. |
| 15 |
- Prior to release 5.0.0 this module was called C(community.aws.aws_inspector_target). |
| 16 |
The usage did not change. |
| 17 |
author: |
| 18 |
- "Dennis Conrad (@dennisconrad)" |
| 19 |
options: |
| 20 |
name: |
| 21 |
description: |
| 22 |
- The user-defined name that identifies the assessment target. |
| 23 |
- The name must be unique within the AWS account. |
| 24 |
required: true |
| 25 |
type: str |
| 26 |
state: |
| 27 |
description: |
| 28 |
- The state of the assessment target. |
| 29 |
choices: |
| 30 |
- absent |
| 31 |
- present |
| 32 |
default: present |
| 33 |
type: str |
| 34 |
tags: |
| 35 |
description: |
| 36 |
- Tags of the EC2 instances to be added to the assessment target. |
| 37 |
- Required if I(state=present). |
| 38 |
type: dict |
| 39 |
extends_documentation_fragment: |
| 40 |
- amazon.aws.common.modules |
| 41 |
- amazon.aws.region.modules |
| 42 |
- amazon.aws.boto3 |
| 43 |
""" |
| 44 |
|
| 45 |
EXAMPLES = r""" |
| 46 |
- name: Create my_target Assessment Target |
| 47 |
community.aws.inspector_target: |
| 48 |
name: my_target |
| 49 |
tags: |
| 50 |
role: scan_target |
| 51 |
|
| 52 |
- name: Update Existing my_target Assessment Target with Additional Tags |
| 53 |
community.aws.inspector_target: |
| 54 |
name: my_target |
| 55 |
tags: |
| 56 |
env: dev |
| 57 |
role: scan_target |
| 58 |
|
| 59 |
- name: Delete my_target Assessment Target |
| 60 |
community.aws.inspector_target: |
| 61 |
name: my_target |
| 62 |
state: absent |
| 63 |
""" |
| 64 |
|
| 65 |
RETURN = r""" |
| 66 |
arn: |
| 67 |
description: The ARN that specifies the Amazon Inspector assessment target. |
| 68 |
returned: success |
| 69 |
type: str |
| 70 |
sample: "arn:aws:inspector:eu-west-1:123456789012:target/0-O4LnL7n1" |
| 71 |
created_at: |
| 72 |
description: The time at which the assessment target was created. |
| 73 |
returned: success |
| 74 |
type: str |
| 75 |
sample: "2018-01-29T13:48:51.958000+00:00" |
| 76 |
name: |
| 77 |
description: The name of the Amazon Inspector assessment target. |
| 78 |
returned: success |
| 79 |
type: str |
| 80 |
sample: "my_target" |
| 81 |
resource_group_arn: |
| 82 |
description: The ARN that specifies the resource group that is associated |
| 83 |
with the assessment target. |
| 84 |
returned: success |
| 85 |
type: str |
| 86 |
sample: "arn:aws:inspector:eu-west-1:123456789012:resourcegroup/0-qY4gDel8" |
| 87 |
tags: |
| 88 |
description: The tags of the resource group that is associated with the |
| 89 |
assessment target. |
| 90 |
returned: success |
| 91 |
type: list |
| 92 |
sample: {"role": "scan_target", "env": "dev"} |
| 93 |
updated_at: |
| 94 |
description: The time at which the assessment target was last updated. |
| 95 |
returned: success |
| 96 |
type: str |
| 97 |
sample: "2018-01-29T13:48:51.958000+00:00" |
| 98 |
""" |
| 99 |
|
| 100 |
try: |
| 101 |
import botocore |
| 102 |
except ImportError: |
| 103 |
pass # caught by AnsibleAWSModule |
| 104 |
|
| 105 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 106 |
|
| 107 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 108 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import ansible_dict_to_boto3_tag_list |
| 109 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 110 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import compare_aws_tags |
| 111 |
|
| 112 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 113 |
|
| 114 |
|
| 115 |
@AWSRetry.jittered_backoff(retries=5, delay=5, backoff=2.0) |
| 116 |
def main(): |
| 117 |
argument_spec = dict( |
| 118 |
name=dict(required=True), |
| 119 |
state=dict(choices=["absent", "present"], default="present"), |
| 120 |
tags=dict(type="dict"), |
| 121 |
) |
| 122 |
|
| 123 |
required_if = [["state", "present", ["tags"]]] |
| 124 |
|
| 125 |
module = AnsibleAWSModule( |
| 126 |
argument_spec=argument_spec, |
| 127 |
supports_check_mode=False, |
| 128 |
required_if=required_if, |
| 129 |
) |
| 130 |
|
| 131 |
name = module.params.get("name") |
| 132 |
state = module.params.get("state").lower() |
| 133 |
tags = module.params.get("tags") |
| 134 |
if tags: |
| 135 |
tags = ansible_dict_to_boto3_tag_list(tags, "key", "value") |
| 136 |
|
| 137 |
client = module.client("inspector") |
| 138 |
|
| 139 |
try: |
| 140 |
existing_target_arn = client.list_assessment_targets( |
| 141 |
filter={"assessmentTargetNamePattern": name}, |
| 142 |
).get( |
| 143 |
"assessmentTargetArns" |
| 144 |
)[0] |
| 145 |
|
| 146 |
existing_target = camel_dict_to_snake_dict( |
| 147 |
client.describe_assessment_targets( |
| 148 |
assessmentTargetArns=[existing_target_arn], |
| 149 |
).get( |
| 150 |
"assessmentTargets" |
| 151 |
)[0] |
| 152 |
) |
| 153 |
|
| 154 |
existing_resource_group_arn = existing_target.get("resource_group_arn") |
| 155 |
existing_resource_group_tags = ( |
| 156 |
client.describe_resource_groups( |
| 157 |
resourceGroupArns=[existing_resource_group_arn], |
| 158 |
) |
| 159 |
.get("resourceGroups")[0] |
| 160 |
.get("tags") |
| 161 |
) |
| 162 |
|
| 163 |
target_exists = True |
| 164 |
except ( |
| 165 |
botocore.exceptions.BotoCoreError, |
| 166 |
botocore.exceptions.ClientError, |
| 167 |
) as e: |
| 168 |
module.fail_json_aws(e, msg="trying to retrieve targets") |
| 169 |
except IndexError: |
| 170 |
target_exists = False |
| 171 |
|
| 172 |
if state == "present" and target_exists: |
| 173 |
ansible_dict_tags = boto3_tag_list_to_ansible_dict(tags) |
| 174 |
ansible_dict_existing_tags = boto3_tag_list_to_ansible_dict(existing_resource_group_tags) |
| 175 |
tags_to_add, tags_to_remove = compare_aws_tags(ansible_dict_tags, ansible_dict_existing_tags) |
| 176 |
if not (tags_to_add or tags_to_remove): |
| 177 |
existing_target.update({"tags": ansible_dict_existing_tags}) |
| 178 |
module.exit_json(changed=False, **existing_target) |
| 179 |
else: |
| 180 |
try: |
| 181 |
updated_resource_group_arn = client.create_resource_group( |
| 182 |
resourceGroupTags=tags, |
| 183 |
).get("resourceGroupArn") |
| 184 |
|
| 185 |
client.update_assessment_target( |
| 186 |
assessmentTargetArn=existing_target_arn, |
| 187 |
assessmentTargetName=name, |
| 188 |
resourceGroupArn=updated_resource_group_arn, |
| 189 |
) |
| 190 |
|
| 191 |
updated_target = camel_dict_to_snake_dict( |
| 192 |
client.describe_assessment_targets( |
| 193 |
assessmentTargetArns=[existing_target_arn], |
| 194 |
).get( |
| 195 |
"assessmentTargets" |
| 196 |
)[0] |
| 197 |
) |
| 198 |
|
| 199 |
updated_target.update({"tags": ansible_dict_tags}) |
| 200 |
module.exit_json(changed=True, **updated_target) |
| 201 |
except ( |
| 202 |
botocore.exceptions.BotoCoreError, |
| 203 |
botocore.exceptions.ClientError, |
| 204 |
) as e: |
| 205 |
module.fail_json_aws(e, msg="trying to update target") |
| 206 |
|
| 207 |
elif state == "present" and not target_exists: |
| 208 |
try: |
| 209 |
new_resource_group_arn = client.create_resource_group( |
| 210 |
resourceGroupTags=tags, |
| 211 |
).get("resourceGroupArn") |
| 212 |
|
| 213 |
new_target_arn = client.create_assessment_target( |
| 214 |
assessmentTargetName=name, |
| 215 |
resourceGroupArn=new_resource_group_arn, |
| 216 |
).get("assessmentTargetArn") |
| 217 |
|
| 218 |
new_target = camel_dict_to_snake_dict( |
| 219 |
client.describe_assessment_targets( |
| 220 |
assessmentTargetArns=[new_target_arn], |
| 221 |
).get( |
| 222 |
"assessmentTargets" |
| 223 |
)[0] |
| 224 |
) |
| 225 |
|
| 226 |
new_target.update({"tags": boto3_tag_list_to_ansible_dict(tags)}) |
| 227 |
module.exit_json(changed=True, **new_target) |
| 228 |
except ( |
| 229 |
botocore.exceptions.BotoCoreError, |
| 230 |
botocore.exceptions.ClientError, |
| 231 |
) as e: |
| 232 |
module.fail_json_aws(e, msg="trying to create target") |
| 233 |
|
| 234 |
elif state == "absent" and target_exists: |
| 235 |
try: |
| 236 |
client.delete_assessment_target( |
| 237 |
assessmentTargetArn=existing_target_arn, |
| 238 |
) |
| 239 |
module.exit_json(changed=True) |
| 240 |
except ( |
| 241 |
botocore.exceptions.BotoCoreError, |
| 242 |
botocore.exceptions.ClientError, |
| 243 |
) as e: |
| 244 |
module.fail_json_aws(e, msg="trying to delete target") |
| 245 |
|
| 246 |
elif state == "absent" and not target_exists: |
| 247 |
module.exit_json(changed=False) |
| 248 |
|
| 249 |
|
| 250 |
if __name__ == "__main__": |
| 251 |
main() |