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

[FEATURE] Add first version of CLI #90

Merged
merged 22 commits into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
update cli and tests
  • Loading branch information
oscarbc96 committed Jan 2, 2020
commit 81888c1e2a27fa45b664fd4294712c2cc4d29592
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ All notable changes to this project will be documented in this file.
wildcards in KMSKey principals.
- Improved granularity of most rules


## [0.11.3] - 2019-12-17
### Improvements
- `S3CrossAccountTrustRule` now accepts resource level exceptions
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ install:
install-dev: install
pip install -e ".[dev]"

install-docs: install-dev
pip install -e ".[docs]"
jsoucheiron marked this conversation as resolved.
Show resolved Hide resolved

format:
isort --recursive .
black .
Expand Down
21 changes: 16 additions & 5 deletions cfripper/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import logging
import sys
from io import TextIOWrapper
Expand All @@ -16,9 +15,16 @@
from cfripper.rule_processor import RuleProcessor
from cfripper.rules import DEFAULT_RULES

LOGGING_LEVELS = {
"ERROR": logging.ERROR,
"WARNING": logging.WARNING,
"INFO": logging.INFO,
"DEBUG": logging.DEBUG,
}


def setup_logging(level: str) -> None:
logging.basicConfig(level=logging._nameToLevel[level], format="%(message)s")
logging.basicConfig(level=LOGGING_LEVELS[level], format="%(message)s")


def get_cfmodel(template: TextIOWrapper) -> CFModel:
Expand All @@ -44,7 +50,9 @@ def process_template(
if output_format == "json":
formatted_result = result.json()
else:
formatted_result = f"Valid: {result.valid}\n" + "\n".join(fr.reason for fr in result.failed_rules)
formatted_result = f"Valid: {result.valid}"
if not result.valid:
formatted_result += "\n" + "\n".join(fr.reason for fr in result.failed_rules)

if output_folder:
output_file = Path(output_folder) / f"{template.name}.cfripper.results.{output_format}"
Expand All @@ -67,7 +75,10 @@ def process_template(
@click.option(
"--resolve-parameters",
type=click.File("r"),
help="JSON/YML file containing key/value pairs. Where key ",
help=(
"JSON/YML file containing key-value pairs used for resolving CloudFormation files with templated parameters. "
'For example, {"abc": "ABC"} will change all occurrences of {"Ref": "abc"} in the CloudFormation file to "ABC".'
),
)
@click.option(
"--format",
Expand All @@ -85,7 +96,7 @@ def process_template(
@click.option(
"--logging",
"logging_level",
type=click.Choice(logging._nameToLevel.keys(), case_sensitive=True),
type=click.Choice(LOGGING_LEVELS.keys(), case_sensitive=True),
default="INFO",
help="Logging level",
show_default=True,
Expand Down
Empty file added docs/cli.md
Empty file.
2 changes: 2 additions & 0 deletions tests/model/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ def test_result_valid_after_removing_failures():
)
# Result has a blocking failure, so it should be invalid
assert result.valid is False
assert result.reason == "mock_rule: mock_reason"

result.failed_rules = []
# Result has no failures, so it should be valid
assert result.valid is True
assert result.reason == ""


def test_result_addition():
Expand Down