aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorJ08nY2025-02-14 14:55:38 +0100
committerJ08nY2025-02-14 16:08:43 +0100
commit461faa44ee51caf9d219dcfba9925d1a02c0a366 (patch)
tree1305ffe6e9afde6f8b7d15f8fb6dfffa07f123fc /src
parent2a2b60565df9904d913cd830e7675af91749c5c1 (diff)
downloadsec-certs-461faa44ee51caf9d219dcfba9925d1a02c0a366.tar.gz
sec-certs-461faa44ee51caf9d219dcfba9925d1a02c0a366.tar.zst
sec-certs-461faa44ee51caf9d219dcfba9925d1a02c0a366.zip
Get rid of RETURNCODE_OK. This is not C99.
Diffstat (limited to 'src')
-rw-r--r--src/sec_certs/constants.py1
-rw-r--r--src/sec_certs/sample/cc.py48
-rw-r--r--src/sec_certs/sample/fips.py16
-rw-r--r--src/sec_certs/sample/protection_profile.py29
-rw-r--r--src/sec_certs/utils/extract.py30
-rw-r--r--src/sec_certs/utils/helpers.py3
-rw-r--r--src/sec_certs/utils/pdf.py9
7 files changed, 62 insertions, 74 deletions
diff --git a/src/sec_certs/constants.py b/src/sec_certs/constants.py
index 125c2265..f099570e 100644
--- a/src/sec_certs/constants.py
+++ b/src/sec_certs/constants.py
@@ -11,7 +11,6 @@ REF_EMBEDDING_METHOD = Literal["tf_idf", "transformer"]
DUMMY_NONEXISTING_PATH = Path("/this/is/dummy/nonexisting/path")
RESPONSE_OK = 200
-RETURNCODE_OK = "ok"
RETURNCODE_NOK = "nok"
REQUEST_TIMEOUT = 20
diff --git a/src/sec_certs/sample/cc.py b/src/sec_certs/sample/cc.py
index ca096a6c..9fcaa853 100644
--- a/src/sec_certs/sample/cc.py
+++ b/src/sec_certs/sample/cc.py
@@ -14,8 +14,6 @@ import numpy as np
import requests
from bs4 import Tag
-import sec_certs.utils.extract
-import sec_certs.utils.pdf
from sec_certs import constants
from sec_certs.cert_rules import SARS_IMPLIED_FROM_EAL, cc_rules, rules
from sec_certs.configuration import config
@@ -28,7 +26,8 @@ from sec_certs.sample.sar import SAR
from sec_certs.serialization.json import ComplexSerializableType
from sec_certs.serialization.pandas import PandasSerializableType
from sec_certs.utils import helpers, sanitization
-from sec_certs.utils.extract import normalize_match_string, scheme_frontpage_functions
+from sec_certs.utils.extract import extract_keywords, normalize_match_string, scheme_frontpage_functions
+from sec_certs.utils.pdf import convert_pdf_file, extract_pdf_metadata
class CCCertificate(
@@ -756,9 +755,7 @@ class CCCertificate(
:param CCCertificate cert: cert to convert the pdf report for
:return CCCertificate: the modified certificate with updated state
"""
- ocr_done, ok_result = sec_certs.utils.pdf.convert_pdf_file(
- cert.state.report.pdf_path, cert.state.report.txt_path
- )
+ ocr_done, ok_result = convert_pdf_file(cert.state.report.pdf_path, cert.state.report.txt_path)
# If OCR was done the result was garbage
cert.state.report.convert_garbage = ocr_done
# And put the whole result into convert_ok
@@ -778,7 +775,7 @@ class CCCertificate(
:param CCCertificate cert: cert to convert the pdf security target for
:return CCCertificate: the modified certificate with updated state
"""
- ocr_done, ok_result = sec_certs.utils.pdf.convert_pdf_file(cert.state.st.pdf_path, cert.state.st.txt_path)
+ ocr_done, ok_result = convert_pdf_file(cert.state.st.pdf_path, cert.state.st.txt_path)
# If OCR was done the result was garbage
cert.state.st.convert_garbage = ocr_done
# And put the whole result into convert_ok
@@ -798,7 +795,7 @@ class CCCertificate(
:param CCCertificate cert: cert to convert the certificate for
:return CCCertificate: the modified certificate with updated state
"""
- ocr_done, ok_result = sec_certs.utils.pdf.convert_pdf_file(cert.state.cert.pdf_path, cert.state.cert.txt_path)
+ ocr_done, ok_result = convert_pdf_file(cert.state.cert.pdf_path, cert.state.cert.txt_path)
# If OCR was done the result was garbage
cert.state.cert.convert_garbage = ocr_done
# And put the whole result into convert_ok
@@ -818,11 +815,11 @@ class CCCertificate(
:param CCCertificate cert: cert to extract the metadata for.
:return CCCertificate: the modified certificate with updated state
"""
- response, cert.pdf_data.report_metadata = sec_certs.utils.pdf.extract_pdf_metadata(cert.state.report.pdf_path)
- if response != constants.RETURNCODE_OK:
- cert.state.report.extract_ok = False
- else:
+ try:
+ cert.pdf_data.report_metadata = extract_pdf_metadata(cert.state.report.pdf_path)
cert.state.report.extract_ok = True
+ except ValueError:
+ cert.state.report.extract_ok = False
return cert
@staticmethod
@@ -833,11 +830,11 @@ class CCCertificate(
:param CCCertificate cert: cert to extract the metadata for.
:return CCCertificate: the modified certificate with updated state
"""
- response, cert.pdf_data.st_metadata = sec_certs.utils.pdf.extract_pdf_metadata(cert.state.st.pdf_path)
- if response != constants.RETURNCODE_OK:
- cert.state.st.extract_ok = False
- else:
+ try:
+ cert.pdf_data.st_metadata = extract_pdf_metadata(cert.state.st.pdf_path)
cert.state.st.extract_ok = True
+ except ValueError:
+ cert.state.st.extract_ok = False
return cert
@staticmethod
@@ -848,11 +845,11 @@ class CCCertificate(
:param CCCertificate cert: cert to extract the metadata for.
:return CCCertificate: the modified certificate with updated state
"""
- response, cert.pdf_data.cert_metadata = sec_certs.utils.pdf.extract_pdf_metadata(cert.state.cert.pdf_path)
- if response != constants.RETURNCODE_OK:
- cert.state.cert.extract_ok = False
- else:
+ try:
+ cert.pdf_data.cert_metadata = extract_pdf_metadata(cert.state.cert.pdf_path)
cert.state.cert.extract_ok = True
+ except ValueError:
+ cert.state.cert.extract_ok = False
return cert
@staticmethod
@@ -867,8 +864,9 @@ class CCCertificate(
if cert.scheme in scheme_frontpage_functions:
header_func = scheme_frontpage_functions[cert.scheme]
- response, cert.pdf_data.report_frontpage[cert.scheme] = header_func(cert.state.report.txt_path)
- if response != constants.RETURNCODE_OK:
+ try:
+ cert.pdf_data.report_frontpage[cert.scheme] = header_func(cert.state.report.txt_path)
+ except ValueError:
cert.state.report.extract_ok = False
return cert
@@ -881,7 +879,7 @@ class CCCertificate(
:param CCCertificate cert: certificate to extract the keywords for.
:return CCCertificate: the modified certificate with extracted keywords.
"""
- report_keywords = sec_certs.utils.extract.extract_keywords(cert.state.report.txt_path, cc_rules)
+ report_keywords = extract_keywords(cert.state.report.txt_path, cc_rules)
if report_keywords is None:
cert.state.report.extract_ok = False
else:
@@ -897,7 +895,7 @@ class CCCertificate(
:param CCCertificate cert: certificate to extract the keywords for.
:return CCCertificate: the modified certificate with extracted keywords.
"""
- st_keywords = sec_certs.utils.extract.extract_keywords(cert.state.st.txt_path, cc_rules)
+ st_keywords = extract_keywords(cert.state.st.txt_path, cc_rules)
if st_keywords is None:
cert.state.st.extract_ok = False
else:
@@ -913,7 +911,7 @@ class CCCertificate(
:param CCCertificate cert: certificate to extract the keywords for.
:return CCCertificate: the modified certificate with extracted keywords.
"""
- cert_keywords = sec_certs.utils.extract.extract_keywords(cert.state.cert.txt_path, cc_rules)
+ cert_keywords = extract_keywords(cert.state.cert.txt_path, cc_rules)
if cert_keywords is None:
cert.state.cert.extract_ok = False
else:
diff --git a/src/sec_certs/sample/fips.py b/src/sec_certs/sample/fips.py
index ce2e6907..7480991c 100644
--- a/src/sec_certs/sample/fips.py
+++ b/src/sec_certs/sample/fips.py
@@ -23,8 +23,9 @@ from sec_certs.sample.certificate import PdfData as BasePdfData
from sec_certs.sample.cpe import CPE
from sec_certs.serialization.json import ComplexSerializableType
from sec_certs.serialization.pandas import PandasSerializableType
-from sec_certs.utils import extract, helpers, pdf, tables
+from sec_certs.utils import extract, helpers, tables
from sec_certs.utils.helpers import fips_dgst
+from sec_certs.utils.pdf import convert_pdf_file, extract_pdf_metadata, repair_pdf
class FIPSHTMLParser:
@@ -590,7 +591,7 @@ class FIPSCertificate(
"""
Converts policy pdf -> txt
"""
- ocr_done, ok_result = pdf.convert_pdf_file(cert.state.policy_pdf_path, cert.state.policy_txt_path)
+ ocr_done, ok_result = convert_pdf_file(cert.state.policy_pdf_path, cert.state.policy_txt_path)
# If OCR was done and the result was garbage
cert.state.policy_convert_garbage = ocr_done
@@ -610,12 +611,9 @@ class FIPSCertificate(
"""
Extract the PDF metadata from the security policy.
"""
- _, metadata = pdf.extract_pdf_metadata(cert.state.policy_pdf_path)
-
- if metadata:
- cert.pdf_data.policy_metadata = metadata
- else:
- cert.pdf_data.policy_metadata = {}
+ try:
+ cert.pdf_data.policy_metadata = extract_pdf_metadata(cert.state.policy_pdf_path)
+ except ValueError:
cert.state.policy_extract_ok = False
return cert
@@ -640,7 +638,7 @@ class FIPSCertificate(
from tabula import read_pdf
if table_rich_page_numbers := tables.find_pages_with_tables(cert.state.policy_txt_path):
- pdf.repair_pdf(cert.state.policy_pdf_path)
+ repair_pdf(cert.state.policy_pdf_path)
try:
tabular_data = read_pdf(cert.state.policy_pdf_path, pages=list(table_rich_page_numbers), silent=True)
cert.heuristics.algorithms |= set(
diff --git a/src/sec_certs/sample/protection_profile.py b/src/sec_certs/sample/protection_profile.py
index 35eab2dc..58754afa 100644
--- a/src/sec_certs/sample/protection_profile.py
+++ b/src/sec_certs/sample/protection_profile.py
@@ -9,8 +9,6 @@ from urllib.parse import unquote_plus, urlparse
import requests
from bs4 import Tag
-import sec_certs.utils.extract
-import sec_certs.utils.pdf
from sec_certs import constants
from sec_certs.cert_rules import cc_rules
from sec_certs.configuration import config
@@ -20,6 +18,8 @@ from sec_certs.sample.certificate import PdfData as BasePdfData
from sec_certs.sample.document_state import DocumentState
from sec_certs.serialization.json import ComplexSerializableType
from sec_certs.utils import cc_html_parsing, helpers, sanitization
+from sec_certs.utils.extract import extract_keywords
+from sec_certs.utils.pdf import convert_pdf_file, extract_pdf_metadata
class ProtectionProfile(
@@ -301,9 +301,7 @@ class ProtectionProfile(
"""
Converts certification reports from pdf to txt.
"""
- ocr_done, ok_result = sec_certs.utils.pdf.convert_pdf_file(
- cert.state.report.pdf_path, cert.state.report.txt_path
- )
+ ocr_done, ok_result = convert_pdf_file(cert.state.report.pdf_path, cert.state.report.txt_path)
cert.state.report.convert_garbage = ocr_done
cert.state.report.convert_ok = ok_result
if not ok_result:
@@ -317,7 +315,7 @@ class ProtectionProfile(
"""
Converts the actual protection profile from pdf to txt.
"""
- ocr_done, ok_result = sec_certs.utils.pdf.convert_pdf_file(cert.state.pp.pdf_path, cert.state.pp.txt_path)
+ ocr_done, ok_result = convert_pdf_file(cert.state.pp.pdf_path, cert.state.pp.txt_path)
cert.state.pp.convert_garbage = ocr_done
cert.state.pp.convert_ok = ok_result
if not ok_result:
@@ -331,8 +329,11 @@ class ProtectionProfile(
"""
Extracts various pdf metadata from the certification report.
"""
- response, cert.pdf_data.report_metadata = sec_certs.utils.pdf.extract_pdf_metadata(cert.state.report.pdf_path)
- cert.state.report.extract_ok = response == constants.RETURNCODE_OK
+ try:
+ cert.pdf_data.report_metadata = extract_pdf_metadata(cert.state.report.pdf_path)
+ cert.state.report.extract_ok = True
+ except ValueError:
+ cert.state.report.extract_ok = False
return cert
@staticmethod
@@ -340,8 +341,12 @@ class ProtectionProfile(
"""
Extracts various pdf metadata from the actual protection profile.
"""
- response, cert.pdf_data.pp_metadata = sec_certs.utils.pdf.extract_pdf_metadata(cert.state.pp.pdf_path)
- cert.state.pp.extract_ok = response == constants.RETURNCODE_OK
+ try:
+ cert.pdf_data.pp_metadata = extract_pdf_metadata(cert.state.pp.pdf_path)
+ cert.state.pp.extract_ok = True
+ except ValueError:
+ cert.state.pp.extract_ok = False
+
return cert
@staticmethod
@@ -349,7 +354,7 @@ class ProtectionProfile(
"""
Extracts keywords using regexes from the certification report.
"""
- report_keywords = sec_certs.utils.extract.extract_keywords(cert.state.report.txt_path, cc_rules)
+ report_keywords = extract_keywords(cert.state.report.txt_path, cc_rules)
if report_keywords is None:
cert.state.report.extract_ok = False
else:
@@ -361,7 +366,7 @@ class ProtectionProfile(
"""
Extracts keywords using regexes from the actual protection profile.
"""
- pp_keywords = sec_certs.utils.extract.extract_keywords(cert.state.pp.txt_path, cc_rules)
+ pp_keywords = extract_keywords(cert.state.pp.txt_path, cc_rules)
if pp_keywords is None:
cert.state.pp.extract_ok = False
else:
diff --git a/src/sec_certs/utils/extract.py b/src/sec_certs/utils/extract.py
index 6af7f4cc..35361e94 100644
--- a/src/sec_certs/utils/extract.py
+++ b/src/sec_certs/utils/extract.py
@@ -267,19 +267,9 @@ def search_only_headers_anssi(filepath: Path): # noqa: C901
relative_filepath = "/".join(str(filepath).split("/")[-4:])
error_msg = f"Failed to parse ANSSI frontpage headers from {relative_filepath}; {e}"
logger.error(error_msg)
- return error_msg, None
+ raise ValueError(error_msg) from e
- # if True:
- # print('# hits for rule')
- # sorted_rules = sorted(num_rules_hits.items(),
- # key=operator.itemgetter(1), reverse=True)
- # used_rules = []
- # for rule in sorted_rules:
- # print('{:4d} : {}'.format(rule[1], rule[0]))
- # if rule[1] > 0:
- # used_rules.append(rule[0])
-
- return constants.RETURNCODE_OK, items_found
+ return items_found
def search_only_headers_bsi(filepath: Path): # noqa: C901
@@ -376,9 +366,9 @@ def search_only_headers_bsi(filepath: Path): # noqa: C901
relative_filepath = "/".join(str(filepath).split("/")[-4:])
error_msg = f"Failed to parse BSI headers from frontpage: {relative_filepath}; {e}"
logger.error(error_msg)
- return error_msg, None
+ raise ValueError(error_msg) from e
- return constants.RETURNCODE_OK, items_found
+ return items_found
def search_only_headers_nscib(filepath: Path): # noqa: C901
@@ -456,9 +446,9 @@ def search_only_headers_nscib(filepath: Path): # noqa: C901
except Exception as e:
error_msg = f"Failed to parse NSCIB headers from frontpage: {filepath}; {e}"
logger.error(error_msg)
- return error_msg, None
+ raise ValueError(error_msg) from e
- return constants.RETURNCODE_OK, items_found
+ return items_found
def search_only_headers_niap(filepath: Path):
@@ -507,9 +497,9 @@ def search_only_headers_niap(filepath: Path):
except Exception as e:
error_msg = f"Failed to parse NIAP headers from frontpage: {filepath}; {e}"
logger.error(error_msg)
- return error_msg, None
+ raise ValueError(error_msg) from e
- return constants.RETURNCODE_OK, items_found
+ return items_found
def search_only_headers_canada(filepath: Path): # noqa: C901
@@ -580,9 +570,9 @@ def search_only_headers_canada(filepath: Path): # noqa: C901
except Exception as e:
error_msg = f"Failed to parse Canada headers from frontpage: {filepath}; {e}"
logger.error(error_msg)
- return error_msg, None
+ raise ValueError(error_msg) from e
- return constants.RETURNCODE_OK, items_found
+ return items_found
def flatten_matches(dct: dict) -> dict:
diff --git a/src/sec_certs/utils/helpers.py b/src/sec_certs/utils/helpers.py
index 63de6fd9..4c3ab43b 100644
--- a/src/sec_certs/utils/helpers.py
+++ b/src/sec_certs/utils/helpers.py
@@ -142,13 +142,12 @@ def download_file( # noqa: C901
if show_progress_bar:
pbar.update(len(data))
- return r.status_code
+ return r.status_code
except requests.exceptions.Timeout:
return requests.codes.timeout
except Exception as e:
logger.error(f"Failed to download from {url}; {e}")
return constants.RETURNCODE_NOK
- return constants.RETURNCODE_NOK
def download_parallel(
diff --git a/src/sec_certs/utils/pdf.py b/src/sec_certs/utils/pdf.py
index 3195e67e..39046717 100644
--- a/src/sec_certs/utils/pdf.py
+++ b/src/sec_certs/utils/pdf.py
@@ -13,7 +13,6 @@ import pikepdf
import pytesseract
from PIL import Image
-from sec_certs import constants
from sec_certs.constants import (
GARBAGE_ALPHA_CHARS_THRESHOLD,
GARBAGE_AVG_LLEN_THRESHOLD,
@@ -151,7 +150,7 @@ def parse_pdf_date(dateval: bytes | None) -> datetime | None:
return None
-def extract_pdf_metadata(filepath: Path) -> tuple[str, dict[str, Any] | None]: # noqa: C901
+def extract_pdf_metadata(filepath: Path) -> dict[str, Any]: # noqa: C901
"""
Extract PDF metadata, such as the number of pages, author, title, etc.
@@ -237,9 +236,9 @@ def extract_pdf_metadata(filepath: Path) -> tuple[str, dict[str, Any] | None]:
relative_filepath = "/".join(str(filepath).split("/")[-4:])
error_msg = f"Failed to read metadata of {relative_filepath}, error: {e}"
logger.error(error_msg)
- return error_msg, None
+ raise ValueError(error_msg)
- return constants.RETURNCODE_OK, metadata
+ return metadata
def text_is_garbage(text: str) -> bool:
@@ -274,7 +273,7 @@ def text_is_garbage(text: str) -> bool:
# If the average length of a line is small, this is garbage.
if avg_line_len < GARBAGE_AVG_LLEN_THRESHOLD:
return True
- # If there a small amount of lines that have more than one character at every second character, this is garbage.
+ # If there is a small amount of lines that have more than one character at every second character, this is garbage.
# This detects the ANSSI spacing issues.
if every_second < GARBAGE_EVERY_SECOND_CHAR_THRESHOLD:
return True