rc 0.0.1
Добавил функцию удаления папок которых нет в списке для обновления. Организовал main. Пока остались тестовые комментарии.
This commit is contained in:
+39
-35
@@ -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=""
|
||||
)
|
||||
+16
-17
@@ -32,10 +32,12 @@ class DSMWorker:
|
||||
self.sid: str | None = None
|
||||
self.token: str | None = None
|
||||
|
||||
def auth_in_DSM(self, path_to_secret) -> DSMAuthInfo | None:
|
||||
|
||||
def auth_in_DSM(self, path_to_secret) -> bool:
|
||||
|
||||
dsm_cred = ServiceFunctions.get_login_pass_from_file(path_to_secret)
|
||||
if dsm_cred is None:
|
||||
logging.error("Unable to load DSM credentials")
|
||||
return False
|
||||
|
||||
url = f"{self.dsm_url}/webapi/entry.cgi"
|
||||
|
||||
@@ -59,13 +61,13 @@ class DSMWorker:
|
||||
response.raise_for_status()
|
||||
except requests.RequestException as e:
|
||||
logging.error(f"Cannot connect to DSM: {e}")
|
||||
return None
|
||||
return False
|
||||
|
||||
try:
|
||||
result = response.json()
|
||||
except ValueError as e:
|
||||
logging.error(f"DSM returned invalid JSON: {e}")
|
||||
return None
|
||||
return False
|
||||
|
||||
if not result.get("success"):
|
||||
error = result.get("error", {})
|
||||
@@ -75,7 +77,7 @@ class DSMWorker:
|
||||
f"code={error.get('code')}"
|
||||
)
|
||||
|
||||
return None
|
||||
return False
|
||||
|
||||
data = result.get("data", {})
|
||||
|
||||
@@ -84,21 +86,18 @@ class DSMWorker:
|
||||
|
||||
if not sid:
|
||||
logging.error("DSM did not return SID")
|
||||
return None
|
||||
return False
|
||||
|
||||
if not token:
|
||||
logging.error("DSM did not return SynoToken")
|
||||
return None
|
||||
return False
|
||||
|
||||
self.sid = sid
|
||||
self.token = token
|
||||
|
||||
logging.info("Successfully authenticated in DSM")
|
||||
|
||||
return DSMAuthInfo(
|
||||
sid=sid,
|
||||
token=token,
|
||||
)
|
||||
return True
|
||||
|
||||
def get_list_certs(self):
|
||||
|
||||
@@ -149,7 +148,7 @@ class DSMWorker:
|
||||
|
||||
return result.get("data")
|
||||
|
||||
def import_cert(self, dsm_info: DSMInfo) -> DSMInfo:
|
||||
def import_cert(self, dsm_info: DSMInfo) -> DSMInfo | None:
|
||||
if not self.sid or not self.token:
|
||||
logging.error("DSM authentication required")
|
||||
return None
|
||||
@@ -166,10 +165,10 @@ class DSMWorker:
|
||||
)
|
||||
return None
|
||||
|
||||
if not dsm_info.path_to_fullchain.is_file():
|
||||
if not dsm_info.path_inter_pem_chain.is_file():
|
||||
logging.error(
|
||||
f"Fullchain certificate not found: "
|
||||
f"{dsm_info.path_to_fullchain}"
|
||||
f"{dsm_info.path_inter_pem_chain}"
|
||||
)
|
||||
return None
|
||||
|
||||
@@ -221,7 +220,7 @@ class DSMWorker:
|
||||
try:
|
||||
with open(dsm_info.private_key_path, "rb") as key_file, \
|
||||
open(dsm_info.certificate_pem_path, "rb") as cert_file, \
|
||||
open(dsm_info.path_to_fullchain, "rb") as fullchain_file:
|
||||
open(dsm_info.path_inter_pem_chain, "rb") as fullchain_file:
|
||||
|
||||
files = {
|
||||
"key": ("private.key", key_file),
|
||||
@@ -324,10 +323,10 @@ class DSMWorker:
|
||||
)
|
||||
return False
|
||||
|
||||
if not dsm_info.path_to_fullchain.is_file():
|
||||
if not dsm_info.path_inter_pem_chain.is_file():
|
||||
logging.error(
|
||||
f"Fullchain certificate not found: "
|
||||
f"{dsm_info.path_to_fullchain}"
|
||||
f"{dsm_info.path_inter_pem_chain}"
|
||||
)
|
||||
return False
|
||||
|
||||
|
||||
@@ -2,110 +2,188 @@
|
||||
from caddy_worker import CaddyWorker
|
||||
from service_functions import ServiceFunctions
|
||||
from dsm_worker import DSMWorker
|
||||
import logging
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
from structs import CertInfo, DSMAuthInfo
|
||||
|
||||
def main():
|
||||
|
||||
|
||||
service_worker = ServiceFunctions()
|
||||
worker = CaddyWorker()
|
||||
|
||||
#### Тест CaddyWorker ##################
|
||||
pem_cert = None
|
||||
|
||||
print(f"search_cert_caddy")
|
||||
cert_path = worker.search_cert_caddy(
|
||||
"D:/Work/Programming/Python/CaddyToDSM/TestData/certs",
|
||||
"wud.stadidisk.ru"
|
||||
)
|
||||
|
||||
print(f"Path to cert: {cert_path}")
|
||||
print(f"\n")
|
||||
|
||||
if cert_path:
|
||||
print(f"get_cert_info")
|
||||
cert_info = worker.get_cert_info(cert_path)
|
||||
service_worker.save_cert_info(cert_info, Path("D:/Work/Programming/Python/CaddyToDSM/TestData/pems"))
|
||||
|
||||
|
||||
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}")
|
||||
print(f"Dir cert: {cert_info.dir_cert}")
|
||||
print(f"\n")
|
||||
|
||||
print(f"convert_to_dsm")
|
||||
pem_cert = worker.convert_to_dsm(cert_info.dir_cert,
|
||||
"D:/Work/Programming/Python/CaddyToDSM/TestData/pems")
|
||||
|
||||
if(pem_cert):
|
||||
print(f"Cert Name: {pem_cert.certificate_name}")
|
||||
print(f"Path to private key: {pem_cert.private_key_path}")
|
||||
print(f"Path to pem: {pem_cert.private_key_path}")
|
||||
print(f"Path to fullchain: {pem_cert.private_key_path}")
|
||||
print(f"\n")
|
||||
else:
|
||||
print("Convert Certificate Fail")
|
||||
print(f"\n")
|
||||
|
||||
settings = service_worker.load_config()
|
||||
#Проверяем все ли папки нужны
|
||||
if service_worker.remove_unnecessary_dsm_certs(settings.path_to_save_folder, settings.list_domains):
|
||||
logging.info(f"DSM certificate folders checked successfully")
|
||||
else:
|
||||
print("Certificate not found")
|
||||
print(f"\n")
|
||||
logging.error(f"Error in removing unnecessary DSM certs")
|
||||
|
||||
dsm_worker = DSMWorker(settings.nas_url)
|
||||
caddy_worker = CaddyWorker()
|
||||
|
||||
#Ищем в папке Caddy те сертификаты которые необходимо обновить, они приведены в настройках
|
||||
list_candidate_update_certs = service_worker.search_caddy_cert(settings.path_to_caddy_cert, settings.list_domains)
|
||||
|
||||
######### Тест DSM worker #################
|
||||
list_update_certs = service_worker.check_update_certs(settings.path_to_save_folder,
|
||||
settings.path_to_caddy_cert,
|
||||
list_candidate_update_certs)
|
||||
|
||||
dsm_worker = DSMWorker("http://stadidisk.local")
|
||||
auth_info = dsm_worker.auth_in_DSM(Path("D:/Work/Programming/Python/CaddyToDSM/TestData/.secret"))
|
||||
print("Starting dsm_worker\n")
|
||||
if not list_update_certs:
|
||||
logging.info(f"No found updated certs for update")
|
||||
return
|
||||
|
||||
if dsm_worker.logout_dsm():
|
||||
print("Logout Success\n")
|
||||
else:
|
||||
print("Logout Fail\n")
|
||||
#Получили все сертификаты которые нужнго обновить
|
||||
if not dsm_worker.auth_in_DSM(Path(settings.credentials)):
|
||||
logging.error(f"Authentication in DSM failed")
|
||||
raise RuntimeError("Authentication in DSM failed")
|
||||
|
||||
if auth_info:
|
||||
print(f"Session id: {auth_info.sid}")
|
||||
print(f"Token: {auth_info.token}")
|
||||
print("\n")
|
||||
|
||||
print("Starting get_list_certs\n")
|
||||
try:
|
||||
list_certs = dsm_worker.get_list_certs()
|
||||
if not list_certs:
|
||||
logging.error(f"Error in get DSM certificate")
|
||||
return
|
||||
|
||||
print("Starting get_list_services\n")
|
||||
list_services = dsm_worker.get_list_services(list_certs)
|
||||
if not list_services:
|
||||
logging.error(f"Error in get list services DSM")
|
||||
return
|
||||
|
||||
if list_services:
|
||||
print(f"Service List: {list_services}\n")
|
||||
else:
|
||||
print("Service list empty\n")
|
||||
for cert in list_update_certs:
|
||||
cert_path = caddy_worker.search_cert_caddy(settings.path_to_caddy_cert, cert)
|
||||
if not cert_path:
|
||||
logging.error(f"Error in searching cert {cert}")
|
||||
continue
|
||||
|
||||
if list_certs:
|
||||
print(f"Cert list: {list_certs}\n")
|
||||
cert_info = caddy_worker.get_cert_info(cert_path)
|
||||
if not cert_info:
|
||||
logging.error(f"Error in get cert info")
|
||||
continue
|
||||
|
||||
print("Starting dsm_worker\n")
|
||||
dsm_cert_info = dsm_worker.import_cert(pem_cert)
|
||||
if dsm_cert_info:
|
||||
print(f"Cert Import Success\n")
|
||||
path_to_dsm_cert = service_worker.save_cert_info(cert_info, Path(settings.path_to_save_folder))
|
||||
|
||||
isSetCert = dsm_worker.set_cert_on_service(list_services, dsm_cert_info)
|
||||
pem_cert = caddy_worker.convert_to_dsm(cert_info.dir_caddy_cert, settings.path_to_save_folder)
|
||||
if not pem_cert:
|
||||
logging.error(f"Error in request PEM certificate")
|
||||
shutil.rmtree(path_to_dsm_cert)
|
||||
continue
|
||||
|
||||
if isSetCert:
|
||||
print(f"Cert Set Success\n")
|
||||
else:
|
||||
print("Cert Set Fail\n")
|
||||
dsm_info = dsm_worker.import_cert(pem_cert)
|
||||
if not dsm_info:
|
||||
logging.error(f"Error in import DSM certificate")
|
||||
shutil.rmtree(path_to_dsm_cert)
|
||||
continue
|
||||
|
||||
else:
|
||||
print(f"Cert Import Fail\n")
|
||||
if not dsm_worker.set_cert_on_service(list_services, dsm_info):
|
||||
logging.error(f"Set cert in DSM failed")
|
||||
shutil.rmtree(path_to_dsm_cert)
|
||||
continue
|
||||
finally:
|
||||
dsm_worker.logout_dsm()
|
||||
|
||||
else:
|
||||
print("Cert list is empty\n")
|
||||
|
||||
else:
|
||||
print(f"Error getting auth data")
|
||||
print("\n")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# #### Тест CaddyWorker ##################
|
||||
# pem_cert = None
|
||||
#
|
||||
# print(f"search_cert_caddy")
|
||||
# cert_path = worker.search_cert_caddy(
|
||||
# "D:/Work/Programming/Python/CaddyToDSM/TestData/certs",
|
||||
# "wud.stadidisk.ru"
|
||||
# )
|
||||
#
|
||||
# print(f"Path to cert: {cert_path}")
|
||||
# print(f"\n")
|
||||
#
|
||||
# if cert_path:
|
||||
# print(f"get_cert_info")
|
||||
# cert_info = worker.get_cert_info(cert_path)
|
||||
# service_worker.save_cert_info(cert_info, Path("D:/Work/Programming/Python/CaddyToDSM/TestData/pems"))
|
||||
#
|
||||
#
|
||||
# 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}")
|
||||
# print(f"Dir cert: {cert_info.dir_cert}")
|
||||
# print(f"\n")
|
||||
#
|
||||
# print(f"convert_to_dsm")
|
||||
# pem_cert = worker.convert_to_dsm(cert_info.dir_cert,
|
||||
# "D:/Work/Programming/Python/CaddyToDSM/TestData/pems")
|
||||
#
|
||||
# if(pem_cert):
|
||||
# print(f"Cert Name: {pem_cert.certificate_name}")
|
||||
# print(f"Path to private key: {pem_cert.private_key_path}")
|
||||
# print(f"Path to pem: {pem_cert.private_key_path}")
|
||||
# print(f"Path to fullchain: {pem_cert.private_key_path}")
|
||||
# print(f"\n")
|
||||
# else:
|
||||
# print("Convert Certificate Fail")
|
||||
# print(f"\n")
|
||||
#
|
||||
# else:
|
||||
# print("Certificate not found")
|
||||
# print(f"\n")
|
||||
#
|
||||
#
|
||||
#
|
||||
# ######### Тест DSM worker #################
|
||||
#
|
||||
# dsm_worker = DSMWorker("http://stadidisk.local")
|
||||
# auth_info = dsm_worker.auth_in_DSM(Path("D:/Work/Programming/Python/CaddyToDSM/TestData/.secret"))
|
||||
# print("Starting dsm_worker\n")
|
||||
#
|
||||
# if dsm_worker.logout_dsm():
|
||||
# print("Logout Success\n")
|
||||
# else:
|
||||
# print("Logout Fail\n")
|
||||
#
|
||||
# if auth_info:
|
||||
# print(f"Session id: {auth_info.sid}")
|
||||
# print(f"Token: {auth_info.token}")
|
||||
# print("\n")
|
||||
#
|
||||
# print("Starting get_list_certs\n")
|
||||
# list_certs = dsm_worker.get_list_certs()
|
||||
#
|
||||
# print("Starting get_list_services\n")
|
||||
# list_services = dsm_worker.get_list_services(list_certs)
|
||||
#
|
||||
# if list_services:
|
||||
# print(f"Service List: {list_services}\n")
|
||||
# else:
|
||||
# print("Service list empty\n")
|
||||
#
|
||||
# if list_certs:
|
||||
# print(f"Cert list: {list_certs}\n")
|
||||
#
|
||||
# print("Starting dsm_worker\n")
|
||||
# dsm_cert_info = dsm_worker.import_cert(pem_cert)
|
||||
# if dsm_cert_info:
|
||||
# print(f"Cert Import Success\n")
|
||||
#
|
||||
# isSetCert = dsm_worker.set_cert_on_service(list_services, dsm_cert_info)
|
||||
#
|
||||
# if isSetCert:
|
||||
# print(f"Cert Set Success\n")
|
||||
# else:
|
||||
# print("Cert Set Fail\n")
|
||||
#
|
||||
# else:
|
||||
# print(f"Cert Import Fail\n")
|
||||
#
|
||||
# else:
|
||||
# print("Cert list is empty\n")
|
||||
#
|
||||
# else:
|
||||
# print(f"Error getting auth data")
|
||||
# print("\n")
|
||||
|
||||
|
||||
|
||||
|
||||
+146
-5
@@ -1,25 +1,30 @@
|
||||
import json
|
||||
from structs import CertInfo, DSMCredentials
|
||||
import shutil
|
||||
from structs import CertInfo, DSMCredentials,ApplSettings
|
||||
from pathlib import Path
|
||||
from dataclasses import asdict
|
||||
import logging
|
||||
from configparser import ConfigParser
|
||||
from caddy_worker import CaddyWorker
|
||||
|
||||
class ServiceFunctions:
|
||||
|
||||
def save_cert_info(self, cert_info: CertInfo, path_to_save_folder: Path):
|
||||
data = asdict(cert_info)
|
||||
|
||||
save_dir = path_to_save_folder / cert_info.dir_cert.name
|
||||
save_dir = path_to_save_folder / cert_info.dir_caddy_cert.name
|
||||
save_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
data["expires_at"] = cert_info.expires_at.isoformat()
|
||||
data["path_to_key"] = str(cert_info.path_to_key)
|
||||
data["dir_cert"] = str(cert_info.dir_cert)
|
||||
data["dir_caddy_cert"] = str(cert_info.dir_caddy_cert)
|
||||
|
||||
(save_dir / cert_info.dir_cert.name).write_text(
|
||||
(save_dir / cert_info.dir_caddy_cert.name).write_text(
|
||||
json.dumps(data, indent=4, ensure_ascii=False),
|
||||
encoding="utf-8"
|
||||
)
|
||||
return save_dir
|
||||
|
||||
|
||||
@staticmethod
|
||||
def get_login_pass_from_file(path_to_file: Path):
|
||||
@@ -59,4 +64,140 @@ class ServiceFunctions:
|
||||
return DSMCredentials(
|
||||
user_name=user_name,
|
||||
password=password,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def load_config(self):
|
||||
|
||||
config_path = Path(__file__).parent / "config.conf"
|
||||
if not config_path.is_file():
|
||||
logging.error(f"Config file not found: {config_path}")
|
||||
raise FileNotFoundError(f"Config file not found: {config_path}")
|
||||
|
||||
config = ConfigParser(allow_no_value=True)
|
||||
config.read(config_path, encoding="utf-8")
|
||||
|
||||
domains = list(config["ListDomains"].keys())
|
||||
|
||||
|
||||
appl_settings = ApplSettings(
|
||||
list_domains = domains,
|
||||
path_to_caddy_cert = Path(config.get("Settings", "pathToCaddyCert")),
|
||||
credentials = config.get("Settings", "pathToCredentials"),
|
||||
path_to_save_folder = Path(config.get("Settings", "pathToSaveFolder")),
|
||||
nas_url = config.get("Settings", "urlNas"),
|
||||
)
|
||||
return appl_settings
|
||||
|
||||
|
||||
def search_caddy_cert(self, path_to_caddy_cert: Path, list_domains: list):
|
||||
update_certs = []
|
||||
|
||||
if not path_to_caddy_cert.is_dir():
|
||||
logging.error(f"Caddy cert folder <{path_to_caddy_cert}> not found")
|
||||
return None
|
||||
|
||||
if not list_domains:
|
||||
logging.error(f"List Domains is empty")
|
||||
return None
|
||||
|
||||
|
||||
for domain in list_domains:
|
||||
folder = path_to_caddy_cert / domain
|
||||
|
||||
if folder.is_dir():
|
||||
update_certs.append(domain)
|
||||
else:
|
||||
logging.warning(f"Cert <{domain}> not found in Caddy Folder")
|
||||
|
||||
return update_certs
|
||||
|
||||
@staticmethod
|
||||
def remove_unnecessary_dsm_certs(path_to_dsm_certs_data: Path, list_domains: list):
|
||||
|
||||
if not path_to_dsm_certs_data.is_dir():
|
||||
logging.error(f"DSM cert folder <{path_to_dsm_certs_data}> not found")
|
||||
return False
|
||||
|
||||
if not list_domains:
|
||||
logging.error(f"List Domains is empty")
|
||||
return False
|
||||
|
||||
for folder in path_to_dsm_certs_data.iterdir():
|
||||
|
||||
if not folder.is_dir():
|
||||
continue
|
||||
|
||||
if folder.name not in list_domains:
|
||||
logging.info(
|
||||
f"Removing unnecessary DSM certificate: {folder.name}"
|
||||
)
|
||||
|
||||
try:
|
||||
shutil.rmtree(folder)
|
||||
except OSError as e:
|
||||
logging.error(
|
||||
f"Cannot remove folder {folder}: {e}"
|
||||
)
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def check_update_certs(self, path_to_dsm_certs_data: Path, path_to_caddy_certs: Path, candidates_update_certs: list):
|
||||
|
||||
if not path_to_dsm_certs_data.is_dir():
|
||||
logging.error(f"Path to certs dir not found: {path_to_dsm_certs_data}")
|
||||
return []
|
||||
|
||||
if not candidates_update_certs:
|
||||
logging.error(f"List Update is empty: {candidates_update_certs}")
|
||||
return []
|
||||
|
||||
need_update_certs = []
|
||||
|
||||
for cert in candidates_update_certs:
|
||||
|
||||
cert_folder = path_to_dsm_certs_data / cert
|
||||
|
||||
if not cert_folder.is_dir():
|
||||
need_update_certs.append(cert)
|
||||
continue
|
||||
|
||||
path_to_caddy_cert = CaddyWorker.search_cert_caddy(path_to_caddy_certs, cert)
|
||||
|
||||
if path_to_caddy_cert is None:
|
||||
logging.error(f"Caddy cert not found: {cert}")
|
||||
continue
|
||||
|
||||
caddy_cert_info = CaddyWorker.get_cert_info(path_to_caddy_cert)
|
||||
if caddy_cert_info is None:
|
||||
logging.error(f"Unable to read Caddy certificate: {cert}")
|
||||
continue
|
||||
|
||||
#Информация о сертификате который загружен в DSM
|
||||
dsm_cert_info = cert_folder / cert
|
||||
|
||||
if dsm_cert_info.is_file() is False:
|
||||
logging.error(f"Cert not found: {dsm_cert_info}")
|
||||
continue
|
||||
|
||||
with open(dsm_cert_info, "r", encoding='utf-8') as file:
|
||||
data_dsm_cert = json.load(file)
|
||||
|
||||
if data_dsm_cert["domain_name"] != caddy_cert_info.domain_name:
|
||||
logging.info(
|
||||
f"Cert DSM: {data_dsm_cert['domain_name']} "
|
||||
f"is not located in the caddy cert "
|
||||
f"{caddy_cert_info.domain_name} folder"
|
||||
)
|
||||
continue
|
||||
|
||||
if data_dsm_cert["fingerprint"] != caddy_cert_info.fingerprint:
|
||||
need_update_certs.append(cert)
|
||||
logging.info(f"Cert: {cert} added to update")
|
||||
else:
|
||||
logging.info(f"Cert: {cert} does not require update")
|
||||
|
||||
return need_update_certs
|
||||
|
||||
|
||||
+12
-3
@@ -1,3 +1,4 @@
|
||||
from ast import List
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
@@ -8,7 +9,7 @@ class CertInfo:
|
||||
fingerprint: str
|
||||
expires_at: datetime
|
||||
path_to_key: Path
|
||||
dir_cert: Path
|
||||
dir_caddy_cert: Path
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -16,7 +17,7 @@ class DSMInfo:
|
||||
certificate_name: str
|
||||
private_key_path: Path
|
||||
certificate_pem_path: Path
|
||||
path_to_fullchain: Path
|
||||
path_inter_pem_chain: Path
|
||||
dsm_id_cert: str
|
||||
|
||||
|
||||
@@ -28,4 +29,12 @@ class DSMAuthInfo:
|
||||
@dataclass
|
||||
class DSMCredentials:
|
||||
user_name: str
|
||||
password: str
|
||||
password: str
|
||||
|
||||
@dataclass
|
||||
class ApplSettings:
|
||||
list_domains: List
|
||||
credentials : str
|
||||
path_to_caddy_cert: Path
|
||||
path_to_save_folder: Path
|
||||
nas_url: str
|
||||
Reference in New Issue
Block a user