How to pass absolute path of the kafka trust store location to spring boot native image

1k Views Asked by At

I'm trying to use spring-native to create the native image of spring application which is using kafka ssl.

But I have an issue to pass the absoulte path of the kafka truststore path.

Becuase the native image can't know where the truststore path is.

  • springboot: 2.4.5
  • spring-native: 0.9.2
  • graalvm: graalvm-ce-java8-21.0.0.2

My truststore file is located in the following path

src
  - main
    - resources
      - truststore
         mytrustsotre.jks
# Kafka properties
spring:
  kafka:
    ssl:
      trustStorePassword: "password"
      trustStoreLocation: "classpath:truststore/mytrustsotre.jks"

Maven plugin build arguments

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <image>
                        <builder>paketobuildpacks/builder:tiny</builder>
                        <env>
                            <BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
                            <BP_NATIVE_IMAGE_BUILD_ARGUMENTS>
                                -H:IncludeResourceBundles=sun.security.util.Resources
                                -H:IncludeResources=truststore/mytrustsotre.jks
                            </BP_NATIVE_IMAGE_BUILD_ARGUMENTS>
                        </env>
                    </image>
                </configuration>
            </plugin>

Build and run the application

mvn spring-boot:build-image -Dspring-boot.build-image.imageName=my.way.com/app:1.0.0-SNAPSHOT

...
...

docker run -it -e "SPRING_PROFILES_ACTIVE=default" \
-e "--spring.application.name=app" \
my.way.com/app:1.0.0-SNAPSHOT

Then error occurs when the message comes.

Caused by: java.lang.IllegalStateException: Resource 'class path resource [truststore/mytrustsotre.jks]' must be on a file system
        at org.springframework.boot.autoconfigure.kafka.KafkaProperties$Ssl.resourceToPath(KafkaProperties.java:1155) ~[com.naver.gla.pgsync.PgSyncApplicationKt:na]
        at org.springframework.boot.context.properties.PropertyMapper$Source.lambda$as$2(PropertyMapper.java:220) ~[na:na]
        at org.springframework.boot.context.properties.PropertyMapper$Source.to(PropertyMapper.java:314) ~[na:na]
        at org.springframework.boot.autoconfigure.kafka.KafkaProperties$Ssl.buildProperties(KafkaProperties.java:1143) ~[com.naver.gla.pgsync.PgSyncApplicationKt:na]
        at org.springframework.boot.autoconfigure.kafka.KafkaProperties.buildCommonProperties(KafkaProperties.java:159) ~[com.naver.gla.pgsync.PgSyncApplicationKt:na]
        at org.springframework.boot.autoconfigure.kafka.KafkaProperties.buildProducerProperties(KafkaProperties.java:190) ~[com.naver.gla.pgsync.PgSyncApplicationKt:na]
        at com.naver.gla.pgsync.config.ApplicationConfiguration.producerFactory(ApplicationConfiguration.kt:70) ~[com.naver.gla.pgsync.PgSyncApplicationKt:na]
        at com.naver.gla.pgsync.config.ApplicationConfiguration.producer(ApplicationConfiguration.kt:73) ~[com.naver.gla.pgsync.PgSyncApplicationKt:na]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:na]
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[na:na]
        ... 49 common frames omitted
Caused by: java.io.FileNotFoundException: class path resource [truststore/mytrustsotre.jks] cannot be resolved to absolute file path because it does not reside in the file system: resource:truststore/mytrustsotre.jks
        at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:217) ~[na:na]
        at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:154) ~[na:na]
        at org.springframework.boot.autoconfigure.kafka.KafkaProperties$Ssl.resourceToPath(KafkaProperties.java:1152) ~[com.naver.gla.pgsync.PgSyncApplicationKt:na]
        ... 58 common frames omitted

Without native image, this application works fine.

How Can I pass the absolute path to the native image?

1

There are 1 best solutions below

0
On

I figure out this issue by myself.

I set the docker volume to link for trust file in my host.

docker run -it -v /my/resources:/app/resources \
-e "SPRING_PROFILES_ACTIVE=default" \
-e "--spring.application.name=app" \
my.way.com/app:1.0.0-SNAPSHOT