From b04703249a976d124cfcea604529b8173bae61bb Mon Sep 17 00:00:00 2001 From: Jerome Fellus Date: Fri, 15 Nov 2024 12:22:57 +0100 Subject: [PATCH] fix: merge intake_by_uuid and modules_by_uuid plugins into a single on_page_read_source hook (as mkdocs forbids multiple on_page_read_source hooks) --- mkdocs.yml | 3 +- ...akes_by_uuid.py => integration_by_uuid.py} | 108 ++++++++++++------ plugins/modules_by_uuid.py | 74 ------------ pyproject.toml | 3 +- 4 files changed, 77 insertions(+), 111 deletions(-) rename plugins/{intakes_by_uuid.py => integration_by_uuid.py} (53%) delete mode 100644 plugins/modules_by_uuid.py diff --git a/mkdocs.yml b/mkdocs.yml index 176458da5..5f2510ae9 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -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 diff --git a/plugins/intakes_by_uuid.py b/plugins/integration_by_uuid.py similarity index 53% rename from plugins/intakes_by_uuid.py rename to plugins/integration_by_uuid.py index e49ec1b6a..2e62affe4 100644 --- a/plugins/intakes_by_uuid.py +++ b/plugins/integration_by_uuid.py @@ -1,5 +1,3 @@ -import re -import string from pathlib import Path import mkdocs @@ -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 = """ @@ -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 = [ @@ -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) @@ -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] + ) diff --git a/plugins/modules_by_uuid.py b/plugins/modules_by_uuid.py deleted file mode 100644 index 4adb78d2c..000000000 --- a/plugins/modules_by_uuid.py +++ /dev/null @@ -1,74 +0,0 @@ -import re -import string -from pathlib import Path - -import mkdocs -from mkdocs.config import Config -from mkdocs.structure.files import File, Files -from mkdocs.utils.meta import get_data - - -class ModulesByUUIDPlugin(mkdocs.plugins.BasePlugin): - """Reading Markdown files that contains an `uuid` metadata to provide - a redirection. - - When such a file is identified, a new - `integration/action_library/uuid//uuid/$uuid.md` file is faked - which will redirect to it.""" - - template = """ - - - - Redirecting... - - - - - - -Redirecting... - -""" - - _redirection_table: dict[str, str] = {} - - def on_files(self, files: Files, config: Config): - source_files = [ - source_file for source_file in files - if source_file.src_path.endswith(".md") - ] - - for source_file in source_files: - filename = Path(config["docs_dir"]) / Path(source_file.src_path) - try: - with filename.open() as f: - _, metadata = get_data(f.read()) - except: - # File may have been generated by an other plugin - continue - - if ( - "uuid" not in metadata - or metadata.get("type").lower() != "playbook" - or not source_file.url.startswith("integration/action_library/") - ): - continue - - self._redirection_table[metadata["uuid"]] = source_file.url - - files.append( - 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_page_read_source(self, page, config): - 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] - ) diff --git a/pyproject.toml b/pyproject.toml index a20354821..e38a02af2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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]