Skip to content

Instantly share code, notes, and snippets.

@nyrahul
Last active March 6, 2024 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nyrahul/5f5f060917bae1cab697314cdc1bca6f to your computer and use it in GitHub Desktop.
Save nyrahul/5f5f060917bae1cab697314cdc1bca6f to your computer and use it in GitHub Desktop.
CSPM get tenant status
# exec into any celery pod
# python3 manage.py shell
# Cut paste below code and execute
# kubectl exec -n accuknox-divy deployments/uwsgi -- python3 manage.py generate_report > report.csv
from django.core.management.base import BaseCommand
from django.db import connection
from django.utils import timezone
from django_tenants.utils import schema_context
# Fetch all tenants
from soarcast.playbook.models import (
CloudChoices,
CloudVariableSet,
PlaybookConfiguration,
)
from source.models import Asset
from tenant.models import Client
class Command(BaseCommand):
help = "Cloud Account and Asset Summary for each client"
def handle(self, *args, **options):
start_date_time = timezone.now()
client_list = list(
Client.objects.exclude(name="root").values_list("schema_name", flat=True).order_by("schema_name"),
)
client_count = len(client_list)
summary = {}
exception_summary = {}
cloud_account_summary = {}
total_assets = 0
total_playbooks = 0
total_cloud_accounts = 0
total_aws_accounts = 0
total_gcp_accounts = 0
total_azure_accounts = 0
total_unknown_accounts = 0
for client_name in client_list:
try:
with schema_context(client_name):
cloud_accounts = list(CloudVariableSet.objects.all().values("cloud_type"))
asset_count = Asset.objects.all().count()
playbook_conf_count = PlaybookConfiguration.objects.all().count()
# summary for overall report
summary[client_name] = [len(cloud_accounts), asset_count, playbook_conf_count]
if client_name not in cloud_account_summary:
cloud_account_summary[client_name] = {"aws": 0, "gcp": 0, "azure": 0, "unknown": 0}
for account in cloud_accounts:
if account.get("cloud_type") in (CloudChoices.AWS, CloudChoices.AWS_ASSUME):
cloud_account_summary[client_name]["aws"] += 1
elif account.get("cloud_type") == CloudChoices.AZURE:
cloud_account_summary[client_name]["azure"] += 1
elif account.get("cloud_type") == CloudChoices.GCP:
cloud_account_summary[client_name]["gcp"] += 1
else:
cloud_account_summary[client_name]["unknown"] += 1
total_cloud_accounts += len(cloud_accounts)
total_assets += asset_count
total_playbooks += playbook_conf_count
total_aws_accounts += cloud_account_summary[client_name]["aws"]
total_gcp_accounts += cloud_account_summary[client_name]["gcp"]
total_azure_accounts += cloud_account_summary[client_name]["azure"]
total_unknown_accounts += cloud_account_summary[client_name]["unknown"]
except Exception as ex:
exception_summary[client_name] = str(ex)
print("\n")
print("\033[33;1mREPORT------------------------------------------------------------------------------ \033[m ")
self.stdout.write(self.style.WARNING(f"Total Number of tenants: {client_count}"))
self.stdout.write(
self.style.SUCCESS(
"{:<10} {:<20} {:<20} {:<15} {:<10} {:<50}".format(
"Serial No.",
"Client",
"Cloud Accounts",
"Assets",
"Playbooks",
"Cloud Type Details",
),
),
)
i = 1
for client, details in summary.items():
cloud_account_details = cloud_account_summary.get(client, {})
cloud_type_details = (
f"\033[36;3m GCP \033[m - {cloud_account_details.get('gcp')}, "
f"\033[33;3m AWS \033[m - {cloud_account_details.get('aws')}, "
f"\033[34;3m Azure \033[m - {cloud_account_details.get('azure')}, "
f"\033[35;3m Unknown \033[m - {cloud_account_details.get('unknown')} "
)
print(
"{:<10} {:<20} {:<20} {:<15} {:<10} {:<50}".format(
i,
client,
details[0],
details[1],
details[2],
cloud_type_details,
),
)
i += 1
print("\n")
# exception details
self.stdout.write(self.style.ERROR("Unable to process summary of following tenants"))
self.stdout.write(
self.style.SUCCESS(
"{:<20} {:<20}".format(
"Client",
"Exception",
),
),
)
for client, ex in exception_summary.items():
print("{:<20} {:<20}".format(client, ex))
end_date_time = timezone.now()
print("-------------------------------------")
print("\033[37;0;46m")
print(f"Total Cloud Accounts: {total_cloud_accounts}")
print(f"Total Playbook Configurations: {total_playbooks}")
print(f"Total Assets: {total_assets}")
print(f"Total Aws Accounts: {total_aws_accounts}")
print(f"Total Azure Accounts: {total_azure_accounts}")
print(f"Total GCP Accounts: {total_gcp_accounts}")
print("\033[m \n")
print("-------------------------------------")
print(f"\033[36;3mFinished in {(end_date_time - start_date_time).seconds} seconds \033[m")
Command().handle()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment