-
Notifications
You must be signed in to change notification settings - Fork 276
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
Initialize api tokens index if it doesn't exist #4899
Changes from all commits
55f184f
b8adfb1
2ef69d1
fc13790
95c390e
267cfd3
ac154fb
1245561
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 |
---|---|---|
|
@@ -41,6 +41,7 @@ | |
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionException; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
@@ -201,8 +202,8 @@ private void initalizeClusterConfiguration(final boolean installDefaultConfig) { | |
try (StoredContext ctx = threadContext.stashContext()) { | ||
threadContext.putHeader(ConfigConstants.OPENDISTRO_SECURITY_CONF_REQUEST_HEADER, "true"); | ||
|
||
createSecurityIndexIfAbsent(); | ||
waitForSecurityIndexToBeAtLeastYellow(); | ||
createIndexIfAbsent(securityIndex); | ||
waitForIndexToBeAtLeastYellow(securityIndex); | ||
|
||
final int initializationDelaySeconds = settings.getAsInt( | ||
ConfigConstants.SECURITY_UNSUPPORTED_DELAY_INITIALIZATION_SECONDS, | ||
|
@@ -324,26 +325,26 @@ private void setupAuditConfigurationIfAny(final boolean auditConfigDocPresent) { | |
} | ||
} | ||
|
||
private boolean createSecurityIndexIfAbsent() { | ||
boolean createIndexIfAbsent(String indexName) { | ||
try { | ||
final Map<String, Object> indexSettings = ImmutableMap.of("index.number_of_shards", 1, "index.auto_expand_replicas", "0-all"); | ||
final CreateIndexRequest createIndexRequest = new CreateIndexRequest(securityIndex).settings(indexSettings); | ||
final CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName).settings(indexSettings); | ||
final boolean ok = client.admin().indices().create(createIndexRequest).actionGet().isAcknowledged(); | ||
LOGGER.info("Index {} created?: {}", securityIndex, ok); | ||
LOGGER.info("Index {} created?: {}", indexName, ok); | ||
return ok; | ||
} catch (ResourceAlreadyExistsException resourceAlreadyExistsException) { | ||
LOGGER.info("Index {} already exists", securityIndex); | ||
LOGGER.info("Index {} already exists", indexName); | ||
return false; | ||
} | ||
} | ||
|
||
private void waitForSecurityIndexToBeAtLeastYellow() { | ||
void waitForIndexToBeAtLeastYellow(String index) { | ||
LOGGER.info("Node started, try to initialize it. Wait for at least yellow cluster state...."); | ||
ClusterHealthResponse response = null; | ||
try { | ||
response = client.admin() | ||
.cluster() | ||
.health(new ClusterHealthRequest(securityIndex).waitForActiveShards(1).waitForYellowStatus()) | ||
.health(new ClusterHealthRequest(index).waitForActiveShards(1).waitForYellowStatus()) | ||
.actionGet(); | ||
} catch (Exception e) { | ||
LOGGER.debug("Caught a {} but we just try again ...", e.toString()); | ||
|
@@ -352,7 +353,7 @@ private void waitForSecurityIndexToBeAtLeastYellow() { | |
while (response == null || response.isTimedOut() || response.getStatus() == ClusterHealthStatus.RED) { | ||
LOGGER.debug( | ||
"index '{}' not healthy yet, we try again ... (Reason: {})", | ||
securityIndex, | ||
index, | ||
response == null ? "no response" : (response.isTimedOut() ? "timeout" : "other, maybe red cluster") | ||
); | ||
try { | ||
|
@@ -362,7 +363,7 @@ private void waitForSecurityIndexToBeAtLeastYellow() { | |
Thread.currentThread().interrupt(); | ||
} | ||
try { | ||
response = client.admin().cluster().health(new ClusterHealthRequest(securityIndex).waitForYellowStatus()).actionGet(); | ||
response = client.admin().cluster().health(new ClusterHealthRequest(index).waitForYellowStatus()).actionGet(); | ||
} catch (Exception e) { | ||
LOGGER.debug("Caught again a {} but we just try again ...", e.toString()); | ||
} | ||
|
@@ -431,6 +432,36 @@ Future<Void> executeConfigurationInitialization(final SecurityMetadata securityM | |
return CompletableFuture.completedFuture(null); | ||
} | ||
|
||
public CompletableFuture<Boolean> createApiTokenIndex() { | ||
CompletableFuture<Boolean> apiTokenIndexTask = new CompletableFuture<>(); | ||
|
||
new Thread(() -> { | ||
try { | ||
// Wait until the cluster is ready | ||
while (clusterService.state().blocks().hasGlobalBlockWithStatus(RestStatus.SERVICE_UNAVAILABLE)) { | ||
LOGGER.info("Waiting for cluster to be available..."); | ||
TimeUnit.SECONDS.sleep(1); | ||
} | ||
|
||
final ThreadContext threadContext = threadPool.getThreadContext(); | ||
try (StoredContext ctx = threadContext.stashContext()) { | ||
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. why do we need to explicitly stashContext here? 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. Like the above, this is taken from the creation of |
||
threadContext.putHeader(ConfigConstants.OPENDISTRO_SECURITY_CONF_REQUEST_HEADER, "true"); | ||
|
||
createIndexIfAbsent(ConfigConstants.OPENSEARCH_API_TOKENS_INDEX); | ||
waitForIndexToBeAtLeastYellow(ConfigConstants.OPENSEARCH_API_TOKENS_INDEX); | ||
} | ||
|
||
LOGGER.info("API token index created successfully."); | ||
apiTokenIndexTask.complete(true); | ||
} catch (Exception e) { | ||
LOGGER.error("Error while creating API token index", e); | ||
apiTokenIndexTask.completeExceptionally(e); | ||
} | ||
}).start(); | ||
|
||
return apiTokenIndexTask; | ||
} | ||
|
||
@Deprecated | ||
public CompletableFuture<Boolean> initOnNodeStart() { | ||
final boolean installDefaultConfig = settings.getAsBoolean(ConfigConstants.SECURITY_ALLOW_DEFAULT_INIT_SECURITYINDEX, false); | ||
|
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 think this class should be reserved for initializing the security index. The security index will always need to be created on bootstrap.
For most plugins, I believe they create a system index when its needed if it doesn't exist instead of creating it on bootstrap. i.e. A user tries to issue an auth token and security tries to store the metadata, but the index doesn't exist so create it at that time.
See this class from AD plugin: https://github.com/opensearch-project/anomaly-detection/blob/main/src/main/java/org/opensearch/ad/indices/ADIndexManagement.java
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.
Sure this approach makes sense too. I also thought of this approach at first, but thought that it will add too much overhead to try and create this index for every request if it doesn't exist. However, we can limit this to index operations (create, revoke, etc.), and do not need to perform this for every authC request. I will try to do it this way instead.