javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed

I am connecting to a SSL site. This is one of the most frequent exceptions seen by a java developer.

To get rid of this, download the cert from the site you want to connect. Just click the lock sign near the browser URL. It will help you to download/export the SSL certificate to .cer format.

Import the certificate to your java cacerts.

I used OpenJDK

d:\java\jdk\bin\keytool -import -noprompt -trustcacerts -alias grassfield -file D:\gfield_ssl_cert.cer -keystore cacerts -storepass changeit
Certificate was added to keystore

You can list the certs in your cacerts store. I do a grep/find to get my cerficate.

d:\java\jdk\bin\keytool -list -keystore cacerts -storepass changeit|find “grassfield”
grassfield, Nov 25, 2019, trustedCertEntry,

Spring application fails to load in STS 4.1

I have a spring web application which is running in Spring 5 (written in 3, later upgraded to 4 and 5 over the time). It was created in STS 2.x, later the workspace has been imported to STS 3.x without any problem. Though I have been using STS 4.1 for other SpringBoot projects, my old web application was still being developed in STS 3.x.

I imported the application to STS4.1 last week. The tomcat starts. But it does not load my application.

Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version: Apache Tomcat/9.0.13
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: Nov 2 2018 14:27:55 UTC
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number: 9.0.13.0
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Windows 10
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 10.0
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: amd64
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home: C:\Program Files\Java\jdk1.8.0_181\jre
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 1.8.0_181-b13
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Oracle Corporation
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: H:\Dev\apache-tomcat-9.0.13
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: H:\Dev\apache-tomcat-9.0.13
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=H:\Dev\apache-tomcat-9.0.13
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=H:\Dev\apache-tomcat-9.0.13
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=H:\Dev\apache-tomcat-9.0.13\wtpwebapps
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=H:\Dev\apache-tomcat-9.0.13\endorsed
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Oct 19, 2019 6:48:23 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: Loaded APR based Apache Tomcat Native library [1.2.18] using APR version [1.6.5].
Oct 19, 2019 6:48:23 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
Oct 19, 2019 6:48:23 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
Oct 19, 2019 6:48:23 AM org.apache.catalina.core.AprLifecycleListener initializeSSL
INFO: OpenSSL successfully initialized [OpenSSL 1.1.1 11 Sep 2018]
Oct 19, 2019 6:48:23 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Oct 19, 2019 6:48:24 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Oct 19, 2019 6:48:24 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Oct 19, 2019 6:48:24 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Oct 19, 2019 6:48:24 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1674 ms
Oct 19, 2019 6:48:24 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Catalina]
Oct 19, 2019 6:48:24 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/9.0.13
Oct 19, 2019 6:48:24 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory [H:\Dev\apache-tomcat-9.0.13\webapps\docs]
Oct 19, 2019 6:48:24 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [H:\Dev\apache-tomcat-9.0.13\webapps\docs] has finished in [22] ms
Oct 19, 2019 6:48:24 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory [H:\Dev\apache-tomcat-9.0.13\webapps\examples]
Oct 19, 2019 6:48:25 AM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
Oct 19, 2019 6:48:25 AM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
Oct 19, 2019 6:48:25 AM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: attributeAdded('StockTicker', 'async.Stockticker@302552ec')
Oct 19, 2019 6:48:25 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [H:\Dev\apache-tomcat-9.0.13\webapps\examples] has finished in [534] ms
Oct 19, 2019 6:48:25 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory [H:\Dev\apache-tomcat-9.0.13\webapps\host-manager]
Oct 19, 2019 6:48:25 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [H:\Dev\apache-tomcat-9.0.13\webapps\host-manager] has finished in [31] ms
Oct 19, 2019 6:48:25 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory [H:\Dev\apache-tomcat-9.0.13\webapps\manager]
Oct 19, 2019 6:48:25 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [H:\Dev\apache-tomcat-9.0.13\webapps\manager] has finished in [32] ms
Oct 19, 2019 6:48:25 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory [H:\Dev\apache-tomcat-9.0.13\webapps\ROOT]
Oct 19, 2019 6:48:25 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [H:\Dev\apache-tomcat-9.0.13\webapps\ROOT] has finished in [46] ms
Oct 19, 2019 6:48:25 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Oct 19, 2019 6:48:25 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Oct 19, 2019 6:48:25 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1299 ms

As you see in the above log, Spring context never loaded. When I tried to load the application URL, it was throwing HTTP Status 404 – Not Found error.

I had faced this kind of problem in earlier version of STS/Eclipse. It would be resolved either by tomcat server configuration or maven update or rebuilding or restart of eclipse. This was not resolved by either of these techniques and drove me crazy for two days.

I found one of the sites was showing the bug in STS 4.x which was fixed in 4.2. So, I want to try this as well. I tried to upgrade. Upgrade went well. But it didnt solve the problem. later I deleted the whole STS folder and used a fresh 4.4.0.RELEASE. Everything went smooth.. 😅😅

Nice picture, isn't it? I captured it at the Temple of tooth, Kandy (6 Jun 19)

Nice picture, isn’t it? I captured it at the Temple of tooth, Kandy (6 Jun 19)

Creating docker image for SpringBoot REST gateway

This post documents the steps I performed to containerize my REST application built with Spring Boot.

This uses the following tools

  1. Spring Tool Suite 4
  2. Maven – STS inbuilt
  3. Open JDK (Built by Azul)
  4. Docker

 

Step 1: Build your spring boot application using Maven:

SpringBoot and Docker 01

This will build the file and jar file will be copied to the target folder of your project folder.

[INFO] --- maven-jar-plugin:3.1.2:jar (default-jar) @ messenger ---
[INFO] Building jar: D:\Pandian\Documents\workspace_hamsa\messenger\target\messenger-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.1.8.RELEASE:repackage (repackage) @ messenger ---
[INFO] Replacing main artifact with repackaged archive
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ messenger ---
[INFO] Installing D:\Pandian\Documents\workspace_hamsa\messenger\target\messenger-0.0.1-SNAPSHOT.jar to C:\Users\barat\.m2\repository\org\grassfield\messenger\0.0.1-SNAPSHOT\messenger-0.0.1-SNAPSHOT.jar
[INFO] Installing D:\Pandian\Documents\workspace_hamsa\messenger\pom.xml to C:\Users\barat\.m2\repository\org\grassfield\messenger\0.0.1-SNAPSHOT\messenger-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  13.659 s
[INFO] Finished at: 2019-10-12T08:57:43+08:00
[INFO] ------------------------------------------------------------------------

You may see above. The jar file messenger-0.0.1-SNAPSHOT.jar is created in the target folder.

Step 2: Create Dockerfile

We start building our own container now. We need to define the docker configuration in a Dockerfile. It will be placed in the root of project folder.

Following is the Docker file for this project.

#this uses alpine base image with openjdk 8
FROM openjdk:8-jdk-alpine

#Maintainer contact details
MAINTAINER Murugapandian Ramaiah 

#where to persist the data generated by the container.
VOLUME /tmp

#which port of this container is to be exposed outside
EXPOSE 8080

#specify the SpringBoot bulky jar
ARG JAR_FILE=target/messenger-0.0.1-SNAPSHOT.jar
ADD ${JAR_FILE} messenger.jar

#specify how to execute the application
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom", "-jar", "/messenger.jar"]

Step 3: Build the container image

SSH the project folder to the /tmp partition of docker host Execute the following command.

$ cd /tmp/messenger
$ docker build -t messenger .
Step 1/7 : FROM openjdk:8-jdk-alpine
 ---> a3562aa0b991
Step 2/7 : MAINTAINER Murugapandian Ramaiah
 ---> Using cache
 ---> 4bb0470845c7
Step 3/7 : VOLUME /tmp
 ---> Using cache
 ---> 232d70e90d54
Step 4/7 : EXPOSE 8080
 ---> Using cache
 ---> 0a1d2e61d4a4
Step 5/7 : ARG JAR_FILE=target/messenger-0.0.1-SNAPSHOT.jar
 ---> Using cache
 ---> f179766aa87f
Step 6/7 : ADD ${JAR_FILE} messenger.jar
 ---> bc76f586ac0b
Step 7/7 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom", "-jar", "/messenger.jar"]
 ---> Running in 0646cdbee037
Removing intermediate container 0646cdbee037
 ---> 574eb9b9004b
Successfully built 574eb9b9004b
Successfully tagged messenger:latest

The container image is created

$ docker image ls
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
messenger            latest              574eb9b9004b        23 minutes ago      123MB
openjdk              8-jdk-alpine        a3562aa0b991        5 months ago        105MB
pandian/centos_ssh   latest              7f019be4424c        14 months ago       294MB
centos               latest              5182e96772bf        14 months ago       200MB
ubuntu               latest              735f80812f90        14 months ago       83.5MB
hello-world          latest              2cb0d9787c4d        15 months ago       1.85kB

messenger is added.

Step 4: run the container.

$ docker run -p 6666:8080 messenger

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.8.RELEASE)

2019-10-12 07:11:42.372  INFO 1 --- [           main] org.grassfield.msg.MessengerApplication  : Starting MessengerApplication v0.0.1-SNAPSHOT on 0f9f70c91b73 with PID 1 (/messenger.jar started by root in /)
2019-10-12 07:11:42.375  INFO 1 --- [           main] org.grassfield.msg.MessengerApplication  : No active profile set, falling back to default profiles: default
2019-10-12 07:11:43.394  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-10-12 07:11:43.430  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-10-12 07:11:43.431  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.24]
2019-10-12 07:11:43.530  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-10-12 07:11:43.530  INFO 1 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1118 ms
2019-10-12 07:11:43.758  INFO 1 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-10-12 07:11:44.005  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-10-12 07:11:44.008  INFO 1 --- [           main] org.grassfield.msg.MessengerApplication  : Started MessengerApplication in 2.072 seconds (JVM running for 2.438)

I endorse this post by Rajeev – https://www.callicoder.com/spring-boot-docker-example/

Dont miss this carving in Temple of tooth, Kandy (Summer 2019)

Dont miss this carving in Temple of tooth, Kandy (Summer 2019)

 

Error while building the docker image lstat /tmp/Dockerfile: no such file or directory

I’m trying to build a docker image. When I issue the build command I got the following error.


$ docker build -t messenger .
unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /tmp/Dockerfile: no such file or directory

I was executing the command from /tmp instead of /tmp/messenger, where the Dockerfile is located. When I executed from /tmp/messenger, it worked.

சாந்தி நிலவ வேண்டும். உலகிலே சாந்தி நிலவ வேண்டும் -  I captured this serene Buddha statue at Temple of tooth, Kandy during 2019 summerat temple of t

சாந்தி நிலவ வேண்டும். உலகிலே சாந்தி நிலவ வேண்டும் – I captured this serene Buddha statue at Temple of tooth, Kandy during 2019 summerat temple of t

Cannot connect to the Docker daemon

So I see this problem when I tried to run a hello-world container

$ docker run hello-world
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.

Check if the docker is running in the host.
Yes it is running

$ sudo service docker status
[sudo] password for pandian:
● docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2019-10-12 09:12:03 +08; 18s ago
     Docs: https://docs.docker.com
 Main PID: 17992 (dockerd)
    Tasks: 18
   CGroup: /system.slice/docker.service
           └─17992 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Oct 12 09:11:59 docker dockerd[17992]: time="2019-10-12T09:11:59.472499831+08:00" level=warning msg="Your kernel does not support swap memory limit"
Oct 12 09:11:59 docker dockerd[17992]: time="2019-10-12T09:11:59.472521907+08:00" level=warning msg="Your kernel does not support cgroup rt period"
Oct 12 09:11:59 docker dockerd[17992]: time="2019-10-12T09:11:59.472528459+08:00" level=warning msg="Your kernel does not support cgroup rt runtime"
Oct 12 09:11:59 docker dockerd[17992]: time="2019-10-12T09:11:59.472676228+08:00" level=info msg="Loading containers: start."
Oct 12 09:12:00 docker dockerd[17992]: time="2019-10-12T09:12:00.302342870+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip
Oct 12 09:12:00 docker dockerd[17992]: time="2019-10-12T09:12:00.604326576+08:00" level=info msg="Loading containers: done."
Oct 12 09:12:03 docker dockerd[17992]: time="2019-10-12T09:12:03.531536271+08:00" level=info msg="Docker daemon" commit=a872fc2f86 graphdriver(s)=overlay2 version=19.03.3
Oct 12 09:12:03 docker dockerd[17992]: time="2019-10-12T09:12:03.535138604+08:00" level=info msg="Daemon has completed initialization"
Oct 12 09:12:03 docker dockerd[17992]: time="2019-10-12T09:12:03.722547518+08:00" level=info msg="API listen on /var/run/docker.sock"
Oct 12 09:12:03 docker systemd[1]: Started Docker Application Container Engine.
lines 1-19/19 (END)

Docker daemon is running now.

$ ls -alt /var/run/docker.sock
srw-rw---- 1 root docker 0 Oct 12 09:11 /var/run/docker.sock

Run the hello-world now.

$ docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/
I captured Kandy lake in Sri Lanka during the summer season of 2019

I captured Kandy lake in Sri Lanka during the summer season of 2019