Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error on clone settings without organization selected #997

Merged
merged 8 commits into from
May 22, 2023
2 changes: 1 addition & 1 deletion rocky/katalogus/templates/clone_settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ <h2>{% translate "Clone settings" %}</h2>
This includes both the KAT-alogus settings as well as enabled and disabled plugins.
{% endblocktranslate %}
</p>
<form action="{% url "katalogus_clone_settings" organization.code %}"
<form action="{% url "katalogus_settings" organization.code %}"
method="post"
class="horizontal-view help"
novalidate>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is novalidate hier nuttig op? Die skipped namelijk ook de required fields daarmee.

Expand Down
4 changes: 2 additions & 2 deletions rocky/katalogus/templates/katalogus_settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<section>
<div>
<h1>{% translate "KAT-alogus Settings" %}</h1>
{% if not object_list %}
{% if not settings %}
<p class="emphasized">
{% translate "There are currently no settings defined. Add settings at plugin detail page." %}
</p>
Expand All @@ -28,7 +28,7 @@ <h1>{% translate "KAT-alogus Settings" %}</h1>
</tr>
</thead>
<tbody>
{% for setting in object_list %}
{% for setting in settings %}
<tr>
<td>
<a href="{% url "plugin_detail" organization_code=organization.code plugin_type=plugin_type plugin_id=setting.plugin_id %}">{{ setting.plugin_name }}</a>
Expand Down
7 changes: 1 addition & 6 deletions rocky/katalogus/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,9 @@
path("view/<view>/", views.KATalogusView.as_view(), name="katalogus"),
path(
"settings/",
views.KATalogusSettingsListView.as_view(),
views.KATalogusSettingsView.as_view(),
name="katalogus_settings",
),
path(
"settings/migrate/",
views.KATalogusSettingsListView.as_view(),
name="katalogus_clone_settings",
),
path(
"settings/migrate/confirmation/<to_organization>/",
views.ConfirmCloneSettingsView.as_view(),
Expand Down
33 changes: 19 additions & 14 deletions rocky/katalogus/views/katalogus_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.urls import reverse
from django.urls.base import reverse_lazy
from django.utils.translation import gettext_lazy as _
from django.views.generic import FormView, ListView, TemplateView
from django.views.generic import FormView, TemplateView
from django_otp.decorators import otp_required
from requests import RequestException
from tools.models import Organization
Expand Down Expand Up @@ -51,11 +51,10 @@ def post(self, request, *args, **kwargs):


@class_view_decorator(otp_required)
class KATalogusSettingsListView(OrganizationPermissionRequiredMixin, OrganizationView, FormView, ListView):
class KATalogusSettingsView(OrganizationPermissionRequiredMixin, OrganizationView, FormView):
"""View that gives an overview of all plugins settings"""

template_name = "katalogus_settings.html"
paginate_by = 10
permission_required = "tools.can_scan_organization"
plugin_type = "boefjes"

Expand All @@ -72,15 +71,15 @@ def get_context_data(self, **kwargs):
},
]
context["plugin_type"] = self.plugin_type
context["settings"] = self.get_settings()

return context

def get_queryset(self):
def get_settings(self):
all_plugins_settings = []
katalogus_client = get_katalogus(self.organization.code)
boefjes = katalogus_client.get_boefjes()
for boefje in boefjes:
plugin_settings = {}

for boefje in katalogus_client.get_boefjes():
try:
plugin_setting = katalogus_client.get_plugin_settings(boefje.id)
except RequestException:
Expand All @@ -89,13 +88,19 @@ def get_queryset(self):
)
continue

if plugin_setting:
plugin_settings["plugin_id"] = boefje.id
plugin_settings["plugin_name"] = boefje.name
for key, value in plugin_setting.items():
plugin_settings["name"] = key
plugin_settings["value"] = value
all_plugins_settings.append(plugin_settings)
if not plugin_setting:
continue

for key, value in plugin_setting.items():
all_plugins_settings.append(
{
"plugin_id": boefje.id,
"plugin_name": boefje.name,
"name": key,
"value": value,
}
)

return all_plugins_settings

def get_form(self, form_class=None):
Expand Down
10 changes: 5 additions & 5 deletions rocky/tests/katalogus/test_katalogus.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from katalogus.client import KATalogusClientV1, parse_plugin
from katalogus.views import ConfirmCloneSettingsView, KATalogusSettingsListView, KATalogusView
from katalogus.views import ConfirmCloneSettingsView, KATalogusSettingsView, KATalogusView
from pytest_django.asserts import assertContains, assertNotContains

from rocky.health import ServiceHealth
Expand Down Expand Up @@ -37,15 +37,15 @@ def test_katalogus_plugin_listing(admin_member, redteam_member, client_member, r
assertNotContains(response_client, "test_binary_edge_normalizer")


def test_katalogus_settings_list_one_organization(redteam_member, rf, mocker):
def test_katalogus_settings_one_organization(redteam_member, rf, mocker):
# Mock katalogus calls: return right boefjes and settings
mock_katalogus = mocker.patch("katalogus.client.KATalogusClientV1")
boefjes_data = get_boefjes_data()
mock_katalogus().get_boefjes.return_value = [parse_plugin(b) for b in boefjes_data if b["type"] == "boefje"]
mock_katalogus().get_plugin_settings.return_value = {"BINARYEDGE_API": "test"}
mock_katalogus().get_plugin_settings.return_value = {"BINARYEDGE_API": "test", "Second": "value"}

request = setup_request(rf.get("katalogus_settings"), redteam_member.user)
response = KATalogusSettingsListView.as_view()(request, organization_code=redteam_member.organization.code)
response = KATalogusSettingsView.as_view()(request, organization_code=redteam_member.organization.code)
assert response.status_code == 200

assertContains(response, "KAT-alogus Settings")
Expand All @@ -68,7 +68,7 @@ def test_katalogus_settings_list_multiple_organization(redteam_member, organizat
create_member(redteam_member.user, organization_b)

request = setup_request(rf.get("katalogus_settings"), redteam_member.user)
response = KATalogusSettingsListView.as_view()(request, organization_code=redteam_member.organization.code)
response = KATalogusSettingsView.as_view()(request, organization_code=redteam_member.organization.code)
assert response.status_code == 200

assertContains(response, "KAT-alogus Settings")
Expand Down