-
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
Merged
jowg-amazon
merged 9 commits into
opensearch-project:feature/threat_intel
from
jowg-amazon:delete_api_pr
Jun 13, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4c92ffd
delete api
jowg-amazon 0f4fa85
clean up
jowg-amazon f12483b
delete api integ test
jowg-amazon c259baf
added validation logic
jowg-amazon 39ac4a9
respond to comments
jowg-amazon c8c2e06
Merge branch 'feature/threat_intel' into delete_api_pr
jowg-amazon 950ed8c
Merge branch 'feature/threat_intel' into delete_api_pr
jowg-amazon 56a9cfb
fix merge conflicts
jowg-amazon a4cda27
fix merge conflicts
jowg-amazon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...va/org/opensearch/securityanalytics/threatIntel/action/SADeleteTIFSourceConfigAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...a/org/opensearch/securityanalytics/threatIntel/action/SADeleteTIFSourceConfigRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 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; | ||
import static org.opensearch.securityanalytics.threatIntel.common.Constants.THREAT_INTEL_SOURCE_CONFIG_ID; | ||
|
||
/** | ||
* 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() { | ||
ActionRequestValidationException validationException = null; | ||
if (id == null || id.isEmpty()) { | ||
validationException = addValidationError(String.format(Locale.getDefault(), "%s is missing", THREAT_INTEL_SOURCE_CONFIG_ID), validationException); | ||
} | ||
return validationException; | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
.../org/opensearch/securityanalytics/threatIntel/action/SADeleteTIFSourceConfigResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...opensearch/securityanalytics/threatIntel/resthandler/RestDeleteTIFSourceConfigAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.THREAT_INTEL_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, THREAT_INTEL_SOURCE_CONFIG_ID))); | ||
} | ||
|
||
@Override | ||
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { | ||
String saTifSourceConfigId = request.param(THREAT_INTEL_SOURCE_CONFIG_ID, SATIFSourceConfigDto.NO_ID); | ||
|
||
if (saTifSourceConfigId == null || saTifSourceConfigId.isBlank()) { | ||
throw new IllegalArgumentException("missing id"); | ||
} | ||
|
||
SADeleteTIFSourceConfigRequest req = new SADeleteTIFSourceConfigRequest(saTifSourceConfigId); | ||
|
||
return channel -> client.execute( | ||
SADeleteTIFSourceConfigAction.INSTANCE, | ||
req, | ||
new RestToXContentListener<>(channel) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.OpenSearchException; | ||
import org.opensearch.ResourceNotFoundException; | ||
import org.opensearch.action.delete.DeleteResponse; | ||
import org.opensearch.action.index.IndexResponse; | ||
|
@@ -16,6 +17,7 @@ | |
import org.opensearch.securityanalytics.threatIntel.model.IocStoreConfig; | ||
import org.opensearch.securityanalytics.threatIntel.model.SATIFSourceConfig; | ||
import org.opensearch.securityanalytics.threatIntel.model.SATIFSourceConfigDto; | ||
import org.opensearch.securityanalytics.util.SecurityAnalyticsException; | ||
|
||
import java.time.Instant; | ||
|
||
|
@@ -162,16 +164,33 @@ public void deleteTIFSourceConfig( | |
final String SaTifSourceConfigId, | ||
final ActionListener<DeleteResponse> listener | ||
) { | ||
// TODO: Delete all IOCs associated with source config | ||
SaTifSourceConfigService.getTIFSourceConfig(SaTifSourceConfigId, ActionListener.wrap( | ||
SaTifSourceConfig -> { | ||
if (SaTifSourceConfig == null) { | ||
throw new ResourceNotFoundException("No threat intel source config exists [{}]", SaTifSourceConfigId); | ||
} | ||
|
||
// Check if all threat intel monitors are deleted | ||
SaTifSourceConfigService.checkAndEnsureThreatIntelMonitorsDeleted(ActionListener.wrap( | ||
isDeleted -> { | ||
if (isDeleted == false) { | ||
throw SecurityAnalyticsException.wrap(new OpenSearchException("All threat intel monitors need to be deleted before deleting last threat intel source config")); | ||
} else { | ||
log.debug("All threat intel monitors are deleted or multiple threat intel source configs exist, can delete threat intel source config [{}]", SaTifSourceConfigId); | ||
} | ||
}, e-> { | ||
log.error("Failed to check if all threat intel monitors are deleted or if multiple threat intel source configs exist"); | ||
listener.onFailure(e); | ||
} | ||
)); | ||
|
||
TIFJobState previousState = SaTifSourceConfig.getState(); | ||
SaTifSourceConfig.setState(TIFJobState.DELETING); | ||
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(SaTifSourceConfig.getState()) == false) { | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
what status implies deleted?
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.
if the delete response is
OK
then the document is deleted successfully.