Java - Adding Certificate to the Java keystore

Problem

When running any of the SeaLights agents, you may come across an error related to PKIx/SSL libraries. For example:

javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake … javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed …

Cause

These errors could point to an issue with creating a secure connection to our server due to a misconfigured or missing certificate. The missing certificate is often the Selaights Security certificate, sometimes your Corporate certificate.

The instructions below detail the steps required to add the SeaLights certificate to your local Java Keystore.

Solution

  1. Retrieve the SeaLights certificate from your dashboard:

  • While logged in, click the “Lock” icon to the left of your address bar.

  • Export the certificate to your local machine:

On Windows: In the Details tab, click on Copy to File and ensure the exported certificate is in .cer/.der format

On Mac: Dragging the certificate icon to your desktop will export it

Make sure the certificate you’re downloading is issued by Amazon Web services. In some organizations, the firewall modifies/replaces the certificate and leds to continue facing the connectivity issue because the certificate will be marked as invalid. In this case, please contact Sealights Support to get a copy via email for example.

Sample certificate details

 

2. Add the certificate to the Java Keystore:

keytool -importcert -alias your-alias -keystore cacerts.jks -file certificate.pem -storepass password

 

(Optional) Convert the .cer certificate file to .pem format:

  • In some cases, it may be necessary to convert the exported certificate to PEM format to be loaded into the JKS (Java Key Store). To do so, use the following command:

openssl x509 -inform der -in certificate.cer -out certificate.pem

The SeaLights certificate should be added to any Java keystore that communicates with SeaLights.

3. Validate the certificate was imported into the keystore file being used by the JVM with the following command:

Workaround

You can disable the cert check (not a long-term solution):

Helpers

Disclaimer:
The scripts provided below are for reference purposes only. They are offered "as-is" without any guarantees or support from Sealights.
Please ensure you understand and test their functionality before deploying them in any environment.

Check the certificates' expiration date

Here are Bash and PowerShell scripts that check the expiration dates of certificates in a Java keystore and notify you about certificates that are either expired or will expire in less than 30 days:

Bash

PowerShell

Bash

PowerShell

Replace /path/to/your/keystore.jks with the actual path to your Java keystore file and your_keystore_password with your keystore password. Set appropriate permissions on the script file and execute it in a proper environment.
Please note that this script assumes that the keytool command is available in your environment (usually comes with Java installations). Also, ensure that you have the necessary permissions to access the keystore file.

Script saving an SSL certificate from a URL to a local file

These scripts should help you download SSL certificates and save them in the specified format.

Bash

PowerShell

Bash

PowerShell

 

Related articles