diff options
| author | J08nY | 2025-02-14 14:55:38 +0100 |
|---|---|---|
| committer | J08nY | 2025-02-14 16:08:43 +0100 |
| commit | 461faa44ee51caf9d219dcfba9925d1a02c0a366 (patch) | |
| tree | 1305ffe6e9afde6f8b7d15f8fb6dfffa07f123fc /src/sec_certs/sample | |
| parent | 2a2b60565df9904d913cd830e7675af91749c5c1 (diff) | |
| download | sec-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/sec_certs/sample')
| -rw-r--r-- | src/sec_certs/sample/cc.py | 48 | ||||
| -rw-r--r-- | src/sec_certs/sample/fips.py | 16 | ||||
| -rw-r--r-- | src/sec_certs/sample/protection_profile.py | 29 |
3 files changed, 47 insertions, 46 deletions
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: |
