From 9f38e23b22c674138df7518addd2d013f8d51699 Mon Sep 17 00:00:00 2001 From: Tola Leng Date: Fri, 16 May 2025 22:21:39 +0800 Subject: [PATCH] Implement the SSL Certs Service Checker --- .../src/services/sslCertificateService.ts | 157 +++--------------- application/src/services/sslCheckerService.ts | 5 + 2 files changed, 31 insertions(+), 131 deletions(-) create mode 100644 application/src/services/sslCheckerService.ts diff --git a/application/src/services/sslCertificateService.ts b/application/src/services/sslCertificateService.ts index 3e8707a..864b82a 100644 --- a/application/src/services/sslCertificateService.ts +++ b/application/src/services/sslCertificateService.ts @@ -1,135 +1,30 @@ -import { pb } from "@/lib/pocketbase"; -import { AddSSLCertificateDto, SSLCertificate } from "@/types/ssl.types"; -import { checkDomainSSL, determineSSLStatus } from "@/utils/sslUtils"; -import { toast } from "sonner"; +// This file re-exports all SSL certificate related services for backward compatibility +import { + checkSSLCertificate, + fetchSSLCertificates, + addSSLCertificate, + checkAndUpdateCertificate +} from './ssl'; -const fetchSSLCertificates = async (): Promise => { - try { - // Using the PocketBase client to fetch SSL certificates - const response = await pb.collection('ssl_certificates').getList(1, 50, { - sort: '-created', - }); +import { determineSSLStatus } from './ssl/sslStatusUtils'; - // Transform the data if needed - return response.items.map(cert => ({ - id: cert.id, - domain: cert.domain, - port: cert.port || 443, - issuer: cert.issuer, - expiration_date: cert.expiration_date, - status: determineSSLStatus(cert.expiration_date, cert.warning_threshold, cert.expiry_threshold), - last_notified: cert.last_notified, - warning_threshold: cert.warning_threshold, - expiry_threshold: cert.expiry_threshold, - notification_channel: cert.notification_channel, - created: cert.created, - updated: cert.updated - })); - } catch (error) { - console.error("Error fetching SSL certificates:", error); - throw error; - } -}; +// Import from the new refactored location +import { + checkAllCertificatesAndNotify, + checkCertificateAndNotify, + shouldRunDailyCheck, + testCertificateNotification +} from './ssl/notification'; -const addSSLCertificate = async (certificateData: AddSSLCertificateDto): Promise => { - try { - // Perform actual SSL check on the domain - const sslCheck = await checkDomainSSL(certificateData.domain, certificateData.port); - - if (sslCheck.error) { - throw new Error(`SSL certificate check failed: ${sslCheck.error}`); - } - - // Prepare data with the actual certificate information - const dataToSubmit = { - ...certificateData, - issuer: sslCheck.issuer, - expiration_date: sslCheck.expirationDate, - status: determineSSLStatus( - sslCheck.expirationDate, - certificateData.warning_threshold, - certificateData.expiry_threshold - ), - }; - - const response = await pb.collection('ssl_certificates').create(dataToSubmit); - - return { - id: response.id, - domain: response.domain, - port: response.port, - issuer: response.issuer, - expiration_date: response.expiration_date, - status: determineSSLStatus( - response.expiration_date, - response.warning_threshold, - response.expiry_threshold - ), - last_notified: response.last_notified || '', - warning_threshold: response.warning_threshold, - expiry_threshold: response.expiry_threshold, - notification_channel: response.notification_channel, - created: response.created, - updated: response.updated - }; - } catch (error) { - console.error("Error adding SSL certificate:", error); - toast.error(error instanceof Error ? error.message : "Failed to add SSL certificate"); - throw error; - } -}; - -// Function to check and update an existing certificate -const checkAndUpdateCertificate = async (certificateId: string): Promise => { - try { - // Fetch the current certificate data - const currentCert = await pb.collection('ssl_certificates').getOne(certificateId); - - // Perform SSL check - const sslCheck = await checkDomainSSL(currentCert.domain, currentCert.port); - - if (sslCheck.error) { - throw new Error(`SSL certificate check failed: ${sslCheck.error}`); - } - - // Update with new information - const updatedData = { - issuer: sslCheck.issuer, - expiration_date: sslCheck.expirationDate, - status: determineSSLStatus( - sslCheck.expirationDate, - currentCert.warning_threshold, - currentCert.expiry_threshold - ), - updated: new Date().toISOString() - }; - - const response = await pb.collection('ssl_certificates').update(certificateId, updatedData); - - return { - id: response.id, - domain: response.domain, - port: response.port, - issuer: response.issuer, - expiration_date: response.expiration_date, - status: determineSSLStatus( - response.expiration_date, - response.warning_threshold, - response.expiry_threshold - ), - last_notified: response.last_notified || '', - warning_threshold: response.warning_threshold, - expiry_threshold: response.expiry_threshold, - notification_channel: response.notification_channel, - created: response.created, - updated: response.updated - }; - } catch (error) { - console.error("Error updating SSL certificate:", error); - toast.error(error instanceof Error ? error.message : "Failed to update SSL certificate"); - throw error; - } -}; - -export { fetchSSLCertificates, addSSLCertificate, checkAndUpdateCertificate }; \ No newline at end of file +export { + checkSSLCertificate, + determineSSLStatus, + fetchSSLCertificates, + addSSLCertificate, + checkAndUpdateCertificate, + checkAllCertificatesAndNotify, + checkCertificateAndNotify, + shouldRunDailyCheck, + testCertificateNotification +}; \ No newline at end of file diff --git a/application/src/services/sslCheckerService.ts b/application/src/services/sslCheckerService.ts new file mode 100644 index 0000000..0b21675 --- /dev/null +++ b/application/src/services/sslCheckerService.ts @@ -0,0 +1,5 @@ + +// This file re-exports the SSL checker functionality from different implementations +// Primary implementation is in sslCheckerService.ts in the ssl folder + +export { checkSSLCertificate, checkSSLApi } from './ssl/sslCheckerService';