iam-policy-validator 1.14.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.
Files changed (106) hide show
  1. iam_policy_validator-1.14.0.dist-info/METADATA +782 -0
  2. iam_policy_validator-1.14.0.dist-info/RECORD +106 -0
  3. iam_policy_validator-1.14.0.dist-info/WHEEL +4 -0
  4. iam_policy_validator-1.14.0.dist-info/entry_points.txt +2 -0
  5. iam_policy_validator-1.14.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 +9 -0
  9. iam_validator/checks/__init__.py +45 -0
  10. iam_validator/checks/action_condition_enforcement.py +1442 -0
  11. iam_validator/checks/action_resource_matching.py +472 -0
  12. iam_validator/checks/action_validation.py +67 -0
  13. iam_validator/checks/condition_key_validation.py +88 -0
  14. iam_validator/checks/condition_type_mismatch.py +257 -0
  15. iam_validator/checks/full_wildcard.py +62 -0
  16. iam_validator/checks/mfa_condition_check.py +105 -0
  17. iam_validator/checks/policy_size.py +114 -0
  18. iam_validator/checks/policy_structure.py +556 -0
  19. iam_validator/checks/policy_type_validation.py +331 -0
  20. iam_validator/checks/principal_validation.py +708 -0
  21. iam_validator/checks/resource_validation.py +135 -0
  22. iam_validator/checks/sensitive_action.py +438 -0
  23. iam_validator/checks/service_wildcard.py +98 -0
  24. iam_validator/checks/set_operator_validation.py +153 -0
  25. iam_validator/checks/sid_uniqueness.py +146 -0
  26. iam_validator/checks/trust_policy_validation.py +509 -0
  27. iam_validator/checks/utils/__init__.py +17 -0
  28. iam_validator/checks/utils/action_parser.py +149 -0
  29. iam_validator/checks/utils/policy_level_checks.py +190 -0
  30. iam_validator/checks/utils/sensitive_action_matcher.py +293 -0
  31. iam_validator/checks/utils/wildcard_expansion.py +86 -0
  32. iam_validator/checks/wildcard_action.py +58 -0
  33. iam_validator/checks/wildcard_resource.py +374 -0
  34. iam_validator/commands/__init__.py +31 -0
  35. iam_validator/commands/analyze.py +549 -0
  36. iam_validator/commands/base.py +48 -0
  37. iam_validator/commands/cache.py +393 -0
  38. iam_validator/commands/completion.py +471 -0
  39. iam_validator/commands/download_services.py +255 -0
  40. iam_validator/commands/post_to_pr.py +86 -0
  41. iam_validator/commands/query.py +485 -0
  42. iam_validator/commands/validate.py +830 -0
  43. iam_validator/core/__init__.py +13 -0
  44. iam_validator/core/access_analyzer.py +671 -0
  45. iam_validator/core/access_analyzer_report.py +640 -0
  46. iam_validator/core/aws_fetcher.py +29 -0
  47. iam_validator/core/aws_service/__init__.py +21 -0
  48. iam_validator/core/aws_service/cache.py +108 -0
  49. iam_validator/core/aws_service/client.py +205 -0
  50. iam_validator/core/aws_service/fetcher.py +641 -0
  51. iam_validator/core/aws_service/parsers.py +149 -0
  52. iam_validator/core/aws_service/patterns.py +51 -0
  53. iam_validator/core/aws_service/storage.py +291 -0
  54. iam_validator/core/aws_service/validators.py +380 -0
  55. iam_validator/core/check_registry.py +679 -0
  56. iam_validator/core/cli.py +134 -0
  57. iam_validator/core/codeowners.py +245 -0
  58. iam_validator/core/condition_validators.py +626 -0
  59. iam_validator/core/config/__init__.py +81 -0
  60. iam_validator/core/config/aws_api.py +35 -0
  61. iam_validator/core/config/aws_global_conditions.py +160 -0
  62. iam_validator/core/config/category_suggestions.py +181 -0
  63. iam_validator/core/config/check_documentation.py +390 -0
  64. iam_validator/core/config/condition_requirements.py +258 -0
  65. iam_validator/core/config/config_loader.py +670 -0
  66. iam_validator/core/config/defaults.py +739 -0
  67. iam_validator/core/config/principal_requirements.py +421 -0
  68. iam_validator/core/config/sensitive_actions.py +672 -0
  69. iam_validator/core/config/service_principals.py +132 -0
  70. iam_validator/core/config/wildcards.py +127 -0
  71. iam_validator/core/constants.py +149 -0
  72. iam_validator/core/diff_parser.py +325 -0
  73. iam_validator/core/finding_fingerprint.py +131 -0
  74. iam_validator/core/formatters/__init__.py +27 -0
  75. iam_validator/core/formatters/base.py +147 -0
  76. iam_validator/core/formatters/console.py +68 -0
  77. iam_validator/core/formatters/csv.py +171 -0
  78. iam_validator/core/formatters/enhanced.py +481 -0
  79. iam_validator/core/formatters/html.py +672 -0
  80. iam_validator/core/formatters/json.py +33 -0
  81. iam_validator/core/formatters/markdown.py +64 -0
  82. iam_validator/core/formatters/sarif.py +251 -0
  83. iam_validator/core/ignore_patterns.py +297 -0
  84. iam_validator/core/ignore_processor.py +309 -0
  85. iam_validator/core/ignored_findings.py +400 -0
  86. iam_validator/core/label_manager.py +197 -0
  87. iam_validator/core/models.py +404 -0
  88. iam_validator/core/policy_checks.py +220 -0
  89. iam_validator/core/policy_loader.py +785 -0
  90. iam_validator/core/pr_commenter.py +780 -0
  91. iam_validator/core/report.py +942 -0
  92. iam_validator/integrations/__init__.py +28 -0
  93. iam_validator/integrations/github_integration.py +1821 -0
  94. iam_validator/integrations/ms_teams.py +442 -0
  95. iam_validator/sdk/__init__.py +220 -0
  96. iam_validator/sdk/arn_matching.py +382 -0
  97. iam_validator/sdk/context.py +222 -0
  98. iam_validator/sdk/exceptions.py +48 -0
  99. iam_validator/sdk/helpers.py +177 -0
  100. iam_validator/sdk/policy_utils.py +451 -0
  101. iam_validator/sdk/query_utils.py +454 -0
  102. iam_validator/sdk/shortcuts.py +283 -0
  103. iam_validator/utils/__init__.py +35 -0
  104. iam_validator/utils/cache.py +105 -0
  105. iam_validator/utils/regex.py +205 -0
  106. iam_validator/utils/terminal.py +22 -0
@@ -0,0 +1,132 @@
1
+ """
2
+ Service principals utilities for resource policy validation.
3
+
4
+ This module provides:
5
+ - Default list of common AWS service principals
6
+ - Utility to check if a principal is any AWS service principal
7
+ - Functions to categorize service principals by type
8
+
9
+ Configuration:
10
+ - Use "*" in allowed_service_principals to allow ALL AWS service principals
11
+ - Use explicit list to restrict to specific services only
12
+ - AWS service principals end with .amazonaws.com or .amazonaws.com.cn
13
+ """
14
+
15
+ from typing import Final
16
+
17
+ # ============================================================================
18
+ # Allowed Service Principals
19
+ # ============================================================================
20
+ # These AWS service principals are commonly used in resource policies
21
+ # and are generally considered safe to allow
22
+
23
+ DEFAULT_SERVICE_PRINCIPALS: Final[tuple[str, ...]] = (
24
+ "cloudfront.amazonaws.com",
25
+ "s3.amazonaws.com",
26
+ "sns.amazonaws.com",
27
+ "lambda.amazonaws.com",
28
+ "logs.amazonaws.com",
29
+ "events.amazonaws.com",
30
+ "elasticloadbalancing.amazonaws.com",
31
+ "cloudtrail.amazonaws.com",
32
+ "config.amazonaws.com",
33
+ "backup.amazonaws.com",
34
+ "cloudwatch.amazonaws.com",
35
+ "monitoring.amazonaws.com",
36
+ "ec2.amazonaws.com",
37
+ "ecs-tasks.amazonaws.com",
38
+ "eks.amazonaws.com",
39
+ "apigateway.amazonaws.com",
40
+ )
41
+
42
+
43
+ def get_service_principals() -> tuple[str, ...]:
44
+ """
45
+ Get tuple of allowed service principals.
46
+
47
+ Returns:
48
+ Tuple of AWS service principal names
49
+ """
50
+ return DEFAULT_SERVICE_PRINCIPALS
51
+
52
+
53
+ def is_allowed_service_principal(principal: str) -> bool:
54
+ """
55
+ Check if a principal is an allowed service principal.
56
+
57
+ Args:
58
+ principal: Principal to check (e.g., "lambda.amazonaws.com")
59
+
60
+ Returns:
61
+ True if principal is in allowed list
62
+
63
+ Performance: O(n) but small list (~16 items)
64
+ """
65
+ return principal in DEFAULT_SERVICE_PRINCIPALS
66
+
67
+
68
+ def is_aws_service_principal(principal: str) -> bool:
69
+ """
70
+ Check if a principal is an AWS service principal (any AWS service).
71
+
72
+ This checks if the principal matches the AWS service principal pattern.
73
+ AWS service principals typically end with ".amazonaws.com" or ".amazonaws.com.cn"
74
+
75
+ Args:
76
+ principal: Principal to check (e.g., "lambda.amazonaws.com", "s3.amazonaws.com.cn")
77
+
78
+ Returns:
79
+ True if principal matches AWS service principal pattern
80
+
81
+ Examples:
82
+ >>> is_aws_service_principal("lambda.amazonaws.com")
83
+ True
84
+ >>> is_aws_service_principal("s3.amazonaws.com.cn")
85
+ True
86
+ >>> is_aws_service_principal("arn:aws:iam::123456789012:root")
87
+ False
88
+ >>> is_aws_service_principal("*")
89
+ False
90
+ """
91
+ if not isinstance(principal, str):
92
+ return False
93
+
94
+ # AWS service principals end with .amazonaws.com or .amazonaws.com.cn
95
+ return principal.endswith(".amazonaws.com") or principal.endswith(".amazonaws.com.cn")
96
+
97
+
98
+ def get_service_principals_by_category() -> dict[str, tuple[str, ...]]:
99
+ """
100
+ Get service principals organized by service category.
101
+
102
+ Returns:
103
+ Dictionary mapping categories to service principal tuples
104
+ """
105
+ return {
106
+ "storage": (
107
+ "s3.amazonaws.com",
108
+ "backup.amazonaws.com",
109
+ ),
110
+ "compute": (
111
+ "lambda.amazonaws.com",
112
+ "ec2.amazonaws.com",
113
+ "ecs-tasks.amazonaws.com",
114
+ "eks.amazonaws.com",
115
+ ),
116
+ "networking": (
117
+ "cloudfront.amazonaws.com",
118
+ "elasticloadbalancing.amazonaws.com",
119
+ "apigateway.amazonaws.com",
120
+ ),
121
+ "monitoring": (
122
+ "logs.amazonaws.com",
123
+ "cloudwatch.amazonaws.com",
124
+ "monitoring.amazonaws.com",
125
+ "cloudtrail.amazonaws.com",
126
+ ),
127
+ "messaging": (
128
+ "sns.amazonaws.com",
129
+ "events.amazonaws.com",
130
+ ),
131
+ "management": ("config.amazonaws.com",),
132
+ }
@@ -0,0 +1,127 @@
1
+ """
2
+ Default wildcard configurations for security best practices checks.
3
+
4
+ These wildcards define which actions are considered "safe" to use with
5
+ Resource: "*" (e.g., read-only describe operations).
6
+
7
+ Using Python tuples instead of YAML lists provides:
8
+ - Zero parsing overhead
9
+ - Immutable by default (tuples)
10
+ - Better performance
11
+ - Easy PyPI packaging
12
+ """
13
+
14
+ from typing import Final
15
+
16
+ # ============================================================================
17
+ # Allowed Wildcards for Resource: "*"
18
+ # ============================================================================
19
+ # These action patterns are considered safe to use with wildcard resources
20
+ # They are typically read-only operations that need broad resource access
21
+
22
+ DEFAULT_ALLOWED_WILDCARDS: Final[tuple[str, ...]] = (
23
+ # Auto Scaling
24
+ "autoscaling:Describe*",
25
+ # CloudWatch
26
+ "cloudwatch:Describe*",
27
+ "cloudwatch:Get*",
28
+ "cloudwatch:List*",
29
+ # DynamoDB
30
+ "dynamodb:Describe*",
31
+ "dynamodb:Get*",
32
+ "dynamodb:List*",
33
+ # EC2
34
+ "ec2:Describe*",
35
+ "ec2:List*",
36
+ # Elastic Load Balancing
37
+ "elasticloadbalancing:Describe*",
38
+ # IAM (non-sensitive read operations)
39
+ "iam:Get*",
40
+ "iam:List*",
41
+ # KMS
42
+ "kms:Describe*",
43
+ # Lambda
44
+ "lambda:Get*",
45
+ "lambda:List*",
46
+ # CloudWatch Logs
47
+ "logs:Describe*",
48
+ "logs:Filter*",
49
+ "logs:Get*",
50
+ # RDS
51
+ "rds:Describe*",
52
+ # Route53
53
+ "route53:Get*",
54
+ "route53:List*",
55
+ # S3 (safe read operations only)
56
+ "s3:Describe*",
57
+ "s3:GetBucket*",
58
+ "s3:GetM*",
59
+ "s3:List*",
60
+ # SQS
61
+ "sqs:Get*",
62
+ "sqs:List*",
63
+ # API Gateway
64
+ "apigateway:GET",
65
+ )
66
+
67
+ # ============================================================================
68
+ # Service-Level Wildcards (Allowed Services)
69
+ # ============================================================================
70
+ # Services that are allowed to use service-level wildcards like "logs:*"
71
+ # These are typically low-risk monitoring/logging services
72
+
73
+ DEFAULT_SERVICE_WILDCARDS: Final[tuple[str, ...]] = (
74
+ "logs",
75
+ "cloudwatch",
76
+ "xray",
77
+ )
78
+
79
+
80
+ def get_allowed_wildcards() -> tuple[str, ...]:
81
+ """
82
+ Get tuple of allowed wildcard action patterns.
83
+
84
+ Returns:
85
+ Tuple of action patterns that are safe to use with Resource: "*"
86
+ """
87
+ return DEFAULT_ALLOWED_WILDCARDS
88
+
89
+
90
+ def get_allowed_service_wildcards() -> tuple[str, ...]:
91
+ """
92
+ Get tuple of services allowed to use service-level wildcards.
93
+
94
+ Returns:
95
+ Tuple of service names (e.g., "logs", "cloudwatch")
96
+ """
97
+ return DEFAULT_SERVICE_WILDCARDS
98
+
99
+
100
+ def is_allowed_wildcard(pattern: str) -> bool:
101
+ """
102
+ Check if a wildcard pattern is in the allowed list.
103
+
104
+ Args:
105
+ pattern: Action pattern to check (e.g., "s3:List*")
106
+
107
+ Returns:
108
+ True if pattern is in allowed wildcards
109
+
110
+ Performance: O(n) but typically small list (~25 items)
111
+ """
112
+ return pattern in DEFAULT_ALLOWED_WILDCARDS
113
+
114
+
115
+ def is_allowed_service_wildcard(service: str) -> bool:
116
+ """
117
+ Check if a service is allowed to use service-level wildcards.
118
+
119
+ Args:
120
+ service: Service name (e.g., "logs", "s3")
121
+
122
+ Returns:
123
+ True if service is in allowed list
124
+
125
+ Performance: O(n) but very small list (~3 items)
126
+ """
127
+ return service in DEFAULT_SERVICE_WILDCARDS
@@ -0,0 +1,149 @@
1
+ """
2
+ Core constants for IAM Policy Validator.
3
+
4
+ This module defines constants used across the validator to ensure consistency
5
+ and provide a single source of truth for shared values. These constants are
6
+ based on AWS service limits and documentation.
7
+
8
+ References:
9
+ - AWS IAM Policy Size Limits: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_iam-quotas.html
10
+ - AWS ARN Format: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference-arns.html
11
+ """
12
+
13
+ # ============================================================================
14
+ # ARN Validation
15
+ # ============================================================================
16
+
17
+ # ARN Validation Pattern
18
+ # This pattern is specifically designed for validation and allows wildcards (*) in region and account fields
19
+ # Unlike the parsing pattern in CompiledPatterns, this is more lenient for validation purposes
20
+ # Supports all AWS partitions: aws, aws-cn, aws-us-gov, aws-eusc, aws-iso*
21
+ DEFAULT_ARN_VALIDATION_PATTERN = r"^arn:(aws|aws-cn|aws-us-gov|aws-eusc|aws-iso|aws-iso-b|aws-iso-e|aws-iso-f):[a-z0-9\-]+:[a-z0-9\-*]*:[0-9*]*:.+$"
22
+
23
+ # Maximum allowed ARN length to prevent ReDoS attacks
24
+ # AWS maximum ARN length is approximately 2048 characters
25
+ MAX_ARN_LENGTH = 2048
26
+
27
+ # ============================================================================
28
+ # AWS IAM Policy Size Limits
29
+ # ============================================================================
30
+ # These limits are enforced by AWS and policies exceeding them will be rejected
31
+ # Note: AWS does not count whitespace when calculating policy size
32
+
33
+ # Managed policy maximum size (characters, excluding whitespace)
34
+ MAX_MANAGED_POLICY_SIZE = 6144
35
+
36
+ # Inline policy maximum size for IAM users (characters, excluding whitespace)
37
+ MAX_INLINE_USER_POLICY_SIZE = 2048
38
+
39
+ # Inline policy maximum size for IAM groups (characters, excluding whitespace)
40
+ MAX_INLINE_GROUP_POLICY_SIZE = 5120
41
+
42
+ # Inline policy maximum size for IAM roles (characters, excluding whitespace)
43
+ MAX_INLINE_ROLE_POLICY_SIZE = 10240
44
+
45
+ # Policy size limits dictionary (for backward compatibility and easy lookup)
46
+ AWS_POLICY_SIZE_LIMITS = {
47
+ "managed": MAX_MANAGED_POLICY_SIZE,
48
+ "inline_user": MAX_INLINE_USER_POLICY_SIZE,
49
+ "inline_group": MAX_INLINE_GROUP_POLICY_SIZE,
50
+ "inline_role": MAX_INLINE_ROLE_POLICY_SIZE,
51
+ }
52
+
53
+ # ============================================================================
54
+ # Configuration Defaults
55
+ # ============================================================================
56
+
57
+ # Default configuration file names (searched in order)
58
+ DEFAULT_CONFIG_FILENAMES = [
59
+ "iam-validator.yaml",
60
+ "iam-validator.yml",
61
+ ".iam-validator.yaml",
62
+ ".iam-validator.yml",
63
+ ]
64
+
65
+ # ============================================================================
66
+ # Severity Levels
67
+ # ============================================================================
68
+ # Severity level groupings for filtering and categorization
69
+ # Used across formatters and report generation
70
+
71
+ # High severity issues that typically fail validation
72
+ HIGH_SEVERITY_LEVELS = ("error", "critical", "high")
73
+
74
+ # Medium severity issues (warnings)
75
+ MEDIUM_SEVERITY_LEVELS = ("warning", "medium")
76
+
77
+ # Low severity issues (informational)
78
+ LOW_SEVERITY_LEVELS = ("info", "low")
79
+
80
+ # ============================================================================
81
+ # GitHub Integration
82
+ # ============================================================================
83
+
84
+ # Bot identifier for GitHub comments and reviews
85
+ BOT_IDENTIFIER = "🤖 IAM Policy Validator"
86
+
87
+ # HTML comment markers for identifying bot-generated content (for cleanup/updates)
88
+ SUMMARY_IDENTIFIER = "<!-- iam-policy-validator-summary -->"
89
+ REVIEW_IDENTIFIER = "<!-- iam-policy-validator-review -->"
90
+ IGNORED_FINDINGS_IDENTIFIER = "<!-- iam-policy-validator-ignored-findings -->"
91
+
92
+ # GitHub comment size limits
93
+ # GitHub's actual limit is 65536 characters, but we use a smaller limit for safety
94
+ GITHUB_MAX_COMMENT_LENGTH = 65000 # Maximum single comment length
95
+ GITHUB_COMMENT_SPLIT_LIMIT = 60000 # Target size when splitting into multiple parts
96
+
97
+ # Comment size estimation parameters (used for multi-part comment splitting)
98
+ COMMENT_BASE_OVERHEAD_CHARS = 2000 # Base overhead for headers/footers
99
+ COMMENT_CHARS_PER_ISSUE_ESTIMATE = 500 # Average characters per issue
100
+ COMMENT_CONTINUATION_OVERHEAD_CHARS = 200 # Overhead for continuation markers
101
+ FORMATTING_SAFETY_BUFFER = 100 # Safety buffer for formatting calculations
102
+
103
+ # ============================================================================
104
+ # Console Display Settings
105
+ # ============================================================================
106
+
107
+ # Panel width for formatted console output
108
+ CONSOLE_PANEL_WIDTH = 100
109
+
110
+ # Rich console color styles
111
+ CONSOLE_HEADER_COLOR = "bright_blue"
112
+
113
+ # ============================================================================
114
+ # Cache and Timeout Settings
115
+ # ============================================================================
116
+
117
+ # Cache TTL (Time To Live) - 7 days
118
+ DEFAULT_CACHE_TTL_HOURS = 168 # 7 days in hours
119
+ DEFAULT_CACHE_TTL_SECONDS = 604800 # 7 days in seconds (168 * 3600)
120
+
121
+ # HTTP request timeout in seconds
122
+ DEFAULT_HTTP_TIMEOUT_SECONDS = 30.0
123
+
124
+ # Time conversion constants
125
+ SECONDS_PER_HOUR = 3600
126
+
127
+ # ============================================================================
128
+ # Policy Type Restrictions
129
+ # ============================================================================
130
+
131
+ # AWS services that support Resource Control Policies (RCP)
132
+ # These services can have wildcard actions in RCP policy statements
133
+ # Reference: https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_policies_rcps.html
134
+ RCP_SUPPORTED_SERVICES = frozenset(
135
+ {
136
+ "s3",
137
+ "sts",
138
+ "sqs",
139
+ "secretsmanager",
140
+ "kms",
141
+ }
142
+ )
143
+
144
+ # ============================================================================
145
+ # AWS Documentation URLs
146
+ # ============================================================================
147
+
148
+ # AWS Service Authorization Reference (for finding valid actions, resources, and condition keys)
149
+ AWS_SERVICE_AUTH_REF_URL = "https://docs.aws.amazon.com/service-authorization/latest/reference/reference_policies_actions-resources-contextkeys.html"