/
opensearch.sp
237 lines (219 loc) · 9.8 KB
/
opensearch.sp
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
locals {
conformance_pack_opensearch_common_tags = merge(local.aws_compliance_common_tags, {
service = "AWS/OpenSearch"
})
}
control "opensearch_domain_encryption_at_rest_enabled" {
title = "OpenSearch domains should have encryption at rest enabled"
description = "This control checks whether Amazon OpenSearch domains have encryption-at-rest configuration enabled. The check fails if encryption at rest is not enabled."
query = query.opensearch_domain_encryption_at_rest_enabled
tags = merge(local.conformance_pack_opensearch_common_tags, {
pci_dss_v321 = "true"
})
}
control "opensearch_domain_fine_grained_access_enabled" {
title = "OpenSearch domains should have fine-grained access control enabled"
description = "This control checks whether OpenSearch domains have fine-grained access control enabled. The control fails if the fine-grained access control is not enabled. Fine-grained access control requires advanced-security-optionsin the OpenSearch parameter update-domain-config to be enabled."
query = query.opensearch_domain_fine_grained_access_enabled
tags = merge(local.conformance_pack_opensearch_common_tags, {
pci_dss_v321 = "true"
})
}
control "opensearch_domain_https_required" {
title = "OpenSearch domains should use HTTPS"
description = "This control checks whether connections to OpenSearch domains are using HTTPS. The rule is non compliant if the Amazon OpenSearch domain 'EnforceHTTPS' is not 'true' or is 'true' and 'TLSSecurityPolicy' is not in 'tlsPolicies'."
query = query.opensearch_domain_https_required
tags = merge(local.conformance_pack_opensearch_common_tags, {
pci_dss_v321 = "true"
})
}
control "opensearch_domain_audit_logging_enabled" {
title = "OpenSearch domains should have audit logging enabled"
description = "This control checks whether Amazon OpenSearch Service domains have audit logging enabled. The rule is non compliant if an OpenSearch Service domain does not have audit logging enabled."
query = query.opensearch_domain_audit_logging_enabled
tags = merge(local.conformance_pack_opensearch_common_tags, {
pci_dss_v321 = "true"
soc_2 = "true"
})
}
control "opensearch_domain_logs_to_cloudwatch" {
title = "OpenSearch domains logs to Amazon CloudWatch Logs"
description = "This control checks whether Amazon OpenSearch Service domains are configured to send logs to Amazon CloudWatch Logs. The rule is non compliant if logging is not configured."
query = query.opensearch_domain_logs_to_cloudwatch
tags = merge(local.conformance_pack_opensearch_common_tags, {
pci_dss_v321 = "true"
soc_2 = "true"
})
}
query "opensearch_domain_encryption_at_rest_enabled" {
sql = <<-EOQ
select
arn as resource,
case
when encryption_at_rest_options ->> 'Enabled' = 'false' then 'alarm'
else 'ok'
end as status,
case
when encryption_at_rest_options ->> 'Enabled' = 'false' then title || ' encryption at rest not enabled.'
else title || ' encryption at rest enabled.'
end as reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
aws_opensearch_domain;
EOQ
}
query "opensearch_domain_fine_grained_access_enabled" {
sql = <<-EOQ
select
arn as resource,
case
when advanced_security_options is null or not (advanced_security_options -> 'Enabled')::boolean then 'alarm'
else 'ok'
end as status,
case
when advanced_security_options is null or not (advanced_security_options -> 'Enabled')::boolean then title || ' has fine-grained access control disabled.'
else title || ' has fine-grained access control enabled.'
end as reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
aws_opensearch_domain;
EOQ
}
query "opensearch_domain_https_required" {
sql = <<-EOQ
select
arn as resource,
case
when (domain_endpoint_options ->> 'EnforceHTTPS' = 'false') or (domain_endpoint_options ->> 'EnforceHTTPS' = 'true' and domain_endpoint_options ->> 'TLSSecurityPolicy' not in ('tlsPolicies')) then 'alarm'
else 'ok'
end status,
case
when (domain_endpoint_options ->> 'EnforceHTTPS' = 'false') or (domain_endpoint_options ->> 'EnforceHTTPS' = 'true' and domain_endpoint_options ->> 'TLSSecurityPolicy' not in ('tlsPolicies')) then title || ' does not use HTTPS.'
else title || ' uses HTTPS.'
end as reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
aws_opensearch_domain;
EOQ
}
query "opensearch_domain_audit_logging_enabled" {
sql = <<-EOQ
select
arn as resource,
case
when log_publishing_options -> 'AUDIT_LOGS' ->> 'Enabled' = 'true' then 'ok'
else 'ok'
end as status,
case
when log_publishing_options -> 'AUDIT_LOGS' ->> 'Enabled' = 'true' then title || ' audit logging enabled.'
else title || ' audit logging disabled.'
end as reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
aws_opensearch_domain;
EOQ
}
query "opensearch_domain_logs_to_cloudwatch" {
sql = <<-EOQ
select
arn as resource,
case
when log_publishing_options is null then 'alarm'
when
( log_publishing_options -> 'AUDIT_LOGS' is null
or log_publishing_options -> 'AUDIT_LOGS' -> 'Enabled' = 'false'
or (log_publishing_options -> 'AUDIT_LOGS' -> 'Enabled' = 'true' and log_publishing_options -> 'AUDIT_LOGS' -> 'CloudWatchLogsLogGroupArn' is not null)
)
and
( log_publishing_options -> 'INDEX_SLOW_LOGS' is null
or log_publishing_options -> 'INDEX_SLOW_LOGS' -> 'Enabled' = 'false'
or (log_publishing_options -> 'INDEX_SLOW_LOGS' -> 'Enabled' = 'true' and log_publishing_options -> 'INDEX_SLOW_LOGS' -> 'CloudWatchLogsLogGroupArn' is not null)
)
and
( log_publishing_options -> 'SEARCH_SLOW_LOGS' is null
or log_publishing_options -> 'SEARCH_SLOW_LOGS' -> 'Enabled' = 'false'
or (log_publishing_options -> 'SEARCH_SLOW_LOGS' -> 'Enabled' = 'true' and log_publishing_options -> 'SEARCH_SLOW_LOGS' -> 'CloudWatchLogsLogGroupArn' is not null)
)
and
( log_publishing_options -> 'ES_APPLICATION_LOGS' is null
or log_publishing_options -> 'ES_APPLICATION_LOGS' -> 'Enabled' = 'false'
or (log_publishing_options -> 'ES_APPLICATION_LOGS' -> 'Enabled' = 'true' and log_publishing_options -> 'ES_APPLICATION_LOGS' -> 'CloudWatchLogsLogGroupArn' is not null)
)
then 'ok'
else 'alarm'
end as status,
case
when log_publishing_options is null then title || ' logging not enabled.'
when
( log_publishing_options -> 'AUDIT_LOGS' is null
or log_publishing_options -> 'AUDIT_LOGS' -> 'Enabled' = 'false'
or (log_publishing_options -> 'AUDIT_LOGS' -> 'Enabled' = 'true' and log_publishing_options -> 'AUDIT_LOGS' -> 'CloudWatchLogsLogGroupArn' is not null)
)
and
( log_publishing_options -> 'INDEX_SLOW_LOGS' is null
or log_publishing_options -> 'INDEX_SLOW_LOGS' -> 'Enabled' = 'false'
or (log_publishing_options -> 'INDEX_SLOW_LOGS' -> 'Enabled' = 'true' and log_publishing_options -> 'INDEX_SLOW_LOGS' -> 'CloudWatchLogsLogGroupArn' is not null)
)
and
( log_publishing_options -> 'SEARCH_SLOW_LOGS' is null
or log_publishing_options -> 'SEARCH_SLOW_LOGS' -> 'Enabled' = 'false'
or (log_publishing_options -> 'SEARCH_SLOW_LOGS' -> 'Enabled' = 'true' and log_publishing_options -> 'SEARCH_SLOW_LOGS' -> 'CloudWatchLogsLogGroupArn' is not null)
)
and
( log_publishing_options -> 'ES_APPLICATION_LOGS' is null
or log_publishing_options -> 'ES_APPLICATION_LOGS' -> 'Enabled' = 'false'
or (log_publishing_options -> 'ES_APPLICATION_LOGS' -> 'Enabled' = 'true' and log_publishing_options -> 'ES_APPLICATION_LOGS' -> 'CloudWatchLogsLogGroupArn' is not null)
) then title || ' send logs to Amazon CloudWatch.'
else title || ' does not send logs to Amazon CloudWatch.'
end as reason
${local.tag_dimensions_sql}
${local.common_dimensions_sql}
from
aws_opensearch_domain;
EOQ
}
# Non-Config rule query
query "opensearch_domain_in_vpc" {
sql = <<-EOQ
with public_subnets as (
select
distinct a -> 'SubnetId' as SubnetId
from
aws_vpc_route_table as t,
jsonb_array_elements(associations) as a,
jsonb_array_elements(routes) as r
where
r ->> 'DestinationCidrBlock' = '0.0.0.0/0'
and r ->> 'GatewayId' like 'igw-%'
), opensearch_domain_with_public_subnet as (
select
arn
from
aws_opensearch_domain ,
jsonb_array_elements(vpc_options -> 'SubnetIds') as s
where
s in (select SubnetId from public_subnets)
)
select
d.arn as resource,
case
when d.vpc_options ->> 'VPCId' is null then 'alarm'
when d.vpc_options ->> 'VPCId' is not null and p.arn is not null then 'alarm'
else 'ok'
end status,
case
when vpc_options ->> 'VPCId' is null then title || ' not in VPC.'
when d.vpc_options ->> 'VPCId' is not null and p.arn is not null then title || ' attached to public subnet.'
else title || ' in VPC ' || (vpc_options ->> 'VPCId') || '.'
end reason
${replace(local.tag_dimensions_qualifier_sql, "__QUALIFIER__", "d.")}
${replace(local.common_dimensions_qualifier_sql, "__QUALIFIER__", "d.")}
from
aws_opensearch_domain as d left join opensearch_domain_with_public_subnet as p
on d.arn = p.arn;
EOQ
}