Skip to content

Commit

Permalink
Add stdin support for reading policy
Browse files Browse the repository at this point in the history
* 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 duo-labs#163
  • Loading branch information
briandbecker committed Mar 23, 2021
1 parent 3873cf5 commit 2abd4b7
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions parliament/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 2abd4b7

Please sign in to comment.