Реализовал функции получения сервисов, установки сертификата на определенный сервис и логоута
This commit is contained in:
@@ -172,4 +172,5 @@ class CaddyWorker:
|
|||||||
private_key_path=key_file,
|
private_key_path=key_file,
|
||||||
certificate_pem_path=cert_pem_path,
|
certificate_pem_path=cert_pem_path,
|
||||||
path_to_fullchain=fullchain_pem_path,
|
path_to_fullchain=fullchain_pem_path,
|
||||||
|
dsm_id_cert=""
|
||||||
)
|
)
|
||||||
+203
-12
@@ -3,7 +3,7 @@ from service_functions import ServiceFunctions
|
|||||||
from structs import DSMAuthInfo, DSMInfo
|
from structs import DSMAuthInfo, DSMInfo
|
||||||
import logging
|
import logging
|
||||||
import requests
|
import requests
|
||||||
|
import json
|
||||||
|
|
||||||
class DSMWorker:
|
class DSMWorker:
|
||||||
|
|
||||||
@@ -21,6 +21,11 @@ class DSMWorker:
|
|||||||
CERT_IMPORT_VERSION = "1"
|
CERT_IMPORT_VERSION = "1"
|
||||||
CERT_IMPORT_METHOD = "import"
|
CERT_IMPORT_METHOD = "import"
|
||||||
|
|
||||||
|
CERT_SERVICE_API = "SYNO.Core.Certificate.Service"
|
||||||
|
CERT_SERVICE_VERSION = "1"
|
||||||
|
CERT_SERVICE_METHOD_SET = "set"
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, dsm_url: str):
|
def __init__(self, dsm_url: str):
|
||||||
self.dsm_url = dsm_url.rstrip("/")
|
self.dsm_url = dsm_url.rstrip("/")
|
||||||
|
|
||||||
@@ -144,37 +149,36 @@ class DSMWorker:
|
|||||||
|
|
||||||
return result.get("data")
|
return result.get("data")
|
||||||
|
|
||||||
|
def import_cert(self, dsm_info: DSMInfo) -> DSMInfo:
|
||||||
def import_cert(self, dsm_info: DSMInfo) -> bool:
|
|
||||||
if not self.sid or not self.token:
|
if not self.sid or not self.token:
|
||||||
logging.error("DSM authentication required")
|
logging.error("DSM authentication required")
|
||||||
return False
|
return None
|
||||||
|
|
||||||
if not dsm_info.private_key_path.is_file():
|
if not dsm_info.private_key_path.is_file():
|
||||||
logging.error(
|
logging.error(
|
||||||
f"Private key not found: {dsm_info.private_key_path}"
|
f"Private key not found: {dsm_info.private_key_path}"
|
||||||
)
|
)
|
||||||
return False
|
return None
|
||||||
|
|
||||||
if not dsm_info.certificate_pem_path.is_file():
|
if not dsm_info.certificate_pem_path.is_file():
|
||||||
logging.error(
|
logging.error(
|
||||||
f"Certificate not found: {dsm_info.certificate_pem_path}"
|
f"Certificate not found: {dsm_info.certificate_pem_path}"
|
||||||
)
|
)
|
||||||
return False
|
return None
|
||||||
|
|
||||||
if not dsm_info.path_to_fullchain.is_file():
|
if not dsm_info.path_to_fullchain.is_file():
|
||||||
logging.error(
|
logging.error(
|
||||||
f"Fullchain certificate not found: "
|
f"Fullchain certificate not found: "
|
||||||
f"{dsm_info.path_to_fullchain}"
|
f"{dsm_info.path_to_fullchain}"
|
||||||
)
|
)
|
||||||
return False
|
return None
|
||||||
|
|
||||||
|
|
||||||
certificates_json = self.get_list_certs()
|
certificates_json = self.get_list_certs()
|
||||||
|
|
||||||
if certificates_json is None:
|
if certificates_json is None:
|
||||||
logging.error("Unable to get certificate list from DSM")
|
logging.error("Unable to get certificate list from DSM")
|
||||||
return False
|
return None
|
||||||
|
|
||||||
certificates = certificates_json.get("certificates", [])
|
certificates = certificates_json.get("certificates", [])
|
||||||
|
|
||||||
@@ -238,10 +242,146 @@ class DSMWorker:
|
|||||||
|
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
logging.error(f"Cannot open certificate files: {e}")
|
logging.error(f"Cannot open certificate files: {e}")
|
||||||
return False
|
return None
|
||||||
|
|
||||||
except requests.RequestException as e:
|
except requests.RequestException as e:
|
||||||
logging.error(f"Certificate import request failed: {e}")
|
logging.error(f"Certificate import request failed: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
try:
|
||||||
|
result = response.json()
|
||||||
|
except ValueError as e:
|
||||||
|
logging.error(f"DSM returned invalid JSON: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
if not result.get("success"):
|
||||||
|
error = result.get("error", {})
|
||||||
|
|
||||||
|
logging.error(
|
||||||
|
f"Certificate import failed: "
|
||||||
|
f"code={error.get('code')}"
|
||||||
|
)
|
||||||
|
return None
|
||||||
|
|
||||||
|
logging.info(
|
||||||
|
f"Certificate successfully imported: "
|
||||||
|
f"{dsm_info.certificate_name}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
dsm_info.dsm_id_cert = result["data"].get("id")
|
||||||
|
|
||||||
|
return dsm_info
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def get_list_services(self, list_certs):
|
||||||
|
|
||||||
|
services = []
|
||||||
|
if not list_certs:
|
||||||
|
logging.error(
|
||||||
|
"list_certs is empty, cannot get list of services",
|
||||||
|
)
|
||||||
|
return None
|
||||||
|
|
||||||
|
certificates = list_certs.get("certificates")
|
||||||
|
|
||||||
|
if not isinstance(certificates, list):
|
||||||
|
logging.error(
|
||||||
|
"Invalid certificates: expected list, got %s",
|
||||||
|
type(certificates).__name__,
|
||||||
|
)
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
for certificate in certificates:
|
||||||
|
for service in certificate.get("services", []):
|
||||||
|
services.append({
|
||||||
|
"service": service,
|
||||||
|
"old_id": certificate["id"],
|
||||||
|
"id": None,
|
||||||
|
})
|
||||||
|
|
||||||
|
return services
|
||||||
|
|
||||||
|
|
||||||
|
def set_cert_on_service(self, list_services, dsm_info: DSMInfo) -> bool:
|
||||||
|
|
||||||
|
if not self.sid or not self.token:
|
||||||
|
logging.error("DSM authentication required")
|
||||||
|
return False
|
||||||
|
|
||||||
|
if not dsm_info.private_key_path.is_file():
|
||||||
|
logging.error(
|
||||||
|
f"Private key not found: {dsm_info.private_key_path}"
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
|
||||||
|
if not dsm_info.certificate_pem_path.is_file():
|
||||||
|
logging.error(
|
||||||
|
f"Certificate not found: {dsm_info.certificate_pem_path}"
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
|
||||||
|
if not dsm_info.path_to_fullchain.is_file():
|
||||||
|
logging.error(
|
||||||
|
f"Fullchain certificate not found: "
|
||||||
|
f"{dsm_info.path_to_fullchain}"
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
|
||||||
|
find_service = None
|
||||||
|
|
||||||
|
for service in list_services:
|
||||||
|
if dsm_info.certificate_name == service["service"].get("display_name"):
|
||||||
|
find_service = service
|
||||||
|
break
|
||||||
|
|
||||||
|
if find_service is None:
|
||||||
|
logging.error(
|
||||||
|
f"Service not found in: {dsm_info.certificate_name}"
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
|
||||||
|
if not dsm_info.dsm_id_cert:
|
||||||
|
logging.error("DSM certificate ID is required")
|
||||||
|
return False
|
||||||
|
|
||||||
|
if find_service["old_id"] == dsm_info.dsm_id_cert:
|
||||||
|
logging.info(
|
||||||
|
f"Certificate already set for service: "
|
||||||
|
f"{dsm_info.certificate_name}"
|
||||||
|
)
|
||||||
|
return True
|
||||||
|
|
||||||
|
find_service["id"] = dsm_info.dsm_id_cert
|
||||||
|
settings = [find_service]
|
||||||
|
|
||||||
|
|
||||||
|
url = f"{self.dsm_url}/webapi/entry.cgi"
|
||||||
|
params = {
|
||||||
|
"api": self.CERT_SERVICE_API,
|
||||||
|
"version": self.CERT_SERVICE_VERSION,
|
||||||
|
"method": self.CERT_SERVICE_METHOD_SET,
|
||||||
|
"SynoToken": self.token,
|
||||||
|
"_sid": self.sid,
|
||||||
|
}
|
||||||
|
|
||||||
|
data = {
|
||||||
|
"settings": json.dumps(settings),
|
||||||
|
}
|
||||||
|
try:
|
||||||
|
response = requests.post(
|
||||||
|
url,
|
||||||
|
params=params,
|
||||||
|
data=data,
|
||||||
|
timeout=30,
|
||||||
|
verify=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
except requests.RequestException as e:
|
||||||
|
logging.error(f"Certificate request request failed: {e}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -254,14 +394,65 @@ class DSMWorker:
|
|||||||
error = result.get("error", {})
|
error = result.get("error", {})
|
||||||
|
|
||||||
logging.error(
|
logging.error(
|
||||||
f"Certificate import failed: "
|
f"Certificate service set failed: "
|
||||||
f"code={error.get('code')}"
|
f"code={error.get('code')}"
|
||||||
)
|
)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
logging.info(
|
logging.info(
|
||||||
f"Certificate successfully imported: "
|
f"Certificate successfully set service: "
|
||||||
f"{dsm_info.certificate_name}"
|
f"{dsm_info.certificate_name}"
|
||||||
)
|
)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def logout_dsm(self) -> bool:
|
||||||
|
if not self.sid:
|
||||||
|
logging.info("DSM session is not active")
|
||||||
|
return True
|
||||||
|
|
||||||
|
url = f"{self.dsm_url}/webapi/entry.cgi"
|
||||||
|
|
||||||
|
params = {
|
||||||
|
"api": self.AUTH_API,
|
||||||
|
"version": self.AUTH_VERSION,
|
||||||
|
"method": "logout",
|
||||||
|
"_sid": self.sid,
|
||||||
|
}
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = requests.get(
|
||||||
|
url,
|
||||||
|
params=params,
|
||||||
|
timeout=30,
|
||||||
|
verify=False,
|
||||||
|
)
|
||||||
|
response.raise_for_status()
|
||||||
|
|
||||||
|
except requests.RequestException as e:
|
||||||
|
logging.error(f"DSM logout request failed: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
try:
|
||||||
|
result = response.json()
|
||||||
|
except ValueError as e:
|
||||||
|
logging.error(f"DSM returned invalid JSON: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
if not result.get("success"):
|
||||||
|
error = result.get("error", {})
|
||||||
|
logging.error(
|
||||||
|
f"DSM logout failed: code={error.get('code')}"
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
|
||||||
|
self.sid = None
|
||||||
|
self.token = None
|
||||||
|
|
||||||
|
logging.info("DSM session successfully closed")
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -61,6 +61,10 @@ def main():
|
|||||||
auth_info = dsm_worker.auth_in_DSM(Path("D:/Work/Programming/Python/CaddyToDSM/TestData/.secret"))
|
auth_info = dsm_worker.auth_in_DSM(Path("D:/Work/Programming/Python/CaddyToDSM/TestData/.secret"))
|
||||||
print("Starting dsm_worker\n")
|
print("Starting dsm_worker\n")
|
||||||
|
|
||||||
|
if dsm_worker.logout_dsm():
|
||||||
|
print("Logout Success\n")
|
||||||
|
else:
|
||||||
|
print("Logout Fail\n")
|
||||||
|
|
||||||
if auth_info:
|
if auth_info:
|
||||||
print(f"Session id: {auth_info.sid}")
|
print(f"Session id: {auth_info.sid}")
|
||||||
@@ -70,17 +74,32 @@ def main():
|
|||||||
print("Starting get_list_certs\n")
|
print("Starting get_list_certs\n")
|
||||||
list_certs = dsm_worker.get_list_certs()
|
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:
|
if list_certs:
|
||||||
print(f"Cert list: {list_certs}\n")
|
print(f"Cert list: {list_certs}\n")
|
||||||
|
|
||||||
print("Starting dsm_worker\n")
|
print("Starting dsm_worker\n")
|
||||||
isOkImport = dsm_worker.import_cert(pem_cert)
|
dsm_cert_info = dsm_worker.import_cert(pem_cert)
|
||||||
if isOkImport:
|
if dsm_cert_info:
|
||||||
print(f"Cert Import Success\n")
|
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:
|
else:
|
||||||
print(f"Cert Import Fail\n")
|
print(f"Cert Import Fail\n")
|
||||||
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print("Cert list is empty\n")
|
print("Cert list is empty\n")
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ class DSMInfo:
|
|||||||
private_key_path: Path
|
private_key_path: Path
|
||||||
certificate_pem_path: Path
|
certificate_pem_path: Path
|
||||||
path_to_fullchain: Path
|
path_to_fullchain: Path
|
||||||
|
dsm_id_cert: str
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class DSMAuthInfo:
|
class DSMAuthInfo:
|
||||||
|
|||||||
Reference in New Issue
Block a user