iam-policy-validator 1.4.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.

Files changed (56) hide show
  1. iam_policy_validator-1.4.0.dist-info/METADATA +1022 -0
  2. iam_policy_validator-1.4.0.dist-info/RECORD +56 -0
  3. iam_policy_validator-1.4.0.dist-info/WHEEL +4 -0
  4. iam_policy_validator-1.4.0.dist-info/entry_points.txt +2 -0
  5. iam_policy_validator-1.4.0.dist-info/licenses/LICENSE +21 -0
  6. iam_validator/__init__.py +27 -0
  7. iam_validator/__main__.py +11 -0
  8. iam_validator/__version__.py +7 -0
  9. iam_validator/checks/__init__.py +27 -0
  10. iam_validator/checks/action_condition_enforcement.py +727 -0
  11. iam_validator/checks/action_resource_constraint.py +151 -0
  12. iam_validator/checks/action_validation.py +72 -0
  13. iam_validator/checks/condition_key_validation.py +70 -0
  14. iam_validator/checks/policy_size.py +151 -0
  15. iam_validator/checks/policy_type_validation.py +299 -0
  16. iam_validator/checks/principal_validation.py +282 -0
  17. iam_validator/checks/resource_validation.py +108 -0
  18. iam_validator/checks/security_best_practices.py +536 -0
  19. iam_validator/checks/sid_uniqueness.py +170 -0
  20. iam_validator/checks/utils/__init__.py +1 -0
  21. iam_validator/checks/utils/policy_level_checks.py +143 -0
  22. iam_validator/checks/utils/sensitive_action_matcher.py +252 -0
  23. iam_validator/checks/utils/wildcard_expansion.py +87 -0
  24. iam_validator/commands/__init__.py +25 -0
  25. iam_validator/commands/analyze.py +434 -0
  26. iam_validator/commands/base.py +48 -0
  27. iam_validator/commands/cache.py +392 -0
  28. iam_validator/commands/download_services.py +260 -0
  29. iam_validator/commands/post_to_pr.py +86 -0
  30. iam_validator/commands/validate.py +539 -0
  31. iam_validator/core/__init__.py +14 -0
  32. iam_validator/core/access_analyzer.py +666 -0
  33. iam_validator/core/access_analyzer_report.py +643 -0
  34. iam_validator/core/aws_fetcher.py +880 -0
  35. iam_validator/core/aws_global_conditions.py +137 -0
  36. iam_validator/core/check_registry.py +469 -0
  37. iam_validator/core/cli.py +134 -0
  38. iam_validator/core/config_loader.py +452 -0
  39. iam_validator/core/defaults.py +393 -0
  40. iam_validator/core/formatters/__init__.py +27 -0
  41. iam_validator/core/formatters/base.py +147 -0
  42. iam_validator/core/formatters/console.py +59 -0
  43. iam_validator/core/formatters/csv.py +170 -0
  44. iam_validator/core/formatters/enhanced.py +434 -0
  45. iam_validator/core/formatters/html.py +672 -0
  46. iam_validator/core/formatters/json.py +33 -0
  47. iam_validator/core/formatters/markdown.py +63 -0
  48. iam_validator/core/formatters/sarif.py +187 -0
  49. iam_validator/core/models.py +298 -0
  50. iam_validator/core/policy_checks.py +656 -0
  51. iam_validator/core/policy_loader.py +396 -0
  52. iam_validator/core/pr_commenter.py +338 -0
  53. iam_validator/core/report.py +859 -0
  54. iam_validator/integrations/__init__.py +28 -0
  55. iam_validator/integrations/github_integration.py +795 -0
  56. iam_validator/integrations/ms_teams.py +442 -0
@@ -0,0 +1,170 @@
1
+ """Statement ID (SID) uniqueness and format check.
2
+
3
+ This check validates that Statement IDs (Sids):
4
+ 1. Are unique within a policy
5
+ 2. Follow AWS naming requirements (alphanumeric, hyphens, underscores only - no spaces)
6
+
7
+ According to AWS best practices, while not strictly required, having unique SIDs
8
+ makes it easier to reference specific statements and improves policy maintainability.
9
+
10
+ This is implemented as a policy-level check that runs once when processing the first
11
+ statement, examining all statements in the policy to find duplicates and format issues.
12
+ """
13
+
14
+ import re
15
+ from collections import Counter
16
+
17
+ from iam_validator.core.aws_fetcher import AWSServiceFetcher
18
+ from iam_validator.core.check_registry import CheckConfig, PolicyCheck
19
+ from iam_validator.core.models import IAMPolicy, Statement, ValidationIssue
20
+
21
+
22
+ def _check_sid_uniqueness_impl(policy: IAMPolicy, severity: str) -> list[ValidationIssue]:
23
+ """Implementation of SID uniqueness and format checking.
24
+
25
+ Args:
26
+ policy: IAM policy to validate
27
+ severity: Severity level for issues found
28
+
29
+ Returns:
30
+ List of ValidationIssue objects for duplicate or invalid SIDs
31
+ """
32
+ issues: list[ValidationIssue] = []
33
+
34
+ # AWS SID requirements: alphanumeric characters, hyphens, and underscores only
35
+ # No spaces allowed
36
+ sid_pattern = re.compile(r"^[a-zA-Z0-9_-]+$")
37
+
38
+ # Collect all SIDs (ignoring None/empty values) and check format
39
+ sids_with_indices: list[tuple[str, int]] = []
40
+ for idx, statement in enumerate(policy.statement):
41
+ if statement.sid: # Only check statements that have a SID
42
+ # Check SID format
43
+ if not sid_pattern.match(statement.sid):
44
+ # Identify the issue
45
+ if " " in statement.sid:
46
+ issue_msg = f"Statement ID '{statement.sid}' contains spaces, which are not allowed by AWS"
47
+ suggestion = (
48
+ f"Remove spaces from the SID. Example: '{statement.sid.replace(' ', '')}'"
49
+ )
50
+ else:
51
+ invalid_chars = "".join(
52
+ set(c for c in statement.sid if not c.isalnum() and c not in "_-")
53
+ )
54
+ issue_msg = f"Statement ID '{statement.sid}' contains invalid characters: {invalid_chars}"
55
+ suggestion = (
56
+ "SIDs must contain only alphanumeric characters, hyphens, and underscores"
57
+ )
58
+
59
+ issues.append(
60
+ ValidationIssue(
61
+ severity="error", # Invalid SID format is an error
62
+ statement_sid=statement.sid,
63
+ statement_index=idx,
64
+ issue_type="invalid_sid_format",
65
+ message=issue_msg,
66
+ suggestion=suggestion,
67
+ line_number=statement.line_number,
68
+ )
69
+ )
70
+
71
+ sids_with_indices.append((statement.sid, idx))
72
+
73
+ # Find duplicates
74
+ sid_counts = Counter(sid for sid, _ in sids_with_indices)
75
+ duplicate_sids = {sid: count for sid, count in sid_counts.items() if count > 1}
76
+
77
+ # Create issues for each duplicate SID
78
+ for duplicate_sid, count in duplicate_sids.items():
79
+ # Find all statement indices with this SID
80
+ indices = [idx for sid, idx in sids_with_indices if sid == duplicate_sid]
81
+
82
+ # Create an issue for each occurrence except the first
83
+ # (the first occurrence is "original", subsequent ones are "duplicates")
84
+ for idx in indices[1:]:
85
+ statement = policy.statement[idx]
86
+ # Convert to 1-indexed statement numbers for user-facing message
87
+ statement_numbers = ", ".join(f"#{i + 1}" for i in indices)
88
+ issues.append(
89
+ ValidationIssue(
90
+ severity=severity,
91
+ statement_sid=duplicate_sid,
92
+ statement_index=idx,
93
+ issue_type="duplicate_sid",
94
+ message=f"Statement ID '{duplicate_sid}' is used {count} times in this policy (found in statements {statement_numbers})",
95
+ suggestion="Change this SID to a unique value. Statement IDs help identify and reference specific statements, so duplicates can cause confusion.",
96
+ line_number=statement.line_number,
97
+ )
98
+ )
99
+
100
+ return issues
101
+
102
+
103
+ class SidUniquenessCheck(PolicyCheck):
104
+ """Validates that Statement IDs (Sids) are unique within a policy.
105
+
106
+ This is a special policy-level check that examines all statements together.
107
+ It only runs once when processing the first statement to avoid duplicate work.
108
+ """
109
+
110
+ @property
111
+ def check_id(self) -> str:
112
+ return "sid_uniqueness"
113
+
114
+ @property
115
+ def description(self) -> str:
116
+ return "Validates that Statement IDs (Sids) are unique and follow AWS naming requirements (no spaces)"
117
+
118
+ @property
119
+ def default_severity(self) -> str:
120
+ return "warning"
121
+
122
+ async def execute(
123
+ self,
124
+ statement: Statement,
125
+ statement_idx: int,
126
+ fetcher: AWSServiceFetcher,
127
+ config: CheckConfig,
128
+ ) -> list[ValidationIssue]:
129
+ """Execute the SID uniqueness check at statement level.
130
+
131
+ This is a policy-level check, so statement-level execution returns empty.
132
+ The actual check runs in execute_policy() which has access to all statements.
133
+
134
+ Args:
135
+ statement: The IAM policy statement (unused)
136
+ statement_idx: Index of the statement in the policy (unused)
137
+ fetcher: AWS service fetcher (unused for this check)
138
+ config: Configuration for this check instance (unused)
139
+
140
+ Returns:
141
+ Empty list (actual check runs in execute_policy())
142
+ """
143
+ del statement, statement_idx, fetcher, config # Unused
144
+ # This is a policy-level check - execution happens in execute_policy()
145
+ return []
146
+
147
+ async def execute_policy(
148
+ self,
149
+ policy: IAMPolicy,
150
+ policy_file: str,
151
+ fetcher: AWSServiceFetcher,
152
+ config: CheckConfig,
153
+ **kwargs,
154
+ ) -> list[ValidationIssue]:
155
+ """Execute the SID uniqueness check on the entire policy.
156
+
157
+ This method examines all statements together to find duplicate SIDs.
158
+
159
+ Args:
160
+ policy: The complete IAM policy to validate
161
+ policy_file: Path to the policy file (unused, kept for API consistency)
162
+ fetcher: AWS service fetcher (unused for this check)
163
+ config: Configuration for this check instance
164
+
165
+ Returns:
166
+ List of ValidationIssue objects for duplicate SIDs
167
+ """
168
+ del policy_file, fetcher # Unused
169
+ severity = self.get_severity(config)
170
+ return _check_sid_uniqueness_impl(policy, severity)
@@ -0,0 +1 @@
1
+ """Utility modules for IAM policy checks."""
@@ -0,0 +1,143 @@
1
+ """Policy-level privilege escalation detection for IAM policy checks.
2
+
3
+ This module provides functionality to detect privilege escalation patterns
4
+ that span multiple statements in a policy.
5
+ """
6
+
7
+ import re
8
+
9
+ from iam_validator.core.check_registry import CheckConfig
10
+ from iam_validator.core.models import ValidationIssue
11
+
12
+
13
+ def check_policy_level_actions(
14
+ all_actions: list[str],
15
+ statement_map: dict[str, list[tuple[int, str | None]]],
16
+ config,
17
+ check_config: CheckConfig,
18
+ check_type: str,
19
+ get_severity_func,
20
+ ) -> list[ValidationIssue]:
21
+ """
22
+ Check for policy-level privilege escalation patterns.
23
+
24
+ This function detects when a policy grants a dangerous combination of
25
+ permissions across multiple statements (e.g., iam:CreateUser + iam:AttachUserPolicy).
26
+
27
+ Args:
28
+ all_actions: All actions across the entire policy
29
+ statement_map: Mapping of action -> [(statement_idx, sid), ...]
30
+ config: The sensitive_actions or sensitive_action_patterns configuration
31
+ check_config: Full check configuration
32
+ check_type: Either "actions" (exact match) or "patterns" (regex match)
33
+ get_severity_func: Function to get severity for the check
34
+
35
+ Returns:
36
+ List of ValidationIssue objects
37
+ """
38
+ issues = []
39
+
40
+ if not config:
41
+ return issues
42
+
43
+ # Handle list of items (could be simple strings or dicts with all_of/any_of)
44
+ if isinstance(config, list):
45
+ for item in config:
46
+ if isinstance(item, dict) and "all_of" in item:
47
+ # This is a privilege escalation pattern - all actions must be present
48
+ issue = _check_all_of_pattern(
49
+ all_actions,
50
+ statement_map,
51
+ item["all_of"],
52
+ check_config,
53
+ check_type,
54
+ get_severity_func,
55
+ )
56
+ if issue:
57
+ issues.append(issue)
58
+
59
+ # Handle dict with all_of at the top level
60
+ elif isinstance(config, dict) and "all_of" in config:
61
+ issue = _check_all_of_pattern(
62
+ all_actions,
63
+ statement_map,
64
+ config["all_of"],
65
+ check_config,
66
+ check_type,
67
+ get_severity_func,
68
+ )
69
+ if issue:
70
+ issues.append(issue)
71
+
72
+ return issues
73
+
74
+
75
+ def _check_all_of_pattern(
76
+ all_actions: list[str],
77
+ statement_map: dict[str, list[tuple[int, str | None]]],
78
+ required_actions: list[str],
79
+ check_config: CheckConfig,
80
+ check_type: str,
81
+ get_severity_func,
82
+ ) -> ValidationIssue | None:
83
+ """
84
+ Check if all required actions/patterns are present in the policy.
85
+
86
+ Args:
87
+ all_actions: All actions across the entire policy
88
+ statement_map: Mapping of action -> [(statement_idx, sid), ...]
89
+ required_actions: List of required actions or patterns
90
+ check_config: Full check configuration
91
+ check_type: Either "actions" (exact match) or "patterns" (regex match)
92
+ get_severity_func: Function to get severity for the check
93
+
94
+ Returns:
95
+ ValidationIssue if privilege escalation detected, None otherwise
96
+ """
97
+ matched_actions = []
98
+
99
+ if check_type == "actions":
100
+ # Exact matching
101
+ matched_actions = [a for a in all_actions if a in required_actions]
102
+ else:
103
+ # Pattern matching - for each pattern, find actions that match
104
+ for pattern in required_actions:
105
+ for action in all_actions:
106
+ try:
107
+ if re.match(pattern, action):
108
+ matched_actions.append(action)
109
+ break # Found at least one match for this pattern
110
+ except re.error:
111
+ continue
112
+
113
+ # Check if ALL required actions/patterns are present
114
+ if len(matched_actions) >= len(required_actions):
115
+ # Privilege escalation detected!
116
+ severity = get_severity_func(check_config, "sensitive_action_check", "error")
117
+
118
+ # Collect which statements these actions appear in
119
+ statement_refs = []
120
+ for action in matched_actions:
121
+ if action in statement_map:
122
+ for stmt_idx, sid in statement_map[action]:
123
+ sid_str = f"'{sid}'" if sid else f"#{stmt_idx}"
124
+ statement_refs.append(f"Statement {sid_str}: {action}")
125
+
126
+ action_list = "', '".join(matched_actions)
127
+ stmt_details = "\n - ".join(statement_refs)
128
+
129
+ return ValidationIssue(
130
+ severity=severity,
131
+ statement_sid=None, # Policy-level issue
132
+ statement_index=-1, # -1 indicates policy-level issue
133
+ issue_type="privilege_escalation",
134
+ message=f"Policy-level privilege escalation detected: grants all of ['{action_list}'] across multiple statements",
135
+ suggestion=f"These actions combined allow privilege escalation. Consider:\n"
136
+ f" 1. Splitting into separate policies for different users/roles\n"
137
+ f" 2. Adding strict conditions to limit when these actions can be used together\n"
138
+ f" 3. Reviewing if all these permissions are truly necessary\n\n"
139
+ f"Actions found in:\n - {stmt_details}",
140
+ line_number=None,
141
+ )
142
+
143
+ return None
@@ -0,0 +1,252 @@
1
+ """Sensitive action matching utilities for IAM policy checks.
2
+
3
+ This module provides functionality to match actions against sensitive action
4
+ configurations, supporting exact matches, regex patterns, and any_of/all_of logic.
5
+ """
6
+
7
+ import re
8
+ from functools import lru_cache
9
+ from re import Pattern
10
+
11
+ from iam_validator.core.check_registry import CheckConfig
12
+
13
+ # Default set of sensitive actions for backward compatibility
14
+ # Using frozenset for O(1) lookups and immutability
15
+ DEFAULT_SENSITIVE_ACTIONS = frozenset(
16
+ {
17
+ "ec2:DeleteVolume",
18
+ "ec2:TerminateInstances",
19
+ "eks:DeleteCluster",
20
+ "iam:AttachRolePolicy",
21
+ "iam:AttachUserPolicy",
22
+ "iam:CreateAccessKey",
23
+ "iam:CreateRole",
24
+ "iam:CreateUser",
25
+ "iam:DeleteRole",
26
+ "iam:DeleteUser",
27
+ "iam:PutRolePolicy",
28
+ "iam:PutUserPolicy",
29
+ "lambda:DeleteFunction",
30
+ "rds:DeleteDBInstance",
31
+ "s3:DeleteBucket",
32
+ "s3:DeleteBucketPolicy",
33
+ "s3:PutBucketPolicy",
34
+ }
35
+ )
36
+
37
+
38
+ # Global regex pattern cache for performance
39
+ @lru_cache(maxsize=256)
40
+ def compile_pattern(pattern: str) -> Pattern[str] | None:
41
+ """Compile and cache regex patterns.
42
+
43
+ Args:
44
+ pattern: Regex pattern string
45
+
46
+ Returns:
47
+ Compiled pattern or None if invalid
48
+ """
49
+ try:
50
+ return re.compile(pattern)
51
+ except re.error:
52
+ return None
53
+
54
+
55
+ def check_sensitive_actions(
56
+ actions: list[str], config: CheckConfig, default_actions: frozenset[str] | None = None
57
+ ) -> tuple[bool, list[str]]:
58
+ """
59
+ Check if actions match sensitive action criteria with any_of/all_of support.
60
+
61
+ Args:
62
+ actions: List of actions to check
63
+ config: Check configuration
64
+ default_actions: Default sensitive actions to use if no config (defaults to DEFAULT_SENSITIVE_ACTIONS)
65
+
66
+ Returns:
67
+ tuple[bool, list[str]]: (is_sensitive, matched_actions)
68
+ - is_sensitive: True if the actions match the sensitive criteria
69
+ - matched_actions: List of actions that matched the criteria
70
+ """
71
+ if default_actions is None:
72
+ default_actions = DEFAULT_SENSITIVE_ACTIONS
73
+
74
+ # Filter out wildcards
75
+ filtered_actions = [a for a in actions if a != "*"]
76
+ if not filtered_actions:
77
+ return False, []
78
+
79
+ # Get configuration for both sensitive_actions and sensitive_action_patterns
80
+ sub_check_config = config.config.get("sensitive_action_check", {})
81
+ if not isinstance(sub_check_config, dict):
82
+ return False, []
83
+
84
+ sensitive_actions_config = sub_check_config.get("sensitive_actions")
85
+ sensitive_patterns_config = sub_check_config.get("sensitive_action_patterns")
86
+
87
+ # Check sensitive_actions (exact matches)
88
+ actions_match, actions_matched = check_actions_config(
89
+ filtered_actions, sensitive_actions_config, default_actions
90
+ )
91
+
92
+ # Check sensitive_action_patterns (regex patterns)
93
+ patterns_match, patterns_matched = check_patterns_config(
94
+ filtered_actions, sensitive_patterns_config
95
+ )
96
+
97
+ # Combine results - if either matched, we consider it sensitive
98
+ is_sensitive = actions_match or patterns_match
99
+ # Use set operations for efficient deduplication
100
+ matched_set = set(actions_matched) | set(patterns_matched)
101
+ matched_actions = list(matched_set)
102
+
103
+ return is_sensitive, matched_actions
104
+
105
+
106
+ def check_actions_config(
107
+ actions: list[str], config, default_actions: frozenset[str]
108
+ ) -> tuple[bool, list[str]]:
109
+ """
110
+ Check actions against sensitive_actions configuration.
111
+
112
+ Supports:
113
+ - Simple list: ["action1", "action2"] (backward compatible, any_of logic)
114
+ - any_of: {"any_of": ["action1", "action2"]}
115
+ - all_of: {"all_of": ["action1", "action2"]}
116
+ - Multiple groups: [{"all_of": [...]}, {"all_of": [...]}, "action3"]
117
+
118
+ Args:
119
+ actions: List of actions to check
120
+ config: Sensitive actions configuration
121
+ default_actions: Default sensitive actions to use if no config
122
+
123
+ Returns:
124
+ tuple[bool, list[str]]: (matches, matched_actions)
125
+ """
126
+ if not config:
127
+ # If no config, fall back to defaults with any_of logic
128
+ # default_actions is already a frozenset for O(1) lookups
129
+ matched = [a for a in actions if a in default_actions]
130
+ return len(matched) > 0, matched
131
+
132
+ # Handle simple list with potential mixed items
133
+ if isinstance(config, list):
134
+ # Use set for O(1) membership checks
135
+ all_matched = set()
136
+ actions_set = set(actions) # Convert once for O(1) lookups
137
+
138
+ for item in config:
139
+ # Each item can be a string, or a dict with any_of/all_of
140
+ if isinstance(item, str):
141
+ # Simple string - check if action matches (O(1) lookup)
142
+ if item in actions_set:
143
+ all_matched.add(item)
144
+ elif isinstance(item, dict):
145
+ # Recurse for dict items
146
+ matches, matched = check_actions_config(actions, item, default_actions)
147
+ if matches:
148
+ all_matched.update(matched)
149
+
150
+ return len(all_matched) > 0, list(all_matched)
151
+
152
+ # Handle dict with any_of/all_of
153
+ if isinstance(config, dict):
154
+ # any_of: at least one action must match
155
+ if "any_of" in config:
156
+ # Convert once for O(1) intersection
157
+ any_of_set = set(config["any_of"])
158
+ actions_set = set(actions)
159
+ matched = list(any_of_set & actions_set)
160
+ return len(matched) > 0, matched
161
+
162
+ # all_of: all specified actions must be present in the statement
163
+ if "all_of" in config:
164
+ all_of_set = set(config["all_of"])
165
+ actions_set = set(actions)
166
+ matched = list(all_of_set & actions_set)
167
+ # All required actions must be present
168
+ return all_of_set.issubset(actions_set), matched
169
+
170
+ return False, []
171
+
172
+
173
+ def check_patterns_config(actions: list[str], config) -> tuple[bool, list[str]]:
174
+ """
175
+ Check actions against sensitive_action_patterns configuration.
176
+
177
+ Supports:
178
+ - Simple list: ["^pattern1.*", "^pattern2.*"] (backward compatible, any_of logic)
179
+ - any_of: {"any_of": ["^pattern1.*", "^pattern2.*"]}
180
+ - all_of: {"all_of": ["^pattern1.*", "^pattern2.*"]}
181
+ - Multiple groups: [{"all_of": [...]}, {"any_of": [...]}, "^pattern.*"]
182
+
183
+ Args:
184
+ actions: List of actions to check
185
+ config: Sensitive action patterns configuration
186
+
187
+ Returns:
188
+ tuple[bool, list[str]]: (matches, matched_actions)
189
+
190
+ Performance:
191
+ Uses cached compiled regex patterns for 10-50x speedup
192
+ """
193
+ if not config:
194
+ return False, []
195
+
196
+ # Handle simple list with potential mixed items
197
+ if isinstance(config, list):
198
+ # Use set for O(1) membership checks instead of list
199
+ all_matched = set()
200
+
201
+ for item in config:
202
+ # Each item can be a string pattern, or a dict with any_of/all_of
203
+ if isinstance(item, str):
204
+ # Simple string pattern - check if any action matches
205
+ # Use cached compiled pattern
206
+ compiled = compile_pattern(item)
207
+ if compiled:
208
+ for action in actions:
209
+ if compiled.match(action):
210
+ all_matched.add(action)
211
+ elif isinstance(item, dict):
212
+ # Recurse for dict items
213
+ matches, matched = check_patterns_config(actions, item)
214
+ if matches:
215
+ all_matched.update(matched)
216
+
217
+ return len(all_matched) > 0, list(all_matched)
218
+
219
+ # Handle dict with any_of/all_of
220
+ if isinstance(config, dict):
221
+ # any_of: at least one action must match at least one pattern
222
+ if "any_of" in config:
223
+ matched = set()
224
+ # Pre-compile all patterns
225
+ compiled_patterns = [compile_pattern(p) for p in config["any_of"]]
226
+
227
+ for action in actions:
228
+ for compiled in compiled_patterns:
229
+ if compiled and compiled.match(action):
230
+ matched.add(action)
231
+ break
232
+ return len(matched) > 0, list(matched)
233
+
234
+ # all_of: at least one action must match ALL patterns
235
+ if "all_of" in config:
236
+ # Pre-compile all patterns
237
+ compiled_patterns = [compile_pattern(p) for p in config["all_of"]]
238
+ # Filter out invalid patterns
239
+ compiled_patterns = [p for p in compiled_patterns if p]
240
+
241
+ if not compiled_patterns:
242
+ return False, []
243
+
244
+ matched = set()
245
+ for action in actions:
246
+ # Check if this action matches ALL patterns
247
+ if all(compiled.match(action) for compiled in compiled_patterns):
248
+ matched.add(action)
249
+
250
+ return len(matched) > 0, list(matched)
251
+
252
+ return False, []
@@ -0,0 +1,87 @@
1
+ """Wildcard action expansion utilities for IAM policy checks.
2
+
3
+ This module provides functionality to expand wildcard actions (like ec2:*, iam:Delete*)
4
+ to their actual action names using the AWS Service Reference API.
5
+ """
6
+
7
+ import re
8
+ from functools import lru_cache
9
+ from re import Pattern
10
+
11
+ from iam_validator.core.aws_fetcher import AWSServiceFetcher
12
+
13
+
14
+ # Global cache for compiled wildcard patterns (shared across checks)
15
+ # Using lru_cache for O(1) pattern reuse and 20-30x performance improvement
16
+ @lru_cache(maxsize=512)
17
+ def compile_wildcard_pattern(pattern: str) -> Pattern[str]:
18
+ """Compile and cache wildcard patterns for O(1) reuse.
19
+
20
+ Args:
21
+ pattern: Wildcard pattern (e.g., "s3:Get*")
22
+
23
+ Returns:
24
+ Compiled regex pattern
25
+
26
+ Performance:
27
+ 20-30x speedup by avoiding repeated pattern compilation
28
+ """
29
+ regex_pattern = "^" + re.escape(pattern).replace(r"\*", ".*") + "$"
30
+ return re.compile(regex_pattern, re.IGNORECASE)
31
+
32
+
33
+ async def expand_wildcard_actions(actions: list[str], fetcher: AWSServiceFetcher) -> list[str]:
34
+ """
35
+ Expand wildcard actions to their actual action names using AWS API.
36
+
37
+ This function expands wildcard patterns like "s3:*", "ec2:Delete*", "iam:*User*"
38
+ to the actual action names they grant. This is crucial for sensitive action
39
+ detection to catch wildcards that include sensitive actions.
40
+
41
+ Examples:
42
+ ["s3:GetObject", "ec2:*"] -> ["s3:GetObject", "ec2:DeleteVolume", "ec2:TerminateInstances", ...]
43
+ ["iam:Delete*"] -> ["iam:DeleteUser", "iam:DeleteRole", "iam:DeleteAccessKey", ...]
44
+
45
+ Args:
46
+ actions: List of action patterns (may include wildcards)
47
+ fetcher: AWS service fetcher for API lookups
48
+
49
+ Returns:
50
+ List of expanded action names (wildcards replaced with actual actions)
51
+ """
52
+ expanded = []
53
+
54
+ for action in actions:
55
+ # Skip full wildcard "*" - it's too broad to expand
56
+ if action == "*":
57
+ expanded.append(action)
58
+ continue
59
+
60
+ # Check if action contains wildcards
61
+ if "*" not in action:
62
+ # No wildcard, keep as-is
63
+ expanded.append(action)
64
+ continue
65
+
66
+ # Action has wildcard - expand it using AWS API
67
+ try:
68
+ # Parse action to get service and action name
69
+ service_prefix, action_name = fetcher.parse_action(action)
70
+
71
+ # Fetch service detail to get all available actions
72
+ service_detail = await fetcher.fetch_service_by_name(service_prefix)
73
+ available_actions = list(service_detail.actions.keys())
74
+
75
+ # Match wildcard pattern against available actions
76
+ _, matched_actions = fetcher._match_wildcard_action(action_name, available_actions)
77
+
78
+ # Add expanded actions with service prefix
79
+ for matched_action in matched_actions:
80
+ expanded.append(f"{service_prefix}:{matched_action}")
81
+
82
+ except Exception:
83
+ # If expansion fails (invalid service, etc.), keep original action
84
+ # This ensures we don't lose actions due to API errors
85
+ expanded.append(action)
86
+
87
+ return expanded
@@ -0,0 +1,25 @@
1
+ """CLI commands for IAM Policy Validator."""
2
+
3
+ from .analyze import AnalyzeCommand
4
+ from .cache import CacheCommand
5
+ from .download_services import DownloadServicesCommand
6
+ from .post_to_pr import PostToPRCommand
7
+ from .validate import ValidateCommand
8
+
9
+ # All available commands
10
+ ALL_COMMANDS = [
11
+ ValidateCommand(),
12
+ PostToPRCommand(),
13
+ AnalyzeCommand(),
14
+ CacheCommand(),
15
+ DownloadServicesCommand(),
16
+ ]
17
+
18
+ __all__ = [
19
+ "ValidateCommand",
20
+ "PostToPRCommand",
21
+ "AnalyzeCommand",
22
+ "CacheCommand",
23
+ "DownloadServicesCommand",
24
+ "ALL_COMMANDS",
25
+ ]