Created
December 3, 2024 19:10
-
-
Save bryaneaton/301ea871a857971eb28502517a8f5da1 to your computer and use it in GitHub Desktop.
Asset - Issue Test Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
from rich.console import Console | |
from rich.table import Table | |
from rich.panel import Panel | |
from rich.text import Text | |
from regscale.models.regscale_models.asset import Asset | |
from regscale.models.regscale_models.issue import Issue | |
def parse_arguments(): | |
"""Parse command line arguments.""" | |
parser = argparse.ArgumentParser( | |
description='Generate an asset and issue report for a specific SSP ID' | |
) | |
parser.add_argument( | |
'--ssp-id', | |
type=int, | |
required=True, | |
help='The Security Plan ID to generate the report for' | |
) | |
return parser.parse_args() | |
def format_date(date_str): | |
"""Format date string for better readability.""" | |
return date_str if date_str else "Not Set" | |
def create_issue_table(asset_issues): | |
"""Create a formatted table for issues.""" | |
table = Table(show_header=True, header_style="bold cyan") | |
table.add_column("Issue Title", style="green") | |
table.add_column("First Detected", style="yellow") | |
table.add_column("Due Date", style="red") | |
for issue in asset_issues: | |
table.add_row( | |
issue.title, | |
format_date(issue.dateFirstDetected), | |
format_date(issue.dueDate) | |
) | |
return table | |
def main(): | |
args = parse_arguments() | |
console = Console() | |
issues = [] | |
# Create a header | |
console.print(f"\n[bold blue]Asset and Issue Report for SSP ID: {args.ssp_id}[/bold blue]\n") | |
try: | |
assets = Asset.get_all_by_parent(args.ssp_id, 'securityplans') | |
if not assets: | |
console.print(f"[yellow]No assets found for SSP ID: {args.ssp_id}[/yellow]") | |
return | |
for asset in assets: | |
asset_issues = Issue.get_all_by_parent(asset.id, 'assets') | |
issues.extend(asset_issues) | |
# Create asset panel with issue count | |
asset_text = Text() | |
asset_text.append("Asset: ", style="bold white") | |
asset_text.append(asset.name, style="bold green") | |
asset_text.append(f"\nTotal Issues: {len(asset_issues)}", style="bold yellow") | |
console.print(Panel( | |
asset_text, | |
title="Asset Information", | |
border_style="blue" | |
)) | |
# Print issue table if there are issues | |
if asset_issues: | |
console.print(create_issue_table(asset_issues)) | |
else: | |
console.print("[italic]No issues found for this asset[/italic]") | |
console.print("\n") # Add spacing between assets | |
# Print summary | |
console.print(f"[bold green]Report Summary:[/bold green]") | |
console.print(f"Total Assets: {len(assets)}") | |
console.print(f"Total Issues: {len(issues)}\n") | |
except Exception as e: | |
console.print(f"[bold red]Error:[/bold red] {str(e)}") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment