-
Notifications
You must be signed in to change notification settings - Fork 78
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
Clean up empty IOC indices created by failed source configs #1267
Changes from 1 commit
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 |
---|---|---|
|
@@ -72,6 +72,7 @@ | |
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.opensearch.jobscheduler.spi.utils.LockService.LOCK_INDEX_NAME; | ||
import static org.opensearch.securityanalytics.settings.SecurityAnalyticsSettings.INDEX_TIMEOUT; | ||
import static org.opensearch.securityanalytics.threatIntel.common.TIFJobState.AVAILABLE; | ||
import static org.opensearch.securityanalytics.threatIntel.common.TIFJobState.REFRESHING; | ||
|
@@ -362,7 +363,7 @@ public void deleteTIFSourceConfig( | |
client.delete(request, ActionListener.wrap( | ||
deleteResponse -> { | ||
if (deleteResponse.status().equals(RestStatus.OK)) { | ||
log.debug("Deleted threat intel source config [{}] successfully", saTifSourceConfig.getId()); | ||
log.info("Deleted threat intel source config [{}] successfully", saTifSourceConfig.getId()); | ||
actionListener.onResponse(deleteResponse); | ||
} else if (deleteResponse.status().equals(RestStatus.NOT_FOUND)) { | ||
actionListener.onFailure(SecurityAnalyticsException.wrap(new OpenSearchStatusException(String.format(Locale.getDefault(), "Threat intel source config with id [{%s}] not found", saTifSourceConfig.getId()), RestStatus.NOT_FOUND))); | ||
|
@@ -376,6 +377,44 @@ public void deleteTIFSourceConfig( | |
)); | ||
} | ||
|
||
// Manually delete threat intel job scheduler lock if job is disabled | ||
public void deleteJobSchedulerLockIfJobDisabled( | ||
SATIFSourceConfig saTifSourceConfig, | ||
final ActionListener<DeleteResponse> actionListener | ||
) { | ||
if (saTifSourceConfig.isEnabled()) { | ||
actionListener.onResponse(null); | ||
return; | ||
} | ||
|
||
// check to make sure the job scheduler lock index exists | ||
if (clusterService.state().metadata().hasIndex(LOCK_INDEX_NAME) == false) { | ||
actionListener.onFailure(SecurityAnalyticsException.wrap(new OpenSearchStatusException("Threat intel job scheduler lock index does not exist", RestStatus.BAD_REQUEST))); | ||
return; | ||
} | ||
|
||
String id = SecurityAnalyticsPlugin.JOB_INDEX_NAME + "-" + 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. Just to clarify, we don't need to convert this string to lowercase because this ID of the document in the 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. Yes, here's an example of what the id in the job scheduler lock index looks like |
||
DeleteRequest request = new DeleteRequest(LOCK_INDEX_NAME, id) | ||
.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) | ||
.timeout(clusterSettings.get(INDEX_TIMEOUT)); | ||
|
||
client.delete(request, ActionListener.wrap( | ||
deleteResponse -> { | ||
if (deleteResponse.status().equals(RestStatus.OK)) { | ||
log.info("Deleted threat intel job scheduler lock [{}] successfully", id); | ||
actionListener.onResponse(deleteResponse); | ||
} else if (deleteResponse.status().equals(RestStatus.NOT_FOUND)) { | ||
actionListener.onFailure(SecurityAnalyticsException.wrap(new OpenSearchStatusException(String.format(Locale.getDefault(), "Threat intel job scheduler lock with id [{%s}] not found", id), RestStatus.NOT_FOUND))); | ||
} else { | ||
actionListener.onFailure(SecurityAnalyticsException.wrap(new OpenSearchStatusException(String.format(Locale.getDefault(), "Failed to delete threat intel job scheduler lock with id [{%s}]", id), deleteResponse.status()))); | ||
} | ||
}, e -> { | ||
log.error("Failed to delete threat intel job scheduler lock with id [{}]", id); | ||
actionListener.onFailure(e); | ||
} | ||
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. Let's make sure we have unit tests for all of these scenarios 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. Yep, will do in a follow up PR and added a github issue to increase our code cov and integ testing overall: #1268 |
||
)); | ||
} | ||
|
||
public void deleteAllIocIndices(Set<String> indicesToDelete, Boolean backgroundJob, ActionListener<AcknowledgedResponse> listener) { | ||
if (indicesToDelete.isEmpty() == false) { | ||
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indicesToDelete.toArray(new String[0])); | ||
|
@@ -398,6 +437,8 @@ public void deleteAllIocIndices(Set<String> indicesToDelete, Boolean backgroundJ | |
} | ||
) | ||
); | ||
} else if (listener != null) { | ||
listener.onResponse(new AcknowledgedResponse(true)); | ||
} | ||
} | ||
|
||
|
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.
I've seen this done both ways and I think either can make sense based on the situation. We can either
It would be worth considering when this API would be invoked to decide whether 1 or 2 is the correct behavior. I could see things like operator intervention resulting in the lock index being deleted and we should be sure that would not cause permanent failures/further issues if we go with approach 1.
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.
Makes sense, this is only called when we want to clean up and delete everything related to the source config that's being deleted so i think the second option makes more sense in this case. Updated the code to not fail when the index doesn't exist or if the document is not found.