use of org.opensearch.action.admin.indices.exists.indices.IndicesExistsResponse in project fess-crawler by codelibs.
the class AbstractCrawlerService method createMapping.
protected void createMapping(final String mappingName) {
boolean exists = false;
try {
final IndicesExistsResponse response = fesenClient.get(c -> c.admin().indices().prepareExists(index).execute());
exists = response.isExists();
} catch (final IndexNotFoundException e) {
// ignore
}
if (!exists) {
final CreateIndexResponse indexResponse = fesenClient.get(c -> {
final String source;
if (numberOfReplicas > 0) {
source = "{\"settings\":{\"index\":{\"number_of_shards\":" + numberOfShards + ",\"number_of_replicas\":0,\"auto_expand_replicas\":\"0-" + numberOfReplicas + "\"}}}";
} else {
source = "{\"settings\":{\"index\":{\"number_of_shards\":" + numberOfShards + ",\"number_of_replicas\":" + numberOfReplicas + "}}}";
}
return c.admin().indices().prepareCreate(index).setSource(source, XContentType.JSON).execute();
});
if (indexResponse.isAcknowledged()) {
logger.info("Created {} index.", index);
} else if (logger.isDebugEnabled()) {
logger.debug("Failed to create {} index.", index);
}
}
final GetMappingsResponse getMappingsResponse = fesenClient.get(c -> c.admin().indices().prepareGetMappings(index).execute());
final ImmutableOpenMap<String, MappingMetadata> indexMappings = getMappingsResponse.mappings().get(index);
if (indexMappings == null || !indexMappings.containsKey("properties")) {
final AcknowledgedResponse putMappingResponse = fesenClient.get(c -> {
final String source = FileUtil.readText("mapping/" + mappingName + ".json");
return c.admin().indices().preparePutMapping(index).setSource(source, XContentType.JSON).execute();
});
if (putMappingResponse.isAcknowledged()) {
logger.info("Created {} mapping.", index);
} else {
logger.warn("Failed to create {} mapping.", index);
}
} else if (logger.isDebugEnabled()) {
logger.debug("{} mapping exists.", index);
}
}
use of org.opensearch.action.admin.indices.exists.indices.IndicesExistsResponse in project OpenSearch by opensearch-project.
the class SimpleBlocksIT method canIndexExists.
private void canIndexExists(String index) {
try {
IndicesExistsResponse r = client().admin().indices().prepareExists(index).execute().actionGet();
assertThat(r, notNullValue());
} catch (ClusterBlockException e) {
fail();
}
}
use of org.opensearch.action.admin.indices.exists.indices.IndicesExistsResponse in project security by opensearch-project.
the class RolesInjectorIntegTest method testRolesInject.
@Test
public void testRolesInject() throws Exception {
setup(Settings.EMPTY, new DynamicSecurityConfig().setSecurityRoles("roles.yml"), Settings.EMPTY);
Assert.assertEquals(clusterInfo.numNodes, clusterHelper.nodeClient().admin().cluster().health(new ClusterHealthRequest().waitForGreenStatus()).actionGet().getNumberOfNodes());
Assert.assertEquals(ClusterHealthStatus.GREEN, clusterHelper.nodeClient().admin().cluster().health(new ClusterHealthRequest().waitForGreenStatus()).actionGet().getStatus());
final Settings tcSettings = Settings.builder().put(minimumSecuritySettings(Settings.EMPTY).get(0)).put("cluster.name", clusterInfo.clustername).put("node.data", false).put("node.master", false).put("node.ingest", false).put("path.data", "./target/data/" + clusterInfo.clustername + "/cert/data").put("path.logs", "./target/data/" + clusterInfo.clustername + "/cert/logs").put("path.home", "./target").put("node.name", "testclient").put("discovery.initial_state_timeout", "8s").put("plugins.security.allow_default_init_securityindex", "true").putList("discovery.zen.ping.unicast.hosts", clusterInfo.nodeHost + ":" + clusterInfo.nodePort).build();
// 1. Without roles injection.
try (Node node = new PluginAwareNode(false, tcSettings, Netty4Plugin.class, OpenSearchSecurityPlugin.class, RolesInjectorPlugin.class).start()) {
waitForInit(node.client());
CreateIndexResponse cir = node.client().admin().indices().create(new CreateIndexRequest("captain-logs-1")).actionGet();
Assert.assertTrue(cir.isAcknowledged());
IndicesExistsResponse ier = node.client().admin().indices().exists(new IndicesExistsRequest("captain-logs-1")).actionGet();
Assert.assertTrue(ier.isExists());
}
// 2. With invalid roles, must throw security exception.
RolesInjectorPlugin.injectedRoles = "invalid_user|invalid_role";
Exception exception = null;
try (Node node = new PluginAwareNode(false, tcSettings, Netty4Plugin.class, OpenSearchSecurityPlugin.class, RolesInjectorPlugin.class).start()) {
waitForInit(node.client());
CreateIndexResponse cir = node.client().admin().indices().create(new CreateIndexRequest("captain-logs-2")).actionGet();
Assert.assertTrue(cir.isAcknowledged());
} catch (OpenSearchSecurityException ex) {
exception = ex;
log.warn(ex.toString());
}
Assert.assertNotNull(exception);
Assert.assertTrue(exception.getMessage().contains("indices:admin/create"));
// 3. With valid roles - which has permission to create index.
RolesInjectorPlugin.injectedRoles = "valid_user|opendistro_security_all_access";
try (Node node = new PluginAwareNode(false, tcSettings, Netty4Plugin.class, OpenSearchSecurityPlugin.class, RolesInjectorPlugin.class).start()) {
waitForInit(node.client());
CreateIndexResponse cir = node.client().admin().indices().create(new CreateIndexRequest("captain-logs-3")).actionGet();
Assert.assertTrue(cir.isAcknowledged());
IndicesExistsResponse ier = node.client().admin().indices().exists(new IndicesExistsRequest("captain-logs-3")).actionGet();
Assert.assertTrue(ier.isExists());
}
}
use of org.opensearch.action.admin.indices.exists.indices.IndicesExistsResponse in project security by opensearch-project.
the class RolesValidationIntegTest method testRolesValidation.
@Test
public void testRolesValidation() throws Exception {
setup(Settings.EMPTY, new DynamicSecurityConfig().setSecurityRoles("roles.yml"), Settings.EMPTY);
final Settings tcSettings = Settings.builder().put(minimumSecuritySettings(Settings.EMPTY).get(0)).put("cluster.name", clusterInfo.clustername).put("node.data", false).put("node.master", false).put("node.ingest", false).put("path.data", "./target/data/" + clusterInfo.clustername + "/cert/data").put("path.logs", "./target/data/" + clusterInfo.clustername + "/cert/logs").put("path.home", "./target").put("node.name", "testclient").put("discovery.initial_state_timeout", "8s").put("plugins.security.allow_default_init_securityindex", "true").putList("discovery.zen.ping.unicast.hosts", clusterInfo.nodeHost + ":" + clusterInfo.nodePort).build();
// 1. Without roles validation
try (Node node = new PluginAwareNode(false, tcSettings, Netty4Plugin.class, OpenSearchSecurityPlugin.class, RolesValidationPlugin.class).start()) {
waitForInit(node.client());
CreateIndexResponse cir = node.client().admin().indices().create(new CreateIndexRequest("captain-logs-1")).actionGet();
Assert.assertTrue(cir.isAcknowledged());
IndicesExistsResponse ier = node.client().admin().indices().exists(new IndicesExistsRequest("captain-logs-1")).actionGet();
Assert.assertTrue(ier.isExists());
}
OpenSearchSecurityException exception = null;
// 2. with roles invalid to the user
RolesValidationPlugin.rolesValidation = "invalid_role";
try (Node node = new PluginAwareNode(false, tcSettings, Netty4Plugin.class, OpenSearchSecurityPlugin.class, RolesValidationPlugin.class).start()) {
waitForInit(node.client());
CreateIndexResponse cir = node.client().admin().indices().create(new CreateIndexRequest("captain-logs-2")).actionGet();
} catch (OpenSearchSecurityException ex) {
exception = ex;
}
Assert.assertNotNull(exception);
Assert.assertTrue(exception.getMessage().contains("No mapping for"));
// 3. with roles valid to the user
RolesValidationPlugin.rolesValidation = "opendistro_security_all_access";
try (Node node = new PluginAwareNode(false, tcSettings, Netty4Plugin.class, OpenSearchSecurityPlugin.class, RolesValidationPlugin.class).start()) {
waitForInit(node.client());
CreateIndexResponse cir = node.client().admin().indices().create(new CreateIndexRequest("captain-logs-3")).actionGet();
Assert.assertTrue(cir.isAcknowledged());
}
}
use of org.opensearch.action.admin.indices.exists.indices.IndicesExistsResponse in project fess-suggest by codelibs.
the class Suggester method createNextIndex.
public void createNextIndex() {
try {
final List<String> prevIndices = new ArrayList<>();
final IndicesExistsResponse response = client.admin().indices().prepareExists(getUpdateAlias(index)).execute().actionGet(suggestSettings.getIndicesTimeout());
if (response.isExists()) {
final GetAliasesResponse getAliasesResponse = client.admin().indices().prepareGetAliases(getUpdateAlias(index)).execute().actionGet(suggestSettings.getIndicesTimeout());
getAliasesResponse.getAliases().keysIt().forEachRemaining(prevIndices::add);
}
final String mappingSource = getDefaultMappings();
final String settingsSource = getDefaultIndexSettings();
final String indexName = createIndexName(index);
if (logger.isInfoEnabled()) {
logger.info("Create next index: {}", indexName);
}
final CreateIndexResponse createIndexResponse = client.admin().indices().prepareCreate(indexName).setSettings(settingsSource, XContentType.JSON).addMapping(SuggestConstants.DEFAULT_TYPE, mappingSource, XContentType.JSON).execute().actionGet(suggestSettings.getIndicesTimeout());
if (!createIndexResponse.isAcknowledged()) {
if (logger.isDebugEnabled()) {
logger.debug("Could not create next index: {}", indexName);
}
throw new SuggesterException("Could not create next index: " + indexName);
}
client.admin().cluster().prepareHealth().setWaitForYellowStatus().execute().actionGet(suggestSettings.getClusterTimeout());
final IndicesAliasesRequestBuilder aliasesRequestBuilder = client.admin().indices().prepareAliases().addAlias(indexName, getUpdateAlias(index));
for (final String prevIndex : prevIndices) {
aliasesRequestBuilder.removeAlias(prevIndex, getUpdateAlias(index));
}
aliasesRequestBuilder.execute().actionGet(suggestSettings.getIndicesTimeout());
} catch (final Exception e) {
if (logger.isDebugEnabled()) {
logger.debug("Failed to replace with new index.", e);
}
throw new SuggesterException("Failed to replace with new index.", e);
}
}
Aggregations