Cert problem solved
authorVásáry Dániel <vasary@elgekko.net>
Wed, 6 Mar 2024 17:29:56 +0000 (18:29 +0100)
committerVásáry Dániel <vasary@elgekko.net>
Wed, 6 Mar 2024 17:29:56 +0000 (18:29 +0100)
sly-crm-app/src/main/resources/keystore/create-keystore.bat
sly-crm-app/src/main/resources/keystore/lis-keystore.jks
sly-crm-app/src/test/java/hu/user/lis/CerttIT.java
sly-crm-service/src/main/java/hu/user/lis/service/nav/RestTemplateSSLConfiguration.java
sly-crm-service/src/main/java/hu/user/lis/service/nav/TaxOfficeConnector.java

index 93358a72cb4c1aac9bf1178a65844e1466812f0e..06eb43d75ed7c39e08bf7d878c9bef1ae2caae42 100644 (file)
@@ -1,4 +1,4 @@
-keytool -importcert -keystore lis-keystore.jks -file eszigno.cer -alias eszigno -storepass password
-keytool -importcert -keystore lis-keystore.jks -file onlineszamla.nav.gov.hu.cer -alias nav -storepass password
+:keytool -storepasswd -keystore lis-keystore.jks
+:keytool -importcert -keystore lis-keystore.jks -file eszigno.cer -alias eszigno -storepass password
+keytool -importcert -keystore lis-keystore.jks -file onlineszamla.nav.gov.hu.cer -alias nav -storepass password -noprompt
 
-:
index 3017d324f295be4149eb7e863c6a135f052d438d..e43abcef501001498311159504fe9ea0fb499bf0 100644 (file)
Binary files a/sly-crm-app/src/main/resources/keystore/lis-keystore.jks and b/sly-crm-app/src/main/resources/keystore/lis-keystore.jks differ
index 63010717112b4b080f6f85ee6ce5c8f8efdbd425..2d212ceff17f1d3c2fe569ebc114938b72d4538e 100644 (file)
@@ -37,26 +37,22 @@ public class CerttIT {
     @Value("${service.nav.trust.store.password}")
     private String trustStorePassword;
 
-    @Test
-    public void testCert() throws Exception {
-        KeyStore keyStore = KeyStore.getInstance("JKS");
-        keyStore.load(trustStore.getInputStream(), trustStorePassword.toCharArray());
-        X509Certificate endEntityCertificate = (X509Certificate) keyStore.getCertificate("nav");
-        X509Certificate rootCertificate = getRootCertificate(endEntityCertificate, keyStore);
-        assertNotNull(rootCertificate);
-    }
-
     static X509Certificate findIssuerCertificate(X509Certificate certificate, KeyStore trustStore)
             throws KeyStoreException {
         Enumeration<String> aliases = trustStore.aliases();
+        log.info("Search for {}", certificate.getIssuerX500Principal());
         while (aliases.hasMoreElements()) {
             String alias = aliases.nextElement();
+            log.info("Current alias {}", alias);
             Certificate cert = trustStore.getCertificate(alias);
             if (cert instanceof X509Certificate) {
                 X509Certificate x509Cert = (X509Certificate) cert;
+                log.info("Checking {}", x509Cert.getSubjectX500Principal());
                 if (x509Cert.getSubjectX500Principal().equals(certificate.getIssuerX500Principal())) {
                     return x509Cert;
                 }
+            } else {
+                log.error("Not X509Certificate alias {}", alias);
             }
         }
         return null;
@@ -84,4 +80,13 @@ public class CerttIT {
         }
     }
 
+    @Test
+    public void testCert() throws Exception {
+        KeyStore keyStore = KeyStore.getInstance("JKS");
+        keyStore.load(trustStore.getInputStream(), trustStorePassword.toCharArray());
+        X509Certificate endEntityCertificate = (X509Certificate) keyStore.getCertificate("eszigno");
+        X509Certificate rootCertificate = getRootCertificate(endEntityCertificate, keyStore);
+        assertNotNull(rootCertificate);
+    }
+
 }
index 1f3eb91c107b69820784edb1d387a335fc467a87..912e2229940e00378f6f0c4387ef27a9b4c29b5c 100644 (file)
@@ -3,7 +3,7 @@ package hu.user.lis.service.nav;
 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClients;
-import org.apache.http.ssl.SSLContextBuilder;
+import org.apache.http.ssl.TrustStrategy;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
@@ -12,11 +12,10 @@ import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
 import org.springframework.web.client.RestTemplate;
 
 import javax.net.ssl.SSLContext;
-import java.io.IOException;
 import java.security.KeyManagementException;
 import java.security.KeyStoreException;
 import java.security.NoSuchAlgorithmException;
-import java.security.cert.CertificateException;
+import java.security.cert.X509Certificate;
 
 @Configuration
 public class RestTemplateSSLConfiguration {
@@ -28,17 +27,16 @@ public class RestTemplateSSLConfiguration {
     private String trustStorePassword;
 
     @Bean
-    public RestTemplate restTemplate() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException,
-            CertificateException, IOException {
+    public RestTemplate restTemplate() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
 
 
-        SSLContext sslContext = new SSLContextBuilder()
-                .loadTrustMaterial(trustStore.getURL(), trustStorePassword.toCharArray()).build();
-        SSLConnectionSocketFactory sslConFactory = new SSLConnectionSocketFactory(sslContext);
+        TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
+        SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
+        SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
+        CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(csf).build();
+        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
+        requestFactory.setHttpClient(httpClient);
 
-        CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslConFactory).build();
-        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
-        //requestFactory.setReadTimeout(15000);
 
         return new RestTemplate(requestFactory);
     }
index 32bc81aa5e54d6023726cdd7a53c0f4ed71b3532..a83122828179893dc3e3572b31a6881c3ee67813 100644 (file)
@@ -1,6 +1,9 @@
 package hu.user.lis.service.nav;
 
 import io.netty.channel.ChannelOption;
+import io.netty.handler.ssl.SslContext;
+import io.netty.handler.ssl.SslContextBuilder;
+import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
 import io.netty.handler.timeout.ReadTimeoutHandler;
 import io.netty.handler.timeout.WriteTimeoutHandler;
 import lombok.Getter;
@@ -25,13 +28,20 @@ public class TaxOfficeConnector {
     private final WebClient client;
 
     @Autowired
-    public TaxOfficeConnector(WebClient.Builder webClientBuilder, @Value("${service.nav.api.url}") String apiUrl) {
+    public TaxOfficeConnector(WebClient.Builder webClientBuilder, @Value("${service.nav.api.url}") String apiUrl) throws Exception {
+
+        SslContext sslContext = SslContextBuilder
+                .forClient()
+                .trustManager(InsecureTrustManagerFactory.INSTANCE)
+                .build();
+
         HttpClient httpClient = HttpClient.create()
+                .secure(t -> t.sslContext(sslContext))
                 .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000)
-                .responseTimeout(Duration.ofMillis(5000))
+                .responseTimeout(Duration.ofMillis(15000))
                 .doOnConnected(conn -> conn
-                        .addHandlerLast(new ReadTimeoutHandler(5000, TimeUnit.MILLISECONDS))
-                        .addHandlerLast(new WriteTimeoutHandler(5000, TimeUnit.MILLISECONDS))
+                        .addHandlerLast(new ReadTimeoutHandler(15000, TimeUnit.MILLISECONDS))
+                        .addHandlerLast(new WriteTimeoutHandler(15000, TimeUnit.MILLISECONDS))
                 );
 
         client = webClientBuilder