Search in sources :

Example 1 with IndicesExistsResponse

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);
    }
}
Also used : IndicesExistsResponse(org.opensearch.action.admin.indices.exists.indices.IndicesExistsResponse) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata) GetMappingsResponse(org.opensearch.action.admin.indices.mapping.get.GetMappingsResponse)

Example 2 with IndicesExistsResponse

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();
    }
}
Also used : IndicesExistsResponse(org.opensearch.action.admin.indices.exists.indices.IndicesExistsResponse) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException)

Example 3 with IndicesExistsResponse

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());
    }
}
Also used : OpenSearchSecurityException(org.opensearch.OpenSearchSecurityException) ClusterHealthRequest(org.opensearch.action.admin.cluster.health.ClusterHealthRequest) Netty4Plugin(org.opensearch.transport.Netty4Plugin) Node(org.opensearch.node.Node) PluginAwareNode(org.opensearch.node.PluginAwareNode) OpenSearchSecurityException(org.opensearch.OpenSearchSecurityException) PluginAwareNode(org.opensearch.node.PluginAwareNode) DynamicSecurityConfig(org.opensearch.security.test.DynamicSecurityConfig) IndicesExistsResponse(org.opensearch.action.admin.indices.exists.indices.IndicesExistsResponse) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) CreateIndexRequest(org.opensearch.action.admin.indices.create.CreateIndexRequest) Settings(org.opensearch.common.settings.Settings) IndicesExistsRequest(org.opensearch.action.admin.indices.exists.indices.IndicesExistsRequest) Test(org.junit.Test) SingleClusterTest(org.opensearch.security.test.SingleClusterTest)

Example 4 with IndicesExistsResponse

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());
    }
}
Also used : OpenSearchSecurityException(org.opensearch.OpenSearchSecurityException) PluginAwareNode(org.opensearch.node.PluginAwareNode) DynamicSecurityConfig(org.opensearch.security.test.DynamicSecurityConfig) Netty4Plugin(org.opensearch.transport.Netty4Plugin) Node(org.opensearch.node.Node) PluginAwareNode(org.opensearch.node.PluginAwareNode) IndicesExistsResponse(org.opensearch.action.admin.indices.exists.indices.IndicesExistsResponse) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) CreateIndexRequest(org.opensearch.action.admin.indices.create.CreateIndexRequest) Settings(org.opensearch.common.settings.Settings) IndicesExistsRequest(org.opensearch.action.admin.indices.exists.indices.IndicesExistsRequest) Test(org.junit.Test) SingleClusterTest(org.opensearch.security.test.SingleClusterTest)

Example 5 with IndicesExistsResponse

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);
    }
}
Also used : GetAliasesResponse(org.opensearch.action.admin.indices.alias.get.GetAliasesResponse) ArrayList(java.util.ArrayList) IndicesExistsResponse(org.opensearch.action.admin.indices.exists.indices.IndicesExistsResponse) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) SuggesterException(org.codelibs.fess.suggest.exception.SuggesterException) IOException(java.io.IOException) SuggesterException(org.codelibs.fess.suggest.exception.SuggesterException) IndicesAliasesRequestBuilder(org.opensearch.action.admin.indices.alias.IndicesAliasesRequestBuilder)

Aggregations

IndicesExistsResponse (org.opensearch.action.admin.indices.exists.indices.IndicesExistsResponse)8 CreateIndexResponse (org.opensearch.action.admin.indices.create.CreateIndexResponse)5 IOException (java.io.IOException)4 SuggesterException (org.codelibs.fess.suggest.exception.SuggesterException)3 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 OpenSearchSecurityException (org.opensearch.OpenSearchSecurityException)2 Alias (org.opensearch.action.admin.indices.alias.Alias)2 IndicesAliasesRequestBuilder (org.opensearch.action.admin.indices.alias.IndicesAliasesRequestBuilder)2 GetAliasesResponse (org.opensearch.action.admin.indices.alias.get.GetAliasesResponse)2 CreateIndexRequest (org.opensearch.action.admin.indices.create.CreateIndexRequest)2 IndicesExistsRequest (org.opensearch.action.admin.indices.exists.indices.IndicesExistsRequest)2 Settings (org.opensearch.common.settings.Settings)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 ZonedDateTime (java.time.ZonedDateTime)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 List (java.util.List)1 ExecutorService (java.util.concurrent.ExecutorService)1 Stream (java.util.stream.Stream)1