Добавил функцию удаления папок которых нет в списке для обновления. Организовал main. Пока остались тестовые комментарии.
This commit is contained in:
Fedorov Dmitriy
2026-08-28 01:23:40 +03:00
parent e425b51966
commit f3acfc2bbf
5 changed files with 371 additions and 140 deletions
+39 -35
View File
@@ -1,4 +1,4 @@
from structs import CertInfo, DSMInfo
from structs import CertInfo, DSMInfo, ApplSettings
from pathlib import Path
from cryptography import x509
@@ -9,36 +9,34 @@ logging.basicConfig(level=logging.INFO)
class CaddyWorker:
def search_cert_caddy(self, path_to_caddy_certs, site_name):
@staticmethod
def search_cert_caddy(path_to_caddy_certs, site_name):
directory = Path(path_to_caddy_certs)
cert_folder = directory / site_name
if cert_folder.is_dir():
logging.info(f"Found certificate folder: {cert_folder}")
crt_path = cert_folder / f"{site_name}.crt"
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
else:
if not cert_folder.is_dir():
logging.info(f"No certificate folder: {cert_folder}")
return None
crt_path = cert_folder / f"{site_name}.crt"
if not crt_path.is_file():
logging.info(f"No certificate file: {crt_path}")
return None
key_path = cert_folder / f"{site_name}.key"
if not key_path.is_file():
logging.info(f"No key file: {key_path}")
return None
logging.info(f"Found certificate and key: {cert_folder}")
return crt_path
def get_cert_info(self, path_to_crt_file) -> CertInfo | None:
@staticmethod
def get_cert_info(path_to_crt_file) -> CertInfo | None:
cert_path = Path(path_to_crt_file)
if not cert_path.is_file():
logging.error(f"Certificate file not found: {cert_path}")
@@ -59,17 +57,27 @@ class CaddyWorker:
fingerprint = certificate.fingerprint(hashes.SHA256()).hex(":").upper()
domain_name = certificate.subject.get_attributes_for_oid(x509.NameOID.COMMON_NAME)[0].value
common_names = certificate.subject.get_attributes_for_oid(
x509.NameOID.COMMON_NAME
)
if not common_names:
logging.error(f"Certificate has no Common Name: {cert_path}")
return None
domain_name = common_names[0].value
return CertInfo(
domain_name=domain_name,
fingerprint=fingerprint,
expires_at=certificate.not_valid_after_utc,
path_to_key=cert_path.with_suffix(".key"),
dir_cert=cert_path.parent,
dir_caddy_cert=cert_path.parent,
)
def convert_to_dsm(self, path_to_dir_cert, path_to_save) -> DSMInfo | None:
@staticmethod
def convert_to_dsm(path_to_dir_cert, path_to_save) -> DSMInfo | None:
cert_dir = Path(path_to_dir_cert)
save_dir = Path(path_to_save) / cert_dir.name / "tmp"
@@ -78,11 +86,11 @@ class CaddyWorker:
crt_file = cert_dir / f"{site_name}.crt"
key_file = cert_dir / f"{site_name}.key"
if not crt_file:
if not crt_file.is_file():
logging.error(f"No certificate found in: {cert_dir}")
return None
if not key_file:
if not key_file.is_file():
logging.error(f"No key found in: {cert_dir}")
return None
@@ -107,7 +115,7 @@ class CaddyWorker:
save_dir.mkdir(parents=True, exist_ok=True)
cert_pem_path = save_dir / "cert.pem"
fullchain_pem_path = save_dir / "fullchain.pem"
inter_pem_chain = save_dir / "fullchain.pem"
try:
@@ -125,7 +133,7 @@ class CaddyWorker:
certificates[0].public_bytes(serialization.Encoding.PEM)
)
fullchain_pem_path.write_bytes(
inter_pem_chain.write_bytes(
b"".join(
cert.public_bytes(serialization.Encoding.PEM)
for cert in certificates[1:]
@@ -163,14 +171,10 @@ class CaddyWorker:
logging.error(f"Invalid private key {key_file}: {e}")
return None
except (OSError, ValueError, TypeError) as e:
logging.error(f"Invalid private key {key_file}: {e}")
return None
return DSMInfo(
certificate_name=domain_name,
private_key_path=key_file,
certificate_pem_path=cert_pem_path,
path_to_fullchain=fullchain_pem_path,
path_inter_pem_chain=inter_pem_chain,
dsm_id_cert=""
)