Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add JSON testing data #175

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions redact_fake_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()
1 change: 1 addition & 0 deletions testdata/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!*.json
Loading