Реализовал функцию получения информации о сертификате
This commit is contained in:
+38
-18
@@ -21,7 +21,14 @@ class CaddyWorker:
|
|||||||
|
|
||||||
if crt_path.is_file():
|
if crt_path.is_file():
|
||||||
logging.info(f"Found certificate file: {crt_path}")
|
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}")
|
logging.info(f"No certificate file: {cert_folder}")
|
||||||
return None
|
return None
|
||||||
@@ -31,20 +38,33 @@ class CaddyWorker:
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
# def GetCertInfo(self, pathToCrtFile) -> CertInfo:
|
def get_cert_info(self, path_to_crt_file) -> CertInfo | None:
|
||||||
# path = Path(pathToCrtFile)
|
path = Path(path_to_crt_file)
|
||||||
#
|
if not path.is_file():
|
||||||
# with open(path, "rb") as file:
|
logging.error(f"Certificate file not found: {path}")
|
||||||
# cert_data = file.read()
|
return None
|
||||||
#
|
|
||||||
# certificate = x509.load_der_x509_certificate(cert_data)
|
try:
|
||||||
#
|
with open(path, "rb") as file:
|
||||||
# fingerprint = certificate.fingerprint(
|
cert_data = file.read()
|
||||||
# hashes.SHA256()
|
except OSError as e:
|
||||||
# ).hex(":").upper()
|
logging.error(f"Cannot open certificate file {path}: {e}")
|
||||||
#
|
return None
|
||||||
# return CertInfo(
|
|
||||||
# fingerprint=fingerprint,
|
try:
|
||||||
# expires_at=certificate.not_valid_after_utc,
|
certificate = x509.load_pem_x509_certificate(cert_data)
|
||||||
# subject=certificate.subject.rfc4514_string()
|
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():
|
def main():
|
||||||
worker = CaddyWorker()
|
worker = CaddyWorker()
|
||||||
|
|
||||||
result = worker.search_cert_caddy(
|
cert_path = worker.search_cert_caddy(
|
||||||
"D:/Work/Programming/Python/CaddyToDSM/TestData/certs",
|
"D:/Work/Programming/Python/CaddyToDSM/TestData/certs",
|
||||||
"wud.stadidisk.ru"
|
"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__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
+2
-1
@@ -4,9 +4,10 @@ from datetime import datetime
|
|||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class CertInfo:
|
class CertInfo:
|
||||||
|
domain_name: str
|
||||||
fingerprint: str
|
fingerprint: str
|
||||||
expires_at: datetime
|
expires_at: datetime
|
||||||
subject: str
|
path_to_key: str
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
|
|||||||
Reference in New Issue
Block a user