Home > Software engineering >  Read a JKS file from AWS S3 bucket and check expiration date of certificate?
Read a JKS file from AWS S3 bucket and check expiration date of certificate?

Time:09-17

I need to implement functionality for a spring boot project that reads JKS files from an S3 bucket and checks expiration dates for certificates and use a cron job to check and send alerts when certificates are X days or weeks away from expiring.

I'm able to get the file in memory as a byte array. That's pretty much as far as it goes. Since the keystore isn't serializable I can't really do anything with it... I'm just stuck with a byte array.

Any takers...? Lemme see ya flex them programmammatic skillzors :)

CodePudding user response:

The KeyStore class has a load() method that takes an InputStream. You can wrap a byte array in a ByteArrayInputStream.

You should be able to load a KeyStore from a byte array like this:

KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new ByteArrayInputStream(myByteArray), myKeystorePassword);

You should be able to follow the solutions posted here to then check the expiration date(s).

CodePudding user response:

I solved it with this:

KeyStore ks = KeyStore.getInstance("JKS");
        ks.load(new ByteArrayInputStream(myByteArray), keyStorePassword.toCharArray());
        Enumeration<String> aliases = ks.aliases();
        for(; aliases.hasMoreElements();) {
            String alias = (String) aliases.nextElement();
            Date certExpiryDate = ((X509Certificate) ks.getCertificate(alias)).getNotAfter();
            System.out.println("certExpiryDate = "   certExpiryDate);}
  • Related