|
| 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