diff options
| author | Adam Janovsky | 2020-11-12 09:28:15 +0100 |
|---|---|---|
| committer | Adam Janovsky | 2020-11-12 09:28:15 +0100 |
| commit | 8d00d14ad76c80ebd63e2e5c10c7baa5b724d5a9 (patch) | |
| tree | 0ec7950212c89ee366dc6f1845f25999f82f3dbe | |
| parent | 35a35bd56f3b3df81749e7778b38b82290496f38 (diff) | |
| download | sec-certs-8d00d14ad76c80ebd63e2e5c10c7baa5b724d5a9.tar.gz sec-certs-8d00d14ad76c80ebd63e2e5c10c7baa5b724d5a9.tar.zst sec-certs-8d00d14ad76c80ebd63e2e5c10c7baa5b724d5a9.zip | |
Union[type, None] -> Optional[type]
Plus adding some abstract methods
| -rw-r--r-- | sec_certs/certificate.py | 32 | ||||
| -rw-r--r-- | sec_certs/dataset.py | 33 |
2 files changed, 36 insertions, 29 deletions
diff --git a/sec_certs/certificate.py b/sec_certs/certificate.py index 2a39bd43..ccf940b0 100644 --- a/sec_certs/certificate.py +++ b/sec_certs/certificate.py @@ -1,13 +1,10 @@ -from typing import Type from datetime import datetime, date from dataclasses import dataclass -import json import logging from . import helpers -from typing import Union from abc import ABC, abstractmethod from bs4 import Tag -from typing import Union +from typing import Union, Optional class Certificate(ABC): @@ -21,6 +18,11 @@ class Certificate(ABC): return 'Not implemented' @abstractmethod + @property + def dgst(self): + raise NotImplementedError('Not meant to be implemented') + + @abstractmethod def to_dict(self) -> dict: raise NotImplementedError('Not meant to be implemented') @@ -32,6 +34,10 @@ class Certificate(ABC): def from_dict(cls, dct: dict) -> 'Certificate': raise NotImplementedError('Mot meant to be implemented') + @abstractmethod + def merge(self, other): + raise NotImplementedError('Not meant to be implemented') + class FIPSCertificate(Certificate): def to_dict(self) -> dict: @@ -41,6 +47,10 @@ class FIPSCertificate(Certificate): def from_dict(cls, dct: dict) -> 'FIPSCertificate': return FIPSCertificate() + @property + def dgst(self): + return None # TODO: Implement me + class CommonCriteriaCert(Certificate): cc_url = 'http://www.commoncriteriaportal.org' @@ -70,7 +80,7 @@ class CommonCriteriaCert(Certificate): Object for holding protection profiles. """ name: str - link: Union[str, None] + link: Optional[str] def __post_init__(self): super().__setattr__('name', helpers.sanitize_string(self.name)) @@ -81,8 +91,8 @@ class CommonCriteriaCert(Certificate): def __init__(self, category: str, name: str, manufacturer: str, scheme: str, security_level: Union[str, set], not_valid_before: date, - not_valid_after: date, report_link: str, st_link: str, src: str, cert_link: Union[str, None], - manufacturer_web: Union[str, None], + not_valid_after: date, report_link: str, st_link: str, src: str, cert_link: Optional[str], + manufacturer_web: Optional[str], protection_profiles: set, maintainance_updates: set): super().__init__() @@ -151,7 +161,7 @@ class CommonCriteriaCert(Certificate): def get_name(cell: Tag) -> str: return list(cell.stripped_strings)[0] - def get_manufacturer(cell: Tag) -> Union[str, None]: + def get_manufacturer(cell: Tag) -> Optional[str]: if lst := list(cell.stripped_strings): return lst[0] else: @@ -163,7 +173,7 @@ class CommonCriteriaCert(Certificate): def get_security_level(cell: Tag) -> set: return set(cell.stripped_strings) - def get_manufacturer_web(cell: Tag) -> Union[str, None]: + def get_manufacturer_web(cell: Tag) -> Optional[str]: for link in cell.find_all('a'): if link is not None and link.get('title') == 'Vendor\'s web site' and link.get('href') != 'http://': return link.get('href') @@ -192,11 +202,11 @@ class CommonCriteriaCert(Certificate): return report_link, security_target_link - def get_cert_link(cell: Tag) -> Union[str, None]: + def get_cert_link(cell: Tag) -> Optional[str]: links = cell.find_all('a') return CommonCriteriaCert.cc_url + links[0].get('href') if links else None - def get_maintainance_div(cell: Tag) -> Union[Tag, None]: + def get_maintainance_div(cell: Tag) -> Optional[Tag]: divs = cell.find_all('div') for d in divs: if d.find('div') and d.stripped_strings and list(d.stripped_strings)[0] == 'Maintenance Report(s)': diff --git a/sec_certs/dataset.py b/sec_certs/dataset.py index ad26d7b1..ccba554e 100644 --- a/sec_certs/dataset.py +++ b/sec_certs/dataset.py @@ -83,8 +83,21 @@ class Dataset(ABC): def get_certs_from_web(self): pass - def merge(self, certs: Dict[str, 'Certificate']): - pass + def merge_certs(self, certs: Dict[str, 'CommonCriteriaCert']): + """ + Merges dictionary of certificates into the dataset. Assuming they all are CommonCriteria certificates + """ + will_be_added = {} + n_merged = 0 + for crt in certs.values(): + if crt not in self: + will_be_added[crt.dgst] = crt + else: + self[crt.dgst].merge(crt) + n_merged += 1 + + self.certs.update(will_be_added) + logging.info(f'Added {len(will_be_added)} new and merged further {n_merged} certificates to the dataset.') class CCDataset(Dataset): @@ -111,22 +124,6 @@ class CCDataset(Dataset): 'cc_pp_archived.csv': 'https://www.commoncriteriaportal.org/pps/pps-archived.csv' } - def merge_certs(self, certs: Dict[str, 'CommonCriteriaCert']): - """ - Merges dictionary of certificates into the dataset. Assuming they all are CommonCriteria certificates - """ - will_be_added = {} - n_merged = 0 - for crt in certs.values(): - if crt not in self: - will_be_added[crt.dgst] = crt - else: - self[crt.dgst].merge(crt) - n_merged += 1 - - self.certs.update(will_be_added) - logging.info(f'Added {len(will_be_added)} new and merged further {n_merged} certificates to the dataset.') - def get_certs_from_web(self, keep_metadata: bool = True): """ Downloads all metadata about certificates from CSV and HTML sources |
