Skip to content

Commit

Permalink
Initialize api tokens index if it doesn't exist
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Ho <dxho@amazon.com>
  • Loading branch information
derek-ho committed Nov 13, 2024
1 parent 3c635c9 commit 55f184f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ public Collection<Object> createComponents(

final XFFResolver xffResolver = new XFFResolver(threadPool);
backendRegistry = new BackendRegistry(settings, adminDns, xffResolver, auditLog, threadPool);
tokenManager = new SecurityTokenManager(cs, threadPool, userService);
tokenManager = new SecurityTokenManager(cs, threadPool, userService, settings, localClient);

final CompatConfig compatConfig = new CompatConfig(environment, transportPassiveAuthSetting);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,13 @@ private void initalizeClusterConfiguration(final boolean installDefaultConfig) {
try (StoredContext ctx = threadContext.stashContext()) {
threadContext.putHeader(ConfigConstants.OPENDISTRO_SECURITY_CONF_REQUEST_HEADER, "true");

createSecurityIndexIfAbsent();
createSecurityIndexIfAbsent(securityIndex);
if (true) {
createSecurityIndexIfAbsent(ConfigConstants.OPENSEARCH_API_TOKENS_INDEX);
}
waitForSecurityIndexToBeAtLeastYellow();


final int initializationDelaySeconds = settings.getAsInt(
ConfigConstants.SECURITY_UNSUPPORTED_DELAY_INITIALIZATION_SECONDS,
0
Expand Down Expand Up @@ -324,15 +328,15 @@ private void setupAuditConfigurationIfAny(final boolean auditConfigDocPresent) {
}
}

private boolean createSecurityIndexIfAbsent() {
private boolean createSecurityIndexIfAbsent(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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
import org.apache.logging.log4j.Logger;

import org.opensearch.OpenSearchSecurityException;
import org.opensearch.client.Client;
import org.opensearch.client.Response;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.action.ActionListener;
import org.opensearch.core.common.transport.TransportAddress;
import org.opensearch.identity.Subject;
import org.opensearch.identity.noop.NoopSubject;
Expand All @@ -32,6 +35,7 @@
import org.opensearch.security.securityconf.ConfigModel;
import org.opensearch.security.securityconf.DynamicConfigModel;
import org.opensearch.security.support.ConfigConstants;
import org.opensearch.security.support.SecurityIndexHandler;
import org.opensearch.security.user.User;
import org.opensearch.security.user.UserService;
import org.opensearch.threadpool.ThreadPool;
Expand All @@ -52,8 +56,8 @@ public class SecurityTokenManager implements TokenManager {

private JwtVendor jwtVendor = null;
private ConfigModel configModel = null;

public SecurityTokenManager(final ClusterService cs, final ThreadPool threadPool, final UserService userService) {
private SecurityIndexHandler securityIndexHandler;
public SecurityTokenManager(final ClusterService cs, final ThreadPool threadPool, final UserService userService, final Settings settings, final Client client) {
this.cs = cs;
this.threadPool = threadPool;
this.userService = userService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,9 @@ public enum RolesMappingResolution {
// Variable for initial admin password support
public static final String OPENSEARCH_INITIAL_ADMIN_PASSWORD = "OPENSEARCH_INITIAL_ADMIN_PASSWORD";

// API Tokens index
public static final String OPENSEARCH_API_TOKENS_INDEX = ".opensearch_security_api_tokens";

public static Set<String> getSettingAsSet(
final Settings settings,
final String key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.junit.runner.RunWith;

import org.opensearch.OpenSearchSecurityException;
import org.opensearch.client.Client;
import org.opensearch.cluster.ClusterName;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.settings.Settings;
Expand Down Expand Up @@ -69,9 +70,14 @@ public class SecurityTokenManagerTest {
@Mock
private UserService userService;

@Mock
private Settings settings;
@Mock
private Client client;

@Before
public void setup() {
tokenManager = spy(new SecurityTokenManager(cs, threadPool, userService));
tokenManager = spy(new SecurityTokenManager(cs, threadPool, userService, settings, client));
}

@After
Expand Down

0 comments on commit 55f184f

Please sign in to comment.