Реализовал функцию получения информации о сертификате
This commit is contained in:
+37
-17
@@ -21,7 +21,14 @@ class CaddyWorker:
|
||||
|
||||
if crt_path.is_file():
|
||||
logging.info(f"Found certificate file: {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"),
|
||||
)
|
||||
|
||||
@@ -5,13 +5,22 @@ from caddy_worker import CaddyWorker
|
||||
def main():
|
||||
worker = CaddyWorker()
|
||||
|
||||
result = worker.search_cert_caddy(
|
||||
cert_path = worker.search_cert_caddy(
|
||||
"D:/Work/Programming/Python/CaddyToDSM/TestData/certs",
|
||||
"wud.stadidisk.ru"
|
||||
)
|
||||
|
||||
print(f"Result: {result}")
|
||||
print(f"Path to cert: {cert_path}")
|
||||
|
||||
if cert_path:
|
||||
cert_info = worker.get_cert_info(cert_path)
|
||||
|
||||
print(f"Domain: {cert_info.domain_name}")
|
||||
print(f"Fingerprint: {cert_info.fingerprint}")
|
||||
print(f"Expires at: {cert_info.expires_at}")
|
||||
print(f"Key path: {cert_info.path_to_key}")
|
||||
else:
|
||||
print("Certificate not found")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
+2
-1
@@ -4,9 +4,10 @@ from datetime import datetime
|
||||
|
||||
@dataclass
|
||||
class CertInfo:
|
||||
domain_name: str
|
||||
fingerprint: str
|
||||
expires_at: datetime
|
||||
subject: str
|
||||
path_to_key: str
|
||||
|
||||
|
||||
@dataclass
|
||||
|
||||
Reference in New Issue
Block a user