aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorJ08nY2025-02-14 16:37:42 +0100
committerJ08nY2025-02-14 23:40:15 +0100
commit3d6c6bc91dd8d8ab96fd0f28498f16038f7473fd (patch)
tree6c240cd6ec28c26bd6ac9be82961edc1afd843ae /src
parenta8cba36b9e3d2711c7f7c4a7c07d1cbe299cd1d7 (diff)
downloadsec-certs-3d6c6bc91dd8d8ab96fd0f28498f16038f7473fd.tar.gz
sec-certs-3d6c6bc91dd8d8ab96fd0f28498f16038f7473fd.tar.zst
sec-certs-3d6c6bc91dd8d8ab96fd0f28498f16038f7473fd.zip
Fix mutable default state.
Diffstat (limited to 'src')
-rw-r--r--src/sec_certs/dataset/cc.py38
-rw-r--r--src/sec_certs/dataset/cpe.py4
-rw-r--r--src/sec_certs/dataset/cve.py6
-rw-r--r--src/sec_certs/dataset/dataset.py16
-rw-r--r--src/sec_certs/dataset/fips.py26
-rw-r--r--src/sec_certs/dataset/fips_algorithm.py6
-rw-r--r--src/sec_certs/dataset/protection_profile.py8
7 files changed, 57 insertions, 47 deletions
diff --git a/src/sec_certs/dataset/cc.py b/src/sec_certs/dataset/cc.py
index fb90c142..b86a03f3 100644
--- a/src/sec_certs/dataset/cc.py
+++ b/src/sec_certs/dataset/cc.py
@@ -91,33 +91,34 @@ class CCDataset(Dataset[CCCertificate], ComplexSerializableType):
def __init__(
self,
- certs: dict[str, CCCertificate] = {},
+ certs: dict[str, CCCertificate] | None = None,
root_dir: str | Path = constants.DUMMY_NONEXISTING_PATH,
name: str | None = None,
description: str = "",
state: Dataset.DatasetInternalState | None = None,
- aux_handlers: dict[type[AuxiliaryDatasetHandler], AuxiliaryDatasetHandler] = {},
+ aux_handlers: dict[type[AuxiliaryDatasetHandler], AuxiliaryDatasetHandler] | None = None,
):
- self.certs = certs
+ self.certs = certs if certs is not None else {}
self.timestamp = datetime.now()
self.sha256_digest = "not implemented"
self.name = name if name else type(self).__name__ + " dataset"
self.description = description if description else datetime.now().strftime("%d/%m/%Y %H:%M:%S")
self.state = state if state else self.DatasetInternalState()
- self.aux_handlers = aux_handlers
self.root_dir = Path(root_dir)
-
- if not self.aux_handlers:
- self.aux_handlers[CPEDatasetHandler] = CPEDatasetHandler(self.auxiliary_datasets_dir)
- self.aux_handlers[CVEDatasetHandler] = CVEDatasetHandler(self.auxiliary_datasets_dir)
- self.aux_handlers[CPEMatchDictHandler] = CPEMatchDictHandler(self.auxiliary_datasets_dir)
- self.aux_handlers[CCSchemeDatasetHandler] = CCSchemeDatasetHandler(self.auxiliary_datasets_dir)
- self.aux_handlers[ProtectionProfileDatasetHandler] = ProtectionProfileDatasetHandler(
- self.auxiliary_datasets_dir
- )
- self.aux_handlers[CCMaintenanceUpdateDatasetHandler] = CCMaintenanceUpdateDatasetHandler(
- self.auxiliary_datasets_dir
- )
+ self.aux_handlers = (
+ aux_handlers
+ if aux_handlers is not None
+ else {
+ CPEDatasetHandler: CPEDatasetHandler(self.auxiliary_datasets_dir),
+ CVEDatasetHandler: CVEDatasetHandler(self.auxiliary_datasets_dir),
+ CPEMatchDictHandler: CPEMatchDictHandler(self.auxiliary_datasets_dir),
+ CCSchemeDatasetHandler: CCSchemeDatasetHandler(self.auxiliary_datasets_dir),
+ ProtectionProfileDatasetHandler: ProtectionProfileDatasetHandler(self.auxiliary_datasets_dir),
+ CCMaintenanceUpdateDatasetHandler: CCMaintenanceUpdateDatasetHandler(self.auxiliary_datasets_dir),
+ }
+ )
+ # Make sure that the auxiliary handlers (if supplied by the user) have the correct root_dir
+ self._set_local_paths()
def to_pandas(self) -> pd.DataFrame:
"""
@@ -820,14 +821,13 @@ class CCDatasetMaintenanceUpdates(CCDataset, ComplexSerializableType):
# Quite difficult to achieve correct behaviour with MyPy here, opting for ignore
def __init__(
self,
- certs: dict[str, CCMaintenanceUpdate] = {}, # type: ignore
+ certs: dict[str, CCMaintenanceUpdate] | None = None, # type: ignore
root_dir: Path = constants.DUMMY_NONEXISTING_PATH,
name: str = "dataset name",
description: str = "dataset_description",
state: CCDataset.DatasetInternalState | None = None,
):
- super().__init__(certs, root_dir, name, description, state) # type: ignore
- self.aux_handlers = {}
+ super().__init__(certs, root_dir, name, description, state, aux_handlers={}) # type: ignore
self.state.meta_sources_parsed = True
@property
diff --git a/src/sec_certs/dataset/cpe.py b/src/sec_certs/dataset/cpe.py
index 3257d2a4..43db8a79 100644
--- a/src/sec_certs/dataset/cpe.py
+++ b/src/sec_certs/dataset/cpe.py
@@ -31,11 +31,11 @@ class CPEDataset(JSONPathDataset, ComplexSerializableType):
def __init__(
self,
- cpes: dict[str, CPE] = {},
+ cpes: dict[str, CPE] | None = None,
json_path: str | Path = constants.DUMMY_NONEXISTING_PATH,
last_update_timestamp: datetime = datetime.fromtimestamp(0),
):
- self.cpes = cpes
+ self.cpes = cpes if cpes is not None else {}
self.json_path = Path(json_path)
self.last_update_timestamp = last_update_timestamp
diff --git a/src/sec_certs/dataset/cve.py b/src/sec_certs/dataset/cve.py
index f76211ee..1dbf8b37 100644
--- a/src/sec_certs/dataset/cve.py
+++ b/src/sec_certs/dataset/cve.py
@@ -29,11 +29,11 @@ class CVEDataset(JSONPathDataset, ComplexSerializableType):
def __init__(
self,
- cves: dict[str, CVE] = {},
+ cves: dict[str, CVE] | None = None,
json_path: str | Path = constants.DUMMY_NONEXISTING_PATH,
last_update_timestamp: datetime = datetime.fromtimestamp(0),
):
- self.cves = cves
+ self.cves = cves if cves is not None else {}
self.json_path = Path(json_path)
self._cpe_uri_to_cve_ids_lookup: dict[str, set[str]] = {}
self._cves_with_vulnerable_configurations: list[CVE] = []
@@ -120,7 +120,7 @@ class CVEDataset(JSONPathDataset, ComplexSerializableType):
def build_lookup_dict(
self,
cpe_match_feed: dict,
- limit_to_cpes: set[CPE] = set(),
+ limit_to_cpes: set[CPE] | None = None,
):
self._cpe_uri_to_cve_ids_lookup = {}
cpe_uris_of_interest = {x.uri for x in limit_to_cpes} if limit_to_cpes else None
diff --git a/src/sec_certs/dataset/dataset.py b/src/sec_certs/dataset/dataset.py
index c17109f3..af7a56ae 100644
--- a/src/sec_certs/dataset/dataset.py
+++ b/src/sec_certs/dataset/dataset.py
@@ -51,22 +51,25 @@ class Dataset(Generic[CertSubType], ComplexSerializableType, ABC):
def __init__(
self,
- certs: dict[str, CertSubType] = {},
+ certs: dict[str, CertSubType] | None = None,
root_dir: str | Path = constants.DUMMY_NONEXISTING_PATH,
name: str | None = None,
description: str = "",
state: DatasetInternalState | None = None,
- aux_handlers: dict[type[AuxiliaryDatasetHandler], AuxiliaryDatasetHandler] = {},
+ aux_handlers: dict[type[AuxiliaryDatasetHandler], AuxiliaryDatasetHandler] | None = None,
):
- self.certs = certs
+ # TODO: This is actually unused, none of the datasets call super().__init__.
+ self.certs = certs if certs is not None else {}
self.timestamp = datetime.now()
self.sha256_digest = "not implemented"
self.name = name if name else type(self).__name__.lower() + "_dataset"
self.description = description if description else "No description provided"
self.state = state if state else self.DatasetInternalState()
- self.aux_handlers = aux_handlers
self.root_dir = Path(root_dir)
+ self.aux_handlers = aux_handlers if aux_handlers is not None else {}
+ # Make sure that the auxiliary handlers (if supplied by the user) have the correct root_dir
+ self._set_local_paths()
@property
def root_dir(self) -> Path:
@@ -250,8 +253,9 @@ class Dataset(Generic[CertSubType], ComplexSerializableType, ABC):
return dset
def _set_local_paths(self) -> None:
- for handler in self.aux_handlers.values():
- handler.set_local_paths(self.auxiliary_datasets_dir)
+ if hasattr(self, "aux_handlers"):
+ for handler in self.aux_handlers.values():
+ handler.set_local_paths(self.auxiliary_datasets_dir)
def move_dataset(self, new_root_dir: str | Path) -> None:
"""
diff --git a/src/sec_certs/dataset/fips.py b/src/sec_certs/dataset/fips.py
index 34b1147d..90d6b5df 100644
--- a/src/sec_certs/dataset/fips.py
+++ b/src/sec_certs/dataset/fips.py
@@ -59,27 +59,32 @@ class FIPSDataset(Dataset[FIPSCertificate], ComplexSerializableType):
def __init__(
self,
- certs: dict[str, FIPSCertificate] = {},
+ certs: dict[str, FIPSCertificate] | None = None,
root_dir: str | Path = constants.DUMMY_NONEXISTING_PATH,
name: str | None = None,
description: str = "",
state: Dataset.DatasetInternalState | None = None,
- aux_handlers: dict[type[AuxiliaryDatasetHandler], AuxiliaryDatasetHandler] = {},
+ aux_handlers: dict[type[AuxiliaryDatasetHandler], AuxiliaryDatasetHandler] | None = None,
):
- self.certs = certs
+ self.certs = certs if certs is not None else {}
self.timestamp = datetime.datetime.now()
self.sha256_digest = "not implemented"
self.name = name if name else type(self).__name__ + " dataset"
self.description = description if description else datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")
self.state = state if state else self.DatasetInternalState()
- self.aux_handlers = aux_handlers
self.root_dir = Path(root_dir)
-
- if not self.aux_handlers:
- self.aux_handlers[CPEDatasetHandler] = CPEDatasetHandler(self.auxiliary_datasets_dir)
- self.aux_handlers[CVEDatasetHandler] = CVEDatasetHandler(self.auxiliary_datasets_dir)
- self.aux_handlers[FIPSAlgorithmDatasetHandler] = FIPSAlgorithmDatasetHandler(self.auxiliary_datasets_dir)
- self.aux_handlers[CPEMatchDictHandler] = CPEMatchDictHandler(self.auxiliary_datasets_dir)
+ self.aux_handlers = (
+ aux_handlers
+ if aux_handlers is not None
+ else {
+ CPEDatasetHandler: CPEDatasetHandler(self.auxiliary_datasets_dir),
+ CVEDatasetHandler: CVEDatasetHandler(self.auxiliary_datasets_dir),
+ FIPSAlgorithmDatasetHandler: FIPSAlgorithmDatasetHandler(self.auxiliary_datasets_dir),
+ CPEMatchDictHandler: CPEMatchDictHandler(self.auxiliary_datasets_dir),
+ }
+ )
+ # Make sure that the auxiliary handlers (if supplied by the user) have the correct root_dir
+ self._set_local_paths()
LIST_OF_CERTS_HTML: Final[dict[str, str]] = {
"fips_modules_active.html": constants.FIPS_ACTIVE_MODULES_URL,
@@ -112,7 +117,6 @@ class FIPSDataset(Dataset[FIPSCertificate], ComplexSerializableType):
def _extract_data_from_html_modules(self) -> None:
"""
Extracts data from html module file
- :param bool fresh: if all certs should be processed, or only the failed ones. Defaults to True
"""
logger.info("Extracting data from html modules.")
certs_to_process = [x for x in self if x.state.module_is_ok_to_analyze()]
diff --git a/src/sec_certs/dataset/fips_algorithm.py b/src/sec_certs/dataset/fips_algorithm.py
index ee7d06a1..c9a8b3c5 100644
--- a/src/sec_certs/dataset/fips_algorithm.py
+++ b/src/sec_certs/dataset/fips_algorithm.py
@@ -21,8 +21,10 @@ logger = logging.getLogger(__name__)
class FIPSAlgorithmDataset(JSONPathDataset, ComplexSerializableType):
- def __init__(self, algs: dict[str, FIPSAlgorithm] = {}, json_path: str | Path = constants.DUMMY_NONEXISTING_PATH):
- self.algs = algs
+ def __init__(
+ self, algs: dict[str, FIPSAlgorithm] | None = None, json_path: str | Path = constants.DUMMY_NONEXISTING_PATH
+ ):
+ self.algs = algs if algs is not None else {}
self.json_path = Path(json_path)
self.alg_number_to_algs: dict[str, set[FIPSAlgorithm]] = {}
diff --git a/src/sec_certs/dataset/protection_profile.py b/src/sec_certs/dataset/protection_profile.py
index 9b22ca75..d5457e7f 100644
--- a/src/sec_certs/dataset/protection_profile.py
+++ b/src/sec_certs/dataset/protection_profile.py
@@ -39,20 +39,20 @@ class ProtectionProfileDataset(Dataset[ProtectionProfile], ComplexSerializableTy
def __init__(
self,
- certs: dict[str, ProtectionProfile] = {},
+ certs: dict[str, ProtectionProfile] | None = None,
root_dir: str | Path = constants.DUMMY_NONEXISTING_PATH,
name: str | None = None,
description: str = "",
state: Dataset.DatasetInternalState | None = None,
- aux_handlers: dict[type[AuxiliaryDatasetHandler], AuxiliaryDatasetHandler] = {},
+ aux_handlers: dict[type[AuxiliaryDatasetHandler], AuxiliaryDatasetHandler] | None = None,
):
- self.certs = certs
+ self.certs = certs if certs is not None else {}
self.timestamp = datetime.now()
self.sha256_digest = "not implemented"
self.name = name if name else type(self).__name__ + " dataset"
self.description = description if description else datetime.now().strftime("%d/%m/%Y %H:%M:%S")
self.state = state if state else self.DatasetInternalState()
- self.aux_handlers = aux_handlers
+ self.aux_handlers = aux_handlers if aux_handlers is not None else {}
self.root_dir = Path(root_dir)
@property