| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Aaron Smith <[email protected]> |
| 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: config_rule |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Manage AWS Config rule resources |
| 12 |
description: |
| 13 |
- Module manages AWS Config rules. |
| 14 |
- Prior to release 5.0.0 this module was called C(community.aws.aws_config_rule). |
| 15 |
The usage did not change. |
| 16 |
author: |
| 17 |
- "Aaron Smith (@slapula)" |
| 18 |
options: |
| 19 |
name: |
| 20 |
description: |
| 21 |
- The name of the AWS Config resource. |
| 22 |
required: true |
| 23 |
type: str |
| 24 |
state: |
| 25 |
description: |
| 26 |
- Whether the Config rule should be present or absent. |
| 27 |
default: present |
| 28 |
choices: ['present', 'absent'] |
| 29 |
type: str |
| 30 |
description: |
| 31 |
description: |
| 32 |
- The description that you provide for the AWS Config rule. |
| 33 |
type: str |
| 34 |
scope: |
| 35 |
description: |
| 36 |
- Defines which resources can trigger an evaluation for the rule. |
| 37 |
suboptions: |
| 38 |
compliance_types: |
| 39 |
description: |
| 40 |
- The resource types of only those AWS resources that you want to trigger an evaluation for the rule. |
| 41 |
You can only specify one type if you also specify a resource ID for I(compliance_id). |
| 42 |
compliance_id: |
| 43 |
description: |
| 44 |
- The ID of the only AWS resource that you want to trigger an evaluation for the rule. If you specify a resource ID, |
| 45 |
you must specify one resource type for I(compliance_types). |
| 46 |
tag_key: |
| 47 |
description: |
| 48 |
- The tag key that is applied to only those AWS resources that you want to trigger an evaluation for the rule. |
| 49 |
tag_value: |
| 50 |
description: |
| 51 |
- The tag value applied to only those AWS resources that you want to trigger an evaluation for the rule. |
| 52 |
If you specify a value for I(tag_value), you must also specify a value for I(tag_key). |
| 53 |
type: dict |
| 54 |
source: |
| 55 |
description: |
| 56 |
- Provides the rule owner (AWS or customer), the rule identifier, and the notifications that cause the function to |
| 57 |
evaluate your AWS resources. |
| 58 |
suboptions: |
| 59 |
owner: |
| 60 |
description: |
| 61 |
- The resource types of only those AWS resources that you want to trigger an evaluation for the rule. |
| 62 |
You can only specify one type if you also specify a resource ID for I(compliance_id). |
| 63 |
identifier: |
| 64 |
description: |
| 65 |
- The ID of the only AWS resource that you want to trigger an evaluation for the rule. |
| 66 |
If you specify a resource ID, you must specify one resource type for I(compliance_types). |
| 67 |
details: |
| 68 |
description: |
| 69 |
- Provides the source and type of the event that causes AWS Config to evaluate your AWS resources. |
| 70 |
- This parameter expects a list of dictionaries. Each dictionary expects the following key/value pairs. |
| 71 |
- Key C(EventSource) The source of the event, such as an AWS service, that triggers AWS Config to evaluate your AWS resources. |
| 72 |
- Key C(MessageType) The type of notification that triggers AWS Config to run an evaluation for a rule. |
| 73 |
- Key C(MaximumExecutionFrequency) The frequency at which you want AWS Config to run evaluations for a custom rule with a periodic trigger. |
| 74 |
type: dict |
| 75 |
required: true |
| 76 |
input_parameters: |
| 77 |
description: |
| 78 |
- A string, in JSON format, that is passed to the AWS Config rule Lambda function. |
| 79 |
type: str |
| 80 |
execution_frequency: |
| 81 |
description: |
| 82 |
- The maximum frequency with which AWS Config runs evaluations for a rule. |
| 83 |
choices: ['One_Hour', 'Three_Hours', 'Six_Hours', 'Twelve_Hours', 'TwentyFour_Hours'] |
| 84 |
type: str |
| 85 |
extends_documentation_fragment: |
| 86 |
- amazon.aws.common.modules |
| 87 |
- amazon.aws.region.modules |
| 88 |
- amazon.aws.boto3 |
| 89 |
""" |
| 90 |
|
| 91 |
EXAMPLES = r""" |
| 92 |
- name: Create Config Rule for AWS Config |
| 93 |
community.aws.config_rule: |
| 94 |
name: test_config_rule |
| 95 |
state: present |
| 96 |
description: 'This AWS Config rule checks for public write access on S3 buckets' |
| 97 |
scope: |
| 98 |
compliance_types: |
| 99 |
- 'AWS::S3::Bucket' |
| 100 |
source: |
| 101 |
owner: AWS |
| 102 |
identifier: 'S3_BUCKET_PUBLIC_WRITE_PROHIBITED' |
| 103 |
""" |
| 104 |
|
| 105 |
RETURN = r"""#""" |
| 106 |
|
| 107 |
|
| 108 |
try: |
| 109 |
import botocore |
| 110 |
except ImportError: |
| 111 |
pass # handled by AnsibleAWSModule |
| 112 |
|
| 113 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 114 |
|
| 115 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code |
| 116 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 117 |
|
| 118 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 119 |
|
| 120 |
|
| 121 |
def rule_exists(client, module, params): |
| 122 |
try: |
| 123 |
rule = client.describe_config_rules( |
| 124 |
ConfigRuleNames=[params["ConfigRuleName"]], |
| 125 |
aws_retry=True, |
| 126 |
) |
| 127 |
return rule["ConfigRules"][0] |
| 128 |
except is_boto3_error_code("NoSuchConfigRuleException"): |
| 129 |
return |
| 130 |
except ( |
| 131 |
botocore.exceptions.ClientError, |
| 132 |
botocore.exceptions.BotoCoreError, |
| 133 |
) as e: # pylint: disable=duplicate-except |
| 134 |
module.fail_json_aws(e) |
| 135 |
|
| 136 |
|
| 137 |
def create_resource(client, module, params, result): |
| 138 |
try: |
| 139 |
client.put_config_rule(ConfigRule=params) |
| 140 |
result["changed"] = True |
| 141 |
return result |
| 142 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 143 |
module.fail_json_aws(e, msg="Couldn't create AWS Config rule") |
| 144 |
|
| 145 |
|
| 146 |
def update_resource(client, module, params, result): |
| 147 |
current_params = client.describe_config_rules( |
| 148 |
ConfigRuleNames=[params["ConfigRuleName"]], |
| 149 |
aws_retry=True, |
| 150 |
) |
| 151 |
|
| 152 |
del current_params["ConfigRules"][0]["ConfigRuleArn"] |
| 153 |
del current_params["ConfigRules"][0]["ConfigRuleId"] |
| 154 |
del current_params["ConfigRules"][0]["EvaluationModes"] |
| 155 |
|
| 156 |
if params != current_params["ConfigRules"][0]: |
| 157 |
try: |
| 158 |
client.put_config_rule(ConfigRule=params) |
| 159 |
result["changed"] = True |
| 160 |
result["rule"] = camel_dict_to_snake_dict(rule_exists(client, module, params)) |
| 161 |
return result |
| 162 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 163 |
module.fail_json_aws(e, msg="Couldn't create AWS Config rule") |
| 164 |
|
| 165 |
|
| 166 |
def delete_resource(client, module, params, result): |
| 167 |
try: |
| 168 |
client.delete_config_rule( |
| 169 |
ConfigRuleName=params["ConfigRuleName"], |
| 170 |
aws_retry=True, |
| 171 |
) |
| 172 |
result["changed"] = True |
| 173 |
result["rule"] = {} |
| 174 |
return result |
| 175 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 176 |
module.fail_json_aws(e, msg="Couldn't delete AWS Config rule") |
| 177 |
|
| 178 |
|
| 179 |
def main(): |
| 180 |
module = AnsibleAWSModule( |
| 181 |
argument_spec={ |
| 182 |
"name": dict(type="str", required=True), |
| 183 |
"state": dict(type="str", choices=["present", "absent"], default="present"), |
| 184 |
"description": dict(type="str"), |
| 185 |
"scope": dict(type="dict"), |
| 186 |
"source": dict(type="dict", required=True), |
| 187 |
"input_parameters": dict(type="str"), |
| 188 |
"execution_frequency": dict( |
| 189 |
type="str", |
| 190 |
choices=[ |
| 191 |
"One_Hour", |
| 192 |
"Three_Hours", |
| 193 |
"Six_Hours", |
| 194 |
"Twelve_Hours", |
| 195 |
"TwentyFour_Hours", |
| 196 |
], |
| 197 |
), |
| 198 |
}, |
| 199 |
supports_check_mode=False, |
| 200 |
) |
| 201 |
|
| 202 |
result = {"changed": False} |
| 203 |
|
| 204 |
name = module.params.get("name") |
| 205 |
state = module.params.get("state") |
| 206 |
|
| 207 |
params = {} |
| 208 |
if name: |
| 209 |
params["ConfigRuleName"] = name |
| 210 |
if module.params.get("description"): |
| 211 |
params["Description"] = module.params.get("description") |
| 212 |
if module.params.get("scope"): |
| 213 |
params["Scope"] = {} |
| 214 |
if module.params.get("scope").get("compliance_types"): |
| 215 |
params["Scope"].update( |
| 216 |
{ |
| 217 |
"ComplianceResourceTypes": module.params.get("scope").get("compliance_types"), |
| 218 |
} |
| 219 |
) |
| 220 |
if module.params.get("scope").get("tag_key"): |
| 221 |
params["Scope"].update( |
| 222 |
{ |
| 223 |
"TagKey": module.params.get("scope").get("tag_key"), |
| 224 |
} |
| 225 |
) |
| 226 |
if module.params.get("scope").get("tag_value"): |
| 227 |
params["Scope"].update( |
| 228 |
{ |
| 229 |
"TagValue": module.params.get("scope").get("tag_value"), |
| 230 |
} |
| 231 |
) |
| 232 |
if module.params.get("scope").get("compliance_id"): |
| 233 |
params["Scope"].update( |
| 234 |
{ |
| 235 |
"ComplianceResourceId": module.params.get("scope").get("compliance_id"), |
| 236 |
} |
| 237 |
) |
| 238 |
if module.params.get("source"): |
| 239 |
params["Source"] = {} |
| 240 |
if module.params.get("source").get("owner"): |
| 241 |
params["Source"].update( |
| 242 |
{ |
| 243 |
"Owner": module.params.get("source").get("owner"), |
| 244 |
} |
| 245 |
) |
| 246 |
if module.params.get("source").get("identifier"): |
| 247 |
params["Source"].update( |
| 248 |
{ |
| 249 |
"SourceIdentifier": module.params.get("source").get("identifier"), |
| 250 |
} |
| 251 |
) |
| 252 |
if module.params.get("source").get("details"): |
| 253 |
params["Source"].update( |
| 254 |
{ |
| 255 |
"SourceDetails": module.params.get("source").get("details"), |
| 256 |
} |
| 257 |
) |
| 258 |
if module.params.get("input_parameters"): |
| 259 |
params["InputParameters"] = module.params.get("input_parameters") |
| 260 |
if module.params.get("execution_frequency"): |
| 261 |
params["MaximumExecutionFrequency"] = module.params.get("execution_frequency") |
| 262 |
params["ConfigRuleState"] = "ACTIVE" |
| 263 |
|
| 264 |
client = module.client("config", retry_decorator=AWSRetry.jittered_backoff()) |
| 265 |
|
| 266 |
existing_rule = rule_exists(client, module, params) |
| 267 |
|
| 268 |
if state == "present": |
| 269 |
if not existing_rule: |
| 270 |
create_resource(client, module, params, result) |
| 271 |
else: |
| 272 |
update_resource(client, module, params, result) |
| 273 |
|
| 274 |
if state == "absent": |
| 275 |
if existing_rule: |
| 276 |
delete_resource(client, module, params, result) |
| 277 |
|
| 278 |
module.exit_json(**result) |
| 279 |
|
| 280 |
|
| 281 |
if __name__ == "__main__": |
| 282 |
main() |