Skip to content

Commit 72d1386

Browse files
authored
Support for checking blanked-out sensitive properties in end2end log (#2119)
Signed-off-by: Andreas Maier <maiera@de.ibm.com>
1 parent bf6957b commit 72d1386

3 files changed

Lines changed: 100 additions & 2 deletions

File tree

Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ help:
300300
@echo " build - Build the distribution files in: $(dist_dir)"
301301
@echo " builddoc - Build documentation in: $(doc_build_dir)"
302302
@echo " all - Do all of the above"
303-
@echo " end2end - Run end2end tests (adds to coverage results)"
303+
@echo " end2end - Run end2end tests (adds to coverage results, checks blanked-out properties in log)"
304304
@echo " end2end_show - Show HMCs defined for end2end tests"
305305
@echo " end2end_check - Check access to all HMCs defined in your HMC inventory file for end2end tests"
306306
@echo " authors - Generate AUTHORS.md file from git log"
@@ -703,8 +703,10 @@ endif
703703

704704
.PHONY: end2end
705705
end2end: $(done_dir)/develop_$(pymn)_$(PACKAGE_LEVEL).done $(package_py_files) $(test_end2end_py_files) $(test_common_py_files) $(coverage_config_file)
706-
bash -c "PYTHONPATH=. TESTEND2END_LOAD=true coverage run --append -m pytest -v -m 'not check_hmcs' $(pytest_general_opts) $(pytest_test_opts) $(test_dir)/end2end"
706+
-$(call RM_FUNC,end2end.log)
707+
bash -c "PYTHONPATH=. TESTLOGFILE=end2end.log TESTEND2END_LOAD=true coverage run --append -m pytest -v -m 'not check_hmcs' $(pytest_general_opts) $(pytest_test_opts) $(test_dir)/end2end"
707708
coverage html
709+
tools/check_blanked.py end2end.log
708710
@echo "Makefile: $@ done."
709711

710712
# TODO: Enable rc checking again once the remaining issues are resolved

changes/noissue.25.feature.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Test: Added support for checking blanked-out sensitive properties in the
2+
zhmcclient log file created during the end2end tests, by adding a new
3+
script tools/check_blanked.py, and running it during "make end2end".

tools/check_blanked.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Check that certain properties in the specified zhmcclient log file have a
5+
blanked-out value.
6+
"""
7+
8+
import sys
9+
import re
10+
import argparse
11+
from zhmcclient import BLANKED_OUT_STRING
12+
13+
14+
# Ends of property names that are checked for being blanked out.
15+
# Keep in sync with BLANKED_OUT_PROPERTY_PATTERN in zhmcclient/_constants.py.
16+
PROPERTY_NAME_ENDS = [
17+
"authentication-code",
18+
"credential",
19+
"key",
20+
"passcode",
21+
"password",
22+
"pw",
23+
"secret",
24+
"session",
25+
"Session"
26+
]
27+
28+
# Pattern for matching a single property name and value
29+
PROPERTY_PATTERN = re.compile(
30+
rf"""(['"])([^'"]*({'|'.join(PROPERTY_NAME_ENDS)}))\1"""
31+
r"""\s*:\s*"""
32+
r"""('(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"|None|null)"""
33+
)
34+
35+
36+
def parse_args():
37+
"""
38+
Parse input arguments
39+
"""
40+
41+
parser = argparse.ArgumentParser(
42+
formatter_class=argparse.RawTextHelpFormatter,
43+
description=f"""
44+
Check that certain properties in the specified zhmcclient log file have a
45+
blanked-out value.
46+
47+
The properties that are checked are those whose names end with:
48+
49+
{'\n '.join(PROPERTY_NAME_ENDS)}
50+
51+
The following syntax forms for the properties in the file are supported:
52+
53+
'name': 'value'
54+
'name': "value"
55+
"name": 'value'
56+
"name": "value"
57+
""")
58+
59+
parser.add_argument(dest="file", metavar='FILE',
60+
help="Path name of the zhmcclient log file to be "
61+
"checked")
62+
63+
return parser.parse_args(sys.argv[1:])
64+
65+
66+
def main():
67+
"""
68+
Main function
69+
"""
70+
args = parse_args()
71+
file = args.file
72+
print(f"Checking blanked properties in file: {file}")
73+
74+
checked_pnames = set()
75+
rc = 0
76+
with open(file, "r", encoding="utf-8") as fp:
77+
for lineno, line in enumerate(fp, start=1):
78+
for match in PROPERTY_PATTERN.finditer(line):
79+
pname = match.group(2)
80+
pvalue = match.group(4).strip('"').strip("'")
81+
checked_pnames.add(pname)
82+
if pvalue != BLANKED_OUT_STRING:
83+
rc = 1
84+
print(f"{file}({lineno}): Found property {pname!r} with "
85+
f"non-blanked value {pvalue!r}")
86+
87+
print("The file contains the following blanked properties: "
88+
f"{', '.join(checked_pnames)}")
89+
sys.exit(rc)
90+
91+
92+
if __name__ == '__main__':
93+
main()

0 commit comments

Comments
 (0)