Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #852 from egovernments/indexersearchIssue
Browse files Browse the repository at this point in the history
indexersearchIssue: Added ssl certificate and auth while calling Indexer
  • Loading branch information
pradeepkumarcm-egov authored Jul 8, 2024
2 parents 717c4b4 + 0016f92 commit 942a461
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,10 @@ public MappingJackson2HttpMessageConverter jacksonConverter(ObjectMapper objectM
@Value("${inbox.property.search.allowed}")
private Boolean isInboxSearchAllowed;

@Value("${egov.indexer.es.username}")
private String esUsername;

@Value("${egov.indexer.es.password}")
private String esPassword;

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
import org.springframework.web.client.RestTemplate;

import java.util.List;
import org.springframework.context.annotation.Primary;
import javax.net.ssl.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Base64;

@Component
public class ElasticSearchRepository {
Expand All @@ -24,15 +29,13 @@ public class ElasticSearchRepository {

private FuzzySearchQueryBuilder queryBuilder;

private RestTemplate restTemplate;

private ObjectMapper mapper;

@Autowired
public ElasticSearchRepository(PropertyConfiguration config, FuzzySearchQueryBuilder queryBuilder, RestTemplate restTemplate, ObjectMapper mapper) {
public ElasticSearchRepository(PropertyConfiguration config, FuzzySearchQueryBuilder queryBuilder, ObjectMapper mapper) {
this.config = config;
this.queryBuilder = queryBuilder;
this.restTemplate = restTemplate;
this.mapper = mapper;
}

Expand All @@ -52,10 +55,13 @@ public Object fuzzySearchProperties(PropertyCriteria criteria, List<String> uuid

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Authorization", getESEncodedCredentials());
final HttpEntity entity = new HttpEntity( headers);
// response = restTemplate.exchange(url.toString(), HttpMethod.GET, entity, Map.class);
HttpEntity<String> requestEntity = new HttpEntity<>(searchQuery, headers);
ResponseEntity response = null;
try {
response = restTemplate.postForEntity(url, requestEntity, Object.class);
response = this.restTemplate().postForEntity(url, requestEntity, Object.class);

} catch (Exception e) {
e.printStackTrace();
Expand All @@ -80,7 +86,45 @@ private String getESURL() {

return builder.toString();
}
public String getESEncodedCredentials() {
String credentials = config.getEsUsername() + ":" + config.getEsPassword();
byte[] credentialsBytes = credentials.getBytes();
byte[] base64CredentialsBytes = Base64.getEncoder().encode(credentialsBytes);
return "Basic " + new String(base64CredentialsBytes);
}
public static void trustSelfSignedSSL() {
try {
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
}

public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
}

public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[]{tm}, null);
SSLContext.setDefault(ctx);

// Disable hostname verification
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
return true;
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}

@Primary
public RestTemplate restTemplate() {
trustSelfSignedSSL();
return new RestTemplate();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ state.level.tenant.id=pb
#Elastic search properties
elasticsearch.host=http://localhost:9200/
elasticsearch.search.endpoint=/_search
egov.indexer.es.username=elastic
egov.indexer.es.password=8fwbD6HbJh6HU0oddsHm8TEI

property.es.index=property-services
pt.search.name.fuziness=2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,12 @@ public class WSConfiguration {
@Value("${egov.es.search.endpoint}")
private String esSearchEndpoint;

@Value("${egov.indexer.es.username}")
private String esUsername;

@Value("${egov.indexer.es.password}")
private String esPassword;

@Value("${egov.ws.search.name.fuziness}")
private String nameFuziness;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
package org.egov.waterconnection.repository;

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;
import java.util.Base64;

import org.egov.tracer.model.CustomException;
import org.egov.waterconnection.config.WSConfiguration;
import org.egov.waterconnection.repository.builder.FuzzySearchQueryBuilder;
import org.egov.waterconnection.web.models.SearchCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.fasterxml.jackson.databind.ObjectMapper;
import javax.net.ssl.*;

import org.springframework.web.client.RestTemplate;

import lombok.extern.slf4j.Slf4j;

import javax.net.ssl.*;

@Slf4j
@Component
public class ElasticSearchRepository {
Expand All @@ -26,15 +37,14 @@ public class ElasticSearchRepository {

private FuzzySearchQueryBuilder queryBuilder;

private RestTemplate restTemplate;

private ObjectMapper mapper;


@Autowired
public ElasticSearchRepository(WSConfiguration config, FuzzySearchQueryBuilder queryBuilder, RestTemplate restTemplate, ObjectMapper mapper) {
public ElasticSearchRepository(WSConfiguration config, FuzzySearchQueryBuilder queryBuilder, ObjectMapper mapper) {
this.config = config;
this.queryBuilder = queryBuilder;
this.restTemplate = restTemplate;
this.mapper = mapper;
}

Expand All @@ -54,10 +64,13 @@ public Object fuzzySearchProperties(SearchCriteria criteria, List<String> ids) {

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Authorization", getESEncodedCredentials());
final HttpEntity entity = new HttpEntity( headers);
// response = restTemplate.exchange(url.toString(), HttpMethod.GET, entity, Map.class);
HttpEntity<String> requestEntity = new HttpEntity<>(searchQuery, headers);
ResponseEntity response = null;
try {
response = restTemplate.postForEntity(url, requestEntity, Object.class);
response = this.restTemplate().postForEntity(url, requestEntity, Object.class);

} catch (Exception e) {
log.error("Failed to fetch data from ES: "+e.getMessage());
Expand All @@ -82,7 +95,44 @@ private String getESURL() {

return builder.toString();
}
public String getESEncodedCredentials() {
String credentials = config.getEsUsername() + ":" + config.getEsPassword();
byte[] credentialsBytes = credentials.getBytes();
byte[] base64CredentialsBytes = Base64.getEncoder().encode(credentialsBytes);
return "Basic " + new String(base64CredentialsBytes);
}
public static void trustSelfSignedSSL() {
try {
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
}

public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
}

public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[]{tm}, null);
SSLContext.setDefault(ctx);

// Disable hostname verification
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
return true;
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}


@Primary
public RestTemplate restTemplate() {
trustSelfSignedSSL();
return new RestTemplate();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,7 @@ sms.edit.water.connection.notification.enabled: true
sms.payment.notification.enabled: true
sms.feedback.notification.enabled: false
sms.workflow.enabled: true

#ES-config
egov.indexer.es.username=elastic
egov.indexer.es.password=8fwbD6HbJh6HU0oddsHm8TEI

0 comments on commit 942a461

Please sign in to comment.