|
12 | 12 | ssm = boto3.client('ssm') |
13 | 13 |
|
14 | 14 | REQUIRED_ENV_VARS = [ |
15 | | - 'EXCLUDE_ACCOUNT_FILTER', 'JIRA_ISSUE_CUSTOM_FIELDS', 'JIRA_ISSUE_TYPE', 'JIRA_PROJECT_KEY', 'JIRA_SECRET_ARN', 'JIRA_SECRET_TYPE' |
| 15 | + 'EXCLUDE_ACCOUNT_FILTER', 'INCLUDE_ACCOUNT_FILTER', 'JIRA_ISSUE_CUSTOM_FIELDS', 'JIRA_ISSUE_TYPE', 'JIRA_PROJECT_KEY', 'JIRA_SECRET_ARN', 'JIRA_SECRET_TYPE' |
16 | 16 | ] |
17 | 17 |
|
18 | 18 | DEFAULT_JIRA_AUTOCLOSE_COMMENT = 'Security Hub finding has been resolved. Autoclosing the issue.' |
@@ -40,7 +40,8 @@ def lambda_handler(event: dict, context: LambdaContext): |
40 | 40 | raise RuntimeError("Required environment variables are missing.") from e |
41 | 41 |
|
42 | 42 | # Retrieve environment variables |
43 | | - exclude_account_filter = os.environ['EXCLUDE_ACCOUNT_FILTER'] |
| 43 | + exclude_account_filter = json.loads(os.environ['EXCLUDE_ACCOUNT_FILTER']) |
| 44 | + include_account_filter = json.loads(os.environ['INCLUDE_ACCOUNT_FILTER']) |
44 | 45 | jira_autoclose_comment = os.getenv( |
45 | 46 | 'JIRA_AUTOCLOSE_COMMENT', DEFAULT_JIRA_AUTOCLOSE_COMMENT) |
46 | 47 | jira_autoclose_transition = os.getenv( |
@@ -85,11 +86,21 @@ def lambda_handler(event: dict, context: LambdaContext): |
85 | 86 | compliance_status = finding['Compliance']['Status'] if 'Compliance' in finding else COMPLIANCE_STATUS_MISSING |
86 | 87 | record_state = finding['RecordState'] |
87 | 88 |
|
88 | | - # Only process finding if account is not excluded |
89 | | - if finding_account_id in exclude_account_filter: |
90 | | - logger.info( |
91 | | - f"Account {finding_account_id} is excluded from Jira ticket creation.") |
92 | | - return |
| 89 | + # Apply account filtering logic |
| 90 | + # Priority: include_account_filter > exclude_account_filter |
| 91 | + if include_account_filter: |
| 92 | + # If include list is provided, only process accounts in the list |
| 93 | + if finding_account_id not in include_account_filter: |
| 94 | + logger.info( |
| 95 | + f"Account {finding_account_id} is not in the include list. Skipping Jira ticket creation.") |
| 96 | + return |
| 97 | + elif exclude_account_filter: |
| 98 | + # If exclude list is provided (and no include list), skip accounts in the list |
| 99 | + if finding_account_id in exclude_account_filter: |
| 100 | + logger.info( |
| 101 | + f"Account {finding_account_id} is excluded from Jira ticket creation.") |
| 102 | + return |
| 103 | + # If neither list is provided, process all accounts |
93 | 104 |
|
94 | 105 | # Handle new findings |
95 | 106 | # Ticket is created when Workflow Status is NEW and Compliance Status is FAILED, WARNING or is missing from the finding (case with e.g. Inspector findings) |
|
0 commit comments