Skip to content

Commit f04d5d1

Browse files
authored
feat: Allow overriding items for each control in cis-alarms module (#41)
1 parent a3c8872 commit f04d5d1

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

examples/cis-alarms/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ No providers.
3232
|------|--------|---------|
3333
| <a name="module_all_cis_alarms"></a> [all\_cis\_alarms](#module\_all\_cis\_alarms) | ../../modules/cis-alarms | n/a |
3434
| <a name="module_aws_sns_topic"></a> [aws\_sns\_topic](#module\_aws\_sns\_topic) | ../fixtures/aws_sns_topic | n/a |
35+
| <a name="module_control_overrides"></a> [control\_overrides](#module\_control\_overrides) | ../../modules/cis-alarms | n/a |
3536
| <a name="module_disabled_all_cis_alarms"></a> [disabled\_all\_cis\_alarms](#module\_disabled\_all\_cis\_alarms) | ../../modules/cis-alarms | n/a |
3637
| <a name="module_log"></a> [log](#module\_log) | ../fixtures/aws_cloudwatch_log_group | n/a |
38+
| <a name="module_second_aws_sns_topic"></a> [second\_aws\_sns\_topic](#module\_second\_aws\_sns\_topic) | ../fixtures/aws_sns_topic | n/a |
3739

3840
## Resources
3941

examples/cis-alarms/main.tf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ module "aws_sns_topic" {
66
source = "../fixtures/aws_sns_topic"
77
}
88

9+
module "second_aws_sns_topic" {
10+
source = "../fixtures/aws_sns_topic"
11+
}
12+
913
module "log" {
1014
source = "../fixtures/aws_cloudwatch_log_group"
1115
}
@@ -30,3 +34,20 @@ module "disabled_all_cis_alarms" {
3034
log_group_name = module.log.cloudwatch_log_group_name
3135
alarm_actions = [module.aws_sns_topic.sns_topic_arn]
3236
}
37+
38+
module "control_overrides" {
39+
source = "../../modules/cis-alarms"
40+
41+
log_group_name = module.log.cloudwatch_log_group_name
42+
alarm_actions = [module.aws_sns_topic.sns_topic_arn]
43+
44+
control_overrides = {
45+
NoMFAConsoleSignin = {
46+
pattern = "{($.eventName=\"ConsoleLogin\") && ($.additionalEventData.MFAUsed != \"Yes\") && ($.userIdentity.sessionContext.sessionIssuer.arn != \"arn:aws:iam::*:role/aws-reserved/sso.amazonaws.com/*\")}"
47+
}
48+
49+
AWSOrganizationsChanges = {
50+
alarm_actions = [module.second_aws_sns_topic.sns_topic_arn]
51+
}
52+
}
53+
}

modules/cis-alarms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ No modules.
3636
|------|-------------|------|---------|:--------:|
3737
| <a name="input_actions_enabled"></a> [actions\_enabled](#input\_actions\_enabled) | Indicates whether or not actions should be executed during any changes to the alarm's state. | `bool` | `true` | no |
3838
| <a name="input_alarm_actions"></a> [alarm\_actions](#input\_alarm\_actions) | List of ARNs to put as Cloudwatch Alarms actions (eg, ARN of SNS topic) | `list(string)` | `[]` | no |
39+
| <a name="input_control_overrides"></a> [control\_overrides](#input\_control\_overrides) | A map of overrides to apply to each control | `any` | `{}` | no |
3940
| <a name="input_create"></a> [create](#input\_create) | Whether to create the Cloudwatch log metric filter and metric alarms | `bool` | `true` | no |
4041
| <a name="input_disabled_controls"></a> [disabled\_controls](#input\_disabled\_controls) | List of IDs of disabled CIS controls | `list(string)` | `[]` | no |
4142
| <a name="input_log_group_name"></a> [log\_group\_name](#input\_log\_group\_name) | The name of the log group to associate the metric filter with | `string` | `""` | no |

modules/cis-alarms/main.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ locals {
7979
###############
8080

8181
prefix = var.use_random_name_prefix ? "${random_pet.this[0].id}-" : var.name_prefix
82-
controls = { for k, v in local.all_controls : k => v if !contains(var.disabled_controls, k) }
82+
controls = { for k, v in local.all_controls : k => merge(v, try(var.control_overrides[k], {})) if var.create && !contains(var.disabled_controls, k) }
8383
}
8484

8585
resource "random_pet" "this" {
@@ -89,7 +89,7 @@ resource "random_pet" "this" {
8989
}
9090

9191
resource "aws_cloudwatch_log_metric_filter" "this" {
92-
for_each = var.create ? local.controls : {}
92+
for_each = local.controls
9393

9494
name = "${local.prefix}${each.key}"
9595
pattern = each.value["pattern"]
@@ -104,7 +104,7 @@ resource "aws_cloudwatch_log_metric_filter" "this" {
104104
}
105105

106106
resource "aws_cloudwatch_metric_alarm" "this" {
107-
for_each = var.create ? local.controls : {}
107+
for_each = local.controls
108108

109109
metric_name = aws_cloudwatch_log_metric_filter.this[each.key].id
110110
namespace = lookup(each.value, "namespace", var.namespace)

modules/cis-alarms/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ variable "name_prefix" {
1616
default = ""
1717
}
1818

19+
variable "control_overrides" {
20+
description = "A map of overrides to apply to each control"
21+
default = {}
22+
type = any
23+
}
24+
1925
variable "disabled_controls" {
2026
description = "List of IDs of disabled CIS controls"
2127
type = list(string)

0 commit comments

Comments
 (0)