|
| 1 | +// SPDX-FileCopyrightText: Copyright 2026 Open Information Security Foundation |
| 2 | +// SPDX-License-Identifier: GPL-2.0-only |
| 3 | + |
| 4 | +use std::path::PathBuf; |
| 5 | + |
| 6 | +use clap::Parser; |
| 7 | +use clap::Subcommand; |
| 8 | +use clap::ValueEnum; |
| 9 | + |
| 10 | +#[derive(Parser, Debug)] |
| 11 | +#[command(about = "Utilities for Suricata configuration files")] |
| 12 | +struct Cli { |
| 13 | + #[command(subcommand)] |
| 14 | + command: Command, |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Subcommand, Debug)] |
| 18 | +enum Command { |
| 19 | + /// Read and print a Suricata configuration file. |
| 20 | + Print(PrintArgs), |
| 21 | + |
| 22 | + /// Validate a Suricata configuration file against a JSON schema. |
| 23 | + Validate(ValidateArgs), |
| 24 | + |
| 25 | + /// Print the embedded Suricata YAML JSON schema. |
| 26 | + PrintSchema, |
| 27 | +} |
| 28 | + |
| 29 | +#[derive(Parser, Debug)] |
| 30 | +struct PrintArgs { |
| 31 | + /// Path to the Suricata configuration file. |
| 32 | + path: PathBuf, |
| 33 | + |
| 34 | + /// Output format. |
| 35 | + #[arg(long, value_enum, default_value_t = OutputFormat::Yaml)] |
| 36 | + format: OutputFormat, |
| 37 | +} |
| 38 | + |
| 39 | +#[derive(Parser, Debug)] |
| 40 | +struct ValidateArgs { |
| 41 | + /// Path to the Suricata configuration file. |
| 42 | + path: PathBuf, |
| 43 | + |
| 44 | + /// Path to a JSON schema file. If omitted, the embedded schema is used. |
| 45 | + #[arg(long)] |
| 46 | + schema: Option<PathBuf>, |
| 47 | + |
| 48 | + /// Quiet mode. Print nothing when validation succeeds. |
| 49 | + #[arg(short, long)] |
| 50 | + quiet: bool, |
| 51 | +} |
| 52 | + |
| 53 | +#[derive(Clone, Copy, Debug, ValueEnum)] |
| 54 | +enum OutputFormat { |
| 55 | + Yaml, |
| 56 | + Json, |
| 57 | + Debug, |
| 58 | + Flat, |
| 59 | +} |
| 60 | + |
| 61 | +/// Parse CLI arguments from the process environment and dispatch the selected subcommand. |
| 62 | +pub fn run_from_env() -> Result<(), Box<dyn std::error::Error>> { |
| 63 | + run_from_iter(std::env::args_os()) |
| 64 | +} |
| 65 | + |
| 66 | +/// Parse CLI arguments from any iterator and dispatch the selected subcommand. |
| 67 | +pub fn run_from_iter<I, T>(args: I) -> Result<(), Box<dyn std::error::Error>> |
| 68 | +where |
| 69 | + I: IntoIterator<Item = T>, |
| 70 | + T: Into<std::ffi::OsString> + Clone, |
| 71 | +{ |
| 72 | + let cli = Cli::parse_from(args); |
| 73 | + |
| 74 | + match cli.command { |
| 75 | + Command::Print(args) => print_config(args), |
| 76 | + Command::Validate(args) => validate_config(args), |
| 77 | + Command::PrintSchema => print_schema(), |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// Load a configuration file and print it in the requested format. |
| 82 | +fn print_config(args: PrintArgs) -> Result<(), Box<dyn std::error::Error>> { |
| 83 | + let config = crate::load_file(&args.path)?; |
| 84 | + |
| 85 | + match args.format { |
| 86 | + OutputFormat::Yaml => print!("{}", crate::print_yaml(&config)?), |
| 87 | + OutputFormat::Json => { |
| 88 | + println!( |
| 89 | + "{}", |
| 90 | + serde_json::to_string_pretty(&crate::config_to_json(&config))? |
| 91 | + ); |
| 92 | + } |
| 93 | + OutputFormat::Debug => println!("{config:#?}"), |
| 94 | + OutputFormat::Flat => print!("{}", crate::print_flat_config(&config)), |
| 95 | + } |
| 96 | + |
| 97 | + Ok(()) |
| 98 | +} |
| 99 | + |
| 100 | +// Print the embedded Suricata YAML JSON schema. |
| 101 | +fn print_schema() -> Result<(), Box<dyn std::error::Error>> { |
| 102 | + print!("{}", crate::SURICATA_YAML_SCHEMA); |
| 103 | + Ok(()) |
| 104 | +} |
| 105 | + |
| 106 | +// Load a configuration file, validate it against a schema, and report all issues. |
| 107 | +fn validate_config(args: ValidateArgs) -> Result<(), Box<dyn std::error::Error>> { |
| 108 | + let config = crate::load_file(&args.path)?; |
| 109 | + let instance = crate::config_to_json(&config); |
| 110 | + |
| 111 | + let (schema, schema_label) = if let Some(schema_path) = args.schema { |
| 112 | + let schema_input = std::fs::read_to_string(&schema_path)?; |
| 113 | + let schema: serde_json::Value = serde_json::from_str(&schema_input)?; |
| 114 | + (schema, schema_path.display().to_string()) |
| 115 | + } else { |
| 116 | + (crate::embedded_schema()?, String::from("embedded schema")) |
| 117 | + }; |
| 118 | + |
| 119 | + let errors = crate::validate_json_schema(&instance, &schema); |
| 120 | + if errors.is_empty() { |
| 121 | + if !args.quiet { |
| 122 | + println!("OK: {}", args.path.display()); |
| 123 | + } |
| 124 | + return Ok(()); |
| 125 | + } |
| 126 | + |
| 127 | + eprintln!( |
| 128 | + "Validation failed: {} issue(s) in {} against {}", |
| 129 | + errors.len(), |
| 130 | + args.path.display(), |
| 131 | + schema_label |
| 132 | + ); |
| 133 | + for error in errors { |
| 134 | + eprintln!("{}", error); |
| 135 | + } |
| 136 | + |
| 137 | + Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "schema validation failed").into()) |
| 138 | +} |
| 139 | + |
0 commit comments