From b6be1a9d5eb7f2d8ba514764b9a54a3fe81570e3 Mon Sep 17 00:00:00 2001 From: pedrooot Date: Wed, 22 May 2024 11:46:05 +0200 Subject: [PATCH 1/8] first cli approach WIP --- cli/cli.py | 43 +++++++++++++++++++++++++++++++++++++++++++ poetry.lock | 4 ++-- pyproject.toml | 1 + 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 cli/cli.py diff --git a/cli/cli.py b/cli/cli.py new file mode 100644 index 000000000000..5d0e1e47e783 --- /dev/null +++ b/cli/cli.py @@ -0,0 +1,43 @@ +import typer +from typing_extensions import Annotated + +app = typer.Typer() + + +@app.command() +def create(username: str): + print(f"Creating user: {username}") + + +@app.command() +def delete( + username: str, + force: Annotated[ + bool, typer.Option(prompt="Are you sure you want to delete the user?") + ], +): + if force: + print(f"Deleting user: {username}") + else: + print("Operation cancelled") + + +@app.command() +def delete_all( + force: Annotated[ + bool, typer.Option(prompt="Are you sure you want to delete ALL users?") + ], +): + if force: + print("Deleting all users") + else: + print("Operation cancelled") + + +@app.command() +def init(): + print("Initializing user database") + + +if __name__ == "__main__": + app() diff --git a/poetry.lock b/poetry.lock index 8d8076a3fc8e..ac4743665fc7 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.8.2 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand. [[package]] name = "about-time" @@ -4906,4 +4906,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "2.0" python-versions = ">=3.9,<3.13" -content-hash = "822e30948dea713ea0f380d94b911f8493199388765eeef2e7a7af613343677e" +content-hash = "5085e3d938203aaff3bab3e296b1fe86fb2a108763566379de587aedb232ed7b" diff --git a/pyproject.toml b/pyproject.toml index e1cb84d2be8a..f2bc21826b0b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -68,6 +68,7 @@ schema = "0.7.7" shodan = "1.31.0" slack-sdk = "3.27.1" tabulate = "0.9.0" +typer = "^0.12.3" tzlocal = "5.2" [tool.poetry.group.dev.dependencies] From db17ca64c6061824d300552862ddc21bbecef6c2 Mon Sep 17 00:00:00 2001 From: pedrooot Date: Thu, 23 May 2024 10:16:56 +0200 Subject: [PATCH 2/8] chore(cli): start working on CLI --- cli/cli.py | 53 +++++++++++++++++++++----------------------------- poetry.lock | 2 +- pyproject.toml | 4 +++- 3 files changed, 26 insertions(+), 33 deletions(-) diff --git a/cli/cli.py b/cli/cli.py index 5d0e1e47e783..8cb04a29c2c6 100644 --- a/cli/cli.py +++ b/cli/cli.py @@ -1,42 +1,33 @@ +import time + import typer -from typing_extensions import Annotated +from rich.progress import track + +from prowler.lib.banner import print_banner +from prowler.lib.check.check import list_services, print_services app = typer.Typer() +listers = typer.Typer() +app.add_typer(listers, name="listers") -@app.command() -def create(username: str): - print(f"Creating user: {username}") +@listers.command( + "list-services", help="List all the services that are supported by the tool." +) +def listServices(provider: str = "aws"): + print_services(list_services(provider)) -@app.command() -def delete( - username: str, - force: Annotated[ - bool, typer.Option(prompt="Are you sure you want to delete the user?") - ], -): - if force: - print(f"Deleting user: {username}") +@app.command("banner", help="Prints the banner of the tool.") +def banner(show: bool = True): + total = 0 + for value in track(range(100), description="Processing..."): + time.sleep(0.01) + total += 1 + if show: + print_banner(show) else: - print("Operation cancelled") - - -@app.command() -def delete_all( - force: Annotated[ - bool, typer.Option(prompt="Are you sure you want to delete ALL users?") - ], -): - if force: - print("Deleting all users") - else: - print("Operation cancelled") - - -@app.command() -def init(): - print("Initializing user database") + print("Banner is not shown.") if __name__ == "__main__": diff --git a/poetry.lock b/poetry.lock index ac4743665fc7..4fc5a7285372 100644 --- a/poetry.lock +++ b/poetry.lock @@ -4906,4 +4906,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "2.0" python-versions = ">=3.9,<3.13" -content-hash = "5085e3d938203aaff3bab3e296b1fe86fb2a108763566379de587aedb232ed7b" +content-hash = "c2ad23ce67ae97f7385e5601406d8e7534d26fd1e19cb1052218ce72422e2f73" diff --git a/pyproject.toml b/pyproject.toml index f2bc21826b0b..8453e56d6bf5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -68,9 +68,11 @@ schema = "0.7.7" shodan = "1.31.0" slack-sdk = "3.27.1" tabulate = "0.9.0" -typer = "^0.12.3" tzlocal = "5.2" +[tool.poetry.group.cli.dependencies] +typer = "^0.12.3" + [tool.poetry.group.dev.dependencies] bandit = "1.7.8" black = "24.4.2" From d0576cacc5460cc6c2149f24ed71e73757bfe0c7 Mon Sep 17 00:00:00 2001 From: pedrooot Date: Thu, 23 May 2024 10:18:56 +0200 Subject: [PATCH 3/8] modify pyproject to make cli optional --- poetry.lock | 2 +- pyproject.toml | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/poetry.lock b/poetry.lock index 4fc5a7285372..b9075e545a73 100644 --- a/poetry.lock +++ b/poetry.lock @@ -4906,4 +4906,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "2.0" python-versions = ">=3.9,<3.13" -content-hash = "c2ad23ce67ae97f7385e5601406d8e7534d26fd1e19cb1052218ce72422e2f73" +content-hash = "626bd859d1f3adbf3ebddfea73ad7ef9e28878b4fbff3f34aaf3da8856f5f7a6" diff --git a/pyproject.toml b/pyproject.toml index 8453e56d6bf5..33bf211e37b1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -70,6 +70,9 @@ slack-sdk = "3.27.1" tabulate = "0.9.0" tzlocal = "5.2" +[tool.poetry.group.cli] +optional = true + [tool.poetry.group.cli.dependencies] typer = "^0.12.3" From 1893a7fb2e864b9f9caefa293392ecd23e01e170 Mon Sep 17 00:00:00 2001 From: pedrooot Date: Thu, 23 May 2024 10:26:41 +0200 Subject: [PATCH 4/8] resolve comments --- cli/__init__.py | 0 cli/cli.py | 7 ------- poetry.lock | 3 +-- pyproject.toml | 2 +- 4 files changed, 2 insertions(+), 10 deletions(-) create mode 100644 cli/__init__.py diff --git a/cli/__init__.py b/cli/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/cli/cli.py b/cli/cli.py index 8cb04a29c2c6..128ce8e8384d 100644 --- a/cli/cli.py +++ b/cli/cli.py @@ -1,7 +1,4 @@ -import time - import typer -from rich.progress import track from prowler.lib.banner import print_banner from prowler.lib.check.check import list_services, print_services @@ -20,10 +17,6 @@ def listServices(provider: str = "aws"): @app.command("banner", help="Prints the banner of the tool.") def banner(show: bool = True): - total = 0 - for value in track(range(100), description="Processing..."): - time.sleep(0.01) - total += 1 if show: print_banner(show) else: diff --git a/poetry.lock b/poetry.lock index 86cd42733368..7a9b2d47592a 100644 --- a/poetry.lock +++ b/poetry.lock @@ -4906,5 +4906,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "2.0" python-versions = ">=3.9,<3.13" - -content-hash = "c16653be42dc00bf447aa564768a27c5836d245d8d7a71a1915fcddc94e5302b" +content-hash = "38e13b603fb9841f0e2b8a913648af3b263268b2600787826526faea578fe1da" diff --git a/pyproject.toml b/pyproject.toml index 7d9ab21a90cc..7c36f809defe 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -74,7 +74,7 @@ tzlocal = "5.2" optional = true [tool.poetry.group.cli.dependencies] -typer = "^0.12.3" +typer = "0.12.3" [tool.poetry.group.dev.dependencies] bandit = "1.7.8" From c8d09f262f5de4fbac4a53679629ad676530748a Mon Sep 17 00:00:00 2001 From: pedrooot Date: Fri, 24 May 2024 09:50:40 +0200 Subject: [PATCH 5/8] update poetry lock --- poetry.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/poetry.lock b/poetry.lock index bb41492fa836..58cffec88b90 100644 --- a/poetry.lock +++ b/poetry.lock @@ -4906,4 +4906,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "2.0" python-versions = ">=3.9,<3.13" -content-hash = "38e13b603fb9841f0e2b8a913648af3b263268b2600787826526faea578fe1da" +content-hash = "0ba1a37f3f5fbb2cecb16c6429fa9ed75c861ab7caeb10ee57f11eb3c5e546eb" From 093cb7e5a398ce540f2004590f51a502d31e8373 Mon Sep 17 00:00:00 2001 From: pedrooot Date: Fri, 24 May 2024 10:47:59 +0200 Subject: [PATCH 6/8] feat(cli): add service listing commands --- cli/cli.md | 6 ++++++ cli/cli.py | 39 +++++++++++++++++++++++++++++++++------ 2 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 cli/cli.md diff --git a/cli/cli.md b/cli/cli.md new file mode 100644 index 000000000000..f62f22808a6b --- /dev/null +++ b/cli/cli.md @@ -0,0 +1,6 @@ +# CLI +To show the banner, use: +`python cli/cli.py banner` +## Listing +List services by provider. +`python cli/cli.py list-services` diff --git a/cli/cli.py b/cli/cli.py index 128ce8e8384d..34185f695e7b 100644 --- a/cli/cli.py +++ b/cli/cli.py @@ -4,15 +4,42 @@ from prowler.lib.check.check import list_services, print_services app = typer.Typer() -listers = typer.Typer() -app.add_typer(listers, name="listers") +aws = typer.Typer() +azure = typer.Typer() +gcp = typer.Typer() +kubernetes = typer.Typer() +app.add_typer(aws, name="aws") +app.add_typer(azure, name="azure") +app.add_typer(gcp, name="gcp") +app.add_typer(kubernetes, name="kubernetes") + + +@aws.command( + "list-services", help="List the AWS services that are supported by Prowler." +) +def list_services_aws(): + print_services(list_services("aws")) + + +@azure.command( + "list-services", help="List the Azure services that are supported by Prowler." +) +def list_services_azure(): + print_services(list_services("azure")) + + +@gcp.command( + "list-services", help="List the GCP services that are supported by Prowler." +) +def list_services_gcp(): + print_services(list_services("gcp")) -@listers.command( - "list-services", help="List all the services that are supported by the tool." +@kubernetes.command( + "list-services", help="List the Kubernetes services that are supported by Prowler." ) -def listServices(provider: str = "aws"): - print_services(list_services(provider)) +def list_services_kubernetes(): + print_services(list_services("kubernetes")) @app.command("banner", help="Prints the banner of the tool.") From 1df94e0ad8fdbe46212de1a0729107d6d5a09363 Mon Sep 17 00:00:00 2001 From: pedrooot Date: Fri, 24 May 2024 11:46:26 +0200 Subject: [PATCH 7/8] resolve comments --- cli/cli.py | 51 +++++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/cli/cli.py b/cli/cli.py index 34185f695e7b..af9384d7466f 100644 --- a/cli/cli.py +++ b/cli/cli.py @@ -1,45 +1,52 @@ import typer from prowler.lib.banner import print_banner -from prowler.lib.check.check import list_services, print_services +from prowler.lib.check.check import ( + list_fixers, + list_services, + print_fixers, + print_services, +) app = typer.Typer() aws = typer.Typer() azure = typer.Typer() gcp = typer.Typer() kubernetes = typer.Typer() + app.add_typer(aws, name="aws") app.add_typer(azure, name="azure") app.add_typer(gcp, name="gcp") app.add_typer(kubernetes, name="kubernetes") -@aws.command( - "list-services", help="List the AWS services that are supported by Prowler." -) -def list_services_aws(): - print_services(list_services("aws")) +def list_resources(provider: str, resource_type: str): + if resource_type == "services": + print_services(list_services(provider)) + elif resource_type == "fixers": + print_fixers(list_fixers(provider)) -@azure.command( - "list-services", help="List the Azure services that are supported by Prowler." -) -def list_services_azure(): - print_services(list_services("azure")) +def create_list_commands(provider_typer: typer.Typer, provider_name: str): + @provider_typer.command( + "list-services", + help=f"List the {provider_name} services that are supported by Prowler.", + ) + def list_services_command(): + list_resources(provider_name, "services") - -@gcp.command( - "list-services", help="List the GCP services that are supported by Prowler." -) -def list_services_gcp(): - print_services(list_services("gcp")) + @provider_typer.command( + "list-fixers", + help=f"List the {provider_name} fixers that are supported by Prowler.", + ) + def list_fixers_command(): + list_resources(provider_name, "fixers") -@kubernetes.command( - "list-services", help="List the Kubernetes services that are supported by Prowler." -) -def list_services_kubernetes(): - print_services(list_services("kubernetes")) +create_list_commands(aws, "aws") +create_list_commands(azure, "azure") +create_list_commands(gcp, "gcp") +create_list_commands(kubernetes, "kubernetes") @app.command("banner", help="Prints the banner of the tool.") From 492e6829d9b4a86c25031288818ffcdd2bf4a9ce Mon Sep 17 00:00:00 2001 From: pedrooot Date: Fri, 24 May 2024 12:05:47 +0200 Subject: [PATCH 8/8] resolve comments --- cli/cli.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/cli/cli.py b/cli/cli.py index af9384d7466f..9e3b8b7d0a42 100644 --- a/cli/cli.py +++ b/cli/cli.py @@ -9,10 +9,10 @@ ) app = typer.Typer() -aws = typer.Typer() -azure = typer.Typer() -gcp = typer.Typer() -kubernetes = typer.Typer() +aws = typer.Typer(name="aws") +azure = typer.Typer(name="azure") +gcp = typer.Typer(name="gcp") +kubernetes = typer.Typer(name="kubernetes") app.add_typer(aws, name="aws") app.add_typer(azure, name="azure") @@ -27,7 +27,9 @@ def list_resources(provider: str, resource_type: str): print_fixers(list_fixers(provider)) -def create_list_commands(provider_typer: typer.Typer, provider_name: str): +def create_list_commands(provider_typer: typer.Typer): + provider_name = provider_typer.info.name + @provider_typer.command( "list-services", help=f"List the {provider_name} services that are supported by Prowler.", @@ -43,10 +45,10 @@ def list_fixers_command(): list_resources(provider_name, "fixers") -create_list_commands(aws, "aws") -create_list_commands(azure, "azure") -create_list_commands(gcp, "gcp") -create_list_commands(kubernetes, "kubernetes") +create_list_commands(aws) +create_list_commands(azure) +create_list_commands(gcp) +create_list_commands(kubernetes) @app.command("banner", help="Prints the banner of the tool.")