-
Notifications
You must be signed in to change notification settings - Fork 77
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
Delete threat intel source config API #1066
Changes from 2 commits
4c92ffd
0f4fa85
f12483b
c259baf
39ac4a9
c8c2e06
950ed8c
56a9cfb
a4cda27
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.securityanalytics.threatIntel.action; | ||
|
||
import org.opensearch.action.ActionType; | ||
|
||
import static org.opensearch.securityanalytics.threatIntel.sacommons.IndexTIFSourceConfigAction.DELETE_TIF_SOURCE_CONFIG_ACTION_NAME; | ||
|
||
/** | ||
* Delete TIF Source Config Action | ||
*/ | ||
public class SADeleteTIFSourceConfigAction extends ActionType<SADeleteTIFSourceConfigResponse> { | ||
|
||
public static final SADeleteTIFSourceConfigAction INSTANCE = new SADeleteTIFSourceConfigAction(); | ||
public static final String NAME = DELETE_TIF_SOURCE_CONFIG_ACTION_NAME; | ||
private SADeleteTIFSourceConfigAction() { | ||
super(NAME, SADeleteTIFSourceConfigResponse::new); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.securityanalytics.threatIntel.action; | ||
|
||
import org.opensearch.action.ActionRequest; | ||
import org.opensearch.action.ActionRequestValidationException; | ||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
|
||
import java.io.IOException; | ||
import java.util.Locale; | ||
|
||
import static org.opensearch.action.ValidateActions.addValidationError; | ||
|
||
/** | ||
* Delete threat intel feed source config request | ||
*/ | ||
public class SADeleteTIFSourceConfigRequest extends ActionRequest { | ||
private final String id; | ||
public SADeleteTIFSourceConfigRequest(String id) { | ||
super(); | ||
this.id = id; | ||
} | ||
|
||
public SADeleteTIFSourceConfigRequest(StreamInput sin) throws IOException { | ||
this(sin.readString()); // id | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeString(id); | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
return null; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.securityanalytics.threatIntel.action; | ||
|
||
import org.opensearch.core.action.ActionResponse; | ||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
import org.opensearch.core.rest.RestStatus; | ||
import org.opensearch.core.xcontent.ToXContentObject; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.securityanalytics.threatIntel.model.SATIFSourceConfigDto; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.opensearch.securityanalytics.util.RestHandlerUtils._ID; | ||
import static org.opensearch.securityanalytics.util.RestHandlerUtils._VERSION; | ||
|
||
public class SADeleteTIFSourceConfigResponse extends ActionResponse implements ToXContentObject { | ||
private final String id; | ||
private final RestStatus status; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what status implies deleted? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if the delete response is |
||
|
||
public SADeleteTIFSourceConfigResponse(String id, RestStatus status) { | ||
super(); | ||
this.id = id; | ||
this.status = status; | ||
} | ||
|
||
public SADeleteTIFSourceConfigResponse(StreamInput sin) throws IOException { | ||
this( | ||
sin.readString(), // id | ||
sin.readEnum(RestStatus.class) // status | ||
); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeString(id); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject() | ||
.field(_ID, id); | ||
return builder.endObject(); | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
|
||
public RestStatus getStatus() { | ||
return status; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,6 @@ | |
public class Constants { | ||
public static final String USER_AGENT_KEY = "User-Agent"; | ||
public static final String USER_AGENT_VALUE = String.format(Locale.ROOT, "OpenSearch/%s vanilla", Version.CURRENT.toString()); | ||
public static final String SOURCE_CONFIG_ID = "source_config_id"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MEGA-NIT: threat_intel_source_config_id? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed to |
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.opensearch.securityanalytics.threatIntel.resthandler; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.client.node.NodeClient; | ||
import org.opensearch.rest.BaseRestHandler; | ||
import org.opensearch.rest.RestRequest; | ||
import org.opensearch.rest.action.RestToXContentListener; | ||
import org.opensearch.securityanalytics.SecurityAnalyticsPlugin; | ||
import org.opensearch.securityanalytics.threatIntel.action.SADeleteTIFSourceConfigAction; | ||
import org.opensearch.securityanalytics.threatIntel.action.SADeleteTIFSourceConfigRequest; | ||
import org.opensearch.securityanalytics.threatIntel.model.SATIFSourceConfigDto; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
import static org.opensearch.securityanalytics.threatIntel.common.Constants.SOURCE_CONFIG_ID; | ||
|
||
public class RestDeleteTIFSourceConfigAction extends BaseRestHandler { | ||
|
||
private static final Logger log = LogManager.getLogger(RestDeleteTIFSourceConfigAction.class); | ||
|
||
@Override | ||
public String getName() { | ||
return "delete_tif_config_action"; | ||
} | ||
|
||
@Override | ||
public List<Route> routes() { | ||
return List.of(new Route(RestRequest.Method.DELETE, String.format(Locale.getDefault(), "%s/{%s}", SecurityAnalyticsPlugin.THREAT_INTEL_SOURCE_URI, SOURCE_CONFIG_ID))); | ||
} | ||
|
||
@Override | ||
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { | ||
String SaTifSourceConfigId = request.param(SOURCE_CONFIG_ID, SATIFSourceConfigDto.NO_ID); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lower case letter to begin variable name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
|
||
if (SaTifSourceConfigId == null || SaTifSourceConfigId.isEmpty()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Stringutils.isBlank() instead There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed |
||
throw new IllegalArgumentException("missing id"); | ||
} | ||
|
||
SADeleteTIFSourceConfigRequest req = new SADeleteTIFSourceConfigRequest(SaTifSourceConfigId); | ||
|
||
return channel -> client.execute( | ||
SADeleteTIFSourceConfigAction.INSTANCE, | ||
req, | ||
new RestToXContentListener<>(channel) | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,6 +148,7 @@ public void deleteTIFSourceConfig( | |
final String SaTifSourceConfigId, | ||
final ActionListener<DeleteResponse> listener | ||
) { | ||
// TODO: Delete all IOCs associated with source config | ||
getTIFSourceConfig(SaTifSourceConfigId, ActionListener.wrap( | ||
SaTifSourceConfigDto -> { | ||
if (SaTifSourceConfigDto == null) { | ||
|
@@ -158,7 +159,8 @@ public void deleteTIFSourceConfig( | |
SATIFSourceConfig SaTifSourceConfig = convertToSATIFConfig(SaTifSourceConfigDto); | ||
SaTifSourceConfigService.deleteTIFSourceConfig(SaTifSourceConfig, ActionListener.wrap( | ||
deleteResponse -> { | ||
log.debug("Successfully deleted threat intel source config"); | ||
log.debug("Successfully deleted threat intel source config [{}]", SaTifSourceConfig.getId()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not blocking, but should we consider making this an info-level log? |
||
listener.onResponse(deleteResponse); | ||
}, e -> { | ||
log.error("Failed to delete threat intel source config [{}]", SaTifSourceConfigId); | ||
if (previousState.equals(SaTifSourceConfigDto.getState()) == false) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,6 @@ | |
import org.opensearch.ResourceAlreadyExistsException; | ||
import org.opensearch.action.StepListener; | ||
import org.opensearch.action.admin.indices.create.CreateIndexRequest; | ||
import org.opensearch.action.admin.indices.create.CreateIndexResponse; | ||
import org.opensearch.action.delete.DeleteRequest; | ||
import org.opensearch.action.delete.DeleteResponse; | ||
import org.opensearch.action.get.GetRequest; | ||
|
@@ -30,7 +29,6 @@ | |
import org.opensearch.core.xcontent.NamedXContentRegistry; | ||
import org.opensearch.core.xcontent.ToXContent; | ||
import org.opensearch.core.xcontent.XContentParser; | ||
import org.opensearch.extensions.AcknowledgedResponse; | ||
import org.opensearch.jobscheduler.spi.LockModel; | ||
import org.opensearch.securityanalytics.SecurityAnalyticsPlugin; | ||
import org.opensearch.securityanalytics.threatIntel.common.StashedThreadContext; | ||
|
@@ -248,7 +246,7 @@ public void deleteTIFSourceConfig( | |
client.delete(request, ActionListener.wrap( | ||
deleteResponse -> { | ||
if (deleteResponse.status().equals(RestStatus.OK)) { | ||
log.info("Deleted threat intel source config [{}] successfully", SaTifSourceConfig.getId()); | ||
log.debug("Deleted threat intel source config [{}] successfully", SaTifSourceConfig.getId()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question as above. |
||
actionListener.onResponse(deleteResponse); | ||
} else if (deleteResponse.status().equals(RestStatus.NOT_FOUND)) { | ||
throw SecurityAnalyticsException.wrap(new OpenSearchStatusException(String.format(Locale.getDefault(), "Threat intel source config with id [{%s}] not found", SaTifSourceConfig.getId()), RestStatus.NOT_FOUND)); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should there be a check here to assert that the
id
is not null or empty?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a check to ensure id exists