|
| 1 | +"""CLI commands for EvidenceForge log generator. |
| 2 | +
|
| 3 | +This module implements the command-line interface using Typer. |
| 4 | +Provides commands for initialization, log generation, and validation. |
| 5 | +""" |
| 6 | + |
| 7 | +import logging |
| 8 | +import sys |
| 9 | +from pathlib import Path |
| 10 | +from typing import Optional |
| 11 | + |
| 12 | +import typer |
| 13 | +from pydantic import ValidationError |
| 14 | +from rich.console import Console |
| 15 | +from rich.logging import RichHandler |
| 16 | + |
| 17 | +from log_generator.generation import GenerationEngine |
| 18 | +from log_generator.models.scenario import Scenario |
| 19 | +from log_generator.utils import load_yaml |
| 20 | + |
| 21 | +# Initialize Typer app and Rich console |
| 22 | +app = typer.Typer( |
| 23 | + name="forge", |
| 24 | + help="EvidenceForge - Generate realistic synthetic security logs for threat hunting training", |
| 25 | + add_completion=False, |
| 26 | +) |
| 27 | +console = Console() |
| 28 | + |
| 29 | +# Exit codes (per TODO.md specification) |
| 30 | +EXIT_SUCCESS = 0 |
| 31 | +EXIT_INPUT_ERROR = 1 |
| 32 | +EXIT_SCHEMA_VALIDATION = 2 |
| 33 | +EXIT_GENERATION_ERROR = 21 |
| 34 | +EXIT_SIGINT = 130 |
| 35 | + |
| 36 | + |
| 37 | +def setup_logging(verbose: bool = False) -> None: |
| 38 | + """Configure logging with Rich handler. |
| 39 | +
|
| 40 | + Args: |
| 41 | + verbose: Enable debug logging if True |
| 42 | + """ |
| 43 | + level = logging.DEBUG if verbose else logging.INFO |
| 44 | + logging.basicConfig( |
| 45 | + level=level, |
| 46 | + format="%(message)s", |
| 47 | + handlers=[RichHandler(console=console, rich_tracebacks=True)] |
| 48 | + ) |
| 49 | + |
| 50 | + |
| 51 | +@app.command() |
| 52 | +def init( |
| 53 | + force: bool = typer.Option( |
| 54 | + False, |
| 55 | + "--force", |
| 56 | + "-f", |
| 57 | + help="Overwrite existing config.yaml if it exists" |
| 58 | + ) |
| 59 | +) -> None: |
| 60 | + """Initialize EvidenceForge by creating config.yaml from template. |
| 61 | +
|
| 62 | + Copies config.example.yaml to config.yaml in the current directory. |
| 63 | + """ |
| 64 | + console.print("[bold blue]EvidenceForge Initialization[/bold blue]") |
| 65 | + |
| 66 | + # Check if config.example.yaml exists |
| 67 | + example_config = Path("config.example.yaml") |
| 68 | + if not example_config.exists(): |
| 69 | + console.print( |
| 70 | + "[bold red]Error:[/bold red] config.example.yaml not found in current directory", |
| 71 | + style="red" |
| 72 | + ) |
| 73 | + console.print( |
| 74 | + "Please run this command from the project root directory or create config.example.yaml" |
| 75 | + ) |
| 76 | + raise typer.Exit(EXIT_INPUT_ERROR) |
| 77 | + |
| 78 | + # Check if config.yaml already exists |
| 79 | + target_config = Path("config.yaml") |
| 80 | + if target_config.exists() and not force: |
| 81 | + console.print( |
| 82 | + "[bold yellow]Warning:[/bold yellow] config.yaml already exists", |
| 83 | + style="yellow" |
| 84 | + ) |
| 85 | + console.print("Use --force to overwrite, or edit config.yaml manually") |
| 86 | + raise typer.Exit(EXIT_SUCCESS) |
| 87 | + |
| 88 | + # Copy config.example.yaml to config.yaml |
| 89 | + try: |
| 90 | + content = example_config.read_text() |
| 91 | + target_config.write_text(content) |
| 92 | + console.print( |
| 93 | + f"[bold green]✓[/bold green] Created config.yaml from {example_config}", |
| 94 | + style="green" |
| 95 | + ) |
| 96 | + console.print("\nNext steps:") |
| 97 | + console.print("1. Edit config.yaml to configure AWS credentials and output settings") |
| 98 | + console.print("2. Run 'forge generate <scenario.yaml>' to generate logs") |
| 99 | + except Exception as e: |
| 100 | + console.print(f"[bold red]Error:[/bold red] Failed to create config.yaml: {e}", style="red") |
| 101 | + raise typer.Exit(EXIT_INPUT_ERROR) |
| 102 | + |
| 103 | + |
| 104 | +@app.command() |
| 105 | +def generate( |
| 106 | + scenario_file: Path = typer.Argument( |
| 107 | + ..., |
| 108 | + help="Path to scenario YAML file", |
| 109 | + exists=True, |
| 110 | + file_okay=True, |
| 111 | + dir_okay=False, |
| 112 | + readable=True, |
| 113 | + ), |
| 114 | + output: Optional[Path] = typer.Option( |
| 115 | + None, |
| 116 | + "--output", |
| 117 | + "-o", |
| 118 | + help="Output directory for generated logs (overrides scenario setting)", |
| 119 | + ), |
| 120 | + config: Optional[Path] = typer.Option( |
| 121 | + None, |
| 122 | + "--config", |
| 123 | + "-c", |
| 124 | + help="Path to configuration file (default: config.yaml)", |
| 125 | + exists=True, |
| 126 | + file_okay=True, |
| 127 | + dir_okay=False, |
| 128 | + readable=True, |
| 129 | + ), |
| 130 | + verbose: bool = typer.Option( |
| 131 | + False, |
| 132 | + "--verbose", |
| 133 | + "-v", |
| 134 | + help="Enable verbose logging" |
| 135 | + ), |
| 136 | +) -> None: |
| 137 | + """Generate synthetic security logs from a scenario file. |
| 138 | +
|
| 139 | + Validates the scenario schema, initializes the generation engine, |
| 140 | + and produces coordinated logs across multiple formats. |
| 141 | +
|
| 142 | + Exit codes: |
| 143 | + - 0: Success |
| 144 | + - 1: Input error (file not found, invalid path) |
| 145 | + - 2: Schema validation error |
| 146 | + - 21: Generation error |
| 147 | + - 130: Interrupted (Ctrl+C) |
| 148 | + """ |
| 149 | + setup_logging(verbose) |
| 150 | + logger = logging.getLogger(__name__) |
| 151 | + |
| 152 | + console.print("[bold blue]EvidenceForge Log Generator[/bold blue]") |
| 153 | + console.print(f"Scenario: {scenario_file}") |
| 154 | + |
| 155 | + # Load and validate scenario |
| 156 | + try: |
| 157 | + console.print("\n[bold]Loading scenario...[/bold]") |
| 158 | + scenario_data = load_yaml(scenario_file) |
| 159 | + scenario = Scenario(**scenario_data) |
| 160 | + console.print(f"[green]✓[/green] Loaded scenario: {scenario.name}") |
| 161 | + console.print(f" Description: {scenario.description}") |
| 162 | + console.print(f" Users: {len(scenario.environment.users)}") |
| 163 | + console.print(f" Systems: {len(scenario.environment.systems)}") |
| 164 | + if scenario.storyline: |
| 165 | + console.print(f" Storyline events: {len(scenario.storyline)}") |
| 166 | + |
| 167 | + except FileNotFoundError: |
| 168 | + console.print( |
| 169 | + f"[bold red]Error:[/bold red] Scenario file not found: {scenario_file}", |
| 170 | + style="red" |
| 171 | + ) |
| 172 | + raise typer.Exit(EXIT_INPUT_ERROR) |
| 173 | + |
| 174 | + except ValidationError as e: |
| 175 | + console.print( |
| 176 | + "[bold red]Error:[/bold red] Schema validation failed", |
| 177 | + style="red" |
| 178 | + ) |
| 179 | + console.print("\nValidation errors:") |
| 180 | + for error in e.errors(): |
| 181 | + field = " -> ".join(str(loc) for loc in error['loc']) |
| 182 | + console.print(f" • {field}: {error['msg']}", style="red") |
| 183 | + raise typer.Exit(EXIT_SCHEMA_VALIDATION) |
| 184 | + |
| 185 | + except Exception as e: |
| 186 | + console.print( |
| 187 | + f"[bold red]Error:[/bold red] Failed to load scenario: {e}", |
| 188 | + style="red" |
| 189 | + ) |
| 190 | + if verbose: |
| 191 | + console.print_exception() |
| 192 | + raise typer.Exit(EXIT_INPUT_ERROR) |
| 193 | + |
| 194 | + # Determine output directory |
| 195 | + if output: |
| 196 | + output_dir = output |
| 197 | + elif scenario.output.destination: |
| 198 | + output_dir = Path(scenario.output.destination) |
| 199 | + else: |
| 200 | + output_dir = Path("./output") |
| 201 | + |
| 202 | + # Create timestamped subdirectory |
| 203 | + from datetime import datetime |
| 204 | + timestamp = datetime.now().strftime("%Y%m%d-%H%M%S") |
| 205 | + output_dir = output_dir / f"{scenario.name}-{timestamp}" |
| 206 | + |
| 207 | + console.print(f"\n[bold]Output directory:[/bold] {output_dir}") |
| 208 | + |
| 209 | + # Generate logs |
| 210 | + try: |
| 211 | + console.print("\n[bold]Starting log generation...[/bold]") |
| 212 | + |
| 213 | + engine = GenerationEngine(scenario=scenario, output_dir=output_dir) |
| 214 | + engine.generate() |
| 215 | + |
| 216 | + console.print("\n[bold green]✓ Generation complete![/bold green]") |
| 217 | + console.print(f"\nGenerated logs:") |
| 218 | + console.print(f" Directory: {output_dir}") |
| 219 | + |
| 220 | + # List generated files |
| 221 | + if output_dir.exists(): |
| 222 | + for file in sorted(output_dir.iterdir()): |
| 223 | + if file.is_file(): |
| 224 | + size = file.stat().st_size |
| 225 | + size_str = f"{size:,} bytes" if size < 1024 else f"{size / 1024:.1f} KB" |
| 226 | + console.print(f" • {file.name} ({size_str})") |
| 227 | + |
| 228 | + # Success - exit normally |
| 229 | + return |
| 230 | + |
| 231 | + except KeyboardInterrupt: |
| 232 | + console.print("\n[bold yellow]Interrupted by user (Ctrl+C)[/bold yellow]") |
| 233 | + logger.info("Generation interrupted by user") |
| 234 | + raise typer.Exit(EXIT_SIGINT) |
| 235 | + |
| 236 | + except Exception as e: |
| 237 | + console.print( |
| 238 | + f"\n[bold red]Error:[/bold red] Generation failed: {e}", |
| 239 | + style="red" |
| 240 | + ) |
| 241 | + if verbose: |
| 242 | + console.print_exception() |
| 243 | + logger.exception("Generation failed") |
| 244 | + raise typer.Exit(EXIT_GENERATION_ERROR) |
| 245 | + |
| 246 | + |
| 247 | +@app.command() |
| 248 | +def version() -> None: |
| 249 | + """Show version information.""" |
| 250 | + console.print("EvidenceForge v0.1.0 (Phase 1 MVP)") |
| 251 | + console.print("Synthetic security log generator for threat hunting training") |
| 252 | + |
| 253 | + |
| 254 | +def main() -> None: |
| 255 | + """Main CLI entry point.""" |
| 256 | + try: |
| 257 | + app() |
| 258 | + except Exception as e: |
| 259 | + console.print(f"[bold red]Fatal error:[/bold red] {e}", style="red") |
| 260 | + sys.exit(EXIT_GENERATION_ERROR) |
| 261 | + |
| 262 | + |
| 263 | +if __name__ == "__main__": |
| 264 | + main() |
0 commit comments