From aba0a5fe3a6b0fe03c19a86a25c037e3f64328c7 Mon Sep 17 00:00:00 2001 From: debasishchakraborty-egovt Date: Mon, 8 Jul 2024 11:14:12 +0530 Subject: [PATCH 1/7] indexersearchIssue: Added ssl certificate and auth while calling Indexer --- .../WaterConnectionApplication.java | 38 +++++++++++++++++++ .../config/WSConfiguration.java | 6 +++ .../repository/ElasticSearchRepository.java | 11 +++++- .../src/main/resources/application.properties | 4 ++ 4 files changed, 58 insertions(+), 1 deletion(-) diff --git a/municipal-services/ws-services/src/main/java/org/egov/waterconnection/WaterConnectionApplication.java b/municipal-services/ws-services/src/main/java/org/egov/waterconnection/WaterConnectionApplication.java index 11e2bd9f7..961f3adc5 100644 --- a/municipal-services/ws-services/src/main/java/org/egov/waterconnection/WaterConnectionApplication.java +++ b/municipal-services/ws-services/src/main/java/org/egov/waterconnection/WaterConnectionApplication.java @@ -3,6 +3,10 @@ import java.util.TimeZone; import javax.annotation.PostConstruct; +import javax.net.ssl.*; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; +import org.springframework.web.client.RestTemplate; import org.egov.tracer.config.TracerConfiguration; import org.springframework.beans.factory.annotation.Value; @@ -33,10 +37,44 @@ public static void main(String[] args) { } + 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(); + } + } + @Bean public ObjectMapper objectMapper() { return new ObjectMapper().configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true) .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES).setTimeZone(TimeZone.getTimeZone(timeZone)); } + @Bean + public RestTemplate restTemplate() { + trustSelfSignedSSL(); + return new RestTemplate(); + } + } diff --git a/municipal-services/ws-services/src/main/java/org/egov/waterconnection/config/WSConfiguration.java b/municipal-services/ws-services/src/main/java/org/egov/waterconnection/config/WSConfiguration.java index 1ee4ced6d..7f418bd30 100644 --- a/municipal-services/ws-services/src/main/java/org/egov/waterconnection/config/WSConfiguration.java +++ b/municipal-services/ws-services/src/main/java/org/egov/waterconnection/config/WSConfiguration.java @@ -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; diff --git a/municipal-services/ws-services/src/main/java/org/egov/waterconnection/repository/ElasticSearchRepository.java b/municipal-services/ws-services/src/main/java/org/egov/waterconnection/repository/ElasticSearchRepository.java index dfca87fa5..3351dd860 100644 --- a/municipal-services/ws-services/src/main/java/org/egov/waterconnection/repository/ElasticSearchRepository.java +++ b/municipal-services/ws-services/src/main/java/org/egov/waterconnection/repository/ElasticSearchRepository.java @@ -1,6 +1,7 @@ package org.egov.waterconnection.repository; import java.util.List; +import java.util.Base64; import org.egov.tracer.model.CustomException; import org.egov.waterconnection.config.WSConfiguration; @@ -54,6 +55,9 @@ public Object fuzzySearchProperties(SearchCriteria criteria, List 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 requestEntity = new HttpEntity<>(searchQuery, headers); ResponseEntity response = null; try { @@ -82,7 +86,12 @@ 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); + } } diff --git a/municipal-services/ws-services/src/main/resources/application.properties b/municipal-services/ws-services/src/main/resources/application.properties index 8fb78d02c..15949ae21 100644 --- a/municipal-services/ws-services/src/main/resources/application.properties +++ b/municipal-services/ws-services/src/main/resources/application.properties @@ -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 From 0d18c4dff8dc7007ec30a6cb95d759f6e3b20562 Mon Sep 17 00:00:00 2001 From: debasishchakraborty-egovt Date: Mon, 8 Jul 2024 11:53:59 +0530 Subject: [PATCH 2/7] indexersearchIssue: Added ssl certificate and auth while calling Indexer --- .../WaterConnectionApplication.java | 38 -------------- .../repository/ElasticSearchRepository.java | 49 +++++++++++++++++-- 2 files changed, 44 insertions(+), 43 deletions(-) diff --git a/municipal-services/ws-services/src/main/java/org/egov/waterconnection/WaterConnectionApplication.java b/municipal-services/ws-services/src/main/java/org/egov/waterconnection/WaterConnectionApplication.java index 961f3adc5..11e2bd9f7 100644 --- a/municipal-services/ws-services/src/main/java/org/egov/waterconnection/WaterConnectionApplication.java +++ b/municipal-services/ws-services/src/main/java/org/egov/waterconnection/WaterConnectionApplication.java @@ -3,10 +3,6 @@ import java.util.TimeZone; import javax.annotation.PostConstruct; -import javax.net.ssl.*; -import java.security.cert.CertificateException; -import java.security.cert.X509Certificate; -import org.springframework.web.client.RestTemplate; import org.egov.tracer.config.TracerConfiguration; import org.springframework.beans.factory.annotation.Value; @@ -37,44 +33,10 @@ public static void main(String[] args) { } - 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(); - } - } - @Bean public ObjectMapper objectMapper() { return new ObjectMapper().configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true) .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES).setTimeZone(TimeZone.getTimeZone(timeZone)); } - @Bean - public RestTemplate restTemplate() { - trustSelfSignedSSL(); - return new RestTemplate(); - } - } diff --git a/municipal-services/ws-services/src/main/java/org/egov/waterconnection/repository/ElasticSearchRepository.java b/municipal-services/ws-services/src/main/java/org/egov/waterconnection/repository/ElasticSearchRepository.java index 3351dd860..e872b34d7 100644 --- a/municipal-services/ws-services/src/main/java/org/egov/waterconnection/repository/ElasticSearchRepository.java +++ b/municipal-services/ws-services/src/main/java/org/egov/waterconnection/repository/ElasticSearchRepository.java @@ -1,5 +1,7 @@ package org.egov.waterconnection.repository; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; import java.util.List; import java.util.Base64; @@ -8,16 +10,22 @@ import org.egov.waterconnection.repository.builder.FuzzySearchQueryBuilder; import org.egov.waterconnection.web.models.SearchCriteria; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; 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 { @@ -27,15 +35,13 @@ 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; } @@ -61,7 +67,8 @@ public Object fuzzySearchProperties(SearchCriteria criteria, List ids) { HttpEntity 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()); @@ -92,6 +99,38 @@ public String getESEncodedCredentials() { 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(); + } + } + @Bean + public RestTemplate restTemplate() { + trustSelfSignedSSL(); + return new RestTemplate(); + } } From 17b93ef0fa4dfb08efc9b044c644b16433ad0319 Mon Sep 17 00:00:00 2001 From: debasishchakraborty-egovt Date: Mon, 8 Jul 2024 12:14:37 +0530 Subject: [PATCH 3/7] indexersearchIssue: Added ssl certificate and auth while calling Indexer --- .../waterconnection/repository/ElasticSearchRepository.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/municipal-services/ws-services/src/main/java/org/egov/waterconnection/repository/ElasticSearchRepository.java b/municipal-services/ws-services/src/main/java/org/egov/waterconnection/repository/ElasticSearchRepository.java index e872b34d7..cb20bda74 100644 --- a/municipal-services/ws-services/src/main/java/org/egov/waterconnection/repository/ElasticSearchRepository.java +++ b/municipal-services/ws-services/src/main/java/org/egov/waterconnection/repository/ElasticSearchRepository.java @@ -38,6 +38,7 @@ public class ElasticSearchRepository { private ObjectMapper mapper; + @Autowired public ElasticSearchRepository(WSConfiguration config, FuzzySearchQueryBuilder queryBuilder, ObjectMapper mapper) { this.config = config; @@ -67,7 +68,6 @@ public Object fuzzySearchProperties(SearchCriteria criteria, List ids) { HttpEntity requestEntity = new HttpEntity<>(searchQuery, headers); ResponseEntity response = null; try { - response = this.restTemplate().postForEntity(url, requestEntity, Object.class); } catch (Exception e) { @@ -127,7 +127,7 @@ public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) { } } - @Bean + @Bean(name="esRestTemplate") public RestTemplate restTemplate() { trustSelfSignedSSL(); return new RestTemplate(); From af1d6c665f012dd8d28aca07bac43d6ae83df419 Mon Sep 17 00:00:00 2001 From: debasishchakraborty-egovt Date: Mon, 8 Jul 2024 12:27:25 +0530 Subject: [PATCH 4/7] indexersearchIssue: Added ssl certificate and auth while calling Indexer --- .../waterconnection/repository/ElasticSearchRepository.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/municipal-services/ws-services/src/main/java/org/egov/waterconnection/repository/ElasticSearchRepository.java b/municipal-services/ws-services/src/main/java/org/egov/waterconnection/repository/ElasticSearchRepository.java index cb20bda74..f64ff1f49 100644 --- a/municipal-services/ws-services/src/main/java/org/egov/waterconnection/repository/ElasticSearchRepository.java +++ b/municipal-services/ws-services/src/main/java/org/egov/waterconnection/repository/ElasticSearchRepository.java @@ -10,6 +10,7 @@ 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.http.HttpEntity; import org.springframework.http.HttpHeaders; @@ -68,7 +69,7 @@ public Object fuzzySearchProperties(SearchCriteria criteria, List ids) { HttpEntity requestEntity = new HttpEntity<>(searchQuery, headers); ResponseEntity response = null; try { - response = this.restTemplate().postForEntity(url, requestEntity, Object.class); + response = this.esRestTemplate().postForEntity(url, requestEntity, Object.class); } catch (Exception e) { log.error("Failed to fetch data from ES: "+e.getMessage()); @@ -128,7 +129,7 @@ public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) { } @Bean(name="esRestTemplate") - public RestTemplate restTemplate() { + public RestTemplate esRestTemplate() { trustSelfSignedSSL(); return new RestTemplate(); } From 70544212f3b5c817ba33d78a42360d033a00db42 Mon Sep 17 00:00:00 2001 From: debasishchakraborty-egovt Date: Mon, 8 Jul 2024 12:42:06 +0530 Subject: [PATCH 5/7] indexersearchIssue: Added ssl certificate and auth while calling Indexer --- .../waterconnection/repository/ElasticSearchRepository.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/municipal-services/ws-services/src/main/java/org/egov/waterconnection/repository/ElasticSearchRepository.java b/municipal-services/ws-services/src/main/java/org/egov/waterconnection/repository/ElasticSearchRepository.java index f64ff1f49..9106f6bc8 100644 --- a/municipal-services/ws-services/src/main/java/org/egov/waterconnection/repository/ElasticSearchRepository.java +++ b/municipal-services/ws-services/src/main/java/org/egov/waterconnection/repository/ElasticSearchRepository.java @@ -12,6 +12,7 @@ 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; @@ -129,6 +130,7 @@ public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) { } @Bean(name="esRestTemplate") + @Primary public RestTemplate esRestTemplate() { trustSelfSignedSSL(); return new RestTemplate(); From d03bf8f8d21c3df85a9c2aaee2655dbf430272bc Mon Sep 17 00:00:00 2001 From: debasishchakraborty-egovt Date: Mon, 8 Jul 2024 13:09:21 +0530 Subject: [PATCH 6/7] indexersearchIssue: Added ssl certificate and auth while calling Indexer --- .../waterconnection/repository/ElasticSearchRepository.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/municipal-services/ws-services/src/main/java/org/egov/waterconnection/repository/ElasticSearchRepository.java b/municipal-services/ws-services/src/main/java/org/egov/waterconnection/repository/ElasticSearchRepository.java index 9106f6bc8..4042ef5a1 100644 --- a/municipal-services/ws-services/src/main/java/org/egov/waterconnection/repository/ElasticSearchRepository.java +++ b/municipal-services/ws-services/src/main/java/org/egov/waterconnection/repository/ElasticSearchRepository.java @@ -70,7 +70,7 @@ public Object fuzzySearchProperties(SearchCriteria criteria, List ids) { HttpEntity requestEntity = new HttpEntity<>(searchQuery, headers); ResponseEntity response = null; try { - response = this.esRestTemplate().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()); @@ -129,9 +129,8 @@ public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) { } } - @Bean(name="esRestTemplate") @Primary - public RestTemplate esRestTemplate() { + public RestTemplate restTemplate() { trustSelfSignedSSL(); return new RestTemplate(); } From 0016f92642d97e34fd2b49492d178d217b6250ad Mon Sep 17 00:00:00 2001 From: debasishchakraborty-egovt Date: Mon, 8 Jul 2024 13:33:47 +0530 Subject: [PATCH 7/7] Added Chnages in Es-search in Property service --- .../egov/pt/config/PropertyConfiguration.java | 6 +++ .../repository/ElasticSearchRepository.java | 52 +++++++++++++++++-- .../src/main/resources/application.properties | 2 + 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/municipal-services/property-services/src/main/java/org/egov/pt/config/PropertyConfiguration.java b/municipal-services/property-services/src/main/java/org/egov/pt/config/PropertyConfiguration.java index 7eb520b6a..5e1241895 100644 --- a/municipal-services/property-services/src/main/java/org/egov/pt/config/PropertyConfiguration.java +++ b/municipal-services/property-services/src/main/java/org/egov/pt/config/PropertyConfiguration.java @@ -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; + } \ No newline at end of file diff --git a/municipal-services/property-services/src/main/java/org/egov/pt/repository/ElasticSearchRepository.java b/municipal-services/property-services/src/main/java/org/egov/pt/repository/ElasticSearchRepository.java index 0dbeec52f..d28d18d76 100644 --- a/municipal-services/property-services/src/main/java/org/egov/pt/repository/ElasticSearchRepository.java +++ b/municipal-services/property-services/src/main/java/org/egov/pt/repository/ElasticSearchRepository.java @@ -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 { @@ -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; } @@ -52,10 +55,13 @@ public Object fuzzySearchProperties(PropertyCriteria criteria, List 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 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(); @@ -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(); + } } diff --git a/municipal-services/property-services/src/main/resources/application.properties b/municipal-services/property-services/src/main/resources/application.properties index bce5a957f..759f658ea 100644 --- a/municipal-services/property-services/src/main/resources/application.properties +++ b/municipal-services/property-services/src/main/resources/application.properties @@ -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