|
1 | 1 | package com.park.utmstack.service.soc_ai; |
2 | 2 |
|
3 | 3 | import com.google.gson.Gson; |
4 | | -import com.park.utmstack.domain.UtmAlertSocaiProcessingRequest; |
5 | | -import com.park.utmstack.domain.application_modules.enums.ModuleName; |
6 | | -import com.park.utmstack.service.UtmAlertSocaiProcessingRequestService; |
| 4 | +import com.park.utmstack.config.Constants; |
| 5 | +import com.park.utmstack.domain.shared_types.alert.UtmAlert; |
7 | 6 | import com.park.utmstack.service.application_modules.UtmModuleService; |
8 | 7 | import okhttp3.*; |
9 | 8 | import org.slf4j.Logger; |
10 | 9 | import org.slf4j.LoggerFactory; |
11 | | -import org.springframework.data.domain.Page; |
12 | | -import org.springframework.data.domain.PageRequest; |
13 | | -import org.springframework.scheduling.annotation.Async; |
14 | | -import org.springframework.scheduling.annotation.Scheduled; |
15 | 10 | import org.springframework.stereotype.Service; |
16 | 11 | import org.springframework.util.StringUtils; |
17 | 12 |
|
18 | | -import java.util.List; |
| 13 | +import javax.net.ssl.SSLContext; |
| 14 | +import javax.net.ssl.TrustManager; |
| 15 | +import javax.net.ssl.X509TrustManager; |
| 16 | +import java.security.cert.X509Certificate; |
19 | 17 | import java.util.concurrent.TimeUnit; |
20 | | -import java.util.stream.Collectors; |
21 | 18 |
|
22 | 19 | @Service |
23 | 20 | public class SocAIService { |
24 | 21 | private static final String CLASSNAME = "SocAIService"; |
25 | 22 | private final Logger log = LoggerFactory.getLogger(SocAIService.class); |
26 | | - private final String SOCAI_PROCESS_URL; |
| 23 | + private final String SOCAI_BASE_URL; |
| 24 | + private final String SOCAI_ANALYZE_ENDPOINT = "/api/v1/analyze"; |
| 25 | + private final OkHttpClient httpClient; |
27 | 26 |
|
28 | | - private final UtmAlertSocaiProcessingRequestService socaiProcessingRequestService; |
29 | 27 | private final UtmModuleService moduleService; |
30 | 28 |
|
31 | | - public SocAIService(UtmAlertSocaiProcessingRequestService socaiProcessingRequestService, |
32 | | - UtmModuleService moduleService) { |
33 | | - this.socaiProcessingRequestService = socaiProcessingRequestService; |
| 29 | + public SocAIService(UtmModuleService moduleService) { |
34 | 30 | this.moduleService = moduleService; |
35 | | - SOCAI_PROCESS_URL = System.getenv("SOC_AI_BASE_URL"); |
| 31 | + SOCAI_BASE_URL = System.getenv("SOC_AI_BASE_URL"); |
| 32 | + this.httpClient = createTrustAllClient(); |
36 | 33 | } |
37 | 34 |
|
38 | | - |
39 | | - public void sendData(Object data) { |
40 | | - final String ctx = CLASSNAME + ".sendData"; |
| 35 | + /** |
| 36 | + * Sends a complete alert to SOC-AI for analysis |
| 37 | + */ |
| 38 | + public void analyzeAlert(UtmAlert alert) { |
| 39 | + final String ctx = CLASSNAME + ".analyzeAlert"; |
41 | 40 | try { |
42 | | - OkHttpClient client = new OkHttpClient.Builder() |
43 | | - .connectTimeout(10, TimeUnit.SECONDS) |
44 | | - .writeTimeout(10, TimeUnit.SECONDS) |
45 | | - .readTimeout(30, TimeUnit.SECONDS) |
46 | | - .build(); |
| 41 | + if (!StringUtils.hasText(SOCAI_BASE_URL)) { |
| 42 | + throw new RuntimeException("SOC_AI_BASE_URL environment variable is not configured"); |
| 43 | + } |
| 44 | + |
| 45 | + String internalKey = System.getenv(Constants.ENV_INTERNAL_KEY); |
| 46 | + if (!StringUtils.hasText(internalKey)) { |
| 47 | + throw new RuntimeException("INTERNAL_KEY environment variable is not configured"); |
| 48 | + } |
| 49 | + |
47 | 50 | MediaType mediaType = MediaType.parse("application/json; charset=utf-8"); |
48 | | - RequestBody body = RequestBody.create(new Gson().toJson(data), mediaType); |
49 | | - Request request = new Request.Builder().url(SOCAI_PROCESS_URL).post(body) |
50 | | - .addHeader("Content-Type", "application/json").build(); |
| 51 | + RequestBody body = RequestBody.create(new Gson().toJson(alert), mediaType); |
51 | 52 |
|
52 | | - try (Response rs = client.newCall(request).execute()) { |
53 | | - if (!rs.isSuccessful()) |
54 | | - throw new Exception(ctx + "Unexpected response: " + rs); |
| 53 | + String url = SOCAI_BASE_URL + SOCAI_ANALYZE_ENDPOINT; |
| 54 | + Request request = new Request.Builder() |
| 55 | + .url(url) |
| 56 | + .post(body) |
| 57 | + .addHeader("Content-Type", "application/json") |
| 58 | + .addHeader("X-Internal-Key", internalKey) |
| 59 | + .build(); |
| 60 | + |
| 61 | + try (Response rs = httpClient.newCall(request).execute()) { |
| 62 | + if (!rs.isSuccessful()) { |
| 63 | + String responseBody = rs.body() != null ? rs.body().string() : "No response body"; |
| 64 | + throw new Exception("Unexpected response: " + rs.code() + " - " + responseBody); |
| 65 | + } |
55 | 66 | } |
56 | 67 | } catch (Exception e) { |
57 | 68 | log.error(ctx + ": " + e.getLocalizedMessage()); |
58 | 69 | throw new RuntimeException(ctx + ": " + e.getLocalizedMessage()); |
59 | 70 | } |
60 | 71 | } |
61 | 72 |
|
62 | | - /*@Scheduled(fixedDelay = 30000)*/ |
63 | | - public void sendRequests() { |
64 | | - final String ctx = CLASSNAME + ".sendRequests"; |
| 73 | + private OkHttpClient createTrustAllClient() { |
65 | 74 | try { |
66 | | - if (!moduleService.isModuleActive(ModuleName.SOC_AI)) |
67 | | - return; |
| 75 | + TrustManager[] trustAllCerts = new TrustManager[]{ |
| 76 | + new X509TrustManager() { |
| 77 | + public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } |
| 78 | + public void checkClientTrusted(X509Certificate[] certs, String authType) {} |
| 79 | + public void checkServerTrusted(X509Certificate[] certs, String authType) {} |
| 80 | + } |
| 81 | + }; |
68 | 82 |
|
69 | | - if (!StringUtils.hasText(SOCAI_PROCESS_URL)) { |
70 | | - log.error(ctx + ": Environment variable SOC_AI_BASE_URL is missing or does not have a value"); |
71 | | - return; |
72 | | - } |
| 83 | + SSLContext sslContext = SSLContext.getInstance("TLS"); |
| 84 | + sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); |
73 | 85 |
|
74 | | - Page<UtmAlertSocaiProcessingRequest> requests = socaiProcessingRequestService.findAll(PageRequest.of(0, 20)); |
75 | | - if (!requests.hasContent()) |
76 | | - return; |
77 | | - List<String> ids = requests.getContent().stream().map(UtmAlertSocaiProcessingRequest::getAlertId) |
78 | | - .collect(Collectors.toList()); |
79 | | - try { |
80 | | - sendData(ids); |
81 | | - socaiProcessingRequestService.delete(ids); |
82 | | - } catch (Exception e) { |
83 | | - log.error(ctx + ": " + e.getLocalizedMessage()); |
84 | | - } |
85 | | - } catch (Exception e) { |
86 | | - throw new RuntimeException(e); |
87 | | - } |
88 | | - } |
89 | | - |
90 | | - @Async |
91 | | - public void requestSocAiProcess(List<String> alertIds) { |
92 | | - final String ctx = CLASSNAME + ".requestSocAiProcess"; |
93 | | - try { |
94 | | - List<UtmAlertSocaiProcessingRequest> ids = alertIds.stream().map(UtmAlertSocaiProcessingRequest::new) |
95 | | - .collect(Collectors.toList()); |
96 | | - socaiProcessingRequestService.saveAll(ids); |
| 86 | + return new OkHttpClient.Builder() |
| 87 | + .sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustAllCerts[0]) |
| 88 | + .hostnameVerifier((hostname, session) -> true) |
| 89 | + .connectTimeout(10, TimeUnit.SECONDS) |
| 90 | + .writeTimeout(10, TimeUnit.SECONDS) |
| 91 | + .readTimeout(30, TimeUnit.SECONDS) |
| 92 | + .build(); |
97 | 93 | } catch (Exception e) { |
98 | | - throw new RuntimeException(ctx + ": " + e.getLocalizedMessage()); |
| 94 | + throw new RuntimeException("Failed to create SSL client: " + e.getMessage()); |
99 | 95 | } |
100 | 96 | } |
101 | 97 | } |
0 commit comments