Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add https support for communication with elastic search #204

Merged
merged 4 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ services:
- event_topic=events
- event_providerName=dev.sunbirdrc.registry.service.impl.KafkaEventService
- elastic_search_connection_url=es:9200
- elastic_search_scheme=${ELASTIC_SEARCH_SCHEME-http}
- elastic_search_auth_enabled=${ELASTIC_SECURITY_ENABLED-false}
- elastic_search_username=${ELASTIC_SEARCH_USERNAME-elastic}
- elastic_search_password=${ELASTIC_SEARCH_PASSWORD}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
import dev.sunbirdrc.registry.middleware.util.JSONUtil;
import java.io.IOException;
import java.net.ConnectException;
import java.net.URL;
import java.util.*;

import org.apache.commons.collections4.KeyValue;
import org.apache.commons.collections4.keyvalue.DefaultKeyValue;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
Expand Down Expand Up @@ -57,6 +60,7 @@ public class ElasticServiceImpl implements IElasticService {
private static boolean authEnabled;
private static String userName;
private static String password;
private static String defaultScheme;

public void setConnectionInfo(String connection) {
connectionInfo = connection;
Expand Down Expand Up @@ -93,13 +97,16 @@ private static void createClient(String indexName, String connectionInfo) {
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(userName, password));
if (!esClient.containsKey(indexName)) {
Map<String, Integer> hostPort = new HashMap<String, Integer>();
for (String info : connectionInfo.split(",")) {
hostPort.put(info.split(":")[0], Integer.valueOf(info.split(":")[1]));
}
Map<String, KeyValue<Integer, String>> hostPort = new HashMap<>();
varadeth marked this conversation as resolved.
Show resolved Hide resolved
List<HttpHost> httpHosts = new ArrayList<>();
for (String host : hostPort.keySet()) {
httpHosts.add(new HttpHost(host, hostPort.get(host)));
for (String info : connectionInfo.split(",")) {
try {
URL url = new URL(info);
httpHosts.add(new HttpHost(url.getHost(), url.getPort(), url.getProtocol()));
} catch (Exception e) {
String port = Optional.ofNullable(info.split(":").length == 1 ? "-1" : info.split(":")[1]).get();
httpHosts.add(new HttpHost(info.split(":")[0], Integer.valueOf(port), defaultScheme));
}
}
RestClientBuilder restClientBuilder = RestClient.builder(httpHosts.toArray(new HttpHost[httpHosts.size()]));
if(authEnabled) {
Expand All @@ -111,6 +118,10 @@ private static void createClient(String indexName, String connectionInfo) {
}
}

public void setScheme(String scheme) {
this.defaultScheme = scheme;
}

/**
* Get client details from map
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ public class GenericConfiguration implements WebMvcConfigurer {
private String schemaUrl;
@Value("${httpConnection.maxConnections:5}")
private int httpMaxConnections;
@Value("${elastic.search.scheme}")
private String scheme;
@Autowired
private DBConnectionInfoMgr dbConnectionInfoMgr;

Expand Down Expand Up @@ -421,6 +423,7 @@ public IElasticService elasticService() throws IOException {
elasticService.setAuthEnabled(Boolean.parseBoolean(authEnabled));
elasticService.setUserName(username);
elasticService.setPassword(password);
elasticService.setScheme(scheme);
elasticService.init(iDefinitionsManager.getAllKnownDefinitions());
}
return elasticService;
Expand Down
1 change: 1 addition & 0 deletions java/registry/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ elastic:
auth_enabled: ${elastic_search_auth_enabled:false}
elastic_username: ${elastic_search_username:elastic}
elastic_password: ${elastic_search_password:elastic}
scheme: ${elastic_search_scheme:http}
filestorage:
url: ${filestorage_connection_url:http://localhost:9000}
accesskey: ${filestorage_access_key:XXXXX}
Expand Down