Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kafka message sending fails with 'class SslBundleSslEngineFactory could not be found' #44414

Closed
kst1980 opened this issue Feb 23, 2025 · 12 comments
Assignees
Labels
type: bug A general bug
Milestone

Comments

@kst1980
Copy link

kst1980 commented Feb 23, 2025

Spring Boot Kafka SSL Issue on EKS Deployment using Spring boot 3.4.3(tried 3.3.5 and 3.4.2)
We are facing an issue where the Kafka producer works fine in local environments (IntelliJ, mvn spring-boot:run, and java -jar). However, after deploying to EKS, we encounter the following exception:

Exception occurred: org.apache.kafka.common.config.ConfigException: Invalid value org.springframework.boot.autoconfigure.kafka.SslBundleSslEngineFactory for configuration ssl.engine.factory.class: Class org.springframework.boot.autoconfigure.kafka.SslBundleSslEngineFactory could not be found.. Stack Trace: [org.apache.kafka.common.config.ConfigDef.parseType(ConfigDef.java:778), org.apache.kafka.common.config.ConfigDef.parseValue(ConfigDef.java:531), org.apache.kafka.common.config.ConfigDef.parse(ConfigDef.java:524), org.apache.kafka.common.config.AbstractConfig.<init>(AbstractConfig.java:114), org.apache.kafka.common.config.AbstractConfig.<init>(AbstractConfig.java:134), org.apache.kafka.clients.producer.ProducerConfig.<init>(ProducerConfig.java:643), org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:295), org.springframework.kafka.core.DefaultKafkaProducerFactory.createRawProducer(DefaultKafkaProducerFactory.java:944), org.springframework.kafka.core.DefaultKafkaProducerFactory.createKafkaProducer(DefaultKafkaProducerFactory.java:826), org.springframework.kafka.core.DefaultKafkaProducerFactory.doCreateProducer(DefaultKafkaProducerFactory.java:793), org.springframework.kafka.core.DefaultKafkaProducerFactory.createProducer(DefaultKafkaProducerFactory.java:768), org.springframework.kafka.core.DefaultKafkaProducerFactory.createProducer(DefaultKafkaProducerFactory.java:762), org.springframework.kafka.core.KafkaTemplate.getTheProducer(KafkaTemplate.java:976), org.springframework.kafka.core.KafkaTemplate.doSend(KafkaTemplate.java:828), org.springframework.kafka.core.KafkaTemplate.observeSend(KafkaTemplate.java:805), org.springframework.kafka.core.KafkaTemplate.send(KafkaTemplate.java:608),

Issue Details:
Kafka producer initialization delayed: Instead of initializing at application startup, it only starts when sending a message, leading to the SslBundleSslEngineFactory class not being found.
Temporary workaround: We manually create the producer in a @PostConstruct method inside a KafkaConfig class.

@Slf4j
@Configuration
@RequiredArgsConstructor
public class KafkaConfig {
    private final ProducerFactory<String, String> producerFactory;

    @PostConstruct
    public void initializeProducer() {
        try {
            Producer<String, String> producer = producerFactory.createProducer(); // Workaround to initialize Kafka producer
            log.info("Kafka Producer initialized successfully: {}", producer);
        } catch (Exception e) {
            log.error("Failed to initialize Kafka Producer", e);
        }
    }
}

Spring Boot Configuration (application.yml)

spring:
  application:
    name: test
  ssl:
    bundle:
      pem:
        kafkaCert:
          keystore:
            certificate: classpath:KafkaKeystoreCert.pem
            private-key: classpath:KafkaKeystoreKey.pem
          truststore:
            certificate: classpath:KafkaTruststorePem.pem
  kafka:
    bootstrap-servers: ${KafkaBootstrapServers}
    ssl:
      bundle: kafkaCert
    security:
      protocol: SSL
    client-id: client-id
    producer:
      retries: 3
      key-serializer: org.apache.kafka.common.serialization.StringSerializer
      value-serializer: org.apache.kafka.common.serialization.StringSerializer

Request for a Proper Solution
Do we have a proper fix for this issue instead of the workaround?

Is there a missing dependency or classpath issue specific to the environment?
Are there configurations that need adjustment to ensure Kafka SSL settings load correctly?
Any best practices for ensuring SslBundleSslEngineFactory is properly available at runtime?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 23, 2025
@mhalbritter
Copy link
Contributor

This could be a classloader issue. It does work locally with SSL enabled, too? Or is SSL only enabled in EKS?

Please take the time to provide a complete minimal sample (something that we can unzip or git clone, build, and deploy) that reproduces the problem.

@mhalbritter mhalbritter added the status: waiting-for-feedback We need additional information before we can continue label Feb 24, 2025
@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Feb 24, 2025
@mhalbritter mhalbritter added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Feb 24, 2025
@kst1980
Copy link
Author

kst1980 commented Feb 24, 2025

Yes, it works locally with the same configuration and SSL enabled, but I read that it might be functioning correctly locally due to the JVM.

I have added the following dependency, which provides org.springframework.boot.autoconfigure.kafka.SslBundleSslEngineFactory, and verified its presence in the image after packaging:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
</dependency>

Since the Spring Kafka producer is lazy-initialized, it throws a ClassNotFoundException after the application startup.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Feb 24, 2025
@mhalbritter
Copy link
Contributor

Please provide a sample to reproduce.

@mhalbritter mhalbritter added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Feb 24, 2025
@kst1980
Copy link
Author

kst1980 commented Feb 24, 2025

kafka-producer.zip - sample

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Feb 24, 2025
@mhalbritter
Copy link
Contributor

mhalbritter commented Feb 25, 2025

Hey, do you have any instructions how to reproduce the issue?

I've added a compose file and some SSL config (the certs in your sample are all empty), and it works, both in tests and locally when running with java -jar. Do i need to do something else to see it fail?

sb-44414.zip

Since the Spring Kafka producer is lazy-initialized, it throws a ClassNotFoundException after the application startup.

Also I don't see any spring-boot-autoconfigure, which you mentioned here.

@mhalbritter mhalbritter added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Feb 25, 2025
@kst1980
Copy link
Author

kst1980 commented Feb 25, 2025

spring-boot-autoconfigure is included in spring-boot-starter, so I didn't add it explicitly. As I mentioned earlier, everything works fine locally. However, we're encountering this issue when deploying to AWS EKS and making a sample request. Additionally, we're using Aiven Kafka.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Feb 25, 2025
@patpatpat123
Copy link

patpatpat123 commented Feb 26, 2025

I do not want to pollute this thread, apologies.

I am facing the same issue with all the above springboot versions mentioned.

For me, I have a case where it is working in "normal" mode and local, but I always encounter this same issue when running with GraalVM native image

Just wondering if I should create a new issue, targeting specifically native images, or if this is related.

@mhalbritter
Copy link
Contributor

@kst1980

However, we're encountering this issue when deploying to AWS EKS and making a sample request. Additionally, we're using Aiven Kafka.

I still think this is a classloader issue. The classloader associated with the thread when using the @PostConstruct workaround is able to the load the class, so it's not a missing dependency.

Is there something different in the dependencies or are any agents attached when running in EKS?

@mhalbritter
Copy link
Contributor

mhalbritter commented Feb 26, 2025

I do not want to pollute this thread, apologies.

I am facing the same issue with all the above springboot versions mentioned.

For me, I have a case where it is working in "normal" mode and local, but I always encounter this same issue when running with GraalVM native image

Just wondering if I should create a new issue, targeting specifically native images, or if this is related.

Please open a new issue, and please attach a reproducer. Thanks!

// Edit: Nevermind, there's a class hint missing. While it's the same error message as this issue, it's not related. I've opened #44435 for that.

@mhalbritter mhalbritter added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Feb 26, 2025
@mhalbritter mhalbritter changed the title Kafka Producer with SSL Bundles Fails While Sending Message – ConfigException: Invalid Value for ssl.engine.factory.class (SslBundleSslEngineFactory Not Found) Kafka message sending fails with 'class SslBundleSslEngineFactory could not be found' Feb 26, 2025
@mhalbritter mhalbritter removed status: waiting-for-feedback We need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged labels Feb 26, 2025
@mhalbritter mhalbritter added the type: bug A general bug label Feb 26, 2025
@mhalbritter mhalbritter added this to the 3.3.x milestone Feb 26, 2025
@mhalbritter
Copy link
Contributor

Instead of putting SslBundleSslEngineFactory as a string in the configuration map, we decided to put the class directly in the config map. This hopefully fixes the classloading issue.

@kst1980
Copy link
Author

kst1980 commented Feb 26, 2025

@kst1980

However, we're encountering this issue when deploying to AWS EKS and making a sample request. Additionally, we're using Aiven Kafka.

I still think this is a classloader issue. The classloader associated with the thread when using the @PostConstruct workaround is able to the load the class, so it's not a missing dependency.

Is there something different in the dependencies or are any agents attached when running in EKS?

Using the Datadog Java Agent https://dtdg.co/latest-java-tracer, when can I test this fix and with which version?

@mhalbritter
Copy link
Contributor

mhalbritter commented Feb 26, 2025

Maybe that's the problem. The fix (hopefully) will be released as 3.3.10 next month (https://calendar.spring.io/), or available on Spring Snapshots repo as 3.3.10-SNAPSHOT.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants