Skip to content

Commit

Permalink
Merge pull request #21 from salesforce/refactor/class-structure-try2
Browse files Browse the repository at this point in the history
Refactored class structure
  • Loading branch information
kmcquade authored Mar 13, 2021
2 parents 7f1b8fa + b3ec545 commit d2ec742
Show file tree
Hide file tree
Showing 24 changed files with 1,998 additions and 640 deletions.
2 changes: 1 addition & 1 deletion azure_guardrails/bin/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.4"
__version__ = "0.0.5"
62 changes: 20 additions & 42 deletions azure_guardrails/command/generate_terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import logging
import click
from click_option_group import optgroup, RequiredMutuallyExclusiveOptionGroup
from azure_guardrails import set_log_level, set_stream_logger
from azure_guardrails import set_log_level
from azure_guardrails.terraform.terraform import TerraformTemplateNoParams, TerraformTemplateWithParams
from azure_guardrails.shared import utils, validate
from azure_guardrails.scrapers.compliance_data import ComplianceCoverage
from azure_guardrails.shared.config import get_default_config, get_config_from_file
from azure_guardrails.guardrails.services import Services, Service
from azure_guardrails.guardrails.services import Services

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -106,13 +106,6 @@
default=False,
help="Do not generate markdown or CSV summary files associated with the Terraform output",
)
# @optgroup.option(
# "--output",
# "-o",
# type=str,
# help="The path to the output directory. Defaults to the current directory.",
# default=os.path.curdir
# )
@click.option(
"-v",
"--verbose",
Expand All @@ -130,7 +123,6 @@ def generate_terraform(
management_group: str,
enforcement_mode: bool,
no_summary: bool,
# output: str,
verbosity: int
):
"""
Expand All @@ -150,52 +142,38 @@ def generate_terraform(
else:
subscription = ""

# if generate_summary:
# if service == "all":
# services = Services(config=config)
# policy_names = services.get_display_names(with_parameters=with_parameters)
# else:
# services = Service(service_name=service, config=config)
# policy_names = services.get_display_names(with_parameters=with_parameters)
# compliance_coverage = ComplianceCoverage(display_names=policy_names)
# markdown_table = compliance_coverage.markdown_table()
# print(markdown_table)
# else:
with_parameters = False
include_empty_defaults = False
summary_file_prefix = ""
if no_params:
include_empty_defaults = False
with_parameters = False
summary_file_prefix = "no-params"
if params_required:
include_empty_defaults = True
with_parameters = True
elif params_required:
summary_file_prefix = "params-required"
if params_optional:
with_parameters = True
include_empty_defaults = False
elif params_optional:
summary_file_prefix = "params-optional"

if service == "all":
services = Services(config=config)
else:
services = Services(service_names=[service], config=config)
if with_parameters:
display_names = services.get_display_names_by_service_with_parameters(
include_empty_defaults=include_empty_defaults)
display_names_list = services.get_display_names(with_parameters=with_parameters)
terraform_template = TerraformTemplateWithParams(parameters=display_names,
subscription_name=subscription,
management_group=management_group,
enforcement_mode=enforcement_mode)
else:
display_names = services.get_display_names_sorted_by_service(with_parameters=with_parameters)
display_names_list = services.get_display_names(with_parameters=with_parameters)

if no_params:
display_names = services.get_display_names_sorted_by_service_no_params()
display_names_list = services.display_names_no_params
terraform_template = TerraformTemplateNoParams(policy_names=display_names,
subscription_name=subscription,
management_group=management_group,
enforcement_mode=enforcement_mode)
else:
display_names = services.get_display_names_sorted_by_service_with_params(params_required=params_required)

if params_required:
display_names_list = services.display_names_params_required
else:
display_names_list = services.display_names_params_optional

terraform_template = TerraformTemplateWithParams(parameters=display_names,
subscription_name=subscription,
management_group=management_group,
enforcement_mode=enforcement_mode)
result = terraform_template.rendered()
print(result)

Expand Down
60 changes: 45 additions & 15 deletions azure_guardrails/command/list_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import json
import yaml
import click
from click_option_group import optgroup, RequiredMutuallyExclusiveOptionGroup
from azure_guardrails import set_log_level
from azure_guardrails.guardrails.services import Services, Service
from azure_guardrails.guardrails.services import Services
from azure_guardrails.shared import utils, validate

logger = logging.getLogger(__name__)
Expand All @@ -22,12 +23,34 @@
help="Services supported by Azure Policy definitions",
callback=validate.click_validate_supported_azure_service,
)
@click.option(
"--with-parameters",
"-p",
@optgroup.group(
"Parameter Options",
cls=RequiredMutuallyExclusiveOptionGroup,
help="",
)
@optgroup.option(
"--all-policies",
is_flag=True,
default=False,
help="Show all policies, regardless of whether or not they have parameters",
)
@optgroup.option(
"--no-params",
is_flag=True,
default=False,
help="Only generate policies that do NOT require parameters",
)
@optgroup.option(
"--params-optional",
is_flag=True,
default=False,
help="Only generate policies where parameters are OPTIONAL",
)
@optgroup.option(
"--params-required",
is_flag=True,
default=False,
help="Include Policies with Parameters",
help="Only generate policies where parameters are REQUIRED",
)
@click.option(
"--format",
Expand All @@ -44,7 +67,7 @@
"verbosity",
count=True,
)
def list_policies(service: str, with_parameters: bool, fmt: str, verbosity: int):
def list_policies(service: str, all_policies: bool, no_params: bool, params_optional: bool, params_required: bool, fmt: str, verbosity: int):
"""
List Azure Policies
"""
Expand All @@ -57,23 +80,30 @@ def list_policies(service: str, with_parameters: bool, fmt: str, verbosity: int)
if verbosity >= 1:
utils.print_grey("Getting policy names according to service\n")
if fmt == "yaml":
print_policies_in_yaml(service=service, with_parameters=with_parameters, verbosity=verbosity)
print_policies_in_yaml(service=service, all_policies=all_policies, no_params=no_params, params_optional=params_optional, params_required=params_required, verbosity=verbosity)
else:
print_policies_in_stdout(service=service, with_parameters=with_parameters, verbosity=verbosity)
print_policies_in_stdout(service=service, all_policies=all_policies, no_params=no_params, params_optional=params_optional, params_required=params_required, verbosity=verbosity)


def get_display_names_sorted_by_service(service: str, with_parameters: bool) -> dict:
def get_display_names_sorted_by_service(service: str, all_policies: bool, no_params: bool, params_optional: bool, params_required: bool) -> dict:
if service == "all":
services = Services()
display_names = services.get_display_names_sorted_by_service(with_parameters=with_parameters)
else:
services = Services(service_names=[service])
display_names = services.get_display_names_sorted_by_service(with_parameters=with_parameters)
display_names = []
if all_policies:
display_names = services.get_all_display_names_sorted_by_service()
elif no_params:
display_names = services.get_display_names_sorted_by_service_no_params()
elif params_optional:
display_names = services.get_display_names_sorted_by_service_with_params(params_required=False)
elif params_required:
display_names = services.get_display_names_sorted_by_service_with_params(params_required=True)
return display_names


def print_policies_in_yaml(service: str, with_parameters: bool, verbosity: int):
display_names = get_display_names_sorted_by_service(service=service, with_parameters=with_parameters)
def print_policies_in_yaml(service: str, all_policies: bool, no_params: bool, params_optional: bool, params_required: bool, verbosity: int):
display_names = get_display_names_sorted_by_service(service=service, all_policies=all_policies, no_params=no_params, params_optional=params_optional, params_required=params_required)
result = yaml.dump(display_names)
total_policies = 0
for service_name in display_names.keys():
Expand All @@ -83,9 +113,9 @@ def print_policies_in_yaml(service: str, with_parameters: bool, verbosity: int):
print(f"total policies: {str(total_policies)}")


def print_policies_in_stdout(service: str, with_parameters: bool, verbosity: int):
def print_policies_in_stdout(service: str, all_policies: bool, no_params: bool, params_optional: bool, params_required: bool, verbosity: int):
# TODO: Figure out if I should just print all of the policies as a list or if they should be indented. If indented, uncomment the commented lines below.
display_names = get_display_names_sorted_by_service(service=service, with_parameters=with_parameters)
display_names = get_display_names_sorted_by_service(service=service, all_policies=all_policies, no_params=no_params, params_optional=params_optional, params_required=params_required)
total_policies = 0
for service_name in display_names.keys():
# print(f"{service_name}:")
Expand Down
Loading

0 comments on commit d2ec742

Please sign in to comment.