Skip to content

Add separate file output for converted rules with directory structure preservation#93

Draft
Copilot wants to merge 6 commits into
mainfrom
copilot/create-separate-files-for-converted-rules
Draft

Add separate file output for converted rules with directory structure preservation#93
Copilot wants to merge 6 commits into
mainfrom
copilot/create-separate-files-for-converted-rules

Conversation

Copilot AI commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Converting multiple Sigma rules currently outputs all results to a single file, making version control and selective deployment impractical. This adds support for writing each converted rule to a separate file with configurable naming and directory structure.

Changes

  • New --output-dir parameter: Specifies output directory for individual rule files (mutually exclusive with --output)
  • New --output-filename-template parameter: Controls output filenames using template variables:
    • {path}: Relative source directory path
    • {stem}: Source filename without extension
    • {index}: Query index for rules emitting multiple queries (1, 2, 3...)
  • Per-rule conversion: Rules are now converted individually when using --output-dir, allowing granular output control
  • Automatic directory creation: Output directories are created as needed based on template
  • Correlation rule validation: Added validation to prevent use of --output-dir with correlation rules, which reference other rules and must be converted as part of the full collection. Exits with clear error message directing users to use --output instead.
  • Filter support: Filters are fully supported with --output-dir as they are resolved at collection load time and baked into individual rule detections

Usage

# Flat structure - all converted files in one directory
sigma convert -t esql -p ecs_windows --output-dir translated/ rules/

# Preserve source directory structure
sigma convert -t esql -p ecs_windows \
  --output-dir translated/ \
  --output-filename-template "{path}/{stem}.esql" rules/

# Handle multi-query rules with indexed filenames
sigma convert -t esql -p ecs_windows \
  --output-dir translated/ \
  --output-filename-template "{stem}-{index}.esql" rules/

Given source structure:

rules/
├── windows/rule_1.yml
├── linux/rule_2.yml

Template {path}/{stem}.esql produces:

translated/
├── windows/rule_1.esql
├── linux/rule_2.esql

Limitations

  • Correlation rules are not supported with --output-dir. Correlation rules reference other rules by name and require the full collection context for conversion. Use --output to write correlation rules to a single file.
  • Filters work correctly with --output-dir and are applied to individual rules as expected.

Testing

  • Added comprehensive tests for correlation rule validation
  • Added tests for filter support with --output-dir
  • All existing tests pass

Copilot AI added 3 commits June 15, 2026 21:02
- Add --output-dir parameter to specify output directory
- Add --output-filename-template parameter with template variables
- Support template variables: {path}, {stem}, {index}
- Handle rules that emit multiple queries with index suffixes
- Create directories as needed for output
- Add comprehensive tests for new functionality
- Document --output-dir and --output-filename-template parameters
- Provide examples for flat and nested directory structures
- Explain template variables {path}, {stem}, {index}
- Change short option from -oT to -ot for consistency
- Improve error messages with actionable guidance
- Add context about expected formats in error messages
Copilot AI changed the title [WIP] Add option to create separate files for converted rules Add separate file output for converted rules with directory structure preservation Jun 15, 2026
Copilot AI requested a review from thomaspatzke June 15, 2026 21:07
Comment thread sigma/cli/convert.py
Comment on lines +146 to +151
# Create a single-rule collection for this rule
single_rule_collection = SigmaCollection([rule])

# Convert the rule
try:
result = backend.convert(single_rule_collection, format, correlation_method)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work with Sigma correlations and filters? These refer to other rules in a collection, I doubt this works if a rule collection is split up in this way. Please add tests for these cases and fix if necessary.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've addressed this in commit 3ba5dd1. Here's what I found and fixed:

Filters: ✅ Work correctly with --output-dir. Filters are resolved at collection load time (via resolve_rule_references()), so their conditions are baked into the rule's detection before individual conversion. Added test to verify.

Correlation rules: ❌ Cannot work with --output-dir because they reference other rules by name. When splitting into individual collections, those references would fail with SigmaRuleNotFoundError.

Solution: Added validation that errors out with a clear message if the collection contains correlation rules when using --output-dir. Users must use --output for correlation rules to keep the full collection intact.

Added comprehensive tests for both scenarios.

Copilot AI added 2 commits June 15, 2026 21:24
- Add validation to prevent use of --output-dir with correlation rules
- Correlation rules reference other rules and cannot be split individually
- Add comprehensive tests for correlation rules and filters
- Filters work correctly as they are resolved at collection load time
- Exit with clear error message if correlation rules are detected
- Replace type().__name__ with isinstance(rule, SigmaCorrelationRule)
- More Pythonic and handles inheritance correctly
- Import SigmaCorrelationRule from sigma.correlations
- Fix trailing whitespace in test file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants