Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom log type implementation #500

Merged
merged 1 commit into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.opensearch.securityanalytics.logtype.LogTypeService;
import org.opensearch.securityanalytics.mapper.IndexTemplateManager;
import org.opensearch.securityanalytics.mapper.MapperService;
import org.opensearch.securityanalytics.model.CustomLogType;
import org.opensearch.securityanalytics.resthandler.*;
import org.opensearch.securityanalytics.transport.*;
import org.opensearch.securityanalytics.model.Rule;
Expand All @@ -66,6 +67,7 @@
import org.opensearch.securityanalytics.settings.SecurityAnalyticsSettings;
import org.opensearch.securityanalytics.util.CorrelationIndices;
import org.opensearch.securityanalytics.util.CorrelationRuleIndices;
import org.opensearch.securityanalytics.util.CustomLogTypeIndices;
import org.opensearch.securityanalytics.util.DetectorIndices;
import org.opensearch.securityanalytics.util.RuleIndices;
import org.opensearch.securityanalytics.util.RuleTopicIndices;
Expand All @@ -87,6 +89,8 @@
public static final String LIST_CORRELATIONS_URI = PLUGINS_BASE_URI + "/correlations";
public static final String CORRELATION_RULES_BASE_URI = PLUGINS_BASE_URI + "/correlation/rules";

public static final String CUSTOM_LOG_TYPE_URI = PLUGINS_BASE_URI + "/logtype";

private CorrelationRuleIndices correlationRuleIndices;

private DetectorIndices detectorIndices;
Expand All @@ -95,6 +99,8 @@

private CorrelationIndices correlationIndices;

private CustomLogTypeIndices customLogTypeIndices;

private MapperService mapperService;

private RuleIndices ruleIndices;
Expand Down Expand Up @@ -126,14 +132,15 @@
detectorIndices = new DetectorIndices(client.admin(), clusterService, threadPool);
ruleTopicIndices = new RuleTopicIndices(client, clusterService, logTypeService);
correlationIndices = new CorrelationIndices(client, clusterService);
customLogTypeIndices = new CustomLogTypeIndices(client.admin(), clusterService);

Check warning on line 135 in src/main/java/org/opensearch/securityanalytics/SecurityAnalyticsPlugin.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/SecurityAnalyticsPlugin.java#L135

Added line #L135 was not covered by tests
indexTemplateManager = new IndexTemplateManager(client, clusterService, indexNameExpressionResolver, xContentRegistry);
mapperService = new MapperService(client, clusterService, indexNameExpressionResolver, indexTemplateManager, logTypeService);
ruleIndices = new RuleIndices(logTypeService, client, clusterService, threadPool);
correlationRuleIndices = new CorrelationRuleIndices(client, clusterService);
this.client = client;

return List.of(
detectorIndices, correlationIndices, correlationRuleIndices, ruleTopicIndices, ruleIndices,
detectorIndices, correlationIndices, correlationRuleIndices, ruleTopicIndices, customLogTypeIndices, ruleIndices,
mapperService, indexTemplateManager, builtinLogTypeLoader
);
}
Expand Down Expand Up @@ -172,7 +179,10 @@
new RestIndexCorrelationRuleAction(),
new RestDeleteCorrelationRuleAction(),
new RestListCorrelationAction(),
new RestSearchCorrelationRuleAction()
new RestSearchCorrelationRuleAction(),
new RestIndexCustomLogTypeAction(),
new RestSearchCustomLogTypeAction(),
new RestDeleteCustomLogTypeAction()
);
}

Expand All @@ -181,7 +191,8 @@
return List.of(
Detector.XCONTENT_REGISTRY,
DetectorInput.XCONTENT_REGISTRY,
Rule.XCONTENT_REGISTRY
Rule.XCONTENT_REGISTRY,
CustomLogType.XCONTENT_REGISTRY
);
}

Expand Down Expand Up @@ -258,7 +269,10 @@
new ActionPlugin.ActionHandler<>(DeleteCorrelationRuleAction.INSTANCE, TransportDeleteCorrelationRuleAction.class),
new ActionPlugin.ActionHandler<>(AlertingActions.SUBSCRIBE_FINDINGS_ACTION_TYPE, TransportCorrelateFindingAction.class),
new ActionPlugin.ActionHandler<>(ListCorrelationsAction.INSTANCE, TransportListCorrelationAction.class),
new ActionPlugin.ActionHandler<>(SearchCorrelationRuleAction.INSTANCE, TransportSearchCorrelationRuleAction.class)
new ActionPlugin.ActionHandler<>(SearchCorrelationRuleAction.INSTANCE, TransportSearchCorrelationRuleAction.class),
new ActionHandler<>(IndexCustomLogTypeAction.INSTANCE, TransportIndexCustomLogTypeAction.class),
new ActionHandler<>(SearchCustomLogTypeAction.INSTANCE, TransportSearchCustomLogTypeAction.class),
new ActionHandler<>(DeleteCustomLogTypeAction.INSTANCE, TransportDeleteCustomLogTypeAction.class)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@

public class CorrelatedFindingRequest extends ActionRequest {

private Detector.DetectorType detectorType;
private String detectorType;

private String findingId;

private long timeWindow;

private int noOfNearbyFindings;

public CorrelatedFindingRequest(String findingId, Detector.DetectorType detectorType, long timeWindow, int noOfNearbyFindings) {
public CorrelatedFindingRequest(String findingId, String detectorType, long timeWindow, int noOfNearbyFindings) {
super();
this.findingId = findingId;
this.detectorType = detectorType;
Expand All @@ -33,7 +33,7 @@
public CorrelatedFindingRequest(StreamInput sin) throws IOException {
this(
sin.readString(),
sin.readEnum(Detector.DetectorType.class),
sin.readString(),

Check warning on line 36 in src/main/java/org/opensearch/securityanalytics/action/CorrelatedFindingRequest.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/CorrelatedFindingRequest.java#L36

Added line #L36 was not covered by tests
sin.readLong(),
sin.readInt()
);
Expand All @@ -47,7 +47,7 @@
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(findingId);
out.writeEnum(detectorType);
out.writeString(detectorType);

Check warning on line 50 in src/main/java/org/opensearch/securityanalytics/action/CorrelatedFindingRequest.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/CorrelatedFindingRequest.java#L50

Added line #L50 was not covered by tests
out.writeLong(timeWindow);
out.writeInt(noOfNearbyFindings);
}
Expand All @@ -56,7 +56,7 @@
return findingId;
}

public Detector.DetectorType getDetectorType() {
public String getDetectorType() {
return detectorType;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.securityanalytics.action;

import org.opensearch.action.ActionType;

public class DeleteCustomLogTypeAction extends ActionType<DeleteCustomLogTypeResponse> {

public static final DeleteCustomLogTypeAction INSTANCE = new DeleteCustomLogTypeAction();

Check warning on line 15 in src/main/java/org/opensearch/securityanalytics/action/DeleteCustomLogTypeAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/DeleteCustomLogTypeAction.java#L15

Added line #L15 was not covered by tests
public static final String NAME = "cluster:admin/opensearch/securityanalytics/logtype/delete";

public DeleteCustomLogTypeAction() {
super(NAME, DeleteCustomLogTypeResponse::new);
}

Check warning on line 20 in src/main/java/org/opensearch/securityanalytics/action/DeleteCustomLogTypeAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/DeleteCustomLogTypeAction.java#L19-L20

Added lines #L19 - L20 were not covered by tests
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.securityanalytics.action;

import org.opensearch.action.ActionRequest;
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.action.support.WriteRequest;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;

import java.io.IOException;

public class DeleteCustomLogTypeRequest extends ActionRequest {

private String logTypeId;

private WriteRequest.RefreshPolicy refreshPolicy;

public DeleteCustomLogTypeRequest(String logTypeId, WriteRequest.RefreshPolicy refreshPolicy) {
super();
this.logTypeId = logTypeId;
this.refreshPolicy = refreshPolicy;
}

Check warning on line 29 in src/main/java/org/opensearch/securityanalytics/action/DeleteCustomLogTypeRequest.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/DeleteCustomLogTypeRequest.java#L26-L29

Added lines #L26 - L29 were not covered by tests

public DeleteCustomLogTypeRequest(StreamInput sin) throws IOException {
this(sin.readString(),
WriteRequest.RefreshPolicy.readFrom(sin));
}

Check warning on line 34 in src/main/java/org/opensearch/securityanalytics/action/DeleteCustomLogTypeRequest.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/DeleteCustomLogTypeRequest.java#L32-L34

Added lines #L32 - L34 were not covered by tests

@Override
public ActionRequestValidationException validate() {
return null;

Check warning on line 38 in src/main/java/org/opensearch/securityanalytics/action/DeleteCustomLogTypeRequest.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/DeleteCustomLogTypeRequest.java#L38

Added line #L38 was not covered by tests
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(logTypeId);
refreshPolicy.writeTo(out);
}

Check warning on line 45 in src/main/java/org/opensearch/securityanalytics/action/DeleteCustomLogTypeRequest.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/DeleteCustomLogTypeRequest.java#L43-L45

Added lines #L43 - L45 were not covered by tests

public String getLogTypeId() {
return logTypeId;

Check warning on line 48 in src/main/java/org/opensearch/securityanalytics/action/DeleteCustomLogTypeRequest.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/DeleteCustomLogTypeRequest.java#L48

Added line #L48 was not covered by tests
}

public WriteRequest.RefreshPolicy getRefreshPolicy() {
return refreshPolicy;

Check warning on line 52 in src/main/java/org/opensearch/securityanalytics/action/DeleteCustomLogTypeRequest.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/DeleteCustomLogTypeRequest.java#L52

Added line #L52 was not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.securityanalytics.action;

import org.opensearch.action.ActionResponse;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.rest.RestStatus;

import java.io.IOException;

import static org.opensearch.securityanalytics.util.RestHandlerUtils._ID;
import static org.opensearch.securityanalytics.util.RestHandlerUtils._VERSION;

public class DeleteCustomLogTypeResponse extends ActionResponse implements ToXContentObject {

private String id;

private Long version;

private RestStatus status;

public DeleteCustomLogTypeResponse(String id, Long version, RestStatus status) {
super();
this.id = id;
this.version = version;
this.status = status;
}

Check warning on line 36 in src/main/java/org/opensearch/securityanalytics/action/DeleteCustomLogTypeResponse.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/DeleteCustomLogTypeResponse.java#L32-L36

Added lines #L32 - L36 were not covered by tests

public DeleteCustomLogTypeResponse(StreamInput sin) throws IOException {
this(
sin.readString(),
sin.readLong(),
sin.readEnum(RestStatus.class)

Check warning on line 42 in src/main/java/org/opensearch/securityanalytics/action/DeleteCustomLogTypeResponse.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/DeleteCustomLogTypeResponse.java#L39-L42

Added lines #L39 - L42 were not covered by tests
);
}

Check warning on line 44 in src/main/java/org/opensearch/securityanalytics/action/DeleteCustomLogTypeResponse.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/DeleteCustomLogTypeResponse.java#L44

Added line #L44 was not covered by tests

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject()
.field(_ID, id)
.field(_VERSION, version);
return builder.endObject();

Check warning on line 51 in src/main/java/org/opensearch/securityanalytics/action/DeleteCustomLogTypeResponse.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/DeleteCustomLogTypeResponse.java#L48-L51

Added lines #L48 - L51 were not covered by tests
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(id);
out.writeLong(version);
}

Check warning on line 58 in src/main/java/org/opensearch/securityanalytics/action/DeleteCustomLogTypeResponse.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/DeleteCustomLogTypeResponse.java#L56-L58

Added lines #L56 - L58 were not covered by tests
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.securityanalytics.action;

import org.opensearch.action.ActionType;

public class IndexCustomLogTypeAction extends ActionType<IndexCustomLogTypeResponse> {

public static final IndexCustomLogTypeAction INSTANCE = new IndexCustomLogTypeAction();

Check warning on line 15 in src/main/java/org/opensearch/securityanalytics/action/IndexCustomLogTypeAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/IndexCustomLogTypeAction.java#L15

Added line #L15 was not covered by tests
public static final String NAME = "cluster:admin/opensearch/securityanalytics/logtype/write";

public IndexCustomLogTypeAction() {
super(NAME, IndexCustomLogTypeResponse::new);
}

Check warning on line 20 in src/main/java/org/opensearch/securityanalytics/action/IndexCustomLogTypeAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/IndexCustomLogTypeAction.java#L19-L20

Added lines #L19 - L20 were not covered by tests
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.securityanalytics.action;

import org.opensearch.action.ActionRequest;
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.action.support.WriteRequest;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.rest.RestRequest;
import org.opensearch.securityanalytics.model.CustomLogType;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IndexCustomLogTypeRequest extends ActionRequest {

private String logTypeId;

private WriteRequest.RefreshPolicy refreshPolicy;

private RestRequest.Method method;

private CustomLogType customLogType;

private static final Pattern IS_VALID_CUSTOM_LOG_NAME = Pattern.compile("[a-zA-Z0-9 _,-.]{5,50}");

Check warning on line 33 in src/main/java/org/opensearch/securityanalytics/action/IndexCustomLogTypeRequest.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/IndexCustomLogTypeRequest.java#L33

Added line #L33 was not covered by tests

public IndexCustomLogTypeRequest(
String logTypeId,
WriteRequest.RefreshPolicy refreshPolicy,
RestRequest.Method method,
CustomLogType customLogType
) {
super();
this.logTypeId = logTypeId;
this.refreshPolicy = refreshPolicy;
this.method = method;
this.customLogType = customLogType;
}

Check warning on line 46 in src/main/java/org/opensearch/securityanalytics/action/IndexCustomLogTypeRequest.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/IndexCustomLogTypeRequest.java#L41-L46

Added lines #L41 - L46 were not covered by tests

public IndexCustomLogTypeRequest(StreamInput sin) throws IOException {
this(
sin.readString(),
WriteRequest.RefreshPolicy.readFrom(sin),
sin.readEnum(RestRequest.Method.class),
CustomLogType.readFrom(sin)

Check warning on line 53 in src/main/java/org/opensearch/securityanalytics/action/IndexCustomLogTypeRequest.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/IndexCustomLogTypeRequest.java#L49-L53

Added lines #L49 - L53 were not covered by tests
);
}

Check warning on line 55 in src/main/java/org/opensearch/securityanalytics/action/IndexCustomLogTypeRequest.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/IndexCustomLogTypeRequest.java#L55

Added line #L55 was not covered by tests

@Override
public ActionRequestValidationException validate() {
Matcher matcher = IS_VALID_CUSTOM_LOG_NAME.matcher(customLogType.getName());
boolean find = matcher.matches();

Check warning on line 60 in src/main/java/org/opensearch/securityanalytics/action/IndexCustomLogTypeRequest.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/IndexCustomLogTypeRequest.java#L59-L60

Added lines #L59 - L60 were not covered by tests
if (!find) {
throw new ActionRequestValidationException();

Check warning on line 62 in src/main/java/org/opensearch/securityanalytics/action/IndexCustomLogTypeRequest.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/IndexCustomLogTypeRequest.java#L62

Added line #L62 was not covered by tests
}
return null;

Check warning on line 64 in src/main/java/org/opensearch/securityanalytics/action/IndexCustomLogTypeRequest.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/IndexCustomLogTypeRequest.java#L64

Added line #L64 was not covered by tests
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(logTypeId);
refreshPolicy.writeTo(out);
out.writeEnum(method);
customLogType.writeTo(out);
}

Check warning on line 73 in src/main/java/org/opensearch/securityanalytics/action/IndexCustomLogTypeRequest.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/IndexCustomLogTypeRequest.java#L69-L73

Added lines #L69 - L73 were not covered by tests

public String getLogTypeId() {
return logTypeId;

Check warning on line 76 in src/main/java/org/opensearch/securityanalytics/action/IndexCustomLogTypeRequest.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/IndexCustomLogTypeRequest.java#L76

Added line #L76 was not covered by tests
}

public WriteRequest.RefreshPolicy getRefreshPolicy() {
return refreshPolicy;

Check warning on line 80 in src/main/java/org/opensearch/securityanalytics/action/IndexCustomLogTypeRequest.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/IndexCustomLogTypeRequest.java#L80

Added line #L80 was not covered by tests
}

public RestRequest.Method getMethod() {
return method;

Check warning on line 84 in src/main/java/org/opensearch/securityanalytics/action/IndexCustomLogTypeRequest.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/IndexCustomLogTypeRequest.java#L84

Added line #L84 was not covered by tests
}

public CustomLogType getCustomLogType() {
return customLogType;

Check warning on line 88 in src/main/java/org/opensearch/securityanalytics/action/IndexCustomLogTypeRequest.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/securityanalytics/action/IndexCustomLogTypeRequest.java#L88

Added line #L88 was not covered by tests
}
}
Loading
Loading