Skip to content

Commit

Permalink
Add init command to create the config file
Browse files Browse the repository at this point in the history
  • Loading branch information
ab-smith committed Sep 28, 2024
1 parent 93d1d32 commit ece5ad3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
1 change: 1 addition & 0 deletions cli/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.tmp.yaml
config.yaml
63 changes: 45 additions & 18 deletions cli/clica.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,44 @@
USERNAME = ""
PASSWORD = ""

with open("config.yaml", "r") as yfile:
cli_cfg = yaml.safe_load(yfile)

@click.group()
def cli():
"""CLICA is the CLI tool to interact with CISO Assistant REST API."""
pass


@click.command()
def init():
"""Create/Reset the config file"""
template_data = {
"rest": {"url": "http://localhost:8000/api"},
"credentials": {"username": "[email protected]", "password": ""},
}
if click.confirm(
"This will create a config.yaml file for you to fill and will RESET any exisiting one. Do you wish to continue?"
):
with open("config.yaml", "w") as yfile:
yaml.safe_dump(
template_data, yfile, default_flow_style=False, sort_keys=False
)


try:
with open("config.yaml", "r") as yfile:
cli_cfg = yaml.safe_load(yfile)
except FileNotFoundError:
print(
"Config file not found. Running the init command to create it but you need to fill it."
)
init()

try:
API_URL = cli_cfg["rest"]["url"]
except KeyError:
print("Missing API URL. Check the yaml file")
print(
"Missing API URL. Check that the config.yaml file is properly set or trigger init command to create a new one."
)
sys.exit(1)

try:
Expand All @@ -48,17 +79,11 @@ def check_auth():
TOKEN = check_auth()


@click.group()
def cli():
"""CLICA is the CLI tool to interact with CISO Assistant REST API."""
pass


@click.command()
@click.option("--email", required=False)
@click.option("--password", required=False)
def auth(email, password):
"""Authenticate to get a temp token. Pass the email and password or set them on the config file"""
"""Authenticate to get a temp token (config file or params). Pass the email and password or set them on the config file"""
url = f"{API_URL}/iam/login/"
if email and password:
data = {"username": email, "password": password}
Expand Down Expand Up @@ -92,21 +117,22 @@ def _get_folders():
for folder in output["results"]:
if folder["content_type"] == "GLOBAL":
GLOBAL_FOLDER_ID = folder["id"]
return GLOBAL_FOLDER_ID
return GLOBAL_FOLDER_ID, output.get("results")


@click.command()
def get_folders():
"""Get folders"""
GLOBAL_FOLDER_ID = _get_folders()
GLOBAL_FOLDER_ID, res = _get_folders()
print("GLOBAL_FOLDER_ID: ", GLOBAL_FOLDER_ID)
print(res)


@click.command()
@click.option("--file", required=True, help="Path of the csv file with assets")
def import_assets(file):
"""import assets from a csv"""
GLOBAL_FOLDER_ID = _get_folders()
"""import assets from a csv. Check the samples for format."""
GLOBAL_FOLDER_ID, _ = _get_folders()
df = pd.read_csv(file)
url = f"{API_URL}/assets/"
headers = {
Expand Down Expand Up @@ -139,9 +165,9 @@ def import_assets(file):
"--file", required=True, help="Path of the csv file with applied controls"
)
def import_controls(file):
"""import applied controls"""
"""import applied controls. Check the samples for format."""
df = pd.read_csv(file)
GLOBAL_FOLDER_ID = _get_folders()
GLOBAL_FOLDER_ID, _ = _get_folders()
url = f"{API_URL}/applied-controls/"
headers = {
"Authorization": f"Token {TOKEN}",
Expand Down Expand Up @@ -173,9 +199,9 @@ def import_controls(file):
"--file", required=True, help="Path of the csv file with the list of evidences"
)
def evidences_templates(file):
"""Create evidences templates"""
"""Create evidences templates. Check the samples for format."""
df = pd.read_csv(file)
GLOBAL_FOLDER_ID = _get_folders()
GLOBAL_FOLDER_ID, _ = _get_folders()

url = f"{API_URL}/evidences/"
headers = {
Expand Down Expand Up @@ -204,6 +230,7 @@ def evidences_templates(file):
cli.add_command(import_assets)
cli.add_command(import_controls)
cli.add_command(evidences_templates)
cli.add_command(init)

if __name__ == "__main__":
cli()
5 changes: 0 additions & 5 deletions cli/config.yaml

This file was deleted.

0 comments on commit ece5ad3

Please sign in to comment.