Repository navigation
Write a detection rule's Markdown copy next to its HTML page #5057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| // Licensed to Elasticsearch B.V under one or more agreements. | ||
| // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
| // See the LICENSE file in the project root for more information | ||
|
|
||
| using System.IO.Abstractions; | ||
| using AwesomeAssertions; | ||
| using Elastic.Documentation; | ||
| using Elastic.Documentation.Configuration; | ||
| using Elastic.Documentation.Diagnostics; | ||
| using Elastic.Documentation.FileSystems; | ||
| using Elastic.Markdown.Exporters; | ||
| using Elastic.Markdown.IO; | ||
|
|
||
| namespace Elastic.Markdown.Tests.DetectionRules; | ||
|
|
||
| public class DetectionRuleExportTests | ||
| { | ||
| // DetectionRule reads the rule from disk, so the docset is written to a temporary folder. The output | ||
| // folder is outside the checkout, so a write next to it is refused. | ||
| [Test] | ||
| public async Task GenerateAll_RuleOutsideDocsFolder_WritesMarkdownNextToHtml() | ||
| { | ||
| var ctx = TestContext.Current!.Execution.CancellationToken; | ||
| var root = Directory.CreateTempSubdirectory("docs-builder-rules-"); | ||
| try | ||
| { | ||
| var repo = root.CreateSubdirectory("repo"); | ||
| WriteFile( | ||
| repo, | ||
| "docs/docset.yml", | ||
| //language=yaml | ||
| """ | ||
| project: test | ||
| extensions: | ||
| - detection-rules | ||
| toc: | ||
| - file: index.md | ||
| - file: rules.md | ||
| detection_rules: | ||
| - ../rules | ||
| """ | ||
| ); | ||
| WriteFile(repo, "docs/index.md", "# Home"); | ||
| WriteFile(repo, "docs/rules.md", "# Rules"); | ||
| WriteFile( | ||
| repo, | ||
| "rules/linux/kernel_ring_buffer.toml", | ||
| //language=toml | ||
| """ | ||
| [metadata] | ||
| creation_date = "2024/08/01" | ||
| maturity = "production" | ||
|
|
||
| [rule] | ||
| author = ["Elastic"] | ||
| description = "Test rule" | ||
| name = "Kernel Ring Buffer" | ||
| rule_id = "abc-123" | ||
| risk_score = 47 | ||
| severity = "medium" | ||
| type = "query" | ||
| license = "Elastic License v2" | ||
| language = "kuery" | ||
| query = "process.name : evil.exe" | ||
| """ | ||
| ); | ||
| _ = repo.CreateSubdirectory(".git"); | ||
|
|
||
| var fs = new FileSystem(); | ||
| var logger = new TestLoggerFactory(); | ||
| await using var collector = new DiagnosticsCollector([]).StartAsync(ctx); | ||
| var documentationFileSystem = DocumentationFileSystem.Resolve( | ||
| fs.DirectoryInfo.New(Path.Join(repo.FullName, "docs")), | ||
| new DocumentationScopeOptions { Inner = fs, Output = Path.Join(root.FullName, "out", "html") } | ||
| ); | ||
| var context = new BuildContext(collector, documentationFileSystem, TestHelpers.CreateConfigurationContext(fs)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Severity: P3 (Low) The test catches the misplaced Markdown copy, but not the scoped-filesystem failure. With Generated by Libra. React with 👍 or 👎 to give feedback on this comment.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In 8da92bd the checkout is now |
||
| { | ||
| SkipStaticAssetExtraction = true | ||
| }; | ||
| var set = new DocumentationSet(context, logger, new TestCrossLinkResolver()); | ||
| var generator = new DocumentationGenerator(set, logger, markdownExporters: [new LlmMarkdownExporter()]); | ||
|
|
||
| await generator.GenerateAll(ctx); | ||
| await collector.StopAsync(ctx); | ||
|
|
||
| var rulePages = Directory | ||
| .EnumerateFiles(root.FullName, "*", SearchOption.AllDirectories) | ||
| .Where(f => f.Contains("kernel_ring_buffer", StringComparison.Ordinal) && !f.EndsWith(".toml", StringComparison.Ordinal)) | ||
| .Select(f => Path.GetRelativePath(set.OutputDirectory.FullName, f).Replace('\\', '/')); | ||
| rulePages.Should().BeEquivalentTo("rules/linux/kernel_ring_buffer/index.html", "rules/linux/kernel_ring_buffer.md"); | ||
| } | ||
| finally | ||
| { | ||
| root.Delete(recursive: true); | ||
| } | ||
| } | ||
|
|
||
| private static void WriteFile(DirectoryInfo root, string relativePath, string content) | ||
| { | ||
| var path = Path.Join(root.FullName, relativePath); | ||
| _ = Directory.CreateDirectory(Path.GetDirectoryName(path)!); | ||
| File.WriteAllText(path, content); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Severity: P3 (Low)
A rule named
rules/index.tomlcan now overwrite the rule overview’srules.mdexport. The remapped path makes the LLM exporter treat the rule as a directory index and write<output>/rules.md, even though the HTML writer emits<output>/rules/index/index.html. If the docset also has adocs/rules.mdoverview, both exporters write the same Markdown file in parallel. Make the HTML and Markdown writers agree on what counts as a directory index.Generated by Libra. React with 👍 or 👎 to give feedback on this comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True for a rule file named
index.toml, sinceLlmMarkdownExportertreats anyindexpage as a folder index. Before this change that copy went to<output>/../rules.md, so it was misplaced either way.elastic/detection-ruleshas noindex.tomltoday (code search finds none), so I left it out of this PR. I can make the exporter skip the folder-index mapping forDetectionRuleFilehere if you'd like it covered.