Skip to content

Commit

Permalink
entire custom logtype implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sbcd90 committed Jul 18, 2023
1 parent 485b2a4 commit 10f5bf1
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -294,20 +294,24 @@ public void onResponse(SearchResponse response) {
bulkRequest.add(indexRequest);
}

Check warning on line 295 in src/main/java/org/opensearch/securityanalytics/logtype/LogTypeService.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/logtype/LogTypeService.java#L291-L295

Added lines #L291 - L295 were not covered by tests

bulkRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
logger.info("Indexing [" + bulkRequest.numberOfActions() + "] customLogTypes");
client.bulk(
bulkRequest,
ActionListener.delegateFailure(listener, (l, r) -> {
if (r.hasFailures()) {
logger.error("Custom LogType Bulk Index had failures:\n ", r.buildFailureMessage());
listener.onFailure(new IllegalStateException(r.buildFailureMessage()));
} else {
logger.info("Loaded [" + r.getItems().length + "] customLogType docs successfully!");
listener.onResponse(null);
}
})
);
if (bulkRequest.numberOfActions() > 0) {
bulkRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
logger.info("Indexing [" + bulkRequest.numberOfActions() + "] customLogTypes");
client.bulk(

Check warning on line 300 in src/main/java/org/opensearch/securityanalytics/logtype/LogTypeService.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/logtype/LogTypeService.java#L298-L300

Added lines #L298 - L300 were not covered by tests
bulkRequest,
ActionListener.delegateFailure(listener, (l, r) -> {

Check warning on line 302 in src/main/java/org/opensearch/securityanalytics/logtype/LogTypeService.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/logtype/LogTypeService.java#L302

Added line #L302 was not covered by tests
if (r.hasFailures()) {
logger.error("Custom LogType Bulk Index had failures:\n ", r.buildFailureMessage());
listener.onFailure(new IllegalStateException(r.buildFailureMessage()));

Check warning on line 305 in src/main/java/org/opensearch/securityanalytics/logtype/LogTypeService.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/logtype/LogTypeService.java#L304-L305

Added lines #L304 - L305 were not covered by tests
} else {
logger.info("Loaded [" + r.getItems().length + "] customLogType docs successfully!");
listener.onResponse(null);

Check warning on line 308 in src/main/java/org/opensearch/securityanalytics/logtype/LogTypeService.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/logtype/LogTypeService.java#L307-L308

Added lines #L307 - L308 were not covered by tests
}
})

Check warning on line 310 in src/main/java/org/opensearch/securityanalytics/logtype/LogTypeService.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/logtype/LogTypeService.java#L310

Added line #L310 was not covered by tests
);
} else {
listener.onResponse(null);
}
} catch (URISyntaxException | IOException e) {
listener.onFailure(e);

Check warning on line 316 in src/main/java/org/opensearch/securityanalytics/logtype/LogTypeService.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/logtype/LogTypeService.java#L315-L316

Added lines #L315 - L316 were not covered by tests
}
Expand Down Expand Up @@ -480,13 +484,13 @@ public void onFailure(Exception e) {
listener,
(delegatedListener, unused) -> {
isConfigIndexInitialized = true;
delegatedListener.onResponse(null);
doIndexLogTypeMetadata(listener);

Check warning on line 487 in src/main/java/org/opensearch/securityanalytics/logtype/LogTypeService.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/logtype/LogTypeService.java#L487

Added line #L487 was not covered by tests
})
);
}));
} else {
if (isConfigIndexInitialized) {
listener.onResponse(null);
doIndexLogTypeMetadata(listener);
return;
}
loadBuiltinLogTypes(ActionListener.delegateFailure(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public class TransportIndexCustomLogTypeAction extends HandledTransportAction<In

private final CustomLogTypeIndices customLogTypeIndices;

private final LogTypeService logTypeService;

private volatile Boolean filterByEnabled;

private volatile TimeValue indexTimeout;
Expand All @@ -80,6 +82,7 @@ public TransportIndexCustomLogTypeAction(TransportService transportService,
ActionFilters actionFilters,
ClusterService clusterService,
CustomLogTypeIndices customLogTypeIndices,
LogTypeService logTypeService,
Settings settings,
ThreadPool threadPool) {
super(IndexCustomLogTypeAction.NAME, transportService, actionFilters, IndexCustomLogTypeRequest::new);
Expand All @@ -88,6 +91,7 @@ public TransportIndexCustomLogTypeAction(TransportService transportService,
this.threadPool = threadPool;
this.settings = settings;
this.customLogTypeIndices = customLogTypeIndices;
this.logTypeService = logTypeService;
this.filterByEnabled = SecurityAnalyticsSettings.FILTER_BY_BACKEND_ROLES.get(this.settings);
this.indexTimeout = SecurityAnalyticsSettings.INDEX_TIMEOUT.get(this.settings);

Check warning on line 96 in src/main/java/org/opensearch/securityanalytics/transport/TransportIndexCustomLogTypeAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/transport/TransportIndexCustomLogTypeAction.java#L88-L96

Added lines #L88 - L96 were not covered by tests

Expand Down Expand Up @@ -326,48 +330,58 @@ public void onFailure(Exception e) {
}

Check warning on line 330 in src/main/java/org/opensearch/securityanalytics/transport/TransportIndexCustomLogTypeAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/transport/TransportIndexCustomLogTypeAction.java#L329-L330

Added lines #L329 - L330 were not covered by tests
});
} else {
MaxAggregationBuilder queryBuilder = AggregationBuilders.max("agg").field("tags.correlation_id");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.aggregation(queryBuilder);
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices(LogTypeService.LOG_TYPE_INDEX);
searchRequest.source(searchSourceBuilder);

client.search(searchRequest, new ActionListener<>() {
logTypeService.ensureConfigIndexIsInitialized(new ActionListener<Void>() {

Check warning on line 333 in src/main/java/org/opensearch/securityanalytics/transport/TransportIndexCustomLogTypeAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/transport/TransportIndexCustomLogTypeAction.java#L333

Added line #L333 was not covered by tests
@Override
public void onResponse(SearchResponse response) {
if (response.isTimedOut()) {
onFailures(new OpenSearchStatusException(response.toString(), RestStatus.INTERNAL_SERVER_ERROR));
return;
}

try {
Max agg = response.getAggregations().get("agg");
int value = Double.valueOf(agg.getValue()).intValue();
request.getCustomLogType().setTags(Map.of("correlation_id", value+1));
IndexRequest indexRequest = new IndexRequest(LogTypeService.LOG_TYPE_INDEX)
.setRefreshPolicy(request.getRefreshPolicy())
.source(request.getCustomLogType().toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS))
.timeout(indexTimeout);

client.index(indexRequest, new ActionListener<>() {
@Override
public void onResponse(IndexResponse response) {
if (response.status() != RestStatus.CREATED) {
onFailures(new OpenSearchStatusException(String.format(Locale.getDefault(), "Log Type with id %s cannot be updated", logTypeId), RestStatus.INTERNAL_SERVER_ERROR));
}
request.getCustomLogType().setId(response.getId());
onOperation(response, request.getCustomLogType());
public void onResponse(Void unused) {
MaxAggregationBuilder queryBuilder = AggregationBuilders.max("agg").field("tags.correlation_id");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.aggregation(queryBuilder);
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices(LogTypeService.LOG_TYPE_INDEX);
searchRequest.source(searchSourceBuilder);

Check warning on line 341 in src/main/java/org/opensearch/securityanalytics/transport/TransportIndexCustomLogTypeAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/transport/TransportIndexCustomLogTypeAction.java#L336-L341

Added lines #L336 - L341 were not covered by tests

client.search(searchRequest, new ActionListener<>() {

Check warning on line 343 in src/main/java/org/opensearch/securityanalytics/transport/TransportIndexCustomLogTypeAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/transport/TransportIndexCustomLogTypeAction.java#L343

Added line #L343 was not covered by tests
@Override
public void onResponse(SearchResponse response) {
if (response.isTimedOut()) {
onFailures(new OpenSearchStatusException(response.toString(), RestStatus.INTERNAL_SERVER_ERROR));
return;

Check warning on line 348 in src/main/java/org/opensearch/securityanalytics/transport/TransportIndexCustomLogTypeAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/transport/TransportIndexCustomLogTypeAction.java#L347-L348

Added lines #L347 - L348 were not covered by tests
}

@Override
public void onFailure(Exception e) {
onFailures(e);
try {
Max agg = response.getAggregations().get("agg");
int value = Double.valueOf(agg.getValue()).intValue();
request.getCustomLogType().setTags(Map.of("correlation_id", value+1));
IndexRequest indexRequest = new IndexRequest(LogTypeService.LOG_TYPE_INDEX)
.setRefreshPolicy(request.getRefreshPolicy())
.source(request.getCustomLogType().toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS))
.timeout(indexTimeout);

Check warning on line 358 in src/main/java/org/opensearch/securityanalytics/transport/TransportIndexCustomLogTypeAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/transport/TransportIndexCustomLogTypeAction.java#L352-L358

Added lines #L352 - L358 were not covered by tests

client.index(indexRequest, new ActionListener<>() {

Check warning on line 360 in src/main/java/org/opensearch/securityanalytics/transport/TransportIndexCustomLogTypeAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/transport/TransportIndexCustomLogTypeAction.java#L360

Added line #L360 was not covered by tests
@Override
public void onResponse(IndexResponse response) {
if (response.status() != RestStatus.CREATED) {
onFailures(new OpenSearchStatusException(String.format(Locale.getDefault(), "Log Type with id %s cannot be updated", logTypeId), RestStatus.INTERNAL_SERVER_ERROR));

Check warning on line 364 in src/main/java/org/opensearch/securityanalytics/transport/TransportIndexCustomLogTypeAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/transport/TransportIndexCustomLogTypeAction.java#L364

Added line #L364 was not covered by tests
}
request.getCustomLogType().setId(response.getId());
onOperation(response, request.getCustomLogType());
}

Check warning on line 368 in src/main/java/org/opensearch/securityanalytics/transport/TransportIndexCustomLogTypeAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/transport/TransportIndexCustomLogTypeAction.java#L366-L368

Added lines #L366 - L368 were not covered by tests

@Override
public void onFailure(Exception e) {
onFailures(e);
}

Check warning on line 373 in src/main/java/org/opensearch/securityanalytics/transport/TransportIndexCustomLogTypeAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/transport/TransportIndexCustomLogTypeAction.java#L372-L373

Added lines #L372 - L373 were not covered by tests
});
} catch (IOException ex) {
onFailures(ex);
}
});
} catch (IOException ex) {
onFailures(ex);
}
}

Check warning on line 378 in src/main/java/org/opensearch/securityanalytics/transport/TransportIndexCustomLogTypeAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/transport/TransportIndexCustomLogTypeAction.java#L375-L378

Added lines #L375 - L378 were not covered by tests

@Override
public void onFailure(Exception e) {
onFailures(e);
}

Check warning on line 383 in src/main/java/org/opensearch/securityanalytics/transport/TransportIndexCustomLogTypeAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/transport/TransportIndexCustomLogTypeAction.java#L382-L383

Added lines #L382 - L383 were not covered by tests
});
}

Check warning on line 385 in src/main/java/org/opensearch/securityanalytics/transport/TransportIndexCustomLogTypeAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/transport/TransportIndexCustomLogTypeAction.java#L385

Added line #L385 was not covered by tests

@Override
Expand Down

0 comments on commit 10f5bf1

Please sign in to comment.