iam-policy-validator 1.7.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of iam-policy-validator might be problematic. Click here for more details.
- iam_policy_validator-1.7.0.dist-info/METADATA +1057 -0
- iam_policy_validator-1.7.0.dist-info/RECORD +83 -0
- iam_policy_validator-1.7.0.dist-info/WHEEL +4 -0
- iam_policy_validator-1.7.0.dist-info/entry_points.txt +2 -0
- iam_policy_validator-1.7.0.dist-info/licenses/LICENSE +21 -0
- iam_validator/__init__.py +27 -0
- iam_validator/__main__.py +11 -0
- iam_validator/__version__.py +7 -0
- iam_validator/checks/__init__.py +43 -0
- iam_validator/checks/action_condition_enforcement.py +884 -0
- iam_validator/checks/action_resource_matching.py +441 -0
- iam_validator/checks/action_validation.py +72 -0
- iam_validator/checks/condition_key_validation.py +92 -0
- iam_validator/checks/condition_type_mismatch.py +259 -0
- iam_validator/checks/full_wildcard.py +71 -0
- iam_validator/checks/mfa_condition_check.py +112 -0
- iam_validator/checks/policy_size.py +147 -0
- iam_validator/checks/policy_type_validation.py +305 -0
- iam_validator/checks/principal_validation.py +776 -0
- iam_validator/checks/resource_validation.py +138 -0
- iam_validator/checks/sensitive_action.py +254 -0
- iam_validator/checks/service_wildcard.py +107 -0
- iam_validator/checks/set_operator_validation.py +157 -0
- iam_validator/checks/sid_uniqueness.py +170 -0
- iam_validator/checks/utils/__init__.py +1 -0
- iam_validator/checks/utils/policy_level_checks.py +143 -0
- iam_validator/checks/utils/sensitive_action_matcher.py +294 -0
- iam_validator/checks/utils/wildcard_expansion.py +87 -0
- iam_validator/checks/wildcard_action.py +67 -0
- iam_validator/checks/wildcard_resource.py +135 -0
- iam_validator/commands/__init__.py +25 -0
- iam_validator/commands/analyze.py +531 -0
- iam_validator/commands/base.py +48 -0
- iam_validator/commands/cache.py +392 -0
- iam_validator/commands/download_services.py +255 -0
- iam_validator/commands/post_to_pr.py +86 -0
- iam_validator/commands/validate.py +600 -0
- iam_validator/core/__init__.py +14 -0
- iam_validator/core/access_analyzer.py +671 -0
- iam_validator/core/access_analyzer_report.py +640 -0
- iam_validator/core/aws_fetcher.py +940 -0
- iam_validator/core/check_registry.py +607 -0
- iam_validator/core/cli.py +134 -0
- iam_validator/core/condition_validators.py +626 -0
- iam_validator/core/config/__init__.py +81 -0
- iam_validator/core/config/aws_api.py +35 -0
- iam_validator/core/config/aws_global_conditions.py +160 -0
- iam_validator/core/config/category_suggestions.py +104 -0
- iam_validator/core/config/condition_requirements.py +155 -0
- iam_validator/core/config/config_loader.py +472 -0
- iam_validator/core/config/defaults.py +523 -0
- iam_validator/core/config/principal_requirements.py +421 -0
- iam_validator/core/config/sensitive_actions.py +672 -0
- iam_validator/core/config/service_principals.py +95 -0
- iam_validator/core/config/wildcards.py +124 -0
- iam_validator/core/constants.py +74 -0
- iam_validator/core/formatters/__init__.py +27 -0
- iam_validator/core/formatters/base.py +147 -0
- iam_validator/core/formatters/console.py +59 -0
- iam_validator/core/formatters/csv.py +170 -0
- iam_validator/core/formatters/enhanced.py +440 -0
- iam_validator/core/formatters/html.py +672 -0
- iam_validator/core/formatters/json.py +33 -0
- iam_validator/core/formatters/markdown.py +63 -0
- iam_validator/core/formatters/sarif.py +251 -0
- iam_validator/core/models.py +327 -0
- iam_validator/core/policy_checks.py +656 -0
- iam_validator/core/policy_loader.py +396 -0
- iam_validator/core/pr_commenter.py +424 -0
- iam_validator/core/report.py +872 -0
- iam_validator/integrations/__init__.py +28 -0
- iam_validator/integrations/github_integration.py +815 -0
- iam_validator/integrations/ms_teams.py +442 -0
- iam_validator/sdk/__init__.py +187 -0
- iam_validator/sdk/arn_matching.py +382 -0
- iam_validator/sdk/context.py +222 -0
- iam_validator/sdk/exceptions.py +48 -0
- iam_validator/sdk/helpers.py +177 -0
- iam_validator/sdk/policy_utils.py +425 -0
- iam_validator/sdk/shortcuts.py +283 -0
- iam_validator/utils/__init__.py +31 -0
- iam_validator/utils/cache.py +105 -0
- iam_validator/utils/regex.py +206 -0
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Resource-Action Matching check.
|
|
3
|
+
|
|
4
|
+
This check validates that resources in a policy statement match the required
|
|
5
|
+
resource types for the actions. This catches common mistakes like:
|
|
6
|
+
|
|
7
|
+
- s3:GetObject with bucket ARN (needs object ARN: arn:aws:s3:::bucket/*)
|
|
8
|
+
- s3:ListBucket with object ARN (needs bucket ARN: arn:aws:s3:::bucket)
|
|
9
|
+
- iam:ListUsers with user ARN (needs wildcard: *)
|
|
10
|
+
|
|
11
|
+
This is inspired by Parliament's RESOURCE_MISMATCH check.
|
|
12
|
+
|
|
13
|
+
Example:
|
|
14
|
+
Policy with mismatch:
|
|
15
|
+
{
|
|
16
|
+
"Effect": "Allow",
|
|
17
|
+
"Action": "s3:GetObject",
|
|
18
|
+
"Resource": "arn:aws:s3:::mybucket" # Missing /* for object path!
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
This check will report: s3:GetObject requires arn:aws:s3:::mybucket/*
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
from iam_validator.core.aws_fetcher import AWSServiceFetcher
|
|
25
|
+
from iam_validator.core.check_registry import CheckConfig, PolicyCheck
|
|
26
|
+
from iam_validator.core.models import Statement, ValidationIssue
|
|
27
|
+
from iam_validator.sdk.arn_matching import (
|
|
28
|
+
arn_strictly_valid,
|
|
29
|
+
convert_aws_pattern_to_wildcard,
|
|
30
|
+
has_template_variables,
|
|
31
|
+
normalize_template_variables,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class ActionResourceMatchingCheck(PolicyCheck):
|
|
36
|
+
"""
|
|
37
|
+
Validates that resources match the required types for actions.
|
|
38
|
+
|
|
39
|
+
This check helps identify policies that are syntactically valid but won't
|
|
40
|
+
work as intended because the resource ARNs don't match what the action requires.
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
@property
|
|
44
|
+
def check_id(self) -> str:
|
|
45
|
+
return "action_resource_matching"
|
|
46
|
+
|
|
47
|
+
@property
|
|
48
|
+
def description(self) -> str:
|
|
49
|
+
return "Validates that resources match required types for actions"
|
|
50
|
+
|
|
51
|
+
@property
|
|
52
|
+
def default_severity(self) -> str:
|
|
53
|
+
return "medium" # Security issue, not IAM validity error
|
|
54
|
+
|
|
55
|
+
async def execute(
|
|
56
|
+
self,
|
|
57
|
+
statement: Statement,
|
|
58
|
+
statement_idx: int,
|
|
59
|
+
fetcher: AWSServiceFetcher,
|
|
60
|
+
config: CheckConfig,
|
|
61
|
+
) -> list[ValidationIssue]:
|
|
62
|
+
"""
|
|
63
|
+
Execute resource-action matching validation on a statement.
|
|
64
|
+
|
|
65
|
+
Args:
|
|
66
|
+
statement: The IAM policy statement to check
|
|
67
|
+
statement_idx: Index of the statement in the policy
|
|
68
|
+
fetcher: AWS service fetcher for action definitions
|
|
69
|
+
config: Configuration for this check
|
|
70
|
+
|
|
71
|
+
Returns:
|
|
72
|
+
List of ValidationIssue objects for resource mismatches
|
|
73
|
+
"""
|
|
74
|
+
issues = []
|
|
75
|
+
|
|
76
|
+
# Check if template variable support is enabled (default: true)
|
|
77
|
+
# Try global settings first, then check-specific config
|
|
78
|
+
allow_template_variables = config.root_config.get("settings", {}).get(
|
|
79
|
+
"allow_template_variables",
|
|
80
|
+
config.config.get("allow_template_variables", True),
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
# Get actions and resources
|
|
84
|
+
actions = statement.get_actions()
|
|
85
|
+
resources = statement.get_resources()
|
|
86
|
+
statement_sid = statement.sid
|
|
87
|
+
line_number = statement.line_number
|
|
88
|
+
|
|
89
|
+
# Skip if we have a wildcard resource (handled by other checks)
|
|
90
|
+
if "*" in resources:
|
|
91
|
+
return issues
|
|
92
|
+
|
|
93
|
+
# Check each action
|
|
94
|
+
for action in actions:
|
|
95
|
+
# Skip wildcard actions
|
|
96
|
+
if action == "*" or ":" not in action:
|
|
97
|
+
continue
|
|
98
|
+
|
|
99
|
+
# Parse service and action name
|
|
100
|
+
try:
|
|
101
|
+
service, action_name = action.split(":", 1)
|
|
102
|
+
except ValueError:
|
|
103
|
+
continue # Invalid action format, handled by action_validation
|
|
104
|
+
|
|
105
|
+
# Skip wildcard actions
|
|
106
|
+
if "*" in service or "*" in action_name:
|
|
107
|
+
continue
|
|
108
|
+
|
|
109
|
+
# Get service definition
|
|
110
|
+
service_detail = await fetcher.fetch_service_by_name(service)
|
|
111
|
+
if not service_detail:
|
|
112
|
+
continue # Unknown service, handled by action_validation
|
|
113
|
+
|
|
114
|
+
# Get action definition
|
|
115
|
+
action_detail = service_detail.actions.get(action_name)
|
|
116
|
+
if not action_detail:
|
|
117
|
+
continue # Unknown action, handled by action_validation
|
|
118
|
+
|
|
119
|
+
# Get required resource types for this action
|
|
120
|
+
required_resources = action_detail.resources or []
|
|
121
|
+
|
|
122
|
+
# If action requires no specific resources, it needs Resource: "*"
|
|
123
|
+
if not required_resources:
|
|
124
|
+
# Check if all resources are "*"
|
|
125
|
+
if not all(r == "*" for r in resources):
|
|
126
|
+
issues.append(
|
|
127
|
+
self._create_mismatch_issue(
|
|
128
|
+
action=action,
|
|
129
|
+
required_format="*",
|
|
130
|
+
required_type="*",
|
|
131
|
+
provided_resources=resources,
|
|
132
|
+
statement_idx=statement_idx,
|
|
133
|
+
statement_sid=statement_sid,
|
|
134
|
+
line_number=line_number,
|
|
135
|
+
config=config,
|
|
136
|
+
reason=f'Action {action} can only use Resource: "*"',
|
|
137
|
+
)
|
|
138
|
+
)
|
|
139
|
+
continue
|
|
140
|
+
|
|
141
|
+
# Check if ANY policy resource matches ANY required resource type
|
|
142
|
+
match_found = False
|
|
143
|
+
|
|
144
|
+
for req_resource in required_resources:
|
|
145
|
+
# Get the resource type name from the action's required resources
|
|
146
|
+
resource_name = req_resource.get("Name", "")
|
|
147
|
+
if not resource_name:
|
|
148
|
+
continue
|
|
149
|
+
|
|
150
|
+
# Look up the full resource type definition in the service's resources
|
|
151
|
+
# The action's Resources field only has names like {"Name": "object"}
|
|
152
|
+
# The service's Resources field has full definitions with ARN formats
|
|
153
|
+
resource_type = service_detail.resources.get(resource_name)
|
|
154
|
+
if not resource_type:
|
|
155
|
+
continue
|
|
156
|
+
|
|
157
|
+
# Get the ARN pattern (first format from ARNFormats array)
|
|
158
|
+
arn_pattern = resource_type.arn_pattern
|
|
159
|
+
if not arn_pattern:
|
|
160
|
+
continue
|
|
161
|
+
|
|
162
|
+
# Convert AWS pattern format (${Partition}, ${BucketName}) to wildcards (*)
|
|
163
|
+
# AWS provides patterns like: arn:${Partition}:s3:::${BucketName}/${ObjectName}
|
|
164
|
+
# We need wildcards like: arn:*:s3:::*/*
|
|
165
|
+
wildcard_pattern = convert_aws_pattern_to_wildcard(arn_pattern)
|
|
166
|
+
|
|
167
|
+
# Check if any policy resource matches this ARN pattern
|
|
168
|
+
for resource in resources:
|
|
169
|
+
# Normalize template variables (Terraform/CloudFormation) before matching
|
|
170
|
+
# This allows policies with ${aws_account_id}, ${AWS::AccountId}, etc.
|
|
171
|
+
validation_resource = resource
|
|
172
|
+
if allow_template_variables and has_template_variables(resource):
|
|
173
|
+
validation_resource = normalize_template_variables(resource)
|
|
174
|
+
|
|
175
|
+
if arn_strictly_valid(wildcard_pattern, validation_resource, resource_name):
|
|
176
|
+
match_found = True
|
|
177
|
+
break
|
|
178
|
+
|
|
179
|
+
if match_found:
|
|
180
|
+
break
|
|
181
|
+
|
|
182
|
+
# If no match found, create an issue
|
|
183
|
+
if not match_found and required_resources:
|
|
184
|
+
# Build helpful error message with required formats
|
|
185
|
+
# Look up each resource type in the service to get ARN patterns
|
|
186
|
+
required_formats = []
|
|
187
|
+
for req_res in required_resources:
|
|
188
|
+
res_name = req_res.get("Name", "")
|
|
189
|
+
if not res_name:
|
|
190
|
+
continue
|
|
191
|
+
res_type = service_detail.resources.get(res_name)
|
|
192
|
+
if res_type and res_type.arn_pattern:
|
|
193
|
+
required_formats.append(
|
|
194
|
+
{
|
|
195
|
+
"type": res_name,
|
|
196
|
+
"format": res_type.arn_pattern,
|
|
197
|
+
}
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
issues.append(
|
|
201
|
+
self._create_mismatch_issue(
|
|
202
|
+
action=action,
|
|
203
|
+
required_format=(required_formats[0]["format"] if required_formats else ""),
|
|
204
|
+
required_type=(required_formats[0]["type"] if required_formats else ""),
|
|
205
|
+
provided_resources=resources,
|
|
206
|
+
statement_idx=statement_idx,
|
|
207
|
+
statement_sid=statement_sid,
|
|
208
|
+
line_number=line_number,
|
|
209
|
+
config=config,
|
|
210
|
+
all_required_formats=required_formats,
|
|
211
|
+
)
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
return issues
|
|
215
|
+
|
|
216
|
+
def _create_mismatch_issue(
|
|
217
|
+
self,
|
|
218
|
+
action: str,
|
|
219
|
+
required_format: str,
|
|
220
|
+
required_type: str,
|
|
221
|
+
provided_resources: list[str],
|
|
222
|
+
statement_idx: int,
|
|
223
|
+
statement_sid: str | None,
|
|
224
|
+
line_number: int | None,
|
|
225
|
+
config: CheckConfig,
|
|
226
|
+
all_required_formats: list[dict] | None = None,
|
|
227
|
+
reason: str | None = None,
|
|
228
|
+
) -> ValidationIssue:
|
|
229
|
+
"""Create a validation issue for resource mismatch."""
|
|
230
|
+
# Build helpful message
|
|
231
|
+
if reason:
|
|
232
|
+
message = reason
|
|
233
|
+
elif all_required_formats and len(all_required_formats) > 1:
|
|
234
|
+
types = ", ".join(f["type"] for f in all_required_formats)
|
|
235
|
+
message = (
|
|
236
|
+
f"No resources match for action '{action}'. This action requires one of: {types}"
|
|
237
|
+
)
|
|
238
|
+
else:
|
|
239
|
+
message = (
|
|
240
|
+
f"No resources match for action '{action}'. "
|
|
241
|
+
f"This action requires resource type: {required_type}"
|
|
242
|
+
)
|
|
243
|
+
|
|
244
|
+
# Build suggestion with examples
|
|
245
|
+
suggestion = self._get_suggestion(action, required_format, provided_resources)
|
|
246
|
+
|
|
247
|
+
return ValidationIssue(
|
|
248
|
+
severity=self.get_severity(config),
|
|
249
|
+
statement_sid=statement_sid,
|
|
250
|
+
statement_index=statement_idx,
|
|
251
|
+
issue_type="resource_mismatch",
|
|
252
|
+
message=message,
|
|
253
|
+
action=action,
|
|
254
|
+
resource=(
|
|
255
|
+
", ".join(provided_resources)
|
|
256
|
+
if len(provided_resources) <= 3
|
|
257
|
+
else f"{provided_resources[0]}..."
|
|
258
|
+
),
|
|
259
|
+
suggestion=suggestion,
|
|
260
|
+
line_number=line_number,
|
|
261
|
+
)
|
|
262
|
+
|
|
263
|
+
def _get_suggestion(
|
|
264
|
+
self,
|
|
265
|
+
action: str,
|
|
266
|
+
required_format: str,
|
|
267
|
+
provided_resources: list[str],
|
|
268
|
+
) -> str:
|
|
269
|
+
"""
|
|
270
|
+
Generate helpful suggestion for fixing the mismatch.
|
|
271
|
+
|
|
272
|
+
This function is service-agnostic and extracts resource type information
|
|
273
|
+
from the ARN pattern to provide contextual examples.
|
|
274
|
+
"""
|
|
275
|
+
if not required_format:
|
|
276
|
+
return "Check AWS documentation for required resource types for this action"
|
|
277
|
+
|
|
278
|
+
# Extract action name for contextual hints (e.g., "GetObject" from "s3:GetObject")
|
|
279
|
+
action_name = action.split(":")[1] if ":" in action else action
|
|
280
|
+
|
|
281
|
+
# Special case: Wildcard resource
|
|
282
|
+
if required_format == "*":
|
|
283
|
+
return (
|
|
284
|
+
f'Action {action} can only use Resource: "*" (wildcard).\n'
|
|
285
|
+
f" This action does not support resource-level permissions.\n"
|
|
286
|
+
f' Example: "Resource": "*"'
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
# Extract resource type from ARN pattern
|
|
290
|
+
# Pattern format: arn:${Partition}:service:${Region}:${Account}:resourceType/...
|
|
291
|
+
# Examples:
|
|
292
|
+
# arn:${Partition}:s3:::${BucketName}/${ObjectName} -> object
|
|
293
|
+
# arn:${Partition}:iam::${Account}:user/${UserName} -> user
|
|
294
|
+
resource_type = self._extract_resource_type_from_pattern(required_format)
|
|
295
|
+
|
|
296
|
+
# Build service-specific suggestion
|
|
297
|
+
suggestion_parts = []
|
|
298
|
+
|
|
299
|
+
# Add action description
|
|
300
|
+
suggestion_parts.append(f"Action {action} requires {resource_type} resource type.")
|
|
301
|
+
|
|
302
|
+
# Add expected format
|
|
303
|
+
suggestion_parts.append(f" Expected format: {required_format}")
|
|
304
|
+
|
|
305
|
+
# Add practical example based on the pattern
|
|
306
|
+
example = self._generate_example_arn(required_format)
|
|
307
|
+
if example:
|
|
308
|
+
suggestion_parts.append(f" Example: {example}")
|
|
309
|
+
|
|
310
|
+
# Add helpful context for common patterns
|
|
311
|
+
context = self._get_resource_context(action_name, resource_type, required_format)
|
|
312
|
+
if context:
|
|
313
|
+
suggestion_parts.append(f" {context}")
|
|
314
|
+
|
|
315
|
+
suggestion = "\n".join(suggestion_parts)
|
|
316
|
+
|
|
317
|
+
# Add current resources to help user understand the mismatch
|
|
318
|
+
if provided_resources and len(provided_resources) <= 3:
|
|
319
|
+
current = ", ".join(provided_resources)
|
|
320
|
+
suggestion += f"\n Current resources: {current}"
|
|
321
|
+
|
|
322
|
+
return suggestion
|
|
323
|
+
|
|
324
|
+
def _extract_resource_type_from_pattern(self, pattern: str) -> str:
|
|
325
|
+
"""
|
|
326
|
+
Extract the resource type from an ARN pattern.
|
|
327
|
+
|
|
328
|
+
Examples:
|
|
329
|
+
arn:${Partition}:s3:::${BucketName}/${ObjectName} -> "object"
|
|
330
|
+
arn:${Partition}:iam::${Account}:user/${UserName} -> "user"
|
|
331
|
+
arn:${Partition}:ec2:${Region}:${Account}:instance/${InstanceId} -> "instance"
|
|
332
|
+
"""
|
|
333
|
+
# Split ARN by colons to get resource part
|
|
334
|
+
parts = pattern.split(":")
|
|
335
|
+
if len(parts) < 6:
|
|
336
|
+
return "resource"
|
|
337
|
+
|
|
338
|
+
# Resource part is everything after the 5th colon
|
|
339
|
+
resource_part = ":".join(parts[5:])
|
|
340
|
+
|
|
341
|
+
# Extract resource type (part before / or entire string)
|
|
342
|
+
if "/" in resource_part:
|
|
343
|
+
resource_type = resource_part.split("/")[0]
|
|
344
|
+
elif ":" in resource_part:
|
|
345
|
+
resource_type = resource_part.split(":")[0]
|
|
346
|
+
else:
|
|
347
|
+
resource_type = resource_part
|
|
348
|
+
|
|
349
|
+
# Remove template variables like ${...}
|
|
350
|
+
import re
|
|
351
|
+
|
|
352
|
+
resource_type = re.sub(r"\$\{[^}]+\}", "", resource_type)
|
|
353
|
+
|
|
354
|
+
return resource_type.strip() or "resource"
|
|
355
|
+
|
|
356
|
+
def _generate_example_arn(self, pattern: str) -> str:
|
|
357
|
+
"""
|
|
358
|
+
Generate a practical example ARN based on the pattern.
|
|
359
|
+
|
|
360
|
+
Converts AWS template variables to realistic examples.
|
|
361
|
+
"""
|
|
362
|
+
import re
|
|
363
|
+
|
|
364
|
+
example = pattern
|
|
365
|
+
|
|
366
|
+
# Common substitutions
|
|
367
|
+
substitutions = {
|
|
368
|
+
r"\$\{Partition\}": "aws",
|
|
369
|
+
r"\$\{Region\}": "us-east-1",
|
|
370
|
+
r"\$\{Account\}": "123456789012",
|
|
371
|
+
r"\$\{BucketName\}": "my-bucket",
|
|
372
|
+
r"\$\{ObjectName\}": "*",
|
|
373
|
+
r"\$\{UserName\}": "my-user",
|
|
374
|
+
r"\$\{UserNameWithPath\}": "my-user",
|
|
375
|
+
r"\$\{RoleName\}": "my-role",
|
|
376
|
+
r"\$\{RoleNameWithPath\}": "my-role",
|
|
377
|
+
r"\$\{GroupName\}": "my-group",
|
|
378
|
+
r"\$\{PolicyName\}": "my-policy",
|
|
379
|
+
r"\$\{FunctionName\}": "my-function",
|
|
380
|
+
r"\$\{TableName\}": "MyTable",
|
|
381
|
+
r"\$\{QueueName\}": "MyQueue",
|
|
382
|
+
r"\$\{TopicName\}": "MyTopic",
|
|
383
|
+
r"\$\{InstanceId\}": "i-1234567890abcdef0",
|
|
384
|
+
r"\$\{VolumeId\}": "vol-1234567890abcdef0",
|
|
385
|
+
r"\$\{SnapshotId\}": "snap-1234567890abcdef0",
|
|
386
|
+
r"\$\{KeyId\}": "my-key",
|
|
387
|
+
r"\$\{StreamName\}": "MyStream",
|
|
388
|
+
r"\$\{LayerName\}": "my-layer",
|
|
389
|
+
r"\$\{Token\}": "*",
|
|
390
|
+
r"\$\{[^}]+\}": "*", # Catch-all for any remaining variables
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
for pattern_var, replacement in substitutions.items():
|
|
394
|
+
example = re.sub(pattern_var, replacement, example)
|
|
395
|
+
|
|
396
|
+
return example
|
|
397
|
+
|
|
398
|
+
def _get_resource_context(self, action_name: str, resource_type: str, pattern: str) -> str:
|
|
399
|
+
"""
|
|
400
|
+
Provide helpful context about resource requirements.
|
|
401
|
+
|
|
402
|
+
Analyzes the ARN pattern structure and action type to provide
|
|
403
|
+
generic, service-agnostic guidance that works for any AWS service.
|
|
404
|
+
"""
|
|
405
|
+
contexts = []
|
|
406
|
+
|
|
407
|
+
# Detect path separator patterns (e.g., bucket/object, layer:version)
|
|
408
|
+
if "/" in pattern:
|
|
409
|
+
# Pattern has path separator - resource needs it too
|
|
410
|
+
parts = pattern.split("/")
|
|
411
|
+
if len(parts) > 1 and "${" in parts[-1]:
|
|
412
|
+
# Last part is a variable like ${ObjectName}, ${InstanceId}
|
|
413
|
+
contexts.append("ARN must include path separator (/) with resource identifier")
|
|
414
|
+
|
|
415
|
+
# Detect colon-separated resource identifiers
|
|
416
|
+
resource_part = ":".join(pattern.split(":")[5:]) if pattern.count(":") >= 5 else ""
|
|
417
|
+
if resource_part.count(":") > 0 and "${" in resource_part:
|
|
418
|
+
# Resource section uses colons, like function:version or layer:version
|
|
419
|
+
contexts.append("ARN uses colon (:) separators in resource section")
|
|
420
|
+
|
|
421
|
+
# Detect List/Describe actions (often need wildcards)
|
|
422
|
+
if (
|
|
423
|
+
action_name.startswith("List")
|
|
424
|
+
or action_name.startswith("Describe")
|
|
425
|
+
or action_name.startswith("Get")
|
|
426
|
+
):
|
|
427
|
+
# Some Get/List actions require specific resources, others need "*"
|
|
428
|
+
# Only suggest wildcard if pattern is actually "*"
|
|
429
|
+
if pattern == "*":
|
|
430
|
+
contexts.append("This action does not support resource-level permissions")
|
|
431
|
+
|
|
432
|
+
# Generic resource type matching hint
|
|
433
|
+
if resource_type and resource_type != "resource":
|
|
434
|
+
# Avoid redundant message if resource type is obvious
|
|
435
|
+
if not any(
|
|
436
|
+
word in resource_type
|
|
437
|
+
for word in ["object", "bucket", "function", "instance", "user", "role"]
|
|
438
|
+
):
|
|
439
|
+
contexts.append(f"Resource ARN must be of type '{resource_type}'")
|
|
440
|
+
|
|
441
|
+
return "Note: " + " | ".join(contexts) if contexts else ""
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"""Action validation check - validates IAM actions against AWS service definitions.
|
|
2
|
+
|
|
3
|
+
This check ensures that all actions specified in IAM policies are valid actions
|
|
4
|
+
defined by AWS services. It helps identify typos or deprecated actions that may
|
|
5
|
+
lead to unintended access permissions.
|
|
6
|
+
|
|
7
|
+
This check is not necessary when using Access Analyzer, as it performs similar
|
|
8
|
+
validations. However, it can be useful in environments where Access Analyzer is
|
|
9
|
+
not available or for pre-deployment policy validation to catch errors early.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from iam_validator.core.aws_fetcher import AWSServiceFetcher
|
|
13
|
+
from iam_validator.core.check_registry import CheckConfig, PolicyCheck
|
|
14
|
+
from iam_validator.core.models import Statement, ValidationIssue
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class ActionValidationCheck(PolicyCheck):
|
|
18
|
+
"""Validates that IAM actions exist in AWS services."""
|
|
19
|
+
|
|
20
|
+
@property
|
|
21
|
+
def check_id(self) -> str:
|
|
22
|
+
return "action_validation"
|
|
23
|
+
|
|
24
|
+
@property
|
|
25
|
+
def description(self) -> str:
|
|
26
|
+
return "Validates that actions exist in AWS service definitions"
|
|
27
|
+
|
|
28
|
+
@property
|
|
29
|
+
def default_severity(self) -> str:
|
|
30
|
+
return "error"
|
|
31
|
+
|
|
32
|
+
async def execute(
|
|
33
|
+
self,
|
|
34
|
+
statement: Statement,
|
|
35
|
+
statement_idx: int,
|
|
36
|
+
fetcher: AWSServiceFetcher,
|
|
37
|
+
config: CheckConfig,
|
|
38
|
+
) -> list[ValidationIssue]:
|
|
39
|
+
"""Execute action validation on a statement.
|
|
40
|
+
|
|
41
|
+
This check ONLY validates that actions exist in AWS service definitions.
|
|
42
|
+
Wildcard security checks are handled by security_best_practices_check.
|
|
43
|
+
"""
|
|
44
|
+
issues = []
|
|
45
|
+
|
|
46
|
+
# Get actions from statement
|
|
47
|
+
actions = statement.get_actions()
|
|
48
|
+
statement_sid = statement.sid
|
|
49
|
+
line_number = statement.line_number
|
|
50
|
+
|
|
51
|
+
for action in actions:
|
|
52
|
+
# Skip wildcard actions - they're handled by security_best_practices_check
|
|
53
|
+
if action == "*" or "*" in action:
|
|
54
|
+
continue
|
|
55
|
+
|
|
56
|
+
# Validate the action exists in AWS
|
|
57
|
+
is_valid, error_msg, _is_wildcard = await fetcher.validate_action(action)
|
|
58
|
+
|
|
59
|
+
if not is_valid:
|
|
60
|
+
issues.append(
|
|
61
|
+
ValidationIssue(
|
|
62
|
+
severity=self.get_severity(config),
|
|
63
|
+
statement_sid=statement_sid,
|
|
64
|
+
statement_index=statement_idx,
|
|
65
|
+
issue_type="invalid_action",
|
|
66
|
+
message=error_msg or f"Invalid action: {action}",
|
|
67
|
+
action=action,
|
|
68
|
+
line_number=line_number,
|
|
69
|
+
)
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
return issues
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"""Condition key validation check - validates condition keys against AWS definitions."""
|
|
2
|
+
|
|
3
|
+
from iam_validator.core.aws_fetcher import AWSServiceFetcher
|
|
4
|
+
from iam_validator.core.check_registry import CheckConfig, PolicyCheck
|
|
5
|
+
from iam_validator.core.models import Statement, ValidationIssue
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class ConditionKeyValidationCheck(PolicyCheck):
|
|
9
|
+
"""Validates condition keys against AWS service definitions and global keys."""
|
|
10
|
+
|
|
11
|
+
@property
|
|
12
|
+
def check_id(self) -> str:
|
|
13
|
+
return "condition_key_validation"
|
|
14
|
+
|
|
15
|
+
@property
|
|
16
|
+
def description(self) -> str:
|
|
17
|
+
return "Validates condition keys against AWS service definitions"
|
|
18
|
+
|
|
19
|
+
@property
|
|
20
|
+
def default_severity(self) -> str:
|
|
21
|
+
return "error" # Invalid condition keys are IAM policy errors
|
|
22
|
+
|
|
23
|
+
async def execute(
|
|
24
|
+
self,
|
|
25
|
+
statement: Statement,
|
|
26
|
+
statement_idx: int,
|
|
27
|
+
fetcher: AWSServiceFetcher,
|
|
28
|
+
config: CheckConfig,
|
|
29
|
+
) -> list[ValidationIssue]:
|
|
30
|
+
"""Execute condition key validation on a statement."""
|
|
31
|
+
issues = []
|
|
32
|
+
|
|
33
|
+
# Get conditions from statement
|
|
34
|
+
if not statement.condition:
|
|
35
|
+
return issues
|
|
36
|
+
|
|
37
|
+
# Check if global condition key warnings are enabled (default: True)
|
|
38
|
+
warn_on_global_keys = config.config.get("warn_on_global_condition_keys", True)
|
|
39
|
+
|
|
40
|
+
statement_sid = statement.sid
|
|
41
|
+
line_number = statement.line_number
|
|
42
|
+
actions = statement.get_actions()
|
|
43
|
+
resources = statement.get_resources()
|
|
44
|
+
|
|
45
|
+
# Extract all condition keys from all condition operators
|
|
46
|
+
for operator, conditions in statement.condition.items():
|
|
47
|
+
for condition_key in conditions.keys():
|
|
48
|
+
# Validate this condition key against each action in the statement
|
|
49
|
+
for action in actions:
|
|
50
|
+
# Skip wildcard actions
|
|
51
|
+
if action == "*":
|
|
52
|
+
continue
|
|
53
|
+
|
|
54
|
+
# Validate against action and resource types
|
|
55
|
+
is_valid, error_msg, warning_msg = await fetcher.validate_condition_key(
|
|
56
|
+
action, condition_key, resources
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
if not is_valid:
|
|
60
|
+
issues.append(
|
|
61
|
+
ValidationIssue(
|
|
62
|
+
severity=self.get_severity(config),
|
|
63
|
+
statement_sid=statement_sid,
|
|
64
|
+
statement_index=statement_idx,
|
|
65
|
+
issue_type="invalid_condition_key",
|
|
66
|
+
message=error_msg or f"Invalid condition key: {condition_key}",
|
|
67
|
+
action=action,
|
|
68
|
+
condition_key=condition_key,
|
|
69
|
+
line_number=line_number,
|
|
70
|
+
)
|
|
71
|
+
)
|
|
72
|
+
# Only report once per condition key (not per action)
|
|
73
|
+
break
|
|
74
|
+
elif warning_msg and warn_on_global_keys:
|
|
75
|
+
# Add warning for global condition keys with action-specific keys
|
|
76
|
+
# Only if warn_on_global_condition_keys is enabled
|
|
77
|
+
issues.append(
|
|
78
|
+
ValidationIssue(
|
|
79
|
+
severity="warning",
|
|
80
|
+
statement_sid=statement_sid,
|
|
81
|
+
statement_index=statement_idx,
|
|
82
|
+
issue_type="global_condition_key_with_action_specific",
|
|
83
|
+
message=warning_msg,
|
|
84
|
+
action=action,
|
|
85
|
+
condition_key=condition_key,
|
|
86
|
+
line_number=line_number,
|
|
87
|
+
)
|
|
88
|
+
)
|
|
89
|
+
# Only report once per condition key (not per action)
|
|
90
|
+
break
|
|
91
|
+
|
|
92
|
+
return issues
|