-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
504 lines (418 loc) · 25.2 KB
/
main.py
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
import json
import sys
import hvac
import socket
from prometheus_client import CollectorRegistry, Gauge, push_to_gateway
class Metrics:
def __init__(self, registry, pushgateway_addr, labelnames, labelvalues):
self.registry = registry
self.pushgateway_addr = pushgateway_addr
self.grouping_key = dict(zip(labelnames, labelvalues))
self.grouping_key['instance'] = socket.gethostname()
self.metrics = {
'total_metrics': Gauge('total_metrics', 'Metrics total from vault backup', registry=self.registry)}
self.metrics['total_metrics'].set(1)
def inc(self, metric_name, metric_description, metric_value=1, metric_type='gauge'):
if metric_name not in self.metrics:
if 'gauge' == metric_type:
self.metrics[metric_name] = Gauge(metric_name, metric_description, registry=self.registry)
self.metrics[metric_name].set(metric_value)
self.metrics['total_metrics'].inc()
else:
if isinstance(self.metrics[metric_name], Gauge):
self.metrics[metric_name].inc(metric_value)
def push_metrics(self):
push_to_gateway(self.pushgateway_addr, job='vault_backup_analyzer',
grouping_key=self.grouping_key, registry=self.registry)
def read_in_chunks(file_object, chunk_size=1024):
"""Lazy function (generator) to read a file piece by piece.
Default chunk size: 1k."""
while True:
data = file_object.read(chunk_size)
if not data:
break
yield data
def process_backup(backup_file_name, metrics, auth_backends, secrets_engines):
buffer = ""
with open(backup_file_name, 'r') as backup_file:
for piece in read_in_chunks(backup_file):
metrics, buffer = process_element(metrics, buffer + piece, auth_backends, secrets_engines)
return metrics
def update_metrics(metrics_pool, prefix, name, m_type='objects', value=1, size=0):
def inc_metric(postfix, val):
# <prefix>_<name>_count
# <prefix>_<name>_size
# auth_approle_count
# auth_approle_size
# auth_approle_secret_id_count
# auth_approle_secret_size
# secrets_kv-project_count
# secrets_kv-project_size
# secrets_kv-project_secrets_count
# secrets_kv-project_secrets_size
# secrets_kv-project_versions_count
# secrets_kv-project_versions_size
# audit_devices_count
# audit_devices_size
m_name = '_'.join((prefix, name, postfix))
# Ensure all dashes replased with ground
m_name = m_name.replace('-', '_')
# Ensure all slashes replased with ground
m_name = m_name.replace('/', '_')
metrics_pool.inc(m_name, description, val)
# Total <m_type> count in <name>
# Total size of <m_type> in <name>
# Total versions count in project_1234_secrets
# Total objects count in core
# Total roles count in auth_kubernetes
description = 'Total {} count in {}'.format(m_type, name)
inc_metric('count', value)
description = 'Total size of {} in {}'.format(m_type, name)
inc_metric('size', size)
return metrics_pool
def process_element(prom_metrics, buffer, authbackends, secretsengines):
def search_for_dict():
nonlocal buffer
dict_start = buffer.find('{')
if 0 != dict_start:
buffer = buffer[dict_start:]
dict_start = buffer.find('{')
dict_end = buffer.find('}')
if -1 != dict_end:
found = buffer[dict_start: dict_end + 1]
buffer = buffer[dict_end + 1:]
return json.loads(found)
else:
return None
element = search_for_dict()
while element is not None:
field = ''
value = ''
if 'Key' in element:
field = 'Key'
value = 'Value'
if 'key' in element:
field = 'key'
value = 'value'
if '' == field:
print('No key field found')
exit(1)
path = element[field].split('/')
# objects_total_devices
prom_metrics = update_metrics(prom_metrics, 'objects', 'total', 'objects', value=1,
size=sys.getsizeof(element[value]))
if 'audit' == path[1]:
# audit_devices
prom_metrics = update_metrics(prom_metrics, path[1], 'objects', 'devices', value=1,
size=sys.getsizeof(element[value]))
elif 'core' == path[1]:
# core_objects
prom_metrics = update_metrics(prom_metrics, path[1], 'objects', 'objects', value=1,
size=sys.getsizeof(element[value]))
elif 'auth' == path[1]:
# auth_objects
prom_metrics = update_metrics(prom_metrics, path[1], 'objects', 'objects', value=1,
size=sys.getsizeof(element[value]))
if 'userpass' == authbackends[path[2]]['type']:
# auth_<auth_backend>_objects
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], authbackends[path[2]]['name'])),
'objects', 'objects', value=1, size=sys.getsizeof(element[value]))
if 'user' == path[3]:
# auth_userpass_users
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], authbackends[path[2]]['name'])),
'users', 'users', value=1, size=sys.getsizeof(element[value]))
else:
# auth_userpass_unknown_objects
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], authbackends[path[2]]['name'])),
'unknown_objects', 'unknown objects', value=1,
size=sys.getsizeof(element[value]))
# for debug
print(authbackends[path[2]]['name'], path)
elif 'ldap' == authbackends[path[2]]['type']:
# auth_ldap_objects
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], authbackends[path[2]]['name'])),
'objects', 'objects', value=1, size=sys.getsizeof(element[value]))
if 'user' == path[3]:
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], authbackends[path[2]]['name'])),
path[3], value=1, size=sys.getsizeof(element[value]))
elif 'group' == path[3]:
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], authbackends[path[2]]['name'])),
path[3], value=1, size=sys.getsizeof(element[value]))
elif path[3] in ['config', 'salt']:
# already counted
pass
else:
# auth_ldap_unknown_objects
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], authbackends[path[2]]['name'])),
'unknown_objects', 'unknown objects', value=1,
size=sys.getsizeof(element[value]))
# for debug
print(authbackends[path[2]]['name'], path)
elif 'approle' == authbackends[path[2]]['type']:
# auth_approle_objects
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], authbackends[path[2]]['name'])),
'objects', 'objects', value=1, size=sys.getsizeof(element[value]))
# auth_approle_secret_id_accessors
if 'accessor' == path[3]:
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], authbackends[path[2]]['name'])),
'secret_id_accessors', 'secret-id accessors',
value=1, size=sys.getsizeof(element[value]))
# auth_approle_role_ids
elif 'role_id' == path[3]:
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], authbackends[path[2]]['name'])),
'role_ids', 'role_ids',
value=1, size=sys.getsizeof(element[value]))
# auth_approle_secret_ids
elif 'secret_id' == path[3]:
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], authbackends[path[2]]['name'])),
'secret_ids', 'secret_ids',
value=1, size=sys.getsizeof(element[value]))
# auth_approle_roles
elif 'role' == path[3]:
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], authbackends[path[2]]['name'])),
'approles', 'approles',
value=1, size=sys.getsizeof(element[value]))
elif path[3] in ['config', 'salt']:
# already counted
pass
else:
# auth_approle_unknown_objects
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], authbackends[path[2]]['name'])),
'unknown_objects', 'unknown objects', value=1,
size=sys.getsizeof(element[value]))
# for debug
print(authbackends[path[2]]['name'], path)
# TODO: token can't find if it can be stored in to <auth>
elif 'token' == authbackends[path[2]]['type']:
# auth_token_objects
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], authbackends[path[2]]['name'])),
'objects', 'objects', value=1, size=sys.getsizeof(element[value]))
else:
# auth_unknown_objects
prom_metrics = update_metrics(prom_metrics, path[1], 'unknown_objects', 'unknown objects', value=1,
size=sys.getsizeof(element[value]))
# for debug
print(authbackends[path[2]]['name'], path)
elif 'logical' == path[1]:
# logical_objects
prom_metrics = update_metrics(prom_metrics, path[1], 'objects', 'objects', value=1,
size=sys.getsizeof(element[value]))
if 'cubbyhole' == secretsengines[path[2]]['type']:
# logical_cubbyhole_objects
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], secretsengines[path[2]]['name'])),
'objects', 'objects', value=1, size=sys.getsizeof(element[value]))
elif 'identity' == secretsengines[path[2]]['type']:
# logical_identity_objects
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], secretsengines[path[2]]['name'])),
'objects', 'objects', value=1, size=sys.getsizeof(element[value]))
elif 'kv' == secretsengines[path[2]]['type']:
# KVv2 should have version field with value equal 2. If not set - KVv1
if 'version' in secretsengines[path[2]]['options']:
if '2' == secretsengines[path[2]]['options']['version'] and len(path) > 4:
# logical_<kv_name>_objects
prom_metrics = update_metrics(prom_metrics,
'_'.join((path[1], secretsengines[path[2]]['name'])),
'objects', 'objects', value=1, size=sys.getsizeof(element[value]))
# logical_<kv_name>_archive
if 'archive' == path[4]:
prom_metrics = update_metrics(prom_metrics,
'_'.join((path[1], secretsengines[path[2]]['name'])),
path[4], 'objects',
value=1, size=sys.getsizeof(element[value]))
# logical_<kv_name>_policy
elif 'policy' == path[4]:
prom_metrics = update_metrics(prom_metrics,
'_'.join((path[1], secretsengines[path[2]]['name'])),
path[4], 'objects',
value=1, size=sys.getsizeof(element[value]))
# logical_<kv_name>_secrets
elif 'metadata' == path[4]:
prom_metrics = update_metrics(prom_metrics,
'_'.join((path[1], secretsengines[path[2]]['name'])),
'secrets', 'secrets',
value=1, size=sys.getsizeof(element[value]))
# logical_<kv_name>_versions
elif 'versions' == path[4]:
prom_metrics = update_metrics(prom_metrics,
'_'.join((path[1], secretsengines[path[2]]['name'])),
'versions', 'versions',
value=1, size=sys.getsizeof(element[value]))
elif path[4] in ['config', 'salt', 'upgrading']:
# already counted
pass
else:
# logical_<kv_name>_unknown_objects
prom_metrics = update_metrics(prom_metrics,
'_'.join((path[1], secretsengines[path[2]]['name'])),
'unknown_objects', 'unknown objects', value=1,
size=sys.getsizeof(element[value]))
# for debug
print(secretsengines[path[2]]['name'], 'kvv2', path)
else:
# TODO: duplication
# logical_<kv_name>_secrets
prom_metrics = update_metrics(prom_metrics,
'_'.join((path[1], secretsengines[path[2]]['name'])),
'objects', 'objects', value=1, size=sys.getsizeof(element[value]))
# logical_<kv_name>_secrets
prom_metrics = update_metrics(prom_metrics,
'_'.join((path[1], secretsengines[path[2]]['name'])),
'secrets', 'secrets', value=1, size=sys.getsizeof(element[value]))
else:
# logical_<kv_name>_secrets
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], secretsengines[path[2]]['name'])),
'objects', 'objects', value=1, size=sys.getsizeof(element[value]))
# logical_<kv_name>_secrets
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], secretsengines[path[2]]['name'])),
'secrets', 'secrets', value=1, size=sys.getsizeof(element[value]))
elif 'transit' == secretsengines[path[2]]['type']:
# logical_<transit_name>_objects
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], secretsengines[path[2]]['name'])),
'objects', 'objects', value=1, size=sys.getsizeof(element[value]))
# logical_<transit_name>_archive
if 'archive' == path[3]:
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], secretsengines[path[2]]['name'])),
path[3], 'archives', value=1, size=sys.getsizeof(element[value]))
# logical_<transit_name>_policy
elif 'policy' == path[3]:
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], secretsengines[path[2]]['name'])),
path[3], 'policies', value=1, size=sys.getsizeof(element[value]))
else:
# logical_<transit_name>_unknown_objects
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], secretsengines[path[2]]['name'])),
'unknown_objects', 'unknown objects', value=1,
size=sys.getsizeof(element[value]))
print(secretsengines[path[2]]['name'], path)
else:
# logical_unknown_objects
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], secretsengines[path[2]]['name'])),
'unknown_objects', 'unknown objects', value=1,
size=sys.getsizeof(element[value]))
# for debug
print(secretsengines[path[2]]['name'], path)
elif 'sys' == path[1]:
# sys_objects
prom_metrics = update_metrics(prom_metrics, path[1], 'objects', 'objects', value=1,
size=sys.getsizeof(element[value]))
if 'counters' == path[2]:
# sys_counters_objects
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], path[2])), 'objects', 'objects', value=1,
size=sys.getsizeof(element[value]))
elif 'expire' == path[2]:
# sys_expire_objects
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], path[2])), 'objects', 'objects', value=1,
size=sys.getsizeof(element[value]))
# sys_expire_id
if 'id' == path[3]:
# will not count. Probably unnecessary statistic
if 'auth' == path[4]:
# sys_expire_<path[4]>_<path[5]>_<path[6]>
# sys_expire_auth_<auth_backend>_{login|renew-self}
if 'login' == path[6]:
prom_metrics = update_metrics(prom_metrics,
'_'.join((path[1], path[2], path[4], path[5])), 'logins',
'logins', value=1, size=sys.getsizeof(element[value]))
elif 'renew-self' == path[6]:
prom_metrics = update_metrics(prom_metrics,
'_'.join((path[1], path[2], path[4], path[5])),
'tokens_renewed', 'tokens renewed', value=1,
size=sys.getsizeof(element[value]))
else:
prom_metrics = update_metrics(prom_metrics,
'_'.join((path[1], path[2], path[4])), 'unknown_objects',
'unknown objects', value=1,
size=sys.getsizeof(element[value]))
# for debug
print(path[4], path[6], path)
elif 'sys' == path[4]:
# sys_expire_wrapped_objects
if 'wrap' == path[6]:
prom_metrics = update_metrics(prom_metrics,
'_'.join((path[1], path[2])), 'wrapped_objects',
'_'.join(('wrapped', 'objects')), value=1,
size=sys.getsizeof(element[value]))
elif path[4] in ['config', 'salt', 'upgrading']:
# already counted
pass
else:
# sys_expire_unknown_objects
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], path[2])),
'unknown_objects', 'unknown objects', value=1,
size=sys.getsizeof(element[value]))
# for debug
print(path[2], path)
elif 'policy' == path[2]:
# sys_policy_objects
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], path[2])), 'objects', 'objects', value=1,
size=sys.getsizeof(element[value]))
elif 'token' == path[2]:
# sys_token_objects
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], path[2])), 'objects', 'objects', value=1,
size=sys.getsizeof(element[value]))
# sys_token_accessors
if 'accessor' == path[3]:
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], path[2])), 'accessors', 'accessors',
value=1, size=sys.getsizeof(element[value]))
# sys_token_ids
elif 'id' == path[3]:
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], path[2])), 'ids', 'ids',
value=1, size=sys.getsizeof(element[value]))
elif 'config' == path[2]:
# sys_config_objects
prom_metrics = update_metrics(prom_metrics, '_'.join((path[1], path[2])), 'objects', 'objects', value=1,
size=sys.getsizeof(element[value]))
else:
# sys_unknown_objects
prom_metrics = update_metrics(prom_metrics, path[1], 'unknown_objects', 'unknown objects', value=1,
size=sys.getsizeof(element[value]))
print(path[2], path)
else:
# <unknown_branch>_unknown_objects
prom_metrics = update_metrics(prom_metrics, path[1], 'unknown_objects', 'unknown objects', value=1,
size=sys.getsizeof(element[value]))
print(path[1], path)
element = search_for_dict()
return prom_metrics, buffer
def convert_hvac_dict(response_dict):
processed_dict = {}
for item in response_dict['data'].keys():
processed_dict[response_dict[item]['uuid']] = response_dict[item]
processed_dict[response_dict[item]['uuid']]['name'] = item.strip('/')
del processed_dict[response_dict[item]['uuid']]['uuid']
return processed_dict
if __name__ == "__main__":
# TODO: args should be parsed in separated function
BACKUP_FNAME = sys.argv[1]
# TODO: variability:
# Should script push metrics to pushgateway or
# * start it's own web server
# * just output metrics to log
PUSHGATEWAY_ADDR = sys.argv[2]
LABELS = sys.argv[3]
# TODO: variability:
# WIth query to vault and without
# Use vault agent for get and store token to file
# If we don't want convert UUIDs to human readable values
# Also It is possible that script will read data with unknown IDs
VAULT_ADDR = sys.argv[4]
VAULT_ROLE_ID = sys.argv[5]
VAULT_SECRET_ID = sys.argv[6]
labels = LABELS.split(',')
label_names = []
label_values = []
for label in labels:
label_name, label_value = label.split('=')
label_names.append(label_name)
label_values.append(label_value)
vault_client = hvac.Client(url=VAULT_ADDR, verify=True)
vault_token = vault_client.auth_approle(VAULT_ROLE_ID, VAULT_SECRET_ID)
auth_backends = convert_hvac_dict(vault_client.sys.list_auth_methods())
secrets_engines = convert_hvac_dict(vault_client.sys.list_mounted_secrets_engines())
prom_registry = CollectorRegistry()
metrics = Metrics(registry=prom_registry, pushgateway_addr=PUSHGATEWAY_ADDR, labelnames=label_names,
labelvalues=label_values)
for auth_backend in auth_backends:
metrics = update_metrics(metrics, '_'.join(('auth', auth_backends[auth_backend]['name'])),
'objects', value=0, size=0)
metrics = process_backup(BACKUP_FNAME, metrics, auth_backends, secrets_engines)
metrics.push_metrics()