/
kms_key_dashboard.sp
258 lines (225 loc) · 4.5 KB
/
kms_key_dashboard.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
dashboard "kms_key_dashboard" {
title = "OCI KMS Key Dashboard"
documentation = file("./dashboards/kms/docs/kms_key_dashboard.md")
tags = merge(local.kms_common_tags, {
type = "Dashboard"
})
container {
card {
query = query.kms_key_count
width = 3
}
card {
query = query.kms_hsm_key_count
width = 3
}
card {
query = query.kms_key_disabled_count
width = 3
}
}
container {
title = "Assessments"
chart {
title = "Lifecycle State"
query = query.kms_key_lifecycle_state
type = "donut"
width = 3
series "count" {
point "ok" {
color = "ok"
}
point "disabled" {
color = "alert"
}
}
}
}
container {
title = "Analysis"
chart {
title = "Keys by Tenancy"
query = query.kms_key_by_tenancy
type = "column"
width = 4
}
chart {
title = "Keys by Compartment"
query = query.kms_key_by_compartment
type = "column"
width = 4
}
chart {
title = "Keys by Region"
query = query.kms_key_by_region
type = "column"
width = 4
}
chart {
title = "Keys by Age"
query = query.kms_key_by_creation_month
type = "column"
width = 4
}
chart {
title = "Keys by Protection Mode"
query = query.kms_key_by_protection_mode
type = "column"
width = 4
}
}
}
# Card Queries
query "kms_key_count" {
sql = <<-EOQ
select count(*) as "Keys" from oci_kms_key;
EOQ
}
query "kms_key_disabled_count" {
sql = <<-EOQ
select
count(*) as value,
'Disabled Keys' as label,
case count(*) when 0 then 'ok' else 'alert' end as "type"
from
oci_kms_key
where
lifecycle_state = 'DISABLED';
EOQ
}
# Key count by protection_mode i.e. HSM or Software
query "kms_hsm_key_count" {
sql = <<-EOQ
select count(*) as "HSM Based Keys" from oci_kms_key where protection_mode = 'HSM' and lifecycle_state <> 'DELETED';
EOQ
}
# Assessment Queries
query "kms_key_lifecycle_state" {
sql = <<-EOQ
select
case when lifecycle_state = 'DISABLED' then 'disabled' else 'ok' end as status,
count(*)
from
oci_kms_key
where
lifecycle_state <> 'DELETED'
group by
status;
EOQ
}
# Analysis Queries
query "kms_key_by_tenancy" {
sql = <<-EOQ
select
t.name as "Tenancy",
count(k.id)::numeric as "Keys"
from
oci_kms_key as k,
oci_identity_tenancy as t
where
t.id = k.tenant_id
group by
t.name
order by
t.name;
EOQ
}
query "kms_key_by_compartment" {
sql = <<-EOQ
with compartments as (
select
id,
'root [' || title || ']' as title
from
oci_identity_tenancy
union (
select
c.id,
c.title || ' [' || t.title || ']' as title
from
oci_identity_compartment c,
oci_identity_tenancy t
where
c.tenant_id = t.id and c.lifecycle_state = 'ACTIVE'
)
)
select
c.title as "Title",
count(k.*) as "Keys"
from
oci_kms_key as k,
compartments as c
where
c.id = k.compartment_id
group by
c.title
order by
c.title;
EOQ
}
query "kms_key_by_region" {
sql = <<-EOQ
select
region,
count(k.*) as total
from
oci_kms_key as k
group by
region;
EOQ
}
query "kms_key_by_creation_month" {
sql = <<-EOQ
with keys as (
select
title,
time_created,
to_char(time_created,
'YYYY-MM') as creation_month
from
oci_kms_key
),
months as (
select
to_char(d,
'YYYY-MM') as month
from
generate_series(date_trunc('month',
(
select
min(time_created)
from keys)),
date_trunc('month',
current_date),
interval '1 month') as d
),
keys_by_month as (
select
creation_month,
count(*)
from
keys
group by
creation_month
)
select
months.month,
keys_by_month.count
from
months
left join keys_by_month on months.month = keys_by_month.creation_month
order by
months.month desc;
EOQ
}
query "kms_key_by_protection_mode" {
sql = <<-EOQ
select
protection_mode,
count(protection_mode)
from
oci_kms_key
group by
protection_mode;
EOQ
}