org.elasticsearch.ElasticsearchStatusException Java Examples

The following examples show how to use org.elasticsearch.ElasticsearchStatusException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RangerSecurityRestFilter.java    From ranger with Apache License 2.0 6 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final NodeClient client)
		throws Exception {
	// Now only support to get user from request,
	// it should work with other elasticsearch identity authentication plugins in fact.
	UsernamePasswordToken user = UsernamePasswordToken.parseToken(request);
	if (user == null) {
		throw new ElasticsearchStatusException("Error: User is null, the request requires user authentication.",
				RestStatus.UNAUTHORIZED);
	} else {
		if (LOG.isDebugEnabled()) {
			LOG.debug("Success to parse user[{}] from request[{}].", user, request);
		}
	}
	threadContext.putTransient(UsernamePasswordToken.USERNAME, user.getUsername());

	String clientIPAddress = RequestUtils.getClientIPAddress(request);
	if (StringUtils.isNotEmpty(clientIPAddress)) {
		threadContext.putTransient(RequestUtils.CLIENT_IP_ADDRESS, clientIPAddress);
	}

	this.restHandler.handleRequest(request, channel, client);
}
 
Example #2
Source File: RangerSecurityActionFilter.java    From ranger with Apache License 2.0 6 votes vote down vote up
@Override
public <Request extends ActionRequest, Response extends ActionResponse> void apply(Task task, String action,
		Request request, ActionListener<Response> listener, ActionFilterChain<Request, Response> chain) {
	String user = threadContext.getTransient(UsernamePasswordToken.USERNAME);
	// If user is not null, then should check permission of the outside caller.
	if (StringUtils.isNotEmpty(user)) {
		List<String> indexs = RequestUtils.getIndexFromRequest(request);
		String clientIPAddress = threadContext.getTransient(RequestUtils.CLIENT_IP_ADDRESS);
		for (String index : indexs) {
			boolean result = rangerElasticsearchAuthorizer.checkPermission(user, null, index, action,
					clientIPAddress);
			if (!result) {
				String errorMsg = "Error: User[{}] could not do action[{}] on index[{}]";
				throw new ElasticsearchStatusException(errorMsg, RestStatus.FORBIDDEN, user, action, index);
			}
		}
	} else {
		if (LOG.isDebugEnabled()) {
			LOG.debug("User is null, no check permission for elasticsearch do action[{}] with request[{}]", action,
					request);
		}
	}
	chain.proceed(task, action, request, listener);
}
 
Example #3
Source File: LogstashIT.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
private static long queryDocumentCount(
        final RestHighLevelClient client)
        throws IOException {
    final SearchSourceBuilder searchSourceBuilder =
            new SearchSourceBuilder()
                    .size(0)
                    .fetchSource(false);
    final SearchRequest searchRequest =
            new SearchRequest(MavenHardcodedConstants.ES_INDEX_NAME)
                    .source(searchSourceBuilder);
    try {
        final SearchResponse searchResponse =
                client.search(searchRequest, RequestOptions.DEFAULT);
        return searchResponse.getHits().getTotalHits().value;
    } catch (ElasticsearchStatusException error) {
        if (RestStatus.NOT_FOUND.equals(error.status())) {
            return 0L;
        }
        throw new IOException(error);
    }
}
 
Example #4
Source File: LogstashIT.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
private static List<Map<String, Object>> queryDocuments(
        final RestHighLevelClient client
) throws IOException {
    final SearchSourceBuilder searchSourceBuilder =
            new SearchSourceBuilder()
                    .size(LOG_EVENT_COUNT)
                    .fetchSource(true);
    final SearchRequest searchRequest =
            new SearchRequest(MavenHardcodedConstants.ES_INDEX_NAME)
                    .source(searchSourceBuilder);
    try {
        final SearchResponse searchResponse =
                client.search(searchRequest, RequestOptions.DEFAULT);
        return Arrays
                .stream(searchResponse.getHits().getHits())
                .map(SearchHit::getSourceAsMap)
                .collect(Collectors.toList());
    } catch (ElasticsearchStatusException error) {
        if (RestStatus.NOT_FOUND.equals(error.status())) {
            return Collections.emptyList();
        }
        throw new IOException(error);
    }
}
 
Example #5
Source File: ElasticsearchRestClientInstrumentationIT_RealReporter.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testTryToDeleteNonExistingIndex() throws IOException {
    ElasticsearchStatusException ese = null;
    try {
        client.indices().delete(new DeleteIndexRequest(SECOND_INDEX), RequestOptions.DEFAULT);
    } catch (ElasticsearchStatusException e) {
        ese = e;
    }
    assertThat(ese).isNotNull();
    assertThat(ese.status().getStatus()).isEqualTo(404);
}
 
Example #6
Source File: IndexCreationFactory.java    From james-project with Apache License 2.0 5 votes vote down vote up
private void createIndexIfNeeded(ReactorElasticSearchClient client, IndexName indexName, XContentBuilder settings) throws IOException {
    try {
        client.indices()
            .create(
                new CreateIndexRequest(indexName.getValue())
                    .source(settings), RequestOptions.DEFAULT);
    } catch (ElasticsearchStatusException exception) {
        if (exception.getMessage().contains(INDEX_ALREADY_EXISTS_EXCEPTION_MESSAGE)) {
            LOGGER.info("Index [{}] already exists", indexName.getValue());
        } else {
            throw exception;
        }
    }
}
 
Example #7
Source File: UsernamePasswordToken.java    From ranger with Apache License 2.0 5 votes vote down vote up
public static UsernamePasswordToken parseToken(RestRequest request) {

		Map<String, List<String>> headers = request.getHeaders();
		if (MapUtils.isEmpty(headers)) {
			return null;
		}
		List<String> authStrs = headers.get(BASIC_AUTH_HEADER);
		if (CollectionUtils.isEmpty(authStrs)) {
			return null;
		}

		String authStr = authStrs.get(0);
		if (StringUtils.isEmpty(authStr)) {
			return null;
		}

		String userPass = "";
		try {
			userPass = new String(Base64.getUrlDecoder().decode(authStr.substring(BASIC_AUTH_PREFIX.length())));
		} catch (IllegalArgumentException e) {
			throw new ElasticsearchStatusException("Error: Failed to parse user authentication.",
					RestStatus.UNAUTHORIZED, e);
		}

		int i = StringUtils.indexOf(userPass, ':');
		if (i <= 0) {
			throw new ElasticsearchStatusException(
					"Error: Parse user authentication to get the wrong userPass[{}].",
					RestStatus.UNAUTHORIZED, userPass);
		}
		return new UsernamePasswordToken(StringUtils.substring(userPass, 0, i),
				StringUtils.substring(userPass, i + 1, userPass.length()));

	}
 
Example #8
Source File: ApplicationConfiguration.java    From spring-data-examples with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void insertDataSample() {

	try {
		RestClients.create(ClientConfiguration.localhost()).rest().indices()
				.create(new CreateIndexRequest("conference-index"), RequestOptions.DEFAULT);
	} catch (IOException | ElasticsearchStatusException e) {
		// just ignore it
	}

	// Remove all documents
	repository.deleteAll().subscribe();

	// Save data sample
	repository
			.save(Conference.builder().date("2014-11-06").name("Spring eXchange 2014 - London")
					.keywords(Arrays.asList("java", "spring")).location(new GeoPoint(51.500152D, -0.126236D)).build())
			.then().as(StepVerifier::create).verifyComplete();
	repository
			.save(Conference.builder().date("2014-12-07").name("Scala eXchange 2014 - London")
					.keywords(Arrays.asList("scala", "play", "java")).location(new GeoPoint(51.500152D, -0.126236D)).build())
			.then().as(StepVerifier::create).verifyComplete();
	repository.save(Conference.builder().date("2014-11-20").name("Elasticsearch 2014 - Berlin")
			.keywords(Arrays.asList("java", "elasticsearch", "kibana")).location(new GeoPoint(52.5234051D, 13.4113999))
			.build()).then().as(StepVerifier::create).verifyComplete();
	repository.save(Conference.builder().date("2014-11-12").name("AWS London 2014")
			.keywords(Arrays.asList("cloud", "aws")).location(new GeoPoint(51.500152D, -0.126236D)).build()).then()
			.as(StepVerifier::create).verifyComplete();
	repository.save(Conference.builder().date("2014-10-04").name("JDD14 - Cracow")
			.keywords(Arrays.asList("java", "spring")).location(new GeoPoint(50.0646501D, 19.9449799)).build()).then()
			.as(StepVerifier::create).verifyComplete();
}
 
Example #9
Source File: LogstashIT.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private static RestHighLevelClient createClient() throws IOException {

        // Instantiate the client.
        LOGGER.info("instantiating the ES client");
        final HttpHost httpHost = new HttpHost(HOST_NAME, MavenHardcodedConstants.ES_PORT);
        final RestClientBuilder clientBuilder =
                RestClient.builder(httpHost);
        final RestHighLevelClient client = new RestHighLevelClient(clientBuilder);

        // Verify the connection.
        LOGGER.info("verifying the ES connection");
        final ClusterHealthResponse healthResponse = client
                .cluster()
                .health(new ClusterHealthRequest(), RequestOptions.DEFAULT);
        Assertions
                .assertThat(healthResponse.getStatus())
                .isNotEqualTo(ClusterHealthStatus.RED);

        // Delete the index.
        LOGGER.info("deleting the ES index");
        final DeleteIndexRequest deleteRequest =
                new DeleteIndexRequest(MavenHardcodedConstants.ES_INDEX_NAME);
        try {
            final AcknowledgedResponse deleteResponse = client
                    .indices()
                    .delete(deleteRequest, RequestOptions.DEFAULT);
            Assertions
                    .assertThat(deleteResponse.isAcknowledged())
                    .isTrue();
        } catch (ElasticsearchStatusException error) {
            Assertions.assertThat(error)
                    .satisfies(ignored -> Assertions
                            .assertThat(error.status())
                            .isEqualTo(RestStatus.NOT_FOUND));
        }

        return client;

    }
 
Example #10
Source File: BatchSearchRunner.java    From datashare with GNU Affero General Public License v3.0 4 votes vote down vote up
int run(BatchSearch batchSearch) {
    int numberOfResults = 0;
    int throttleMs = parseInt(propertiesProvider.get(BATCH_SEARCH_THROTTLE).orElse("0"));
    int maxTimeSeconds = parseInt(propertiesProvider.get(BATCH_SEARCH_MAX_TIME).orElse("100000"));
    int scrollSize = min(parseInt(propertiesProvider.get(SCROLL_SIZE).orElse("1000")), MAX_SCROLL_SIZE);

    logger.info("running {} queries for batch search {} on project {} with throttle {}ms and scroll size of {}",
            batchSearch.queries.size(), batchSearch.uuid, batchSearch.project, throttleMs, scrollSize);
    repository.setState(batchSearch.uuid, State.RUNNING);
    String query = null;
    try {
        for (String s : batchSearch.queries.keySet()) {
            query = s;
            Indexer.Searcher searcher = indexer.search(batchSearch.project.getId(), Document.class).
                    with(query, batchSearch.fuzziness, batchSearch.phraseMatches).
                    withFieldValues("contentType", batchSearch.fileTypes.toArray(new String[]{})).
                    withPrefixQuery("dirname", batchSearch.paths.toArray(new String[]{})).
                    withoutSource("content").limit(scrollSize);
            List<? extends Entity> docsToProcess = searcher.scroll().collect(toList());

            long beforeScrollLoop = DatashareTime.getInstance().currentTimeMillis();
            while (docsToProcess.size() != 0 && numberOfResults < MAX_BATCH_RESULT_SIZE - MAX_SCROLL_SIZE) {
                repository.saveResults(batchSearch.uuid, query, (List<Document>) docsToProcess);
                if (DatashareTime.getInstance().currentTimeMillis() - beforeScrollLoop < maxTimeSeconds*1000) {
                    DatashareTime.getInstance().sleep(throttleMs);
                } else {
                    throw new TimeoutException("Batch timed out after " + maxTimeSeconds + "s");
                }
                numberOfResults += docsToProcess.size();
                docsToProcess = searcher.scroll().collect(toList());
            }
        }
    } catch (ElasticsearchStatusException esEx) {
        logger.error("elasticsearch exception when running batch " + batchSearch.uuid, esEx);
        repository.setState(batchSearch.uuid, new SearchException(query,
                stream(esEx.getSuppressed()).filter(t -> t instanceof ResponseException).findFirst().orElse(esEx)));
        return numberOfResults;
    } catch (Exception ex) {
        logger.error("error when running batch " + batchSearch.uuid, ex);
        repository.setState(batchSearch.uuid, new SearchException(query, ex));
        return numberOfResults;
    }
    repository.setState(batchSearch.uuid, State.SUCCESS);
    logger.info("done batch search {} with success", batchSearch.uuid);
    return numberOfResults;
}
 
Example #11
Source File: ElasticsearchIndexerTest.java    From datashare with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test(expected = ElasticsearchStatusException.class)
public void test_tag_unknown_document() throws IOException {
    indexer.tag(project(TEST_INDEX), "unknown", "routing", tag("foo"), tag("bar"));
}
 
Example #12
Source File: ElasticSearchSinkTests.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Test(enabled = false, expectedExceptions = ElasticsearchStatusException.class)
public final void invalidIndexNameTest() throws Exception {
    map.put("indexName", "myIndex");
    sink.open(map, mockSinkContext);
}