-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
136 lines (105 loc) · 3.55 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
Paths for TOML and YAML files
*/
locals{
elastic_rule_dir = "${path.module}/detection-rules/rules"
custom_rule_dir = "${path.module}/custom"
exception_dir = "${path.module}/exceptions"
exception_container_dir = "${local.exception_dir}/rules"
exception_item_dir = "${local.exception_dir}/items"
list_dir = "${path.module}/lists"
list_item_dir = "${local.list_dir}/items"
}
// ================== Rules ==================
/*
Terraform Resource that creates Elastic Rules from TOMLs
*/
resource "universe" "rules_elastic" {
provider = universe.detection_rule
for_each = fileset(local.elastic_rule_dir, "**/**.toml")
config =<<CONFIG
${file("${local.elastic_rule_dir}/${each.key}")}
%{ if fileexists("${local.exception_container_dir}/${split(".", each.key)[0]}.yaml") }
# Add an Exception Container to each rule
[[rule.exceptions_list]]
id = "${replace(split(".",each.key)[0], "/", "-")}"
list_id = "${replace(split(".",each.key)[0], "/", "-")}"
namespace_type = "single"
type = "detection"
%{ endif }
CONFIG
depends_on = [universe.exceptions, universe.exception_items]
}
/*
Terraform Resource that creates Custom Rules from TOMLs
*/
resource "universe" "rules_custom" {
provider = universe.detection_rule
for_each = fileset(local.custom_rule_dir, "**/**.toml")
config =<<CONFIG
${file("${local.custom_rule_dir}/${each.key}")}
%{ if fileexists("${local.exception_container_dir}/${split(".", each.key)[0]}.yaml") }
# Add an Exception Container to each rule
[[rule.exceptions_list]]
id = "${replace(split(".",each.key)[0], "/", "-")}"
list_id = "${replace(split(".",each.key)[0], "/", "-")}"
namespace_type = "single"
type = "detection"
%{ endif }
CONFIG
depends_on = [universe.exceptions, universe.exception_items]
}
// ================== Exceptions ==================
/*
Terraform Resource that creates Exception Containers
using the filepath to generate their IDs
*/
resource "universe" "exceptions" {
provider = universe.exception_container
for_each = fileset(local.exception_container_dir, "**/**.yaml")
config =<<CONFIG
list_id: "${replace(split(".",each.key)[0], "/", "-")}"
${file("${local.exception_container_dir}/${each.key}")}
CONFIG
}
/*
Terraform Resource that creates Exception Items
and matches them with Exception Containers based on filepaths
*/
resource "universe" "exception_items" {
provider = universe.exception_item
for_each = fileset(local.exception_item_dir, "**/**.yaml")
config =<<CONFIG
item_id: "${replace(split(".",each.key)[0], "/", "-")}"
list_id: "${join("-", slice(split("/", each.key), 0, length(split("/", each.key))-1))}"
${file("${local.exception_item_dir}/${each.key}")}
CONFIG
depends_on = [universe.exceptions, universe.lists]
}
// ================== Lists ==================
/*
Terraform Resource that creates List Containers
using the filepath to generate their IDs
*/
resource "universe" "lists" {
provider = universe.list_container
for_each = fileset(local.list_dir, "**.yaml")
config =<<CONFIG
id: "${replace(split(".",each.key)[0], "/", "-")}"
${file("${local.list_dir}/${each.key}")}
CONFIG
}
/*
Terraform Resource that creates List Items
and matches them with Exception Containers based on filepaths
*/
resource "universe" "list_items" {
provider = universe.list_item
for_each = fileset(local.list_item_dir, "**/**.yaml")
config =<<CONFIG
id: "${replace(split(".",each.key)[0], "/", "-")}"
list_id: "${join("-", slice(split("/", each.key), 0, length(split("/", each.key))-1))}"
${file("${local.list_item_dir}/${each.key}")}
CONFIG
depends_on = [universe.lists]
}