-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathJobScheduler.java
More file actions
417 lines (349 loc) · 17.5 KB
/
JobScheduler.java
File metadata and controls
417 lines (349 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package com.paladincloud.jobscheduler.service;
import com.amazonaws.auth.BasicSessionCredentials;
import com.amazonaws.services.directory.model.AuthenticationFailedException;
import com.paladincloud.jobscheduler.auth.CredentialProvider;
import com.paladincloud.jobscheduler.config.ConfigUtil;
import com.paladincloud.jobscheduler.schema.jobs_and_rule_scheduling.Event;
import com.paladincloud.jobscheduler.schema.jobs_and_rule_scheduling.marshaller.Marshaller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.eventbridge.EventBridgeClient;
import software.amazon.awssdk.services.eventbridge.model.*;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
@Service
public class JobScheduler {
private static final Logger logger = LoggerFactory.getLogger(JobScheduler.class);
private static final String EVENT_SOURCE = "paladincloud.jobs-scheduler";
private static final String EVENT_DETAIL_TYPE = "Paladin Cloud Job Scheduling Event";
public static final String AZURE_ENABLED = "azure.enabled";
public static final String GCP_ENABLED = "gcp.enabled";
public static final String AWS_ENABLED = "aws.enabled";
public static final String QUALYS_ENABLED = "qualys.enabled";
public static final String AQUA_ENABLED = "aqua.enabled";
public static final String TENABLE_ENABLED = "tenable.enabled";
public static final String CONTRAST_ENABLED = "contrast.enabled";
public static final String PLUGIN_TYPE_TENABLE = "tenable";
public static final String PLUGIN_TYPE_QUALYS = "qualys";
public static final String PLUGIN_TYPE_AQUA = "aqua";
public static final String PLUGIN_TYPE_CONTRAST = "contrast";
public static final String REQUEST_ENTRY = "Request entry: {} ";
protected static final List<String> ALL_CLOUDS_LIST = Arrays.asList("aws","azure","gcp");
public static final String FAILED_WITH_ERROR_CODE = "Injection failed with Error Code: {} ";
public static final String EVENT_ID = "Event Id: {} ";
public static final String CURRENT_MILLISECONDS = "Current milliseconds: {}";
public static final String PLUGIN_TYPE_GCP = "gcp";
public static final String PLUGIN_TYPE_REDHAT = "redhat";
@Autowired
CredentialProvider credentialProvider;
@Value("${aws.eventbridge.bus.details}")
private String awsBusDetails;
@Value("${gcp.eventbridge.bus.details}")
private String gcpBusDetails;
@Value("${azure.eventbridge.bus.details}")
private String azureBusDetails;
@Value("${vulnerability.eventbridge.bus.details}")
private String vulnerabilityBusDetails;
private boolean azureEnabled;
private boolean gcpEnabled;
private boolean awsEnabled;
private boolean qualysEnabled;
private boolean aquaEnabled;
private boolean tenableEnabled;
@Value("${base.region}")
private String region;
@Value("${base.account}")
private String baseAccount;
@Value("${scheduler.role}")
private String roleName;
@Value("${plugins.in.v1}")
private String pluginUsingV1;
@Value("#{'${composite.plugins:}'.split(',')}")
private Set<String> compositePlugins;
@Autowired
private DataCollectorSQSService dataCollectorSQSServic;
@Autowired
private Environment env;
@Scheduled(initialDelayString = "${scheduler.collector.initial.delay}", fixedDelayString = "${scheduler.interval}")
public void scheduleCollectorJobs() {
// print the current milliseconds
logger.info("Current milliseconds: {} ", System.currentTimeMillis());
logger.info("Job Scheduler for collector is running...");
EventBridgeClient eventBrClient = getEventBridgeClient();
List<PutEventsRequestEntry> putEventsRequestEntries = new ArrayList<>();
try {
ConfigUtil.setConfigProperties();
azureEnabled = Boolean.parseBoolean(env.getProperty(AZURE_ENABLED));
awsEnabled = Boolean.parseBoolean(env.getProperty(AWS_ENABLED));
boolean compositePluginEnabled = isCompositeEnabled();
if (!compositePluginEnabled && awsEnabled) {
addCollectorEvent(putEventsRequestEntries, awsBusDetails);
}
if (!compositePluginEnabled && azureEnabled) {
addCollectorEvent(putEventsRequestEntries, azureBusDetails);
}
// Sending SQS message to trigger Data-Collector
String[] plugins = pluginUsingV1.split(",");
List<String> configuredPlugins = dataCollectorSQSServic.pluginsUsingVersion1AndConfigured(plugins);
for (String plugin : configuredPlugins) {
if (compositePluginEnabled && "gcp".equalsIgnoreCase(plugin)) {
continue;
}
dataCollectorSQSServic.sendSQSMessage(plugin);
}
if (!putEventsRequestEntries.isEmpty()) {
PutEventsRequest eventsRequest = PutEventsRequest.builder().entries(putEventsRequestEntries)
.build();
PutEventsResponse result = eventBrClient.putEvents(eventsRequest);
for (PutEventsResultEntry resultEntry : result.entries()) {
if (resultEntry.eventId() != null) {
logger.info(EVENT_ID, resultEntry.eventId());
} else {
logger.info(FAILED_WITH_ERROR_CODE, resultEntry.errorCode());
}
}
}
} catch (EventBridgeException e) {
logger.error(e.awsErrorDetails().errorMessage());
System.exit(1);
} catch (Exception e) {
logger.error(e.getMessage());
System.exit(1);
}
eventBrClient.close();
}
private void putPluginRuleRequestEntries(int batchNo, String busDetails, List<PutEventsRequestEntry> reqEntryList, String pluginType) {
String detailString = null;
//plugin-saasdev-all-clouds:2
//Generate event for all clouds for plugin
for(String cloudType:ALL_CLOUDS_LIST) {
String cloudName = pluginType+"-"+cloudType;
Event event = populateEventForRule(cloudName, batchNo);
detailString = getMarshalledEvent(detailString, event);
PutEventsRequestEntry reqEntry = PutEventsRequestEntry.builder().source(EVENT_SOURCE).detailType(EVENT_DETAIL_TYPE).detail(detailString).eventBusName(busDetails.split(":")[0]).build();
// print the request entry
logger.info(REQUEST_ENTRY, reqEntry);
// Add the PutEventsRequestEntry to a putEventsRequestEntries
reqEntryList.add(reqEntry);
}
}
private void putPluginRuleRequestEntriesForContrast(int batchNo, String busDetails, List<PutEventsRequestEntry> reqEntryList) {
String detailString = null;
Event event = populateEventForRule(JobScheduler.PLUGIN_TYPE_CONTRAST, batchNo);
detailString = getMarshalledEvent(detailString, event);
PutEventsRequestEntry reqEntry = PutEventsRequestEntry.builder().source(EVENT_SOURCE).detailType(
EVENT_DETAIL_TYPE).detail(detailString).eventBusName(busDetails.split(":")[0]).build();
// print the request entry
logger.info(REQUEST_ENTRY, reqEntry);
// Add the PutEventsRequestEntry to a putEventsRequestEntries
reqEntryList.add(reqEntry);
}
@Scheduled(initialDelayString = "${vulnerability.collector.initial.delay}", fixedDelayString = "${vulnerability.interval}")
public void schedulePluginCollectorJobs() {
// print the current milliseconds
logger.info("Current milliseconds: {} ", System.currentTimeMillis());
logger.info("Job Scheduler for custom plugin is running...");
EventBridgeClient eventBrClient = getEventBridgeClient();
List<PutEventsRequestEntry> putEventsRequestEntries = new ArrayList<>();
try {
ConfigUtil.setConfigProperties();
qualysEnabled=Boolean.parseBoolean(env.getProperty(QUALYS_ENABLED));
aquaEnabled=Boolean.parseBoolean(env.getProperty(AQUA_ENABLED));
tenableEnabled=Boolean.parseBoolean(env.getProperty(TENABLE_ENABLED));
if (qualysEnabled) {
addPluginCollectorEvent(putEventsRequestEntries, vulnerabilityBusDetails, PLUGIN_TYPE_QUALYS);
}
if (aquaEnabled) {
addPluginCollectorEvent(putEventsRequestEntries, vulnerabilityBusDetails, PLUGIN_TYPE_AQUA);
}
if (tenableEnabled) {
addPluginCollectorEvent(putEventsRequestEntries, vulnerabilityBusDetails, PLUGIN_TYPE_TENABLE);
}
// check if events to put is > 0
if (!putEventsRequestEntries.isEmpty()) {
PutEventsRequest eventsRequest = PutEventsRequest.builder().entries(putEventsRequestEntries).build();
PutEventsResponse result = eventBrClient.putEvents(eventsRequest);
for (PutEventsResultEntry resultEntry : result.entries()) {
if (resultEntry.eventId() != null) {
logger.info(EVENT_ID, resultEntry.eventId());
} else {
logger.info(FAILED_WITH_ERROR_CODE, resultEntry.errorCode());
}
}
}
} catch (EventBridgeException e) {
logger.error(e.awsErrorDetails().errorMessage());
System.exit(1);
} catch (Exception e) {
logger.error(e.getMessage());
System.exit(1);
}
eventBrClient.close();
}
public void schedulePluginShipperJobs() {
// print the current milliseconds
logger.info(CURRENT_MILLISECONDS, System.currentTimeMillis());
logger.info("Job Scheduler for plugin shipper is running...");
EventBridgeClient eventBrClient = getEventBridgeClient();
List<PutEventsRequestEntry> putEventsRequestEntries = new ArrayList<>();
try {
ConfigUtil.setConfigProperties();
qualysEnabled = Boolean.parseBoolean(System.getProperty(QUALYS_ENABLED));
if (qualysEnabled) {
addPluginShipperEvent(putEventsRequestEntries, vulnerabilityBusDetails, PLUGIN_TYPE_QUALYS);
}
if (!putEventsRequestEntries.isEmpty()) {
PutEventsRequest eventsRequest = PutEventsRequest.builder().entries(putEventsRequestEntries).build();
PutEventsResponse result = eventBrClient.putEvents(eventsRequest);
for (PutEventsResultEntry resultEntry : result.entries()) {
if (resultEntry.eventId() != null) {
logger.info(EVENT_ID, resultEntry.eventId());
} else {
logger.info(FAILED_WITH_ERROR_CODE, resultEntry.errorCode());
}
}
}
} catch (EventBridgeException e) {
logger.error(e.awsErrorDetails().errorMessage());
System.exit(1);
} catch (Exception e) {
logger.error(e.getMessage());
System.exit(1);
}
eventBrClient.close();
}
private void addCollectorEvent(List<PutEventsRequestEntry> putEventsRequestEntries, String busDetails) {
String detailString = null;
// populate events for each event bus
String[] busDetailsArray = busDetails.split(",");
for (String busDetail : busDetailsArray) {
String cloudName = busDetail.split(":")[0].split("-")[1];
Event event = populateEventForCollector(cloudName);
detailString = getMarshalledEvent(detailString, event);
PutEventsRequestEntry reqEntry = PutEventsRequestEntry.builder().source(EVENT_SOURCE).detailType(EVENT_DETAIL_TYPE).detail(detailString).eventBusName(busDetail.split(":")[0]).build();
// Add the PutEventsRequestEntry to a putEventsRequestEntries
putEventsRequestEntries.add(reqEntry);
// print the request entry
logger.info(REQUEST_ENTRY, reqEntry);
}
}
private void addPluginCollectorEvent(List<PutEventsRequestEntry> putEventsRequestEntries, String busDetails, String pluginType) {
String detailString = null;
//For custom plugins like qualys, aqua, tenable- "aqua-saasdev-aws_gcp_azure:145"
// populate events for each event bus
String[] busDetailsArray = busDetails.split(",");
for (String busDetail : busDetailsArray) {
String customBusDetail = busDetail.split(":")[0];
for(String cloudType:ALL_CLOUDS_LIST){
String cloudName = pluginType+"-"+cloudType;
Event event = populateEventForCollector(cloudName);
detailString = getMarshalledEvent(detailString, event);
PutEventsRequestEntry reqEntry = PutEventsRequestEntry.builder().source(EVENT_SOURCE).detailType(EVENT_DETAIL_TYPE).detail(detailString).eventBusName(customBusDetail).build();
// Add the PutEventsRequestEntry to a putEventsRequestEntries
putEventsRequestEntries.add(reqEntry);
// print the request entry
logger.info(REQUEST_ENTRY, reqEntry);
}
}
}
private void addPluginShipperEvent(List<PutEventsRequestEntry> putEventsRequestEntries, String busDetails, String pluginType) {
String detailString = null;
// populate events for each event bus
String[] busDetailsArray = busDetails.split(",");
for (String busDetail : busDetailsArray) {
String customBusDetail = busDetail.split(":")[0];
for(String cloudType:ALL_CLOUDS_LIST) {
String cloudName = pluginType+"-"+cloudType;
Event event = populateEventForShipper(cloudName);
detailString = getMarshalledEvent(detailString, event);
PutEventsRequestEntry reqEntry = PutEventsRequestEntry.builder().source(EVENT_SOURCE).detailType(EVENT_DETAIL_TYPE).detail(detailString).eventBusName(customBusDetail).build();
// Add the PutEventsRequestEntry to a putEventsRequestEntries
putEventsRequestEntries.add(reqEntry);
// print the request entry
logger.info(REQUEST_ENTRY, reqEntry);
}
}
}
private EventBridgeClient getEventBridgeClient() {
Region reg = Region.of(region);
BasicSessionCredentials tempCredentials = null;
try {
tempCredentials = credentialProvider.getCredentials(this.baseAccount, roleName);
} catch (Exception e) {
logger.error("{\"errcode\":\"NO_CRED\" , \"account\":\"" + this.baseAccount + "\", \"Message\":\"Error getting credentials for account " + this.baseAccount + "\" , \"cause\":\"" + e.getMessage() + "\"}");
// ErrorManageUtil.uploadError(accountId, "all", "all", e.getMessage());
}
if (tempCredentials == null) {
throw new AuthenticationFailedException("can not get the temp credentials!!");
}
return EventBridgeClient.builder()
.region(reg)
.credentialsProvider(StaticCredentialsProvider
.create(AwsSessionCredentials
.create(tempCredentials.getAWSAccessKeyId(), tempCredentials.getAWSSecretKey(), tempCredentials.getSessionToken())))
.build();
}
private String getMarshalledEvent(String detailString, Event event) {
try {
detailString = Marshaller.marshal(event);
} catch (IOException e) {
//Failed to serialise the event as a JSON formatted string. Let's quit.
e.printStackTrace();
System.exit(1);
}
return detailString;
}
private Event populateEventForCollector(String cloudType) {
Event event = new Event();
event.setBatchNo(BigDecimal.valueOf(1));
event.setCloudName(cloudType);
event.setSubmitJob(true);
event.setIsRule(false);
event.setIsCollector(true);
event.setIsShipper(false);
return event;
}
private Event populateEventForShipper(String cloudType) {
Event event = new Event();
event.setBatchNo(BigDecimal.valueOf(1));
event.setCloudName(cloudType);
event.setSubmitJob(true);
event.setIsRule(false);
event.setIsCollector(false);
event.setIsShipper(true);
return event;
}
private Event populateEventForRule(String cloudType, int batchNo) {
Event event = new Event();
event.setBatchNo(BigDecimal.valueOf(batchNo));
event.setCloudName(cloudType);
event.setSubmitJob(true);
event.setIsRule(true);
event.setIsCollector(false);
event.setIsShipper(false);
return event;
}
public boolean isCompositeEnabled() {
for (String plugin : compositePlugins) {
String isEnabledKey = plugin + ".enabled";
String isEnabledValue = env.getProperty(isEnabledKey);
if (isEnabledValue != null && (isEnabledValue.equalsIgnoreCase("true") ||
isEnabledValue.equalsIgnoreCase("1"))) {
return true;
}
}
return false;
}
}