Repository: incubator-griffin Updated Branches: refs/heads/master e70347107 -> 530c2dafc
MetricStoreImpl enhancement for Elasticsearch connections. **MetricStoreImpl enhancement for Elasticsearch connections.** #1. Support http/https scheme and basic authentication for elasticsearch configuration in MetricStoreImpl. Author: Ding, Minyi <âd...@outlook.comâ> Closes #204 from dingminyi/MetricStoreImplEnhance. Project: http://git-wip-us.apache.org/repos/asf/incubator-griffin/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-griffin/commit/530c2daf Tree: http://git-wip-us.apache.org/repos/asf/incubator-griffin/tree/530c2daf Diff: http://git-wip-us.apache.org/repos/asf/incubator-griffin/diff/530c2daf Branch: refs/heads/master Commit: 530c2dafc400b30f3266ac5b737cfbd41daa1ddd Parents: e703471 Author: Ding, Minyi <âd...@outlook.comâ> Authored: Thu Feb 1 16:00:48 2018 +0800 Committer: Lionel Liu <bhlx3l...@163.com> Committed: Thu Feb 1 16:00:48 2018 +0800 ---------------------------------------------------------------------- .../griffin/core/metric/MetricStoreImpl.java | 36 ++++++++++++++++---- .../src/main/resources/application.properties | 5 ++- .../core/metric/MetricStoreImplTest.java | 20 +++++++++++ .../src/test/resources/application.properties | 5 ++- 4 files changed, 57 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-griffin/blob/530c2daf/service/src/main/java/org/apache/griffin/core/metric/MetricStoreImpl.java ---------------------------------------------------------------------- diff --git a/service/src/main/java/org/apache/griffin/core/metric/MetricStoreImpl.java b/service/src/main/java/org/apache/griffin/core/metric/MetricStoreImpl.java index 4ddf581..b641cc7 100644 --- a/service/src/main/java/org/apache/griffin/core/metric/MetricStoreImpl.java +++ b/service/src/main/java/org/apache/griffin/core/metric/MetricStoreImpl.java @@ -25,13 +25,16 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.griffin.core.metric.model.MetricValue; import org.apache.griffin.core.util.JsonUtil; +import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.entity.ContentType; +import org.apache.http.message.BasicHeader; import org.apache.http.nio.entity.NStringEntity; import org.apache.http.util.EntityUtils; import org.elasticsearch.client.Response; import org.elasticsearch.client.RestClient; +import org.elasticsearch.client.RestClientBuilder; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; @@ -40,6 +43,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import java.io.IOException; +import java.nio.charset.Charset; import java.util.*; @Component @@ -50,17 +54,29 @@ public class MetricStoreImpl implements MetricStore { private static final String URL_BASE = "/griffin/accuracy"; private RestClient client; - private HttpHeaders headers; + private HttpHeaders responseHeaders; private String url_get; private String url_delete; private String url_post; private ObjectMapper mapper; - public MetricStoreImpl(@Value("${elasticsearch.host}") String host, @Value("${elasticsearch.port}") int port) { - client = RestClient.builder(new HttpHost(host, port, "http")).build(); - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_JSON); - this.headers = headers; + public MetricStoreImpl(@Value("${elasticsearch.host}") String host, + @Value("${elasticsearch.port}") int port, + @Value("${elasticsearch.scheme:http}") String scheme, + @Value("${elasticsearch.user:}") String user, + @Value("${elasticsearch.password:}") String password) { + HttpHost httpHost = new HttpHost(host, port, scheme); + RestClientBuilder builder = RestClient.builder(httpHost); + if (!user.isEmpty() && !password.isEmpty()) { + String encodedAuth = buildBasicAuthString(user, password); + Header[] requestHeaders = new Header[]{ + new BasicHeader(org.apache.http.HttpHeaders.AUTHORIZATION, encodedAuth)}; + builder.setDefaultHeaders(requestHeaders); + } + client = builder.build(); + HttpHeaders responseHeaders = new HttpHeaders(); + responseHeaders.setContentType(MediaType.APPLICATION_JSON); + this.responseHeaders = responseHeaders; this.url_get = URL_BASE + "/_search?filter_path=hits.hits._source"; this.url_post = URL_BASE + "/_bulk"; this.url_delete = URL_BASE + "/_delete_by_query"; @@ -132,6 +148,12 @@ public class MetricStoreImpl implements MetricStore { private ResponseEntity getResponseEntityFromResponse(Response response) throws IOException { String body = EntityUtils.toString(response.getEntity()); HttpStatus status = HttpStatus.valueOf(response.getStatusLine().getStatusCode()); - return new ResponseEntity<>(body, headers, status); + return new ResponseEntity<>(body, responseHeaders, status); + } + + private static String buildBasicAuthString (String user, String password) { + String auth = user + ":" + password; + String encodedAuth = "Basic " + Base64.getEncoder().encodeToString(auth.getBytes()); + return encodedAuth; } } http://git-wip-us.apache.org/repos/asf/incubator-griffin/blob/530c2daf/service/src/main/resources/application.properties ---------------------------------------------------------------------- diff --git a/service/src/main/resources/application.properties b/service/src/main/resources/application.properties index c7b57be..b04f304 100644 --- a/service/src/main/resources/application.properties +++ b/service/src/main/resources/application.properties @@ -67,4 +67,7 @@ fs.defaultFS = hdfs://hdfs-default-name # elasticsearch elasticsearch.host = localhost -elasticsearch.port = 9200 \ No newline at end of file +elasticsearch.port = 9200 +elasticsearch.scheme = http +# elasticsearch.user = user +# elasticsearch.password = password \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-griffin/blob/530c2daf/service/src/test/java/org/apache/griffin/core/metric/MetricStoreImplTest.java ---------------------------------------------------------------------- diff --git a/service/src/test/java/org/apache/griffin/core/metric/MetricStoreImplTest.java b/service/src/test/java/org/apache/griffin/core/metric/MetricStoreImplTest.java new file mode 100644 index 0000000..2ff5a3f --- /dev/null +++ b/service/src/test/java/org/apache/griffin/core/metric/MetricStoreImplTest.java @@ -0,0 +1,20 @@ +package org.apache.griffin.core.metric; + +import org.junit.Test; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import static org.junit.Assert.*; + +public class MetricStoreImplTest { + + @Test + public void testBuildBasicAuthString() + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + Method m = MetricStoreImpl.class.getDeclaredMethod("buildBasicAuthString", String.class, String.class); + m.setAccessible(true); + String authStr = (String) m.invoke(null, "user", "password"); + assertTrue(authStr.equals("Basic dXNlcjpwYXNzd29yZA==")); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-griffin/blob/530c2daf/service/src/test/resources/application.properties ---------------------------------------------------------------------- diff --git a/service/src/test/resources/application.properties b/service/src/test/resources/application.properties index ebd6a41..02df856 100644 --- a/service/src/test/resources/application.properties +++ b/service/src/test/resources/application.properties @@ -67,4 +67,7 @@ fs.defaultFS = hdfs://hdfs-default-name # elasticsearch elasticsearch.host = localhost -elasticsearch.port = 9200 \ No newline at end of file +elasticsearch.port = 9200 +elasticsearch.scheme = http +# elasticsearch.user = user +# elasticsearch.password = password \ No newline at end of file