Skip to content

Commit

Permalink
Merge branch 'fix/do-not-add-produces-mime-types' of https://github.c…
Browse files Browse the repository at this point in the history
…om/minvws/nl-kat-coordination into fix/do-not-add-produces-mime-types
  • Loading branch information
noamblitz committed Sep 26, 2024
2 parents 23573e2 + 6f9ba71 commit b586c21
Show file tree
Hide file tree
Showing 13 changed files with 315 additions and 86 deletions.
1 change: 1 addition & 0 deletions boefjes/tests/integration/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def test_update_plugins(test_client, organisation):

response = test_client.get(f"/v1/organisations/{organisation.id}/plugins/{boefje.id}")
assert response.json()["description"] == "4"
assert response.json()["scan_level"] == 3
assert response.json()["enabled"] is True
assert response.json()["scan_level"] == 3
assert response.json()["interval"] == 20
Expand Down
8 changes: 8 additions & 0 deletions boefjes/tests/integration/test_sql_repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,17 @@ def test_bare_boefje_storage(plugin_storage):

boefje.description = "4"
boefje.scan_level = 3

assert storage.boefje_by_id(boefje.id).description == "4"
boefje.description = "4"

with plugin_storage as storage:
storage.update_boefje(boefje.id, {"scan_level": 3})

assert storage.boefje_by_id(boefje.id).scan_level == 3

boefje.description = "4"
boefje.scan_level = 3
all_plugins = storage.get_all()
assert all_plugins == [boefje]

Expand Down
12 changes: 12 additions & 0 deletions rocky/katalogus/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,18 @@ def create_plugin(self, plugin: Plugin) -> None:
else:
logger.info("Plugin %s could not be created", plugin.name)

def edit_plugin(self, plugin: Plugin) -> None:
response = self.session.patch(
f"{self.organization_uri}/boefjes/{plugin.id}",
content=plugin.model_dump_json(exclude_none=True),
)
response.raise_for_status()

if response.status_code == codes.CREATED:
logger.info("Plugin %s", plugin.name)
else:
logger.info("Plugin %s could not be created", plugin.name)


def parse_boefje(boefje: dict) -> Boefje:
scan_level = SCAN_LEVEL(boefje["scan_level"])
Expand Down
17 changes: 15 additions & 2 deletions rocky/katalogus/templates/boefje_setup.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<div class="layout-form">
{% if boefje_variant %}
<h1>{% translate "Boefje variant setup" %}</h1>
{% elif edit_boefje_name %}
<h1>{% translate "Edit" %} "{{ edit_boefje_name }}"</h1>
{% else %}
<h1>{% translate "Boefje setup" %}</h1>
{% endif %}
Expand All @@ -25,8 +27,19 @@ <h1>{% translate "Boefje setup" %}</h1>
{% include "partials/form/fieldset.html" with legend=fieldset_legend fields=form %}

<div class="button-container">
<button type="submit">{% translate "Create variant" %}</button>
<button type="button" class="ghost">{% translate "Discard variant" %}</button>
{% if edit_boefje_name %}
<button type="submit">{% translate "Save changes" %}</button>
<a href="{% url "boefje_detail" organization_code=organization.code plugin_id=return_to_plugin_id %}"
class="button ghost">{% translate "Discard changes" %}</a>
{% elif boefje_variant %}
<button type="submit">{% translate "Create variant" %}</button>
<a href="{% url "boefje_detail" organization_code=organization.code plugin_id=return_to_plugin_id %}"
class="button ghost">{% translate "Discard variant" %}</a>
{% else %}
<button type="submit">{% translate "Create new Boefje" %}</button>
<a href="{% url "katalogus" organization_code=organization.code %}"
class="button ghost">{% translate "Discard new Boefje" %}</a>
{% endif %}
</div>
</form>
</div>
Expand Down
13 changes: 13 additions & 0 deletions rocky/katalogus/templates/plugin_container_image.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% load static %}
{% load humanize %}
{% load i18n %}

<div>
Expand Down Expand Up @@ -44,6 +45,7 @@ <h3>{% translate "Variants" %}</h3>
<th scope="col">{% translate "Name" %}</th>
<th scope="col">{% translate "Scan level" %}</th>
<th scope="col">{% translate "Status" %}</th>
<th scope="col">{% translate "Age" %}</th>
<th scope="col"></th>
</tr>
</thead>
Expand All @@ -70,6 +72,9 @@ <h3>{% translate "Variants" %}</h3>
<span class="label tags-color-4-medium">{% translate "Disabled" %}</span>
{% endif %}
</td>
<td>
<span title="{{ variant.created }} UTC">{{ variant.created|naturaltime }}</span>
</td>
<td class="actions">
<button class="expando-button"
data-icon-open-class="icon ti-chevron-down"
Expand All @@ -81,6 +86,8 @@ <h3>{% translate "Variants" %}</h3>
</tr>
<tr class="expando-row">
<td colspan="5">
<h5>{% translate "Boefje ID" %}</h5>
<p>{{ variant.id }}</p>
<h5>{% translate "Creation date" %}</h5>
{% if variant.created %}
<p>{{ variant.created }}</p>
Expand All @@ -94,6 +101,12 @@ <h5>{% translate "Arguments" %}</h5>
{% else %}
<p>{% translate "There are no arguments used for this Boefje variant." %}</p>
{% endif %}
{% if perms.tools.can_add_boefje %}
<div class="horizontal-view toolbar">
<a class="button ghost"
href="{% url "edit_boefje" plugin_id=variant.id organization_code=organization.code %}"><span aria-hidden="true" class="icon ti-edit action-button"></span>{% translate "Edit variant" %}</a>
</div>
{% endif %}
</td>
</tr>
{% endfor %}
Expand Down
7 changes: 6 additions & 1 deletion 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 AddBoefjeVariantView, AddBoefjeView
from katalogus.views.boefje_setup import AddBoefjeVariantView, AddBoefjeView, EditBoefjeView
from katalogus.views.change_clearance_level import ChangeClearanceLevel
from katalogus.views.katalogus import (
AboutPluginsView,
Expand Down Expand Up @@ -63,6 +63,11 @@
BoefjeDetailView.as_view(),
name="boefje_detail",
),
path(
"plugins/boefje/<plugin_id>/edit/",
EditBoefjeView.as_view(),
name="edit_boefje",
),
path(
"plugins/normalizer/<plugin_id>/",
NormalizerDetailView.as_view(),
Expand Down
183 changes: 135 additions & 48 deletions rocky/katalogus/views/boefje_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,15 @@ def form_valid(self, form):
"""If the form is valid, redirect to the supplied URL."""
form_data = form.cleaned_data

plugin_id = str(uuid.uuid4())
return_to_plugin_id = self.kwargs.get("plugin_id", plugin_id)

arguments = [] if form_data["oci_arguments"] == "" else form_data["oci_arguments"].split()
consumes = [] if form_data["consumes"] == "" else form_data["consumes"].strip("[]").replace("'", "").split(", ")
produces = [] if form_data["produces"] == "" else form_data["produces"].split(",")
produces = [p.strip() for p in produces]
input_objects = []

for input_object in consumes:
input_objects.append(type_by_name(input_object))

boefje = Boefje(
id=plugin_id,
name=form_data.get("name"),
created=str(datetime.now()),
description=form_data.get("description"),
enabled=False,
type="boefje",
scan_level=form_data["scan_level"],
consumes=input_objects,
produces=produces,
boefje_schema=form_data["boefje_schema"],
oci_image=form_data.get("oci_image"),
oci_arguments=arguments,
)
plugin = create_boefje_with_form_data(form_data, self.plugin_id, str(datetime.now()))

get_katalogus(self.organization.code).create_plugin(boefje)
get_katalogus(self.organization.code).create_plugin(plugin)
query_params = urlencode({"new_variant": True})

return redirect(
reverse(
"boefje_detail", kwargs={"organization_code": self.organization.code, "plugin_id": return_to_plugin_id}
"boefje_detail",
kwargs={"organization_code": self.organization.code, "plugin_id": self.return_to_plugin_id},
)
+ "?"
+ query_params
Expand All @@ -65,6 +41,12 @@ def form_valid(self, form):
class AddBoefjeView(BoefjeSetupView):
"""View where the user can create a new Boefje"""

def setup(self, request, *args, **kwargs):
super().setup(request, *args, **kwargs)

self.plugin_id = str(uuid.uuid4())
self.return_to_plugin_id = self.plugin_id

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)

Expand All @@ -85,45 +67,150 @@ class AddBoefjeVariantView(BoefjeSetupView):
def setup(self, request, *args, **kwargs):
super().setup(request, *args, **kwargs)

plugin_id = self.kwargs["plugin_id"]
self.plugin_id = str(uuid.uuid4())
self.return_to_plugin_id = self.kwargs.get("plugin_id")
katalogus = get_katalogus(self.organization.code)
self.plugin = katalogus.get_plugin(plugin_id)
consumes = []
self.plugin = katalogus.get_plugin(self.return_to_plugin_id)

for input_object in list(self.plugin.consumes):
consumes.append(input_object.__name__)
def get_initial(self):
initial = super().get_initial()

self.initial = {
"oci_image": self.plugin.oci_image,
"oci_arguments": " ".join(self.plugin.oci_arguments),
"boefje_schema": self.plugin.boefje_schema,
"consumes": consumes,
"produces": ", ".join(self.plugin.produces),
"scan_level": self.plugin.scan_level,
}
consumes = []

def get_form(self, form_class=None) -> BoefjeAddForm:
if form_class is None:
form_class = self.get_form_class()
for input_object in self.plugin.consumes:
consumes.append(input_object.__name__)

form = super().get_form(form_class)
form.fields["oci_image"].disabled = True
initial["oci_image"] = self.plugin.oci_image
initial["oci_arguments"] = " ".join(self.plugin.oci_arguments)
initial["boefje_schema"] = self.plugin.boefje_schema
initial["consumes"] = consumes
initial["produces"] = ", ".join(self.plugin.produces)
initial["scan_level"] = self.plugin.scan_level

return form
return initial

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["boefje_variant"] = True
context["return_to_plugin_id"] = self.return_to_plugin_id

context["breadcrumbs"] = [
{"url": reverse("katalogus", kwargs={"organization_code": self.organization.code}), "text": "KAT-alogus"},
{
"url": reverse(
"boefje_detail",
kwargs={"organization_code": self.organization.code, "plugin_id": self.return_to_plugin_id},
),
"text": "Boefje detail page",
},
{
"url": reverse(
"boefje_variant_setup",
kwargs={"organization_code": self.organization.code, "plugin_id": self.plugin.id},
kwargs={"organization_code": self.organization.code, "plugin_id": self.return_to_plugin_id},
),
"text": "Boefje variant setup",
},
]

return context


class EditBoefjeView(BoefjeSetupView):
"""View where the user can update a Boefje."""

def setup(self, request, *args, **kwargs):
super().setup(request, *args, **kwargs)

plugin_id = self.kwargs.get("plugin_id")
katalogus = get_katalogus(self.organization.code)
self.plugin = katalogus.get_plugin(plugin_id)

def get_initial(self):
initial = super().get_initial()

consumes = []

for input_object in self.plugin.consumes:
consumes.append(input_object.__name__)

initial["name"] = self.plugin.name
initial["description"] = self.plugin.description
initial["oci_image"] = self.plugin.oci_image
initial["oci_arguments"] = " ".join(self.plugin.oci_arguments)
initial["boefje_schema"] = self.plugin.boefje_schema
initial["consumes"] = consumes
initial["produces"] = ", ".join(self.plugin.produces)
initial["scan_level"] = self.plugin.scan_level

return initial

def form_valid(self, form):
"""If the form is valid, redirect to the supplied URL."""
form_data = form.cleaned_data
plugin = create_boefje_with_form_data(form_data, self.kwargs.get("plugin_id"), self.plugin.created)

get_katalogus(self.organization.code).edit_plugin(plugin)

return redirect(
reverse("boefje_detail", kwargs={"organization_code": self.organization.code, "plugin_id": plugin.id})
)

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["edit_boefje_name"] = self.plugin.name
context["return_to_plugin_id"] = self.plugin.id

context["breadcrumbs"] = [
{
"url": reverse("katalogus", kwargs={"organization_code": self.organization.code}),
"text": "KAT-alogus",
},
{
"url": reverse(
"boefje_detail",
kwargs={
"organization_code": self.organization.code,
"plugin_id": self.plugin.id,
},
),
"text": self.plugin.name,
},
{
"url": reverse(
"edit_boefje",
kwargs={
"organization_code": self.organization.code,
"plugin_id": self.plugin.id,
},
),
"text": 'Edit "' + self.plugin.name + '"',
},
]

return context


def create_boefje_with_form_data(form_data, plugin_id: str, created: str):
arguments = [] if form_data["oci_arguments"] == "" else form_data["oci_arguments"].split()
consumes = [] if form_data["consumes"] == "" else form_data["consumes"].strip("[]").replace("'", "").split(", ")
produces = [] if form_data["produces"] == "" else form_data["produces"].split(",")
produces = [p.strip() for p in produces]
input_objects = []

for input_object in consumes:
input_objects.append(type_by_name(input_object))

return Boefje(
id=plugin_id,
name=form_data.get("name"),
created=created,
description=form_data.get("description"),
enabled=False,
type="boefje",
scan_level=form_data["scan_level"],
consumes=input_objects,
produces=produces,
boefje_schema=form_data["boefje_schema"],
oci_image=form_data.get("oci_image"),
oci_arguments=arguments,
)
6 changes: 6 additions & 0 deletions rocky/katalogus/views/plugin_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Any

from account.mixins import OrganizationView
from dateutil import parser
from django.contrib import messages
from django.http import FileResponse
from django.shortcuts import redirect
Expand Down Expand Up @@ -130,6 +131,11 @@ def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["new_variant"] = self.request.GET.get("new_variant")
context["variants"] = get_katalogus(self.organization.code).get_plugins(oci_image=self.plugin.oci_image)

for variant in context["variants"]:
if variant.created:
variant.created = parser.isoparse(variant.created)

context["select_ooi_filter_form"] = SelectOOIFilterForm
if "show_all" in self.request.GET:
context["select_oois_form"] = SelectOOIForm(
Expand Down
Loading

0 comments on commit b586c21

Please sign in to comment.