Реализовал функцию получения информации о сертификате
This commit is contained in:
+38
-18
@@ -21,7 +21,14 @@ class CaddyWorker:
|
||||
|
||||
if crt_path.is_file():
|
||||
logging.info(f"Found certificate file: {crt_path}")
|
||||
return crt_path
|
||||
|
||||
key_path = cert_folder / f"{site_name}.key"
|
||||
if key_path.is_file():
|
||||
logging.info(f"Found key file: {key_path}")
|
||||
return crt_path
|
||||
else:
|
||||
logging.info(f"No key file: {key_path}")
|
||||
return None
|
||||
|
||||
logging.info(f"No certificate file: {cert_folder}")
|
||||
return None
|
||||
@@ -31,20 +38,33 @@ class CaddyWorker:
|
||||
|
||||
|
||||
|
||||
# def GetCertInfo(self, pathToCrtFile) -> CertInfo:
|
||||
# path = Path(pathToCrtFile)
|
||||
#
|
||||
# with open(path, "rb") as file:
|
||||
# cert_data = file.read()
|
||||
#
|
||||
# certificate = x509.load_der_x509_certificate(cert_data)
|
||||
#
|
||||
# fingerprint = certificate.fingerprint(
|
||||
# hashes.SHA256()
|
||||
# ).hex(":").upper()
|
||||
#
|
||||
# return CertInfo(
|
||||
# fingerprint=fingerprint,
|
||||
# expires_at=certificate.not_valid_after_utc,
|
||||
# subject=certificate.subject.rfc4514_string()
|
||||
# )
|
||||
def get_cert_info(self, path_to_crt_file) -> CertInfo | None:
|
||||
path = Path(path_to_crt_file)
|
||||
if not path.is_file():
|
||||
logging.error(f"Certificate file not found: {path}")
|
||||
return None
|
||||
|
||||
try:
|
||||
with open(path, "rb") as file:
|
||||
cert_data = file.read()
|
||||
except OSError as e:
|
||||
logging.error(f"Cannot open certificate file {path}: {e}")
|
||||
return None
|
||||
|
||||
try:
|
||||
certificate = x509.load_pem_x509_certificate(cert_data)
|
||||
except ValueError as e:
|
||||
logging.error(f"Invalid certificate {path}: {e}")
|
||||
return None
|
||||
|
||||
fingerprint = certificate.fingerprint(
|
||||
hashes.SHA256()
|
||||
).hex(":").upper()
|
||||
|
||||
domain_name =certificate.subject.get_attributes_for_oid(x509.NameOID.COMMON_NAME)[0].value
|
||||
return CertInfo(
|
||||
domain_name=domain_name,
|
||||
fingerprint=fingerprint,
|
||||
expires_at=certificate.not_valid_after_utc,
|
||||
path_to_key=path_to_crt_file.with_suffix(".key"),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user