Search code examples
javagradlejwtkeystorevert.x

How to get my Keystore file to work with my Java Vert.x project (Invalid keystore format)


I have a Java Vert.x Gradle project and I am trying to get Vert.x auth JWT working with it, but when I run my project I get a Invalid keystore format error.

I am trying to do this locally; I am on an iMac.

Here is part of my build.gradle file:

build.gradle

// Import to replace ant-style tokens (@environmentVariable@) with values
import org.apache.tools.ant.filters.ReplaceTokens

buildscript {
  ext.vertxVersion = '3.5.4'
}

dependencies {
  compile "io.vertx:vertx-core:$vertxVersion"
  compile "io.vertx:vertx-web:$vertxVersion"
  compile "io.vertx:vertx-web-client:$vertxVersion"
  compile "io.vertx:vertx-web-templ-thymeleaf:3.5.4"
  compile "io.vertx:vertx-unit:$vertxVersion"
  compile 'io.vertx:vertx-auth-jwt:3.5.4'
  compile group: 'io.jsonwebtoken', name: 'jjwt', version: '0.9.1'
  compile "org.slf4j:slf4j-api:1.7.25"
  testCompile group: 'junit', name: 'junit', version: '4.12'
  implementation 'com.google.code.gson:gson:2.8.5'
}

//Replace ant-style tokens with properties.
processResources {
    filter( ReplaceTokens, tokens:  properties )
}

Here is how I am implementing in my Vert.x verticle.

MainVerticle.java

JWTAuthOptions config = new JWTAuthOptions();
config.setKeyStore(new KeyStoreOptions()
        .setType(Constants.KEY_STORE_TYPE) /* = "jceks" */
        .setPath(Constants.KEY_STORE_PATH) /* = "keystore.jceks" */
        .setPassword(Constants.AUTH_KEY_STORE_PASSWORD)); /* correct password */

JWTAuth provider = JWTAuth.create(vertx, config);

router.route(Constants.HOME_PAGE + "/*").handler(JWTAuthHandler.create(provider));

Here is where I am keeping my keystore.jceks file:

keystore.jceks

My-Project
  |----src
        |----main
              |----resources
                      |----keystore.jceks

I first build my project with gradle:

$ ./gradlew clean build

Building my project works fine. Then I try to run my project:

$ java -jar build/libs/MyProject-far-1.0.jar

When I run my project, I get the following error:

java.lang.RuntimeException: java.io.IOException: Invalid keystore format
at io.vertx.ext.auth.jwt.impl.JWTAuthProviderImpl.<init>(JWTAuthProviderImpl.java:107)
at io.vertx.ext.auth.jwt.JWTAuth.create(JWTAuth.java:56)
at com.ucg.warehouselocationmapping.app.MainVerticle.start(MainVerticle.java:54)
at io.vertx.core.impl.DeploymentManager.lambda$doDeploy$8(DeploymentManager.java:491)
at io.vertx.core.impl.ContextImpl.lambda$wrapTask$2(ContextImpl.java:339)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:463)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:886)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:745)

I was using this as a reference of how to implement Vert.x auth JWT, as well as another project's code.

The keystore.jceks file I am using was copied from another project where it works perfectly. It was first copied by dragging it around Eclipse. When that didn't work, I tried copying it in the terminal:

$ cp /Users/Me/workspace/My-Service/src/main/resources/keystore.jceks /Users/Me/workspace/My-Project/src/main/resources

That didn't work, so I tried copying it via Finder. That didn't work either.

Anyone ever come across this issue before? I'm not really familiar with jks/jceks files or authentication.

ANSWER

The import org.apache.tools.ant.filters.ReplaceTokens plugin was finding @ signs in my keystore.jceks file and manipulating them, when I really only wanted ant-style tokens to be replaced in my properties files. I updated my build.gradle file to only replace files ending in .properties, and that fixed my issue.

//Replace ant-style tokens with properties.
processResources {
    filesMatching('**/*.properties') {
        filter( ReplaceTokens, tokens:  properties )
    }
}

Solution

  • The import org.apache.tools.ant.filters.ReplaceTokens plugin was finding @ signs in my keystore.jceks file and manipulating them, when I really only wanted ant-style tokens to be replaced in my properties files. I updated my build.gradle file to only replace files ending in .properties, and that fixed my issue.

    //Replace ant-style tokens with properties.
    processResources {
        filesMatching('**/*.properties') {
            filter( ReplaceTokens, tokens:  properties )
        }
    }