feat: Sigma rules - #196
Draft
MichaelThamm wants to merge 33 commits into
Draft
MichaelThamm wants to merge 33 commits into
MichaelThamm wants to merge 33 commits into
Conversation
Co-authored-by: Copilot <copilot@github.com>
* feat: log files ignored due to unrecognized suffix in _multi_suffix_glob When rule files are skipped because they don't carry a recognized suffix (.rule, .rules, .yml, .yaml), emit an info-level log so operators can understand why their files are being silently ignored. Closes canonical/cos-configuration-k8s-operator#129 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * bump version * fix: replace deprecated urlopen capath with ssl.SSLContext; bump to 1.9.2 cafile/capath/cadefault params were removed in Python 3.13. Build an SSLContext from the cert path and pass it via the context= param instead. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * test: add unit tests for LokiEmitter._send_request cert handling Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * test: verify _multi_suffix_glob logs files with unrecognized suffixes Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * style: wrap long line in test to satisfy black Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
MichaelThamm
commented
Jun 25, 2026
|
|
||
| Unlike Prometheus/Loki rules, Sigma rules are independent (no grouping concept): | ||
| detection logic lives in a ``detection`` block instead of an ``expr`` field. A rule | ||
| may carry an ``id`` (UUID) for reference, but it is optional and not enforced here as |
Contributor
Author
There was a problem hiding this comment.
Make sure to be careful with UUID since it can cause many relation-changed events. Ask Luca for details.
MichaelThamm
commented
Jun 25, 2026
Contributor
Author
There was a problem hiding this comment.
Revert this change
MichaelThamm
commented
Jun 25, 2026
|
|
||
| # --- Free functions for file I/O, reusable across rule backends --- # | ||
|
|
||
| _RULE_FILE_SUFFIXES = [".rule", ".rules", ".yml", ".yaml"] |
Contributor
Author
There was a problem hiding this comment.
Make sure that this works with Sigma rules. I think this is all-encompasing, but Sigma should filter to only YAML.
4 tasks
Signed-off-by: Michael Thamm <mike.thamm@canonical.com>
MichaelThamm
commented
Jun 26, 2026
| def test_existing_juju_model_uuid_tag_not_overwritten(sigma): | ||
| sigma.add(_rule("Upstream UUID", tags=["juju_model_uuid.deadbeef"])) | ||
| tags = sigma.rules[0]["tags"] | ||
| assert "juju_model_uuid.deadbeef" in tags |
Contributor
Author
There was a problem hiding this comment.
update name deadbeef
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue
We need a Sigma rules feature in cos-lib to unblock these PRs:
Solution
What
Introduces a
SigmaRulesutility and the supporting type definitions so downstream charm libraries (e.g.charmlibs.interfaces.otlp) can aggregate and forward Sigma detection rules with Juju topology, the same wayAlertRules/RecordingRuleshandle PromQL/LogQL.SigmaRules(src/cosl/rules.py)add(rule_dict)— accepts a single Sigma rule or a{"rules": [...]}collection; skips entries missing required fields.add_path(dir_path, *, recursive=False)— aggregates rule files (.rule/.rules/.yml/.yaml) from a file or directory.as_dict()— returns the collection inSigmaRuleFileFormat.tagsasnamespace.value(e.g.juju_model.foo):juju_*tags win),relation-changedchurn),Unlike PromQL/LogQL, Sigma rules have no grouping/
exprconcept, so there is no expression rewriting — onlytagsenrichment.Types (
src/cosl/types.py)TypedDicts:SigmaRuleLogSource,SigmaRuleFormat,SigmaRuleFileFormat.Tests
tests/test_rules_sigma.py(348 lines) covering add/add_path/as_dict, topology injection precedence, deterministic tag ordering, and idempotent re-injection.tests/sigma_rules/(single rules + a collection).Notes
AlertRules/RecordingRulesbehavior.Checklist
project.versionfield in thepyproject.tomlfile.Context
Testing Instructions
Upgrade Notes