Skip to content

Commit 114a20e

Browse files
dekkersDonnypeunderdarknl
authored
Handle normalizer configs as well when cloning settings between organizations (1.19) (#4598)
Co-authored-by: Donny Peeters <46660228+Donnype@users.noreply.github.com> Co-authored-by: Jan Klopper <janklopper+underdark@gmail.com>
1 parent 614a8c0 commit 114a20e

6 files changed

Lines changed: 65 additions & 3 deletions

File tree

boefjes/boefjes/dependencies/plugins.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,29 @@ def get_all_settings(self, organisation_id: str, plugin_id: str):
8686
return self.config_storage.get_all_settings(organisation_id, plugin_id)
8787

8888
def clone_settings_to_organisation(self, from_organisation: str, to_organisation: str):
89-
# One requirement is that only boefjes enabled in the from_organisation end up being enabled for the target.
89+
# One requirement is that only boefjes enabled in the from_organisation end up being enabled for the target,
90+
# and only normalizers disabled in the from_organisation end up being disabled.
9091
for plugin_id in self.config_storage.get_enabled_boefjes(to_organisation):
9192
self.set_enabled_by_id(plugin_id, to_organisation, enabled=False)
9293

93-
for plugin in self.get_all(from_organisation):
94+
for plugin_id in self.config_storage.get_enabled_normalizers(to_organisation):
95+
self.set_enabled_by_id(plugin_id, to_organisation, enabled=True)
96+
97+
# Clone all settings from the from_organisation to the to_organisation
98+
for plugin in self._get_all_without_enabled().values():
99+
if not isinstance(plugin, Boefje): # Only boefjes have settings
100+
continue
94101
if all_settings := self.get_all_settings(from_organisation, plugin.id):
95102
self.upsert_settings(all_settings, to_organisation, plugin.id)
96103

104+
# Enable the same boefjes
97105
for plugin_id in self.config_storage.get_enabled_boefjes(from_organisation):
98106
self.set_enabled_by_id(plugin_id, to_organisation, enabled=True)
99107

108+
# Disable the same normalizers
109+
for plugin_id in self.config_storage.get_disabled_normalizers(from_organisation):
110+
self.set_enabled_by_id(plugin_id, to_organisation, enabled=False)
111+
100112
def upsert_settings(self, settings: dict, organisation_id: str, plugin_id: str):
101113
self._assert_settings_match_schema(settings, plugin_id, organisation_id)
102114
self._put_boefje(plugin_id)

boefjes/boefjes/sql/config_storage.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,31 @@ def get_enabled_normalizers(self, organisation_id: str) -> list[str]:
169169

170170
return [plugin[0] for plugin in enabled_normalizers.all()]
171171

172+
def get_disabled_boefjes(self, organisation_id: str) -> list[str]:
173+
enabled_boefjes = (
174+
self.session.query(BoefjeInDB.plugin_id)
175+
.join(BoefjeConfigInDB)
176+
.filter(BoefjeConfigInDB.boefje_id == BoefjeInDB.id)
177+
.join(OrganisationInDB)
178+
.filter(BoefjeConfigInDB.organisation_pk == OrganisationInDB.pk)
179+
.filter(OrganisationInDB.id == organisation_id)
180+
.filter(BoefjeConfigInDB.enabled == False) # noqa: E712
181+
)
182+
return [x[0] for x in enabled_boefjes.all()]
183+
184+
def get_disabled_normalizers(self, organisation_id: str) -> list[str]:
185+
enabled_normalizers = (
186+
self.session.query(NormalizerInDB.plugin_id)
187+
.join(NormalizerConfigInDB)
188+
.filter(NormalizerConfigInDB.normalizer_id == NormalizerInDB.id)
189+
.join(OrganisationInDB)
190+
.filter(NormalizerConfigInDB.organisation_pk == OrganisationInDB.pk)
191+
.filter(OrganisationInDB.id == organisation_id)
192+
.filter(NormalizerConfigInDB.enabled == False) # noqa: E712
193+
)
194+
195+
return [plugin[0] for plugin in enabled_normalizers.all()]
196+
172197
def get_states_for_organisation(self, organisation_id: str) -> dict[str, bool]:
173198
enabled_boefjes = (
174199
self.session.query(BoefjeInDB.plugin_id, BoefjeConfigInDB.enabled)

boefjes/boefjes/storage/interfaces.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ def get_enabled_boefjes(self, organisation_id: str) -> list[str]:
157157
def get_enabled_normalizers(self, organisation_id: str) -> list[str]:
158158
raise NotImplementedError
159159

160+
def get_disabled_boefjes(self, organisation_id: str) -> list[str]:
161+
raise NotImplementedError
162+
163+
def get_disabled_normalizers(self, organisation_id: str) -> list[str]:
164+
raise NotImplementedError
165+
160166
def get_states_for_organisation(self, organisation_id: str) -> dict[str, bool]:
161167
raise NotImplementedError
162168

boefjes/boefjes/storage/memory.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,20 @@ def get_enabled_normalizers(self, organisation_id: str) -> list[str]:
131131
if enabled and "norm" in plugin_id
132132
]
133133

134+
def get_disabled_boefjes(self, organisation_id: str) -> list[str]:
135+
return [
136+
plugin_id
137+
for plugin_id, enabled in self._enabled.get(organisation_id, {}).items()
138+
if not enabled and "norm" not in plugin_id
139+
]
140+
141+
def get_disabled_normalizers(self, organisation_id: str) -> list[str]:
142+
return [
143+
plugin_id
144+
for plugin_id, enabled in self._enabled.get(organisation_id, {}).items()
145+
if not enabled and "norm" in plugin_id
146+
]
147+
134148
def get_states_for_organisation(self, organisation_id: str) -> dict[str, bool]:
135149
return self._enabled.get(organisation_id, {})
136150

boefjes/tests/integration/test_api.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ def test_clone_settings_and_config_api_shows_both(test_client, organisation):
316316
json={"test_key": "test value", "test_key_2": "test value 2"},
317317
)
318318
test_client.patch(f"/v1/organisations/{organisation.id}/plugins/{plug}", json={"enabled": True})
319+
test_client.patch(f"/v1/organisations/{organisation.id}/plugins/kat_dns_normalize", json={"enabled": False})
319320

320321
assert test_client.get(f"/v1/organisations/{organisation.id}/{plug}/settings").json() == {
321322
"test_key": "test value",
@@ -352,6 +353,10 @@ def test_clone_settings_and_config_api_shows_both(test_client, organisation):
352353
response = test_client.get(f"/v1/organisations/{new_org_id}/plugins/nmap")
353354
assert response.json()["enabled"] is False
354355

356+
# And the originally disabled normalizer got disabled
357+
response = test_client.get(f"/v1/organisations/{new_org_id}/plugins/kat_dns_normalize")
358+
assert response.json()["enabled"] is False
359+
355360
# Assert we can fetch the settings with the new configs API
356361
expected = [
357362
{

mula/scheduler/utils/cron.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ def next_run(expression: str, start_time: datetime | None = None) -> datetime:
88
start_time = datetime.now(timezone.utc)
99

1010
cron = croniter(expression, start_time)
11-
return cron.get_next(datetime) # type: ignore
11+
return cron.get_next(datetime)

0 commit comments

Comments
 (0)