| 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: accessanalyzer_validate_policy_info |
| 10 |
version_added: 5.0.0 |
| 11 |
short_description: Performs validation of IAM policies |
| 12 |
description: |
| 13 |
- Requests the validation of a policy and returns a list of findings. |
| 14 |
options: |
| 15 |
policy: |
| 16 |
description: |
| 17 |
- A properly json formatted policy. |
| 18 |
type: json |
| 19 |
aliases: ['policy_document'] |
| 20 |
required: true |
| 21 |
locale: |
| 22 |
description: |
| 23 |
- The locale to use for localizing the findings. |
| 24 |
- Supported locales include C(DE), C(EN), C(ES), C(FR), C(IT), C(JA), C(KO), C(PT_BR), |
| 25 |
C(ZH_CN) and C(ZH_TW). |
| 26 |
- For more information about supported locales see the AWS Documentation |
| 27 |
C(https://docs.aws.amazon.com/access-analyzer/latest/APIReference/API_ValidatePolicy.html) |
| 28 |
type: str |
| 29 |
required: false |
| 30 |
default: 'EN' |
| 31 |
policy_type: |
| 32 |
description: |
| 33 |
- The type of policy to validate. |
| 34 |
- C(identity) policies grant permissions to IAM principals, including both managed and inline |
| 35 |
policies for IAM roles, users, and groups. |
| 36 |
- C(resource) policies policies grant permissions on AWS resources, including trust policies |
| 37 |
for IAM roles and bucket policies for S3 buckets. |
| 38 |
type: str |
| 39 |
choices: ['identity', 'resource', 'service_control'] |
| 40 |
default: 'identity' |
| 41 |
required: false |
| 42 |
resource_type: |
| 43 |
description: |
| 44 |
- The type of resource to attach to your resource policy. |
| 45 |
- Ignored unless I(policy_type=resource). |
| 46 |
- Supported resource types include C(AWS::S3::Bucket), C(AWS::S3::AccessPoint), |
| 47 |
C(AWS::S3::MultiRegionAccessPoint) and C(AWS::S3ObjectLambda::AccessPoint) |
| 48 |
- For resource types not supported as valid values, IAM Access Analyzer runs policy checks |
| 49 |
that apply to all resource policies. |
| 50 |
- For more information about supported locales see the AWS Documentation |
| 51 |
C(https://docs.aws.amazon.com/access-analyzer/latest/APIReference/API_ValidatePolicy.html) |
| 52 |
type: str |
| 53 |
required: false |
| 54 |
results_filter: |
| 55 |
description: |
| 56 |
- Filter the findings and limit them to specific finding types. |
| 57 |
type: list |
| 58 |
elements: str |
| 59 |
choices: ['error', 'security', 'suggestion', 'warning'] |
| 60 |
required: false |
| 61 |
author: |
| 62 |
- Mark Chappell (@tremble) |
| 63 |
extends_documentation_fragment: |
| 64 |
- amazon.aws.common.modules |
| 65 |
- amazon.aws.region.modules |
| 66 |
- amazon.aws.boto3 |
| 67 |
""" |
| 68 |
|
| 69 |
EXAMPLES = r""" |
| 70 |
# Validate a policy |
| 71 |
- name: Validate a simple IAM policy |
| 72 |
community.aws.accessanalyzer_validate_policy_info: |
| 73 |
policy: "{{ lookup('template', 'managed_policy.json.j2') }}" |
| 74 |
""" |
| 75 |
|
| 76 |
RETURN = r""" |
| 77 |
findings: |
| 78 |
description: The list of findings in a policy returned by IAM Access Analyzer based on its suite of policy checks. |
| 79 |
returned: success |
| 80 |
type: list |
| 81 |
elements: dict |
| 82 |
contains: |
| 83 |
finding_details: |
| 84 |
description: |
| 85 |
- A localized message describing the finding. |
| 86 |
type: str |
| 87 |
returned: success |
| 88 |
sample: 'Resource ARN does not match the expected ARN format. Update the resource portion of the ARN.' |
| 89 |
finding_type: |
| 90 |
description: |
| 91 |
- The severity of the finding. |
| 92 |
type: str |
| 93 |
returned: success |
| 94 |
sample: 'ERROR' |
| 95 |
issue_code: |
| 96 |
description: |
| 97 |
- An identifier for the type of issue found. |
| 98 |
type: str |
| 99 |
returned: success |
| 100 |
sample: 'INVALID_ARN_RESOURCE' |
| 101 |
learn_more_link: |
| 102 |
description: |
| 103 |
- A link to additional information about the finding type. |
| 104 |
type: str |
| 105 |
returned: success |
| 106 |
sample: 'https://docs.aws.amazon.com/IAM/latest/UserGuide/access-analyzer-reference-policy-checks.html' |
| 107 |
locations: |
| 108 |
description: |
| 109 |
- The location of the item resulting in the recommendations. |
| 110 |
type: list |
| 111 |
returned: success |
| 112 |
elements: dict |
| 113 |
contains: |
| 114 |
path: |
| 115 |
description: A path in a policy, represented as a sequence of path elements. |
| 116 |
type: list |
| 117 |
elements: dict |
| 118 |
returned: success |
| 119 |
sample: [{"value": "Statement"}, {"index": 0}, {"value": "Resource"}, {"index": 0}] |
| 120 |
span: |
| 121 |
description: |
| 122 |
- Where in the policy the finding refers to. |
| 123 |
- Note - when using lookups or passing dictionaries to I(policy) the policy string may be |
| 124 |
converted to a single line of JSON, changing th column, line and offset values. |
| 125 |
type: dict |
| 126 |
contains: |
| 127 |
start: |
| 128 |
description: The start position of the span. |
| 129 |
type: dict |
| 130 |
returned: success |
| 131 |
contains: |
| 132 |
column: |
| 133 |
description: The column of the position, starting from C(0). |
| 134 |
type: int |
| 135 |
returned: success |
| 136 |
line: |
| 137 |
description: The line of the position, starting from C(1). |
| 138 |
type: int |
| 139 |
returned: success |
| 140 |
offset: |
| 141 |
description: The offset within the policy that corresponds to the position, starting from C(0). |
| 142 |
type: int |
| 143 |
returned: success |
| 144 |
end: |
| 145 |
description: The end position of the span. |
| 146 |
type: dict |
| 147 |
returned: success |
| 148 |
contains: |
| 149 |
column: |
| 150 |
description: The column of the position, starting from C(0). |
| 151 |
type: int |
| 152 |
returned: success |
| 153 |
line: |
| 154 |
description: The line of the position, starting from C(1). |
| 155 |
type: int |
| 156 |
returned: success |
| 157 |
offset: |
| 158 |
description: The offset within the policy that corresponds to the position, starting from C(0). |
| 159 |
type: int |
| 160 |
returned: success |
| 161 |
""" |
| 162 |
|
| 163 |
try: |
| 164 |
import botocore |
| 165 |
except ImportError: |
| 166 |
pass # Handled by AnsibleAWSModule |
| 167 |
|
| 168 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 169 |
|
| 170 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 171 |
|
| 172 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 173 |
|
| 174 |
|
| 175 |
def filter_findings(findings, type_filter): |
| 176 |
if not type_filter: |
| 177 |
return findings |
| 178 |
|
| 179 |
# Convert type_filter to the findingType strings returned by the API |
| 180 |
filter_map = dict(error="ERROR", security="SECURITY_WARNING", suggestion="SUGGESTION", warning="WARNING") |
| 181 |
allowed_types = [filter_map[t] for t in type_filter] |
| 182 |
|
| 183 |
filtered_results = [f for f in findings if f.get("findingType", None) in allowed_types] |
| 184 |
return filtered_results |
| 185 |
|
| 186 |
|
| 187 |
def main(): |
| 188 |
# Botocore only supports specific values for locale and resource_type, however the supported |
| 189 |
# values are likely to be expanded, let's avoid hard coding limits which might not hold true in |
| 190 |
# the long term... |
| 191 |
argument_spec = dict( |
| 192 |
policy=dict(required=True, type="json", aliases=["policy_document"]), |
| 193 |
locale=dict(required=False, type="str", default="EN"), |
| 194 |
policy_type=dict( |
| 195 |
required=False, type="str", default="identity", choices=["identity", "resource", "service_control"] |
| 196 |
), |
| 197 |
resource_type=dict(required=False, type="str"), |
| 198 |
results_filter=dict( |
| 199 |
required=False, type="list", elements="str", choices=["error", "security", "suggestion", "warning"] |
| 200 |
), |
| 201 |
) |
| 202 |
|
| 203 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 204 |
|
| 205 |
policy_type_map = dict( |
| 206 |
identity="IDENTITY_POLICY", resource="RESOURCE_POLICY", service_control="SERVICE_CONTROL_POLICY" |
| 207 |
) |
| 208 |
|
| 209 |
policy = module.params.get("policy") |
| 210 |
policy_type = policy_type_map[module.params.get("policy_type")] |
| 211 |
locale = module.params.get("locale").upper() |
| 212 |
resource_type = module.params.get("resource_type") |
| 213 |
results_filter = module.params.get("results_filter") |
| 214 |
|
| 215 |
try: |
| 216 |
client = module.client("accessanalyzer", retry_decorator=AWSRetry.jittered_backoff()) |
| 217 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 218 |
module.fail_json_aws(e, msg="Failed to connect to AWS") |
| 219 |
|
| 220 |
params = dict(locale=locale, policyDocument=policy, policyType=policy_type) |
| 221 |
if policy_type == "RESOURCE_POLICY" and resource_type: |
| 222 |
params["policyType"] = resource_type |
| 223 |
|
| 224 |
results = client.validate_policy(aws_retry=True, **params) |
| 225 |
|
| 226 |
findings = filter_findings(results.get("findings", []), results_filter) |
| 227 |
results["findings"] = findings |
| 228 |
|
| 229 |
results = camel_dict_to_snake_dict(results) |
| 230 |
|
| 231 |
module.exit_json(changed=False, **results) |
| 232 |
|
| 233 |
|
| 234 |
if __name__ == "__main__": |
| 235 |
main() |