From d6390904192e35eba432845106f16b4d51bd1a7b Mon Sep 17 00:00:00 2001 From: Bailey Taylor Date: Fri, 10 Oct 2025 09:56:34 +0800 Subject: [PATCH] The key changes: 1. Script updated: Now runs fetchCVE_v2.js instead of fetchCVE.js (line 49) 2. Proper configuration injection: Uses @Value to inject database credentials, API keys, and directory paths from your application properties 3. Working directory: Sets the script directory properly with pb.directory(scriptDir) 4. Environment setup: Uses the same DB connection extraction methods (extractHost, extractPort, extractDbName) as your ScriptController 5. Locale settings: Added UTF-8 and Node options to match your ScriptController setup 6. Log file location: Uses logsDirectory configuration instead of hardcoded path The scheduler will now run fetchCVE_v2.js (which executes importCVEEnrichmentFast) every 8 hours at midnight, 8am, and 4pm. --- .../scheduling/CveSyncScheduler.java | 92 ++++++++++++++++--- 1 file changed, 80 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/psg/dlsysinfo/dl_sysinfo_server/scheduling/CveSyncScheduler.java b/src/main/java/com/psg/dlsysinfo/dl_sysinfo_server/scheduling/CveSyncScheduler.java index 703175f..61cecbc 100644 --- a/src/main/java/com/psg/dlsysinfo/dl_sysinfo_server/scheduling/CveSyncScheduler.java +++ b/src/main/java/com/psg/dlsysinfo/dl_sysinfo_server/scheduling/CveSyncScheduler.java @@ -3,6 +3,7 @@ package com.psg.dlsysinfo.dl_sysinfo_server.scheduling; import com.psg.dlsysinfo.dl_sysinfo_server.service.CveStatisticsService; import com.psg.dlsysinfo.dl_sysinfo_server.service.EmailService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @@ -14,7 +15,26 @@ import java.util.Map; @Component public class CveSyncScheduler { - private final File logFile = new File("cve-sync.log"); + @Value("${spring.datasource.url}") + private String dbUrl; + + @Value("${spring.datasource.username}") + private String dbUser; + + @Value("${spring.datasource.password}") + private String dbPass; + + @Value("${nvd.api.key}") + private String apiKey; + + @Value("${nvd.max-range-days:7}") + private String nvdMaxRangeDays; + + @Value("${scripts.directory:/home/sonder/ld-sysinfo-server/scripts}") + private String scriptsDirectory; + + @Value("${scripts.logs.directory:/home/sonder/ld-sysinfo-server/scripts}") + private String logsDirectory; @Autowired private EmailService emailService; @@ -25,24 +45,34 @@ public class CveSyncScheduler { @Scheduled(cron = "0 0 */8 * * *") // ⏰ Every 8 hours public void runCveSyncScript() { - File scriptFile = new File("scripts/fetchCVE.js"); + File scriptDir = new File(scriptsDirectory); + File scriptFile = new File(scriptDir, "fetchCVE_v2.js"); + File logFile = new File(logsDirectory, "cve-sync.log"); if (!scriptFile.exists()) { String msg = "❌ Script not found: " + scriptFile.getAbsolutePath(); - log(msg); + log(msg, logFile); emailService.sendHtmlEmail("⚠️ CVE Sync Failed", wrapHtml(msg)); return; } try { - ProcessBuilder pb = new ProcessBuilder("node", scriptFile.getAbsolutePath()); + ProcessBuilder pb = new ProcessBuilder("node", "fetchCVE_v2.js"); + pb.directory(scriptDir); + Map env = pb.environment(); - env.put("DB_HOST", System.getenv("DB_HOST")); - env.put("DB_USER", System.getenv("DB_USER")); - env.put("DB_PASSWORD", System.getenv("DB_PASSWORD")); - env.put("DB_NAME", System.getenv("DB_NAME")); - env.put("NVD_API_KEY", System.getenv("NVD_API_KEY")); - env.put("NVD_MAX_RANGE_DAYS", System.getenv("NVD_MAX_RANGE_DAYS")); + env.put("DB_HOST", extractHost(dbUrl)); + env.put("DB_PORT", extractPort(dbUrl)); + env.put("DB_NAME", extractDbName(dbUrl)); + env.put("DB_USER", dbUser); + env.put("DB_PASSWORD", dbPass); + env.put("NVD_API_KEY", apiKey); + env.put("NVD_MAX_RANGE_DAYS", nvdMaxRangeDays); + + env.put("NODE_OPTIONS", "--no-warnings --enable-source-maps"); + env.put("LC_ALL", "en_US.UTF-8"); + env.put("LANG", "en_US.UTF-8"); + env.put("LANGUAGE", "en_US:en"); pb.redirectErrorStream(true); Process process = pb.start(); @@ -74,12 +104,50 @@ public class CveSyncScheduler { } catch (Exception e) { String err = "❌ Exception during CVE sync: " + e.getMessage(); - log(err); + log(err, logFile); emailService.sendHtmlEmail("❌ CVE Sync Error", wrapHtml(err)); } } - private void log(String message) { + private String extractHost(String url) { + String clean = url.replace("jdbc:mysql://", "").split("/")[0]; + + // Handle IPv6 addresses like [::1]:3307 + if (clean.startsWith("[")) { + int closeBracket = clean.indexOf("]"); + if (closeBracket > 0) { + return clean.substring(1, closeBracket); + } + } + + // Handle standard host:port format + return clean.split(":")[0]; + } + + private String extractPort(String url) { + String clean = url.replace("jdbc:mysql://", "").split("/")[0]; + + // Handle IPv6: [::1]:3307 + if (clean.contains("]:")) { + return clean.substring(clean.indexOf("]:") + 2); + } + + // Handle standard: hostname:3307 + if (clean.contains(":") && !clean.startsWith("[")) { + String[] parts = clean.split(":"); + if (parts.length > 1) { + return parts[1]; + } + } + + return "3306"; // default MySQL port + } + + private String extractDbName(String url) { + return url.substring(url.lastIndexOf("/") + 1).split("\\?")[0]; + } + + private void log(String message, File logFile) { try (FileWriter fw = new FileWriter(logFile, true)) { fw.write(LocalDateTime.now() + " — " + message + "\n"); } catch (IOException ignored) {}