Skip to content

Commit

Permalink
Validate the final environment against the schema before running the …
Browse files Browse the repository at this point in the history
…boefje.
  • Loading branch information
Donnype committed Aug 20, 2024
1 parent 7008d8c commit a224b13
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion boefjes/boefjes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def get_task(task_id, scheduler_client):
def create_boefje_meta(task, local_repository):
boefje = task.data.boefje
boefje_resource = local_repository.by_id(boefje.id)
environment = get_environment_settings(task.data)
environment = get_environment_settings(task.data, boefje_resource.schema)

organization = task.data.organization
input_ooi = task.data.input_ooi
Expand Down
13 changes: 11 additions & 2 deletions boefjes/boefjes/job_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import httpx
import structlog
from httpx import HTTPError
from jsonschema.exceptions import ValidationError
from jsonschema.validators import validate

from boefjes.clients.bytes_client import BytesAPIClient
from boefjes.config import settings
Expand All @@ -15,6 +17,7 @@
from boefjes.local_repository import LocalPluginRepository
from boefjes.plugins.models import _default_mime_types
from boefjes.runtime_interfaces import BoefjeJobRunner, Handler, NormalizerJobRunner
from boefjes.storage.interfaces import SettingsNotConformingToSchema
from octopoes.api.models import Affirmation, Declaration, Observation
from octopoes.connector.octopoes import OctopoesAPIConnector
from octopoes.models import Reference, ScanLevel
Expand All @@ -35,7 +38,7 @@ def get_octopoes_api_connector(org_code: str) -> OctopoesAPIConnector:
return OctopoesAPIConnector(str(settings.octopoes_api), org_code)


def get_environment_settings(boefje_meta: BoefjeMeta) -> dict[str, str]:
def get_environment_settings(boefje_meta: BoefjeMeta, schema: dict | None = None) -> dict[str, str]:
try:
katalogus_api = str(settings.katalogus_api).rstrip("/")
response = httpx.get(
Expand All @@ -58,6 +61,12 @@ def get_environment_settings(boefje_meta: BoefjeMeta) -> dict[str, str]:
for key, value in settings_from_katalogus.items:
new_env[key] = value

if schema is not None:
try:
validate(instance=new_env, schema=schema)
except ValidationError as e:
raise SettingsNotConformingToSchema(boefje_meta.boefje.id, e.message) from e

return new_env


Expand Down Expand Up @@ -99,7 +108,7 @@ def handle(self, boefje_meta: BoefjeMeta) -> None:
boefje_meta.arguments["input"] = ooi.serialize()

boefje_meta.runnable_hash = boefje_resource.runnable_hash
boefje_meta.environment = get_environment_settings(boefje_meta)
boefje_meta.environment = get_environment_settings(boefje_meta, boefje_resource.schema)

mime_types = _default_mime_types(boefje_meta.boefje)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,33 @@
Create Date: 2024-08-20 06:08:20.943924
"""
from alembic import op

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = '870fc302b852'
down_revision = '5be152459a7b'
revision = "870fc302b852"
down_revision = "5be152459a7b"
branch_labels = None
depends_on = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('boefje', 'environment_keys')
op.drop_column('normalizer', 'environment_keys')
op.drop_column("boefje", "environment_keys")
op.drop_column("normalizer", "environment_keys")
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('normalizer', sa.Column('environment_keys', postgresql.ARRAY(sa.VARCHAR(length=128)), autoincrement=False, nullable=False))
op.add_column('boefje', sa.Column('environment_keys', postgresql.ARRAY(sa.VARCHAR(length=128)), autoincrement=False, nullable=False))
op.add_column(
"normalizer",
sa.Column("environment_keys", postgresql.ARRAY(sa.VARCHAR(length=128)), autoincrement=False, nullable=False),
)
op.add_column(
"boefje",
sa.Column("environment_keys", postgresql.ARRAY(sa.VARCHAR(length=128)), autoincrement=False, nullable=False),
)
# ### end Alembic commands ###

0 comments on commit a224b13

Please sign in to comment.