Skip to content

Commit

Permalink
set CAP signature check based on env variable (#831)
Browse files Browse the repository at this point in the history
* set CAP signature check based on env variable

* use str2bool as suggested
  • Loading branch information
maaikelimper authored Jan 7, 2025
1 parent baeea3e commit 5af06bf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
8 changes: 7 additions & 1 deletion docs/source/reference/running/data-pipeline-plugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ A typical BUFR4 plugin workflow definition would be defined as follows:

This plugin takes the incoming XML file, then validates it against the
`CAP v1.2 schema <https://docs.oasis-open.org/emergency/cap/v1.2/CAP-v1.2-os.html>`_
and verifies the digital signature before publishing.
and optionally verifies the digital signature before publishing.

The validation is performed using the `capvalidator <https://github.com/wmo-im/capvalidator>`_
package.
Expand All @@ -135,6 +135,12 @@ A typical CAP message plugin workflow definition would be defined as follows:
- ${WIS2BOX_STORAGE_INCOMING}
file-pattern: '^.*\.xml$'
By default the XML signature validation is set to ``False``. To enable the validation add the following environment variable to your ``wis2box.env`` file:

.. code-block:: bash
CHECK_CAP_SIGNATURE=True
``wis2box.data.universal.UniversalData``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
9 changes: 8 additions & 1 deletion wis2box-management/wis2box/data/cap_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
###############################################################################

from datetime import datetime
import os
import logging

from pathlib import Path
from typing import Union

from capvalidator import validate_cap_message, get_dates

from wis2box.data.base import BaseAbstractData
from wis2box.util import str2bool

LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -74,8 +77,12 @@ def transform(self, input_data: Union[Path, bytes],
# add relative filepath to _meta
_meta['relative_filepath'] = self.get_local_filepath(_meta['data_date']) # noqa

# check CAP signature based on ENV variable, default is False
check_cap_signature = str2bool(os.getenv('CHECK_CAP_SIGNATURE', False))

LOGGER.info(f'Checking CAP signature: {check_cap_signature}')
# validate the CAP XML string content using the capvalidator package
result = validate_cap_message(input_bytes, strict=False)
result = validate_cap_message(input_bytes, strict=check_cap_signature)
if not result.passed:
LOGGER.error(
f'Invalid CAP XML, not publishing. Reason: {result.message}')
Expand Down
20 changes: 20 additions & 0 deletions wis2box-management/wis2box/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@
LOGGER = logging.getLogger(__name__)


def str2bool(value: Union[bool, str]) -> bool:
"""
helper function to return Python boolean
type (source: https://stackoverflow.com/a/715468)
:param value: value to be evaluated
:returns: `bool` of whether the value is boolean-ish
"""

value2 = False

if isinstance(value, bool):
value2 = value
else:
value2 = value.lower() in ('yes', 'true', 't', '1', 'on')

return value2


def get_typed_value(value) -> Union[float, int, str]:
"""
Derive true type from data value
Expand Down

0 comments on commit 5af06bf

Please sign in to comment.