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

Improve Health Status of security-auditlog Index in Single Node Clusters #5030

Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
package org.opensearch.security.auditlog.sink;

import java.io.IOException;
import java.util.Map;

import com.google.common.collect.ImmutableMap;
import org.opensearch.ResourceAlreadyExistsException;
import org.opensearch.action.DocWriteRequest;
import org.opensearch.action.admin.indices.create.CreateIndexRequest;
import org.opensearch.action.index.IndexRequestBuilder;
import org.opensearch.action.support.WriteRequest.RefreshPolicy;
import org.opensearch.client.Client;
Expand Down Expand Up @@ -51,6 +55,29 @@ public void close() throws IOException {

}

private boolean indexExists(String indexName) {
return clientProvider
Ganesh-RB marked this conversation as resolved.
Show resolved Hide resolved
.admin()
.indices()
.prepareExists(indexName)
.execute()
.actionGet()
.isExists();
}

private boolean createIndexIfAbsent(String indexName){
Ganesh-RB marked this conversation as resolved.
Show resolved Hide resolved
try {
final Map<String, Object> indexSettings = ImmutableMap.of("index.number_of_shards", 1, "index.auto_expand_replicas", "0-all");
Ganesh-RB marked this conversation as resolved.
Show resolved Hide resolved
final CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName).settings(indexSettings);
final boolean ok = clientProvider.admin().indices().create(createIndexRequest).actionGet().isAcknowledged();
log.info("Index {} created?: {}", indexName, ok);
return ok;
} catch (ResourceAlreadyExistsException resourceAlreadyExistsException) {
log.info("Index {} already exists", indexName);
Ganesh-RB marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
}

public boolean doStore(final AuditMessage msg, String indexName) {

if (Boolean.parseBoolean(
Expand All @@ -62,6 +89,10 @@ public boolean doStore(final AuditMessage msg, String indexName) {
return true;
}

if(!indexExists(indexName)){
createIndexIfAbsent(indexName);
}

try (StoredContext ctx = threadPool.getThreadContext().stashContext()) {
try {
final IndexRequestBuilder irb = clientProvider.prepareIndex(indexName)
Expand Down
Loading