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