forked from prometheus-community/smartctl_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redact_fake_json.py
executable file
·65 lines (55 loc) · 1.98 KB
/
redact_fake_json.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
#! /usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0
# Redact potentially sensitive information in smartctl JSON files
# This script does an in-place modification.
import json
import sys
import copy
import os
def main():
for arg in sys.argv[1:]:
print(arg)
redact_one_file(arg)
def redact_one_file(filename):
data = None
tmpname = filename+".new"
with open(filename, "r") as jsonFile:
data = json.load(jsonFile)
newdata = redact_data(data)
with open(tmpname, "w") as jsonFile:
json.dump(newdata, jsonFile, indent="\t", sort_keys=True)
os.rename(tmpname, filename)
def mutate_nested_dict(d, keys, newvalue, if_present=False):
# if_present=True: only mutate if the full key path exists.
if len(keys) == 1:
if not if_present or keys[0] in d:
d[keys[0]] = newvalue
else:
k = keys[0]
if k in d:
mutate_nested_dict(d[k], keys[1:], newvalue, if_present=if_present)
REDACTED_STRING = 'REDACTED'
REDACTED_TIME_T = 1234567890
REDACTED_ASCTIME = "Fri Feb 13 23:31:30 2009 UTC" # TODO: generate from TIME_T, with UTC
REDACTED_HEX16_STR = '0x1234567890abcdef'
REDACTED_UINT32 = 1234567890
REDACT_FIELDS = [
{'k': ['smartctl','platform_info'], 'v': REDACTED_STRING},
{'k': ['smartctl','build_info'], 'v': REDACTED_STRING},
{'k': ['serial_number'], 'v': REDACTED_STRING},
{'k': ['firmware_version'], 'v': REDACTED_STRING},
{'k': ['local_time', 'time_t'], 'v': REDACTED_TIME_T},
{'k': ['local_time', 'asctime'], 'v': REDACTED_ASCTIME},
{'k': ['logical_unit_id'], 'v': REDACTED_HEX16_STR},
{'k': ['wwn','id'], 'v': REDACTED_UINT32},
# TODO: how to redact /dev/sdX /dev/nvmeN ??
]
def redact_data(data):
newdata = copy.deepcopy(data)
for f in REDACT_FIELDS:
#newval = str(f['v'])+str(f['k']) # for debugging
newval = f['v']
mutate_nested_dict(newdata, f['k'], newval, if_present=True)
return newdata
if __name__ == '__main__':
main()