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

[Backport 1.3] [Backport 1.x] Fix lost privileges during auto initializing of the index #2643

Merged
merged 1 commit into from
Apr 4, 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 @@ -46,6 +46,7 @@
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.opensearch.rest.RestStatus;
import org.opensearch.security.auditlog.config.AuditConfig;
import org.opensearch.security.support.SecurityUtils;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -118,8 +119,10 @@ private ConfigurationRepository(Settings settings, final Path configPath, Thread
public void run() {
try {
LOGGER.info("Background init thread started. Install default config?: "+installDefaultConfig.get());


while (clusterService.state().blocks().hasGlobalBlockWithStatus(RestStatus.SERVICE_UNAVAILABLE)) {
LOGGER.info("Wait for cluster to be available ...");
TimeUnit.SECONDS.sleep(1);
}
if(installDefaultConfig.get()) {

try {
Expand Down
46 changes: 25 additions & 21 deletions src/main/java/org/opensearch/security/support/ConfigHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;

import org.opensearch.security.securityconf.impl.Meta;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -71,29 +73,31 @@ public static void uploadFile(Client tc, String filepath, String index, CType cT
public static void uploadFile(Client tc, String filepath, String index, CType cType, int configVersion, boolean populateEmptyIfFileMissing) throws Exception {
final String configType = cType.toLCString();
LOGGER.info("Will update '" + configType + "' with " + filepath + " and populate it with empty doc if file missing and populateEmptyIfFileMissing=" + populateEmptyIfFileMissing);
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
if (!populateEmptyIfFileMissing) {
ConfigHelper.fromYamlFile(filepath, cType, configVersion, 0, 0);
}

if (!populateEmptyIfFileMissing) {
ConfigHelper.fromYamlFile(filepath, cType, configVersion, 0, 0);
}

try (Reader reader = createFileOrStringReader(cType, configVersion, filepath, populateEmptyIfFileMissing)) {

final IndexRequest indexRequest = new IndexRequest(index)
.type(configVersion == 1 ? "security" : "_doc")
.id(configType)
.opType(OpType.CREATE)
.setRefreshPolicy(RefreshPolicy.IMMEDIATE)
.source(configType, readXContent(reader, XContentType.YAML));
final String res = tc.index(indexRequest).actionGet().getId();

if (!configType.equals(res)) {
throw new Exception(" FAIL: Configuration for '" + configType
+ "' failed for unknown reasons. Pls. consult logfile of opensearch");
try (Reader reader = createFileOrStringReader(cType, configVersion, filepath, populateEmptyIfFileMissing)) {

final IndexRequest indexRequest = new IndexRequest(index)
.type(configVersion == 1 ? "security" : "_doc")
.id(configType)
.opType(OpType.CREATE)
.setRefreshPolicy(RefreshPolicy.IMMEDIATE)
.source(configType, readXContent(reader, XContentType.YAML));
final String res = tc.index(indexRequest).actionGet().getId();

if (!configType.equals(res)) {
throw new Exception(" FAIL: Configuration for '" + configType
+ "' failed for unknown reasons. Pls. consult logfile of opensearch");
}
LOGGER.info("Doc with id '{}' and version {} is updated in {} index.", configType, configVersion, index);
} catch (VersionConflictEngineException versionConflictEngineException) {
LOGGER.info("Index {} already contains doc with id {}, skipping update.", index, configType);
}
LOGGER.info("Doc with id '{}' and version {} is updated in {} index.", configType, configVersion, index);
} catch (VersionConflictEngineException versionConflictEngineException) {
LOGGER.info("Index {} already contains doc with id {}, skipping update.", index, configType);
}
return null;
});
}

public static Reader createFileOrStringReader(CType cType, int configVersion, String filepath, boolean populateEmptyIfFileMissing) throws Exception {
Expand Down