Skip to content

Commit

Permalink
Test with encryption enabled and fix key check (#774)
Browse files Browse the repository at this point in the history
  • Loading branch information
Donnype authored Apr 20, 2023
1 parent f33e2c0 commit 0426f7d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from sqlalchemy.exc import OperationalError

from boefjes.config import settings
from boefjes.katalogus.dependencies.encryption import IdentityMiddleware
from boefjes.katalogus.models import Organisation, Repository, Boefje
from boefjes.katalogus.storage.interfaces import (
OrganisationNotFound,
Expand All @@ -18,7 +17,7 @@
from boefjes.sql.db import get_engine, SQL_BASE
from boefjes.sql.organisation_storage import SQLOrganisationStorage
from boefjes.sql.repository_storage import SQLRepositoryStorage
from boefjes.sql.setting_storage import SQLSettingsStorage
from boefjes.sql.setting_storage import SQLSettingsStorage, create_encrypter
from boefjes.sql.plugin_enabled_storage import SQLPluginEnabledStorage


Expand All @@ -41,7 +40,7 @@ def setUp(self) -> None:
session = sessionmaker(bind=self.engine)()
self.organisation_storage = SQLOrganisationStorage(session, settings)
self.repository_storage = SQLRepositoryStorage(session, settings)
self.settings_storage = SQLSettingsStorage(session, IdentityMiddleware())
self.settings_storage = SQLSettingsStorage(session, create_encrypter())
self.plugin_state_storage = SQLPluginEnabledStorage(session, settings)

def tearDown(self) -> None:
Expand Down
7 changes: 4 additions & 3 deletions boefjes/boefjes/sql/setting_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,22 @@ def __init__(self, session: Session, encryption: EncryptMiddleware):

def get_by_key(self, key: str, organisation_id: str, plugin_id: str) -> str:
instance = self._db_instance_by_id(organisation_id, plugin_id)
values = json.loads(self.encryption.decode(instance.values))

if key not in instance.values:
if key not in values:
raise SettingsNotFound(organisation_id, plugin_id) from ObjectNotFoundException(
SettingsInDB, organisation_id=organisation_id
)

return json.loads(self.encryption.decode(instance.values))[key]
return values[key]

def get_all(self, organisation_id: str, plugin_id: str) -> Dict[str, str]:
try:
instance = self._db_instance_by_id(organisation_id, plugin_id)
except SettingsNotFound:
return {}

return {key: value for key, value in json.loads(self.encryption.decode(instance.values)).items()}
return json.loads(self.encryption.decode(instance.values))

def create(self, key: str, value, organisation_id: str, plugin_id: str) -> None:
logger.info("Saving settings: %s for organisation %s", settings, organisation_id)
Expand Down

0 comments on commit 0426f7d

Please sign in to comment.