diff --git a/application/src/services/ssl/index.ts b/application/src/services/ssl/index.ts index 129df11..95fee33 100644 --- a/application/src/services/ssl/index.ts +++ b/application/src/services/ssl/index.ts @@ -2,9 +2,6 @@ // Re-export all SSL-related functionality for domain SSL checking // Use explicit re-exports to avoid naming conflicts -// SSL Checker service -export { checkSSLCertificate, checkSSLApi } from './sslCheckerService'; - // SSL Status utilities export { determineSSLStatus } from './sslStatusUtils'; @@ -28,10 +25,7 @@ export { } from './notification'; // Export types -export type { SSLCheckerResponse, SSLCertificate, AddSSLCertificateDto, SSLNotification } from './types'; +export type { SSLCertificate, AddSSLCertificateDto, SSLNotification } from './types'; -// Export utility functions -export { normalizeDomain, createErrorResponse } from './sslCheckerUtils'; - -// Export checking mechanisms -export { checkWithFetch } from './sslPrimaryChecker'; \ No newline at end of file +// Export utility functions for SSL operations +export { calculateDaysRemaining, isValid } from './utils'; \ No newline at end of file diff --git a/application/src/services/ssl/notification/sslCheckNotifier.ts b/application/src/services/ssl/notification/sslCheckNotifier.ts index 1b5cab9..356dadf 100644 --- a/application/src/services/ssl/notification/sslCheckNotifier.ts +++ b/application/src/services/ssl/notification/sslCheckNotifier.ts @@ -1,7 +1,6 @@ import { pb } from "@/lib/pocketbase"; import { SSLCertificate } from "../types"; -import { checkSSLCertificate } from "../sslCheckerService"; import { determineSSLStatus } from "../sslStatusUtils"; import { sendSSLNotification } from "./sslNotificationSender"; import { toast } from "sonner"; @@ -35,20 +34,14 @@ export async function checkAllCertificatesAndNotify(): Promise { /** * Checks a specific SSL certificate and sends notification if needed * This respects the Warning and Expiry Thresholds set on the certificate + * Note: SSL checking is now handled by the Go service, this function focuses on notifications */ export async function checkCertificateAndNotify(certificate: SSLCertificate): Promise { console.log(`Checking certificate for ${certificate.domain}...`); try { - // Get fresh SSL data - const sslData = await checkSSLCertificate(certificate.domain); - if (!sslData || !sslData.result) { - console.error(`Failed to check SSL for ${certificate.domain}`); - return false; - } - - // Extract days left from the check result - const daysLeft = sslData.result.days_left || 0; + // Use the current certificate data (updated by Go service) + const daysLeft = certificate.days_left || 0; // Get threshold values (ensure they are numbers) const warningThreshold = Number(certificate.warning_threshold) || 30; @@ -76,21 +69,10 @@ export async function checkCertificateAndNotify(certificate: SSLCertificate): Pr console.log(`${certificate.domain}: ${daysLeft} days left, status: ${status}, should notify: ${shouldNotify}, critical: ${isCritical}`); - // Update certificate data in database - const updateData: Partial = { - days_left: daysLeft, - status: status, - // Other fields from the SSL check that should be updated - issuer_o: sslData.result.issuer_o || certificate.issuer_o, - valid_from: sslData.result.valid_from || certificate.valid_from, - valid_till: sslData.result.valid_till || certificate.valid_till, - validity_days: sslData.result.validity_days || certificate.validity_days, - cert_sans: sslData.result.cert_sans, - cert_alg: sslData.result.cert_alg, - serial_number: sslData.result.cert_sn - }; - - await pb.collection('ssl_certificates').update(certificate.id, updateData); + // Update certificate status in database + await pb.collection('ssl_certificates').update(certificate.id, { + status: status + }); // Send notification if needed if (shouldNotify && certificate.notification_channel) { @@ -135,34 +117,4 @@ export async function checkCertificateAndNotify(certificate: SSLCertificate): Pr toast.error(`Error checking certificate: ${error instanceof Error ? error.message : "Unknown error"}`); return false; } -} - -/** - * Manual trigger for certificate notification test - * Useful for testing notification channels - */ -export async function testCertificateNotification(certificate: SSLCertificate): Promise { - try { - console.log(`Testing notification for ${certificate.domain}...`); - - // Create test message - const message = `🧪 TEST: SSL Certificate notification for ${certificate.domain}.`; - - // We set isCritical to false for test notifications - const notificationSent = await sendSSLNotification(certificate, message, false); - - if (notificationSent) { - console.log(`Test notification sent for ${certificate.domain}`); - toast.success(`Test notification sent for ${certificate.domain}`); - return true; - } else { - console.error(`Failed to send test notification for ${certificate.domain}`); - toast.error(`Failed to send test notification for ${certificate.domain}`); - return false; - } - } catch (error) { - console.error(`Error sending test notification for ${certificate.domain}:`, error); - toast.error(`Error sending test notification: ${error instanceof Error ? error.message : "Unknown error"}`); - return false; - } } \ No newline at end of file diff --git a/application/src/services/ssl/sslCertificateOperations.ts b/application/src/services/ssl/sslCertificateOperations.ts index 9dd9c16..82f434d 100644 --- a/application/src/services/ssl/sslCertificateOperations.ts +++ b/application/src/services/ssl/sslCertificateOperations.ts @@ -1,42 +1,32 @@ import { pb } from "@/lib/pocketbase"; import type { AddSSLCertificateDto, SSLCertificate } from "./types"; -import { checkSSLCertificate } from "./sslCheckerService"; import { determineSSLStatus } from "./sslStatusUtils"; import { checkCertificateAndNotify } from "./notification"; // Import notification service import { toast } from "sonner"; /** * Add a new SSL certificate to monitor + * Note: SSL checking is now handled by the Go service */ export const addSSLCertificate = async ( certificateData: AddSSLCertificateDto ): Promise => { try { - // First check if the SSL certificate is valid and can be fetched - const sslData = await checkSSLCertificate(certificateData.domain); - - if (!sslData || !sslData.result) { - throw new Error(`Could not fetch SSL certificate for ${certificateData.domain}`); - } - // Prepare the data for saving to database + // The Go service will handle the actual SSL checking const data = { domain: certificateData.domain, - issued_to: sslData.result.issued_to || certificateData.domain, - issuer_o: sslData.result.issuer_o || "", - status: determineSSLStatus( - sslData.result.days_left || 0, - certificateData.warning_threshold, - certificateData.expiry_threshold - ), - cert_sans: sslData.result.cert_sans || "", - cert_alg: sslData.result.cert_alg || "", - serial_number: sslData.result.cert_sn || "", - valid_from: sslData.result.valid_from || new Date().toISOString(), - valid_till: sslData.result.valid_till || new Date().toISOString(), - validity_days: sslData.result.validity_days || 0, - days_left: sslData.result.days_left || 0, + issued_to: certificateData.domain, // Will be updated by Go service + issuer_o: "", // Will be updated by Go service + status: "pending", // Initial status + cert_sans: "", + cert_alg: "", + serial_number: "", + valid_from: new Date().toISOString(), // Will be updated by Go service + valid_till: new Date().toISOString(), // Will be updated by Go service + validity_days: 0, // Will be updated by Go service + days_left: 0, // Will be updated by Go service warning_threshold: Number(certificateData.warning_threshold) || 30, expiry_threshold: Number(certificateData.expiry_threshold) || 7, notification_channel: certificateData.notification_channel || "", @@ -54,6 +44,7 @@ export const addSSLCertificate = async ( /** * Check and update a specific SSL certificate + * Note: This now relies on the Go service for SSL data fetching */ export const checkAndUpdateCertificate = async ( certificateId: string @@ -67,45 +58,13 @@ export const checkAndUpdateCertificate = async ( } const typedCertificate = certificate as unknown as SSLCertificate; - const domain = typedCertificate.domain; - - // Check SSL certificate - const sslData = await checkSSLCertificate(domain); - - if (!sslData || !sslData.result) { - throw new Error(`Could not fetch SSL certificate for ${domain}`); - } - - // Update certificate data - const updateData = { - issued_to: sslData.result.issued_to || domain, - issuer_o: sslData.result.issuer_o || typedCertificate.issuer_o, - status: determineSSLStatus( - sslData.result.days_left || 0, - typedCertificate.warning_threshold, - typedCertificate.expiry_threshold - ), - cert_sans: sslData.result.cert_sans || typedCertificate.cert_sans, - cert_alg: sslData.result.cert_alg || typedCertificate.cert_alg, - serial_number: sslData.result.cert_sn || typedCertificate.serial_number, - valid_from: sslData.result.valid_from || typedCertificate.valid_from, - valid_till: sslData.result.valid_till || typedCertificate.valid_till, - validity_days: sslData.result.validity_days || typedCertificate.validity_days, - days_left: sslData.result.days_left || 0, - }; - - // Update in database - const updatedCert = await pb - .collection("ssl_certificates") - .update(certificateId, updateData); - const updatedCertificate = updatedCert as unknown as SSLCertificate; + // The Go service will handle the actual SSL checking and updating + // For now, we'll just trigger notifications based on current data + await checkCertificateAndNotify(typedCertificate); - // After updating, check if notification should be sent - // This will respect the Warning and Expiry Thresholds - await checkCertificateAndNotify(updatedCertificate); - - return updatedCertificate; + // Return the current certificate data + return typedCertificate; } catch (error) { console.error("Error updating SSL certificate:", error); throw error; @@ -127,6 +86,7 @@ export const deleteSSLCertificate = async (id: string): Promise => { /** * Refresh all SSL certificates + * Note: The Go service handles the actual SSL checking */ export const refreshAllCertificates = async (): Promise<{ success: number; failed: number }> => { try { @@ -140,7 +100,7 @@ export const refreshAllCertificates = async (): Promise<{ success: number; faile for (const cert of certificates) { try { - await checkAndUpdateCertificate(cert.id); + await checkCertificateAndNotify(cert); success++; } catch (error) { console.error(`Failed to refresh certificate ${cert.domain}:`, error); diff --git a/application/src/services/ssl/types.ts b/application/src/services/ssl/types.ts index 5797aa7..4e4697c 100644 --- a/application/src/services/ssl/types.ts +++ b/application/src/services/ssl/types.ts @@ -1,74 +1,40 @@ -// SSL Checker response type -export interface SSLCheckerResponse { - version: string; - app: string; - host: string; - response_time_sec: string; - status: string; // "ok", "error" - message?: string; - error?: string; - result: { - host: string; - resolved_ip?: string; - issued_to: string; - issued_o?: string | null; - issuer_c?: string; - issuer_o?: string | null; - issuer_ou?: string | null; - issuer_cn?: string; - cert_sn: string; - cert_sha1?: string; - cert_alg: string; - cert_ver?: number; - cert_sans: string; - cert_exp?: boolean; - cert_valid?: boolean; - valid_from?: string; - valid_till?: string; - validity_days?: number; - days_left?: number; - valid_days_to_expire?: number; - hsts_header_enabled?: boolean; - }; - } - - // SSL Certificate DTO for adding new certificates - export interface AddSSLCertificateDto { - domain: string; - warning_threshold: number; - expiry_threshold: number; - notification_channel: string; - } - - // SSL Certificate model - export interface SSLCertificate { - id: string; - domain: string; - issued_to: string; - issuer_o: string; - status: string; - cert_sans?: string; - cert_alg?: string; - serial_number?: number | string; - valid_from: string; - valid_till: string; - validity_days: number; - days_left: number; - valid_days_to_expire?: number; - warning_threshold: number; - expiry_threshold: number; - notification_channel: string; - last_notified?: string; - created?: string; - updated?: string; - } - - // SSL specific notification types - export interface SSLNotification { - certificateId: string; - domain: string; - message: string; - isCritical: boolean; - timestamp: string; - } \ No newline at end of file +// SSL Certificate DTO for adding new certificates +export interface AddSSLCertificateDto { + domain: string; + warning_threshold: number; + expiry_threshold: number; + notification_channel: string; +} + +// SSL Certificate model +export interface SSLCertificate { + id: string; + domain: string; + issued_to: string; + issuer_o: string; + status: string; + cert_sans?: string; + cert_alg?: string; + serial_number?: number | string; + valid_from: string; + valid_till: string; + validity_days: number; + days_left: number; + valid_days_to_expire?: number; + warning_threshold: number; + expiry_threshold: number; + notification_channel: string; + last_notified?: string; + created?: string; + updated?: string; +} + +// SSL specific notification types +export interface SSLNotification { + certificateId: string; + domain: string; + message: string; + isCritical: boolean; + timestamp: string; +} \ No newline at end of file diff --git a/application/src/services/ssl/utils.ts b/application/src/services/ssl/utils.ts index dc243b3..9140590 100644 --- a/application/src/services/ssl/utils.ts +++ b/application/src/services/ssl/utils.ts @@ -1,6 +1,4 @@ -import { SSLCheckerResponse } from "./types"; - // Calculate days remaining from expiration date export function calculateDaysRemaining(validTo: string): number { try { @@ -22,30 +20,4 @@ export function isValid(validTo: string): boolean { console.error("Error checking certificate validity:", error); return false; } -} - -// Convert results to our expected response format -export function convertResultToResponse(result: any): SSLCheckerResponse { - return { - version: "1.0", - app: "ssl-checker", - host: result.host || "", - response_time_sec: result.response_time_sec || "0.5", - status: result.status || "ok", - result: { - host: result.host || "", - issued_to: result.subject || result.host || "", - issuer_o: result.issuer || "Unknown", - cert_sn: result.serial_number || "0", - cert_alg: result.algorithm || "Unknown", - cert_sans: result.sans || "", - cert_exp: !result.is_valid, - cert_valid: result.is_valid || false, - valid_from: result.valid_from || new Date().toISOString(), - valid_till: result.valid_to || new Date().toISOString(), - validity_days: result.validity_days || 365, - days_left: result.days_remaining || 0, - valid_days_to_expire: result.days_remaining || 0 - } - }; } \ No newline at end of file diff --git a/application/src/services/sslCertificateService.ts b/application/src/services/sslCertificateService.ts index 8682135..c946274 100644 --- a/application/src/services/sslCertificateService.ts +++ b/application/src/services/sslCertificateService.ts @@ -1,7 +1,6 @@ // This file re-exports all SSL certificate related services for backward compatibility import { - checkSSLCertificate, fetchSSLCertificates, addSSLCertificate, checkAndUpdateCertificate @@ -17,7 +16,6 @@ import { } from './ssl/notification'; export { - checkSSLCertificate, determineSSLStatus, fetchSSLCertificates, addSSLCertificate,