Skip to content

Commit

Permalink
new: add new version API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ostefano committed Mar 6, 2025
1 parent 1062a85 commit 03870b8
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 167 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/release-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
jobs:

release:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
steps:

- name: Checkout repository
Expand Down Expand Up @@ -44,14 +44,14 @@ jobs:
token: ${{ secrets.GITHUB_TOKEN }}

docs:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
steps:

- name: Checkout repository
uses: actions/checkout@v4

- name: Install packages
run: sudo apt-get install libpoppler-cpp-dev libzbar0 tesseract-ocr yara
run: sudo apt-get install libpoppler-cpp-dev

- name: Set up Python 3.12
uses: actions/setup-python@v5
Expand All @@ -73,7 +73,7 @@ jobs:
path: site/

deploy-gh-pages:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
needs: docs

permissions:
Expand All @@ -90,13 +90,13 @@ jobs:
uses: actions/deploy-pages@v4

build:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install packages
run: sudo apt-get install libpoppler-cpp-dev libzbar0 tesseract-ocr yara
run: sudo apt-get install libgl1 libpoppler-cpp-dev libpoppler-cpp0v5 libzbar0 tesseract-ocr

- name: Set up Python 3.12
uses: actions/setup-python@v5
Expand All @@ -119,7 +119,7 @@ jobs:
path: dist/

publish-to-pypi:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
needs: build

permissions:
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/test-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ on:
jobs:

docs:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
steps:

- name: Checkout repository
uses: actions/checkout@v4

- name: Install packages
run: sudo apt-get install libpoppler-cpp-dev libzbar0 tesseract-ocr yara
run: sudo apt-get install libpoppler-cpp-dev

- name: Set up Python 3.12
uses: actions/setup-python@v5
Expand All @@ -32,7 +32,7 @@ jobs:
run: make generate_docs

test:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04

strategy:
fail-fast: false
Expand All @@ -45,7 +45,7 @@ jobs:
uses: actions/checkout@v4

- name: Install packages
run: sudo apt-get install libpoppler-cpp-dev libzbar0 tesseract-ocr yara
run: sudo apt-get install libgl1 libpoppler-cpp-dev libpoppler-cpp0v5 libzbar0 tesseract-ocr

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
Expand Down Expand Up @@ -89,7 +89,7 @@ jobs:
path: dist/

publish-to-test-pypi:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
needs: test

permissions:
Expand Down
18 changes: 9 additions & 9 deletions misp_modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import enum
import importlib
import importlib.abc
import importlib.metadata
import importlib.resources
import importlib.util
import pathlib
Expand All @@ -43,6 +44,14 @@ class ModuleType(enum.Enum):
ACTION_MOD = "action_mod"


def get_version() -> str:
"""Return the version."""
try:
return importlib.metadata.version("misp-modules")
except importlib.metadata.PackageNotFoundError:
raise ValueError


def is_valid_module(module: importlib.abc.Traversable) -> bool:
"""Whether the reference is a valid module file."""
if not module.is_file():
Expand All @@ -65,15 +74,6 @@ def is_valid_module_type(module_type: importlib.abc.Traversable) -> bool:
return True


def iterate_helpers(
helpers_dir: typing.Union[importlib.abc.Traversable, pathlib.Path],
) -> typing.Generator[importlib.abc.Traversable, None, None]:
"""Iterate helpers and return helper references."""
for helper in helpers_dir.iterdir():
if is_valid_module(helper):
yield helper


def iterate_modules(
modules_dir: typing.Union[importlib.abc.Traversable, pathlib.Path],
) -> typing.Generator[tuple[importlib.abc.Traversable, importlib.abc.Traversable], None, None]:
Expand Down
40 changes: 18 additions & 22 deletions misp_modules/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,22 @@ def init_logger(debug: bool = False) -> None:
LOGGER.addHandler(handler)


class Healthcheck(tornado.web.RequestHandler):
"""Healthcheck handler."""
class VersionCheck(tornado.web.RequestHandler):
"""VersionCheck handler."""

def get(self):
LOGGER.debug("MISP Healthcheck request")
LOGGER.debug("VersionCheck request")
try:
self.write(orjson.dumps({"version": misp_modules.get_version()}))
except ValueError:
self.send_error(500)


class HealthCheck(tornado.web.RequestHandler):
"""HealthCheck handler."""

def get(self):
LOGGER.debug("Healthcheck request")
self.write(b'{"status": true}')


Expand All @@ -143,7 +154,7 @@ def _build_handlers_data(cls) -> bytes:
)

def get(self):
LOGGER.debug("MISP ListModules request")
LOGGER.debug("ListModules request")
if not self.CACHE:
self.CACHE = self._build_handlers_data()
self.write(self.CACHE)
Expand All @@ -159,7 +170,7 @@ class QueryModule(tornado.web.RequestHandler):

@tornado_concurrent.run_on_executor
def run_request(self, module_name, json_payload, dict_payload):
LOGGER.debug("MISP QueryModule %s request %s", module_name, json_payload)
LOGGER.debug("QueryModule %s request %s", module_name, json_payload)
try:
response = MODULES_HANDLERS[module_name].dict_handler(request=dict_payload)
except AttributeError:
Expand Down Expand Up @@ -211,22 +222,6 @@ def main():
# Load libraries as root modules
misp_modules.promote_lib_to_root()

# Load helpers
for helper in misp_modules.iterate_helpers(
importlib.resources.files(__package__).joinpath(misp_modules.HELPERS_DIR)
):
helper_name = os.path.splitext(helper.name)[0]
absolute_helper_name = ".".join([__package__, misp_modules.HELPERS_DIR, helper_name])
try:
imported_helper = importlib.import_module(absolute_helper_name)
if test_error := imported_helper.selftest():
raise ImportError(test_error)
except ImportError as e:
LOGGER.warning("Helper %s failed: %s", helper_name, e)
continue
HELPERS_HANDLERS[helper_name] = imported_helper
LOGGER.info("Helper %s loaded", helper_name)

# Load modules
for module_type, module in misp_modules.iterate_modules(
importlib.resources.files(__package__).joinpath(misp_modules.MODULES_DIR)
Expand Down Expand Up @@ -262,7 +257,8 @@ def main():
[
(r"/modules", ListModules),
(r"/query", QueryModule),
(r"/healthcheck", Healthcheck),
(r"/healthcheck", HealthCheck),
(r"/version", VersionCheck),
]
),
max_buffer_size=MAX_BUFFER_SIZE,
Expand Down
Empty file removed misp_modules/helpers/__init__.py
Empty file.
99 changes: 0 additions & 99 deletions misp_modules/helpers/cache.py

This file was deleted.

26 changes: 3 additions & 23 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "misp-modules"
version = "3.0.0"
version = "3.0.1"
description = "MISP modules are autonomous modules that can be used for expansion and other services in MISP"
authors = [
{name = "Alexandre Dulaunoy", email = "[email protected]"}
Expand All @@ -20,7 +20,6 @@ dependencies = [
## core dependencies
"orjson",
"psutil",
"redis",
"tornado",
## minimum dependencies
"beautifulsoup4",
Expand Down

0 comments on commit 03870b8

Please sign in to comment.