Skip to content

Commit

Permalink
fix: merge intake_by_uuid and modules_by_uuid plugins into a single o…
Browse files Browse the repository at this point in the history
…n_page_read_source hook (as mkdocs forbids multiple on_page_read_source hooks)
  • Loading branch information
jeromefellus-sekoia committed Nov 15, 2024
1 parent 5ba4b2b commit b047032
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 111 deletions.
3 changes: 1 addition & 2 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -662,8 +662,7 @@ plugins:
xdr/features/collect/integrations/network/watchguard_firebox.md: integration/categories/network_security/watchguard_firebox.md
xdr/features/investigate/dork_language.md: xdr/features/investigate/events_query_language.md
- redoc
- intakes_by_uuid
- modules_by_uuid
- integration_by_uuid
- sass
repo_url: https://github.com/SEKOIA-IO/documentation
site_name: Sekoia.io Documentation
Expand Down
108 changes: 75 additions & 33 deletions plugins/intakes_by_uuid.py → plugins/integration_by_uuid.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import re
import string
from pathlib import Path

import mkdocs
Expand All @@ -8,13 +6,15 @@
from mkdocs.utils.meta import get_data


class IntakesByUUIDPlugin(mkdocs.plugins.BasePlugin):
"""Reading Markdown files that contains an `uuid` metadata to provide
class IntegrationByUUIDPlugin(mkdocs.plugins.BasePlugin):
"""
Reading Markdown files that contains an `uuid` metadata to provide
a redirection.
When such a file is identified, a new
`operation_center/integration_catalog/uuid/$uuid.md` file is faked
which will redirect to it."""
which will redirect to it.
"""

template = """<!doctype html>
<html lang="en">
Expand All @@ -34,6 +34,61 @@ class IntakesByUUIDPlugin(mkdocs.plugins.BasePlugin):
_redirection_table: dict[str, str] = {}
_integrations: list[dict[str, str]] = []

def process_intake_file(
self,
source_file: File,
metadata: dict,
config: Config,
):
new_files = []
dialect_uuids = (uuid.strip() for uuid in metadata["uuid"].split(","))

for dialect_uuid in dialect_uuids:
self._redirection_table[dialect_uuid] = source_file.url
self._integrations.append(
{
"uuid": dialect_uuid,
"name": metadata.get("name"),
"destination": source_file.url,
}
)

newfile = File(
path=f"operation_center/integration_catalog/uuid/{dialect_uuid}.md",
src_dir="operation_center/integration_catalog/uuid",
dest_dir=config["site_dir"],
use_directory_urls=True,
)
new_files.append(newfile)

new_files.append(
File(
path="integration/categories/index.md",
src_dir="operation_center/integration_catalog/",
dest_dir=config["site_dir"],
use_directory_urls=True,
)
)

return new_files

def process_module_file(
self,
source_file: File,
metadata: dict,
config: Config,
):
self._redirection_table[metadata["uuid"]] = source_file.url

return [
File(
path=f"integration/action_library/uuid/{metadata['uuid']}.md",
src_dir="integration/action_library/uuid",
dest_dir=config["site_dir"],
use_directory_urls=True,
)
]

def on_files(self, files: Files, config: Config):
new_files: list[File] = []
source_files = [
Expand All @@ -45,37 +100,18 @@ def on_files(self, files: Files, config: Config):
with filename.open() as f:
_, metadata = get_data(f.read())

if "uuid" not in metadata or metadata.get("type").lower() != "intake":
if "uuid" not in metadata:
continue

dialect_uuids = (uuid.strip() for uuid in metadata["uuid"].split(","))

for dialect_uuid in dialect_uuids:
self._redirection_table[dialect_uuid] = source_file.url
self._integrations.append(
{
"uuid": dialect_uuid,
"name": metadata.get("name"),
"destination": source_file.url,
}
)

newfile = File(
path=f"operation_center/integration_catalog/uuid/{dialect_uuid}.md",
src_dir="operation_center/integration_catalog/uuid",
dest_dir=config["site_dir"],
use_directory_urls=True,
)
new_files.append(newfile)
if metadata.get("type").lower() == "intake":
new_files += self.process_intake_file(source_file, metadata, config)
elif metadata.get(
"type"
).lower() == "playbook" and source_file.url.startswith(
"integration/action_library/"
):
new_files += self.process_module_file(source_file, metadata, config)

new_files.append(
File(
path="integration/categories/index.md",
src_dir="operation_center/integration_catalog/",
dest_dir=config["site_dir"],
use_directory_urls=True,
)
)
for file in new_files:
if file.src_uri in files._src_uris:
files.remove(file)
Expand All @@ -102,3 +138,9 @@ def on_page_read_source(self, page, config):
content += f"- [{page['name']}]({href})\n"

return content

if page.file.src_path.startswith("integration/action_library/uuid/"):
if page.file.name in self._redirection_table:
return self.template.format(
destination=self._redirection_table[page.file.name]
)
74 changes: 0 additions & 74 deletions plugins/modules_by_uuid.py

This file was deleted.

3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ packages = [

[tool.poetry.plugins."mkdocs.plugins"]
redoc = "plugins.redoc:RedocPlugin"
intakes_by_uuid = "plugins.intakes_by_uuid:IntakesByUUIDPlugin"
modules_by_uuid = "plugins.modules_by_uuid:ModulesByUUIDPlugin"
integration_by_uuid = "plugins.integration_by_uuid:IntegrationByUUIDPlugin"
sass = "plugins.sass:CompileSCSSPlugin"

[tool.poetry.dependencies]
Expand Down

0 comments on commit b047032

Please sign in to comment.