From 2abd4b73174746d8484fbe6e9feb4cce31b015e6 Mon Sep 17 00:00:00 2001 From: Brian Date: Tue, 23 Mar 2021 15:49:13 -0400 Subject: [PATCH] Add stdin support for reading policy * Add ability to read from stdin for a policy to be analyzed such as `cat file.json | parliament` * Adds new FileType argument to read from stdin and don't allow both --file and stdin * Implements #163 --- parliament/cli.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/parliament/cli.py b/parliament/cli.py index 4905e82..b01eade 100755 --- a/parliament/cli.py +++ b/parliament/cli.py @@ -138,6 +138,11 @@ def main(): type=str, ) parser.add_argument("--file", help="Provide a policy in a file", type=str) + parser.add_argument('--stdinfile', + help="Provide a policy via stdin instead of --file", + nargs='?', + type=argparse.FileType('r'), + default=sys.stdin) parser.add_argument( "--directory", help="Provide a path to directory with policy files", type=str ) @@ -212,6 +217,10 @@ def main(): if args.minimal and args.json: raise Exception("You cannot choose both minimal and json output") + # If I have some stdin to read it should be my policy so don't allow file input + if not sys.stdin.isatty() and args.file: + parser.error("You cannot pass a file with --file and use stdin together") + # Change the exit status if there are errors exit_status = 0 findings = [] @@ -321,6 +330,17 @@ def main(): config=config, ) findings.extend(policy.findings) + elif not sys.stdin.isatty(): + contents = args.stdinfile.read() + args.stdinfile.close() + policy = analyze_policy_string( + contents, + args.file, + private_auditors_custom_path=args.private_auditors, + include_community_auditors=args.include_community_auditors, + config=config, + ) + findings.extend(policy.findings) elif args.directory: file_paths = find_files( directory=args.directory,