Skip to content

Commit

Permalink
style: reduce line length
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterBolha committed Dec 5, 2024
1 parent cbfc716 commit e7a7950
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 34 deletions.
4 changes: 3 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from flask import Flask, Request, Response, request

from config_processors.config_processor import ConfigProcessor
from config_processors.config_processors_initializer import ConfigProcessorsInitializer
from config_processors.config_processors_initializer import (
ConfigProcessorsInitializer,
)
from utils.config_loader import ConfigLoader
from utils.data_validator import validate_data
from utils.signature_validator import SignatureValidator
Expand Down
16 changes: 12 additions & 4 deletions config_version_managers/config_version_manager_initializer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from config_version_managers.config_version_manager import ConfigVersionManager
from config_version_managers.git_config_version_manager import GitConfigVersionManager
from config_version_managers.config_version_manager import (
ConfigVersionManager,
)
from config_version_managers.git_config_version_manager import (
GitConfigVersionManager,
)
from config_version_managers.local_config_version_manager import (
LocalConfigVersionManager,
)
Expand All @@ -12,8 +16,12 @@ def __init__(self, config_version_manager_cfg):

def get_config_version_manager(self) -> ConfigVersionManager:
try:
version_manager_type_in_cfg = self.__VERSION_MANAGER_CFG.get("type")
version_manager_type = ConfigVersionManagerType[version_manager_type_in_cfg]
version_manager_type_in_cfg = self.__VERSION_MANAGER_CFG.get(
"type"
)
version_manager_type = ConfigVersionManagerType[
version_manager_type_in_cfg
]
except KeyError:
raise ValueError(
f"Invalid version manager type '"
Expand Down
12 changes: 9 additions & 3 deletions config_version_managers/file_config_version_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

import yaml

from config_version_managers.config_version_manager import ConfigVersionManager
from config_version_managers.config_version_manager import (
ConfigVersionManager,
)
from enums.config_file_format import ConfigFileFormat


class FileConfigVersionManager(ConfigVersionManager):
def __init__(self, file_version_manager_cfg):
self._CONFIG_FILE_NAME = file_version_manager_cfg.get("config_file_name")
self._CONFIG_FILE_NAME = file_version_manager_cfg.get(
"config_file_name"
)

try:
config_file_format_in_cfg = file_version_manager_cfg.get(
Expand Down Expand Up @@ -42,7 +46,9 @@ def save_config_json(self, config, file_path: str) -> str:
def save_config_yaml(self, config, file_path: str) -> str:
file_path += ".yaml"
with open(file_path, "w") as yaml_file:
yaml.dump(config, yaml_file, default_flow_style=False, sort_keys=False)
yaml.dump(
config, yaml_file, default_flow_style=False, sort_keys=False
)
return file_path

def save_config_to_file(
Expand Down
8 changes: 6 additions & 2 deletions config_version_managers/git_config_version_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
from dulwich.porcelain import status
from dulwich.repo import Repo

from config_version_managers.file_config_version_manager import FileConfigVersionManager
from config_version_managers.file_config_version_manager import (
FileConfigVersionManager,
)


class GitConfigVersionManager(FileConfigVersionManager):
Expand Down Expand Up @@ -97,7 +99,9 @@ def save_configuration(self, config: dict[str, Any]) -> None:
)
repo = self.get_repo()

saved_config_file_path = self.save_config_to_file(cfg_file_path, config)
saved_config_file_path = self.save_config_to_file(
cfg_file_path, config
)

if self.has_file_changed(repo, saved_config_file_path):
self.publish_file_to_git(repo, saved_config_file_path)
4 changes: 3 additions & 1 deletion config_version_managers/local_config_version_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from pathlib import Path
from typing import Any

from config_version_managers.file_config_version_manager import FileConfigVersionManager
from config_version_managers.file_config_version_manager import (
FileConfigVersionManager,
)
from enums.config_file_format import ConfigFileFormat


Expand Down
64 changes: 44 additions & 20 deletions tests/test_data_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"client_secret": "",
"redirect_uri": "",
"dynamic_registration": False,
"id_hash": "b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
"id_hash":
"b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
"is_active": False,
"comment": "",
"metadata_hash": "some hash",
Expand All @@ -33,33 +34,38 @@
"entity_id": "test_entityid_3",
"metadata_url": "https://example-metadata-url.com",
"entity_type": "SAML_SP",
"id_hash": "b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
"id_hash":
"b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
}

MINIMAL_VALID_SAML_DATA = {
"entity_id": "test_entityid_3",
"metadata_url": "https://example-metadata-url.com",
"entity_type": "SAML_SP",
"id_hash": "b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
"id_hash":
"b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
}

INVALID_SAML_DATA_NO_ENTITY_ID = {
"metadata_url": "https://example-metadata-url.com",
"entity_type": "SAML_SP",
"id_hash": "b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
"id_hash":
"b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
}

INVALID_SAML_DATA_NO_METADATA_URL = {
"entity_id": "test_entityid_3",
"entity_type": "SAML_SP",
"id_hash": "b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
"id_hash":
"b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
}

INVALID_SAML_DATA_BAD_FORMAT_METADATA_URL = {
"entity_id": "test_entityid_3",
"metadata_url": "NOT_URL",
"entity_type": "SAML_SP",
"id_hash": "b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
"id_hash":
"b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
}

INVALID_SAML_DATA_NO_ID_HASH = {
Expand All @@ -71,14 +77,16 @@
INVALID_SAML_DATA_NO_ENTITY_TYPE = {
"entity_id": "test_entityid_3",
"metadata_url": "https://example-metadata-url.co",
"id_hash": "b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
"id_hash":
"b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
}

INVALID_SAML_DATA_UNKNOWN_ENTITY_TYPE = {
"entity_id": "test_entityid_3",
"metadata_url": "https://example-metadata-url.co",
"entity_type": "INVALID_ENTITY_TYPE",
"id_hash": "b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
"id_hash":
"b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
}

REDUCED_VALID_OIDC_OP_DATA = {
Expand All @@ -87,41 +95,47 @@
"client_id": "test_entityid_3",
"discovery_url": "https://example-discovery-url.com",
"entity_type": "OIDC_OP",
"id_hash": "b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
"id_hash":
"b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
}

MINIMAL_VALID_OIDC_OP_DATA = {
"client_id": "test_entityid_3",
"discovery_url": "https://example-discovery-url.com",
"entity_type": "OIDC_OP",
"id_hash": "b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
"id_hash":
"b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
}

INVALID_OIDC_OP_DATA_NO_CLIENT_ID = {
"discovery_url": "https://example-discovery-url.com",
"entity_type": "OIDC_OP",
"id_hash": "b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
"id_hash":
"b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
}

INVALID_OIDC_OP_DATA_NO_DISCOVERY_URL = {
"client_id": "test_entityid_3",
"entity_type": "OIDC_OP",
"id_hash": "b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
"id_hash":
"b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
}

INVALID_OIDC_OP_DATA_BAD_FORMAT_DISCOVERY_URL = {
"client_id": "test_entityid_3",
"discovery_url": "NOT_URL",
"entity_type": "OIDC_OP",
"id_hash": "b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
"id_hash":
"b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
}

MINIMAL_VALID_OIDC_RP_DYNAMIC_REGISTRATION_DATA = {
"client_id": "test_entityid_3",
"redirect_uri": "https://example-redirect-uri.com",
"dynamic_registration": True,
"entity_type": "OIDC_RP",
"id_hash": "b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
"id_hash":
"b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
}

MINIMAL_VALID_OIDC_RP_STATIC_REGISTRATION_DATA = {
Expand All @@ -130,15 +144,17 @@
"redirect_uri": "https://example-redirect-uri.com",
"dynamic_registration": False,
"entity_type": "OIDC_RP",
"id_hash": "b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
"id_hash":
"b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
}

INVALID_OIDC_RP_STATIC_REGISTRATION_DATA_NO_CLIENT_SECRET = {
"client_id": "test_entityid_3",
"redirect_uri": "https://example-redirect-uri.com",
"dynamic_registration": False,
"entity_type": "OIDC_RP",
"id_hash": "b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
"id_hash":
"b943ca408d3a2a4f13b9db8db10afe4c9bf0e323173f498ac188590b0843d8d9",
}


Expand Down Expand Up @@ -173,7 +189,9 @@ def test_minimal_valid_saml_data(self):
self.assertEqual(True, validation_result.has_valid_data)

def test_invalid_saml_data_no_entity_id(self):
validation_result = self.get_validation_result(INVALID_SAML_DATA_NO_ENTITY_ID)
validation_result = self.get_validation_result(
INVALID_SAML_DATA_NO_ENTITY_ID
)
self.assertEqual(False, validation_result.has_valid_data)
self.assertIn(
"{'entity_id': ['Missing data for required field.']}",
Expand Down Expand Up @@ -201,15 +219,19 @@ def test_invalid_saml_data_bad_format_metadata_url(self):
)

def test_invalid_saml_data_no_id_hash(self):
validation_result = self.get_validation_result(INVALID_SAML_DATA_NO_ID_HASH)
validation_result = self.get_validation_result(
INVALID_SAML_DATA_NO_ID_HASH
)
self.assertEqual(False, validation_result.has_valid_data)
self.assertIn(
"{'id_hash': ['Missing data for required field.']}",
str(validation_result.message),
)

def test_invalid_saml_data_no_entity_type(self):
validation_result = self.get_validation_result(INVALID_SAML_DATA_NO_ENTITY_TYPE)
validation_result = self.get_validation_result(
INVALID_SAML_DATA_NO_ENTITY_TYPE
)
self.assertEqual(False, validation_result.has_valid_data)
self.assertIn(
"{'entity_type': ['Missing data for required field.']}",
Expand All @@ -228,7 +250,9 @@ def test_invalid_saml_data_unknown_entity_type(self):
)

def test_minimal_valid_oidc_op_data(self):
validation_result = self.get_validation_result(MINIMAL_VALID_OIDC_OP_DATA)
validation_result = self.get_validation_result(
MINIMAL_VALID_OIDC_OP_DATA
)
self.assertEqual(True, validation_result.has_valid_data)

def test_invalid_oidc_op_data_no_client_id(self):
Expand Down
4 changes: 3 additions & 1 deletion utils/config_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ def load_config() -> Dict[str, Any]:
f"{ConfigLoader.__GLOBAL_CFG_PATH}'"
)

with open(ConfigLoader.__GLOBAL_CFG_PATH, "r", encoding="utf-8") as yml_cfg:
with open(
ConfigLoader.__GLOBAL_CFG_PATH, "r", encoding="utf-8"
) as yml_cfg:
loaded_cfg = yaml.safe_load(yml_cfg)

return loaded_cfg
3 changes: 2 additions & 1 deletion utils/data_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def validate_dynamic_or_secret(self, data, **kwargs):
# Either dynamic_registration or client_secret must be provided
if not (dynamic_registration or client_secret):
raise ValidationError(
"You must specify either 'dynamic_registration' or " "'client_secret'.",
"You must specify either 'dynamic_registration' or "
"'client_secret'.",
field_names=["dynamic_registration", "client_secret"],
)

Expand Down
4 changes: 3 additions & 1 deletion utils/signature_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def has_valid_signature(self, request: Request) -> bool:
msg=digest_payload,
digestmod=sha256,
)
has_valid_signature &= hmac.compare_digest(digest.hexdigest(), signature)
has_valid_signature &= hmac.compare_digest(
digest.hexdigest(), signature
)

return has_valid_signature

0 comments on commit e7a7950

Please sign in to comment.