Skip to content

Commit

Permalink
Merge branch 'main' into feature/update-boefje-documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
underdarknl committed Sep 18, 2024
2 parents 879e01d + 61ef453 commit 65a0964
Show file tree
Hide file tree
Showing 22 changed files with 332 additions and 115 deletions.
3 changes: 2 additions & 1 deletion boefjes/boefjes/dependencies/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,5 +224,6 @@ def get_plugins_filter_parameters(
ids: list[str] | None = Query(None),
plugin_type: Literal["boefje", "normalizer", "bit"] | None = None,
state: bool | None = None,
oci_image: str | None = None,
) -> FilterParameters:
return FilterParameters(q=q, ids=ids, type=plugin_type, state=state)
return FilterParameters(q=q, ids=ids, type=plugin_type, state=state, oci_image=oci_image)
4 changes: 4 additions & 0 deletions boefjes/boefjes/katalogus/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ def list_plugins(
if filter_params.state is not None:
plugins = filter(lambda x: x.enabled is filter_params.state, plugins)

# filter plugins by oci_image
if filter_params.oci_image is not None:
plugins = filter(lambda x: x.type == "boefje" and x.oci_image == filter_params.oci_image, plugins)

# filter plugins by scan level for boefje plugins
plugins = list(filter(lambda x: x.type != "boefje" or x.scan_level >= filter_params.scan_level, plugins))

Expand Down
1 change: 1 addition & 0 deletions boefjes/boefjes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,4 @@ class FilterParameters(BaseModel):
ids: list[str] | None = None
state: bool | None = None
scan_level: int = 0
oci_image: str | None = None
2 changes: 1 addition & 1 deletion boefjes/boefjes/sql/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __exit__(self, exc_type: type[Exception], exc_value: str, exc_traceback: str
error = None

try:
logger.info("Committing session")
logger.debug("Committing session")
self.session.commit()
except DatabaseError as e:
error = e
Expand Down
16 changes: 16 additions & 0 deletions boefjes/tests/integration/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ def test_filter_plugins(test_client, organisation):
response = test_client.get(f"/v1/organisations/{organisation.id}/plugins?limit=10")
assert len(response.json()) == 10

response = test_client.get(
f"/v1/organisations/{organisation.id}/plugins", params={"oci_image": "ghcr.io/minvws/openkat/nmap:latest"}
)
assert {x["id"] for x in response.json()} == {"nmap", "nmap-udp"} # Nmap TCP and UDP

boefje = Boefje(
id="test_plugin", name="My test boefje", static=False, oci_image="ghcr.io/minvws/openkat/nmap:latest"
)
response = test_client.post(f"/v1/organisations/{organisation.id}/plugins", content=boefje.json())
assert response.status_code == 201

response = test_client.get(
f"/v1/organisations/{organisation.id}/plugins", params={"oci_image": "ghcr.io/minvws/openkat/nmap:latest"}
)
assert {x["id"] for x in response.json()} == {"nmap", "nmap-udp", "test_plugin"} # Nmap TCP and UDP


def test_cannot_add_plugin_reserved_id(test_client, organisation):
boefje = Boefje(id="dns-records", name="My test boefje", static=False)
Expand Down
1 change: 1 addition & 0 deletions octopoes/octopoes/repositories/origin_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def serialize(cls, origin: Origin) -> dict[str, Any]:
data = origin.model_dump()
data[cls.pk_prefix] = origin.id
data["type"] = origin.__class__.__name__
data["result"] = list(dict.fromkeys(data["result"]))
return data

@classmethod
Expand Down
28 changes: 27 additions & 1 deletion octopoes/tests/integration/test_io.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import json
import os
import time
import uuid
from datetime import datetime
from operator import itemgetter

import pytest

from octopoes.api.models import Declaration
from octopoes.api.models import Declaration, Observation
from octopoes.connector.octopoes import OctopoesAPIConnector
from octopoes.models.ooi.network import Network

Expand Down Expand Up @@ -78,3 +79,28 @@ def test_io(octopoes_api_connector: OctopoesAPIConnector, valid_time: datetime):
time.sleep(3)

assert len(list(map(itemgetter("txOps"), octopoes_api_connector.export_all()))) > len(transactions)


def test_duplicate_origin_result_filter(octopoes_api_connector: OctopoesAPIConnector, valid_time: datetime):
network1 = Network(name="1")
network2 = Network(name="2")
octopoes_api_connector.save_observation(
Observation(
method="normalizer_id",
source=network1.reference,
source_method=None,
task_id=uuid.uuid4(),
valid_time=valid_time,
result=[
network1,
network1,
network2,
network2,
],
)
)
origin = octopoes_api_connector.list_origins(task_id={}, valid_time=valid_time)
assert len(origin) == 1
assert len(origin[0].result) == 2
assert origin[0].result[0] == network1.reference
assert origin[0].result[1] == network2.reference
3 changes: 2 additions & 1 deletion rocky/katalogus/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ def parse_boefje(boefje: dict) -> Boefje:
return Boefje(
id=boefje["id"],
name=boefje.get("name") or boefje["id"],
description=boefje["description"],
created=boefje.get("created"),
description=boefje.get("description"),
enabled=boefje["enabled"],
type=boefje["type"],
scan_level=scan_level,
Expand Down
6 changes: 5 additions & 1 deletion rocky/katalogus/templates/boefje_setup.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
<main id="main-content">
<section>
<div class="layout-form">
<h1>{% translate "Boefje setup" %}</h1>
{% if boefje_variant %}
<h1>{% translate "Boefje variant setup" %}</h1>
{% else %}
<h1>{% translate "Boefje setup" %}</h1>
{% endif %}
<p>
{% blocktranslate %}
You can create a new Boefje. If you want more information on this,
Expand Down
58 changes: 44 additions & 14 deletions rocky/katalogus/templates/plugin_container_image.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,22 @@ <h3>{% translate "Variants" %}</h3>
{% endblocktranslate %}
</p>
</div>
{% if variants %}
{% if perms.tools.can_add_boefje %}
<div class="horizontal-view toolbar">
<a class="button ghost" href=""><span class="icon ti-plus"></span>{% translate "Add variant" %}</a>
<a class="button ghost"
href="{% url "boefje_variant_setup" plugin_id=plugin.id organization_code=organization.code %}"><span aria-hidden="true" class="icon ti-plus"></span>{% translate "Add variant" %}</a>
</div>
{% endif %}
</div>
{% if variants %}
{% if new_variant %}
<p class="confirmation" aria-label="{% translate "confirmation" %}">
<span>{% blocktranslate %}Variant {{ plugin.name }} created.{% endblocktranslate%}</span>
{% blocktranslate trimmed %}
The Boefje variant is successfully created and can now be used.
{% endblocktranslate %}
</p>
{% endif %}
<div class="horizontal-scroll">
<ul class="accordion">
<li>
Expand All @@ -34,18 +43,33 @@ <h3>{% translate "Variants" %}</h3>
<tr>
<th scope="col">{% translate "Name" %}</th>
<th scope="col">{% translate "Scan level" %}</th>
<th scope="col">{% translate "Published by" %}</th>
<th scope="col">{% translate "Status" %}</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
{% for variant in variants %}
<tr>
<td>{{ variant.name }}name</td>
<td>{{ variant.scan_level }}scan_level</td>
<td>{{ variant.published_by }}published_by</td>
<td>{{ variant.status }}status</td>
<td>
<a href="{% url "boefje_detail" organization_code=organization.code plugin_id=variant.id %}">
{% if variant.id == plugin.id %}
{{ variant.name }} ({% translate "current" %})
{% else %}
{{ variant.name }}
{% endif %}
</a>
</td>
<td>
{% include "partials/scan_level_indicator.html" with value=variant.scan_level %}

</td>
<td>
{% if variant.enabled %}
<span class="label tags-color-2-medium">{% translate "Enabled" %}</span>
{% else %}
<span class="label tags-color-4-medium">{% translate "Disabled" %}</span>
{% endif %}
</td>
<td class="actions">
<button class="expando-button"
data-icon-open-class="icon ti-chevron-down"
Expand All @@ -57,11 +81,19 @@ <h3>{% translate "Variants" %}</h3>
</tr>
<tr class="expando-row">
<td colspan="5">
<h5>{% translate "Creation date" %}</h5>
{% if variant.created %}
<p>{{ variant.created }}</p>
{% else %}
<p>-</p>
{% endif %}
<h5>{% translate "Arguments" %}</h5>
<p>{% translate "The following arguments are used for this Boefje variant." %}</p>
<div>
<p>Some code example</p>
</div>
{% if variant.oci_arguments %}
<p>{% translate "The following arguments are used for this Boefje variant:" %}</p>
<p class="explanation" aria-label="{% translate "explanation" %}">{{ variant.oci_arguments|join:" " }}</p>
{% else %}
<p>{% translate "There are no arguments used for this Boefje variant." %}</p>
{% endif %}
</td>
</tr>
{% endfor %}
Expand All @@ -72,9 +104,7 @@ <h5>{% translate "Arguments" %}</h5>
</ul>
</div>
{% else %}
<p class="explanation"
role="group"
aria-label="{% translate "no variants explanation" %}">
<p class="explanation" aria-label="{% translate "explanation" %}">
<span>{% translate "This Boefje has no variants yet." %}</span>
{% blocktranslate trimmed %}
You can make a variant and change the arguments and JSON Schema
Expand Down
10 changes: 1 addition & 9 deletions rocky/katalogus/templates/plugin_settings_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ <h2>{% translate "Settings" %}</h2>
</div>
<div class="horizontal-view toolbar">
<a class="button ghost"
href="{% url 'plugin_settings_add' organization_code=organization.code plugin_type=plugin.type plugin_id=plugin.id %}"><span class="icon ti-settings"></span>{% translate "Edit Settings" %}</a>
href="{% url 'plugin_settings_add' organization_code=organization.code plugin_type=plugin.type plugin_id=plugin.id %}"><span class="icon ti-settings"></span>{% translate "Configure Settings" %}</a>
</div>
</div>
<div class="horizontal-scroll">
Expand All @@ -25,7 +25,6 @@ <h2>{% translate "Settings" %}</h2>
<tr>
<th scope="col">{% translate "Variable" %}</th>
<th scope="col">{% translate "Value" %}</th>
<th scope="col">{% translate "Required" %}</th>
</tr>
</thead>
<tbody>
Expand All @@ -41,13 +40,6 @@ <h2>{% translate "Settings" %}</h2>
{{ setting.value }}
{% endif %}
</td>
<td>
{% if setting.required %}
{% translate "Yes" %}
{% else %}
{% translate "No" %}
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
Expand Down
9 changes: 7 additions & 2 deletions rocky/katalogus/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.urls import path, re_path

from katalogus.views.boefje_setup import BoefjeSetupView
from katalogus.views.boefje_setup import AddBoefjeVariantView, AddBoefjeView
from katalogus.views.change_clearance_level import ChangeClearanceLevel
from katalogus.views.katalogus import (
AboutPluginsView,
Expand Down Expand Up @@ -34,9 +34,14 @@
),
path(
"plugins/boefjes/add/",
BoefjeSetupView.as_view(),
AddBoefjeView.as_view(),
name="boefje_setup",
),
path(
"plugins/boefjes/add-variant/<plugin_id>/",
AddBoefjeVariantView.as_view(),
name="boefje_variant_setup",
),
re_path(
r"^plugins/boefjes/(?P<view_type>(grid|table))/$",
BoefjeListView.as_view(),
Expand Down
Loading

0 comments on commit 65a0964

Please sign in to comment.