<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Keystore on Micha Kops&#39; Tech Notes</title>
    <link>https://www.hascode.com/tags/keystore/</link>
    <description>Recent content in Keystore on Micha Kops&#39; Tech Notes</description>
    <generator>Hugo</generator>
    <language>en</language>
    <copyright>Copyright © 2010 - 2025 Micha Kops. #e9d956c0c0154a221ad83c925346a8fa0e72f866</copyright>
    <lastBuildDate>Tue, 02 Aug 2022 00:00:00 +0200</lastBuildDate>
    <atom:link href="https://www.hascode.com/tags/keystore/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Single Class Java HTTP Client with Proxy and SSL/TLS Keystore Settings</title>
      <link>https://www.hascode.com/single-class-java-http-client-with-proxy-and-ssl/tls-keystore-settings/</link>
      <pubDate>Tue, 02 Aug 2022 00:00:00 +0200</pubDate>
      <guid>https://www.hascode.com/single-class-java-http-client-with-proxy-and-ssl/tls-keystore-settings/</guid>
      <description>&lt;div class=&#34;paragraph&#34;&gt;
&lt;p&gt;Sometimes this is useful for the diagnosis of configuration and network problems of ones Java application.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&#34;paragraph&#34;&gt;
&lt;p&gt;This is our single-class HTTP client example without the need for external dependencies:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&#34;listingblock&#34;&gt;
&lt;div class=&#34;title&#34;&gt;HttpTest.java&lt;/div&gt;
&lt;div class=&#34;content&#34;&gt;
&lt;pre class=&#34;highlight&#34;&gt;&lt;code class=&#34;language-java&#34; data-lang=&#34;java&#34;&gt;import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.time.Duration;
import java.time.temporal.ChronoUnit;

public class HttpTest {
    public static void main(String[] args)
        throws URISyntaxException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException, IOException, CertificateException {
        String bodyPayload = &amp;#34;&amp;#34;&amp;#34;
            ourpayload, json, xml, ...
            &amp;#34;&amp;#34;&amp;#34;;

        String keyStorePath = &amp;#34;/opt/keystore.jks&amp;#34;;
        String keyStorePassword = &amp;#34;ABCDEFG&amp;#34;;

        String proxyHost = &amp;#34;ourproxy.proxy&amp;#34;;
        String uriString = &amp;#34;https://some-service/api&amp;#34;;
        int proxyPort = 8080;
        int timeoutInSeconds = 60;

        KeyStore keyStore = KeyStore.getInstance(&amp;#34;JKS&amp;#34;);
        keyStore.load(new FileInputStream(keyStorePath), keyStorePassword.toCharArray());

        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(&amp;#34;PKIX&amp;#34;);
        keyManagerFactory.init(keyStore, null);

        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(&amp;#34;PKIX&amp;#34;); // using same keystore for both
        trustManagerFactory.init(keyStore);

        SSLContext sslContext = SSLContext.getInstance(&amp;#34;TLS&amp;#34;);
        sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);

        HttpClient client = HttpClient.newBuilder()
                                      .proxy(
                                          ProxySelector.of(new InetSocketAddress(proxyHost, proxyPort)))
                                      .sslContext(sslContext)
                                      .build();
        URI uri = new URI(uriString);
        HttpRequest request = HttpRequest.newBuilder(uri)
                                         .POST(BodyPublishers.ofString(bodyPayload))
                                         .timeout(Duration.of(timeoutInSeconds, ChronoUnit.SECONDS))
                                         .build();
        System.out.printf(&amp;#34;Sending POST request to %s, timeout: %ds%n&amp;#34;, uri, timeoutInSeconds);
        try {
            HttpResponse response = client.send(request, BodyHandlers.ofString());
            System.out.println(&amp;#34;Response received...&amp;#34;);
            System.out.printf(&amp;#34;\tResponse-Status: %d%n&amp;#34;, response.statusCode());
            System.out.printf(&amp;#34;\tResponse-Body: %s%n&amp;#34;, response.body());
            System.out.println(&amp;#34;-------------------------------------%n&amp;#34;);

        } catch (IOException e) {
            System.err.printf(&amp;#34;IOException caught: %s%n&amp;#34;, e.getMessage());
            e.printStackTrace(System.err);
        } catch (InterruptedException e) {
            System.err.printf(&amp;#34;IOException caught: %s%n&amp;#34;, e.getMessage());
            e.printStackTrace(System.err);
        }
    }
}&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;</description>
    </item>
    <item>
      <title>Java Snippets</title>
      <link>https://www.hascode.com/java-snippets/</link>
      <pubDate>Mon, 01 Mar 2010 00:00:00 +0100</pubDate>
      <guid>https://www.hascode.com/java-snippets/</guid>
      <description>&lt;div class=&#34;sect1&#34;&gt;
&lt;h2 id=&#34;_remote_debug_a_pods_java_process&#34;&gt;Remote Debug a Pod’s Java Process&lt;/h2&gt;
&lt;div class=&#34;sectionbody&#34;&gt;
&lt;div class=&#34;paragraph&#34;&gt;
&lt;p&gt;Simple steps for remote debugging a Java process running on a k8 pod:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&#34;olist arabic&#34;&gt;
&lt;ol class=&#34;arabic&#34;&gt;
&lt;li&gt;
&lt;p&gt;Edit deployment and add the following parameters to the Java start line: &lt;code&gt;-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=127.0.0.1:5005&lt;/code&gt;&lt;/p&gt;
&lt;div class=&#34;paragraph&#34;&gt;
&lt;p&gt;Also add the following port mapping at the section &lt;code&gt;container → ports&lt;/code&gt; in the deployment:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&#34;listingblock&#34;&gt;
&lt;div class=&#34;content&#34;&gt;
&lt;pre class=&#34;highlight&#34;&gt;&lt;code&gt;- containerPort: 5005
  protocol: TCP&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Safe, wait for the new pods and then add a port forward for port 5005 for this pod:&lt;/p&gt;
&lt;div class=&#34;listingblock&#34;&gt;
&lt;div class=&#34;content&#34;&gt;
&lt;pre class=&#34;highlight&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;kubectl port-forward podname 5005&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;</description>
    </item>
    <item>
      <title>Spring Boot Snippets</title>
      <link>https://www.hascode.com/spring-boot-snippets/</link>
      <pubDate>Mon, 01 Mar 2010 00:00:00 +0100</pubDate>
      <guid>https://www.hascode.com/spring-boot-snippets/</guid>
      <description>&lt;div class=&#34;sect1&#34;&gt;
&lt;h2 id=&#34;_define_and_configure_log_groups&#34;&gt;Define and Configure Log Groups&lt;/h2&gt;
&lt;div class=&#34;sectionbody&#34;&gt;
&lt;div class=&#34;admonitionblock tip&#34;&gt;
&lt;table&gt;
&lt;tbody&gt;&lt;tr&gt;
&lt;td class=&#34;icon&#34;&gt;
&lt;i class=&#34;fa icon-tip&#34; title=&#34;Tip&#34;&gt;&lt;/i&gt;
&lt;/td&gt;
&lt;td class=&#34;content&#34;&gt;
This allows to configure a group of loggers at the same time
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;div class=&#34;olist arabic&#34;&gt;
&lt;ol class=&#34;arabic&#34;&gt;
&lt;li&gt;
&lt;p&gt;Define a log group named &lt;code&gt;myaspect&lt;/code&gt; with two packages&lt;/p&gt;
&lt;div class=&#34;listingblock&#34;&gt;
&lt;div class=&#34;title&#34;&gt;application.properties&lt;/div&gt;
&lt;div class=&#34;content&#34;&gt;
&lt;pre class=&#34;highlight&#34;&gt;&lt;code class=&#34;language-properties&#34; data-lang=&#34;properties&#34;&gt;logging.group.myaspect=com.hascode.package1,com.hascode.package2&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Configure the log group and set all loggers to level &lt;code&gt;TRACE&lt;/code&gt;&lt;/p&gt;
&lt;div class=&#34;listingblock&#34;&gt;
&lt;div class=&#34;title&#34;&gt;application.properties&lt;/div&gt;
&lt;div class=&#34;content&#34;&gt;
&lt;pre class=&#34;highlight&#34;&gt;&lt;code class=&#34;language-properties&#34; data-lang=&#34;properties&#34;&gt;logging.level.myaspect=TRACE&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;This is also possible as parameter on startup&lt;/p&gt;
&lt;div class=&#34;listingblock&#34;&gt;
&lt;div class=&#34;content&#34;&gt;
&lt;pre class=&#34;highlight&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;java -Dlogging.level.myaspect=TRACE myapp.jar&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;sect1&#34;&gt;
&lt;h2 id=&#34;_use_junit_5_with_spring_boot&#34;&gt;Use JUnit 5 with Spring Boot&lt;/h2&gt;
&lt;div class=&#34;sectionbody&#34;&gt;
&lt;div class=&#34;paragraph&#34;&gt;
&lt;p&gt;Use newer versions of Surefire and Failsafe plugins:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&#34;listingblock&#34;&gt;
&lt;div class=&#34;content&#34;&gt;
&lt;pre class=&#34;highlight&#34;&gt;&lt;code class=&#34;language-xml&#34; data-lang=&#34;xml&#34;&gt;&amp;lt;properties&amp;gt;
[..]
  &amp;lt;maven-failsafe-plugin.version&amp;gt;2.22.0&amp;lt;/maven-failsafe-plugin.version&amp;gt;
  &amp;lt;maven-surefire-plugin.version&amp;gt;2.22.0&amp;lt;/maven-surefire-plugin.version&amp;gt;
&amp;lt;/properties&amp;gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;</description>
    </item>
  </channel>
</rss>
