Skip to content

Commit 585d9b2

Browse files
committed
Fix KAFKA_TLS_CONFIGURATION_NAME environment variable not being recognized
Fixes quarkusio#51195
1 parent 0c80b95 commit 585d9b2

File tree

3 files changed

+101
-2
lines changed

3 files changed

+101
-2
lines changed

extensions/kafka-client/runtime/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@
7878
<artifactId>assertj-core</artifactId>
7979
<scope>test</scope>
8080
</dependency>
81+
<dependency>
82+
<groupId>org.mockito</groupId>
83+
<artifactId>mockito-core</artifactId>
84+
<scope>test</scope>
85+
</dependency>
8186
</dependencies>
8287

8388
<build>

extensions/kafka-client/runtime/src/main/java/io/quarkus/kafka/client/runtime/KafkaRuntimeConfigProducer.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
public class KafkaRuntimeConfigProducer {
1818

1919
public static final String TLS_CONFIG_NAME_KEY = "tls-configuration-name";
20+
private static final String TLS_CONFIG_NAME_KEY_NORMALIZED = "tls.configuration.name";
2021

2122
// not "kafka.", because we also inspect env vars, which start with "KAFKA_"
2223
private static final String CONFIG_PREFIX = "kafka";
@@ -46,9 +47,15 @@ public Map<String, Object> createKafkaRuntimeConfig(Config config, ApplicationCo
4647
String effectivePropertyName = propertyNameLowerCase.substring(CONFIG_PREFIX.length() + 1).toLowerCase()
4748
.replace("_", ".");
4849
String value = config.getOptionalValue(propertyName, String.class).orElse("");
49-
result.put(effectivePropertyName, value);
50-
if (effectivePropertyName.equals(TLS_CONFIG_NAME_KEY)) {
50+
// Normalize both property names for comparison (replace hyphens with dots)
51+
// This ensures both YAML properties (kafka.tls-configuration-name) and
52+
// environment variables (KAFKA_TLS_CONFIGURATION_NAME) are matched correctly
53+
if (effectivePropertyName.equals(TLS_CONFIG_NAME_KEY) ||
54+
effectivePropertyName.equals(TLS_CONFIG_NAME_KEY_NORMALIZED)) {
55+
result.put(TLS_CONFIG_NAME_KEY, value);
5156
result.put("ssl.engine.factory.class", QuarkusKafkaSslEngineFactory.class.getName());
57+
} else {
58+
result.put(effectivePropertyName, value);
5259
}
5360
}
5461

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package io.quarkus.kafka.client.runtime;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.mockito.Mockito.mock;
5+
import static org.mockito.Mockito.when;
6+
7+
import java.util.Map;
8+
import java.util.Optional;
9+
10+
import org.eclipse.microprofile.config.Config;
11+
import org.junit.jupiter.api.Test;
12+
13+
import io.quarkus.kafka.client.tls.QuarkusKafkaSslEngineFactory;
14+
import io.quarkus.runtime.ApplicationConfig;
15+
import io.smallrye.config.SmallRyeConfig;
16+
import io.smallrye.config.SmallRyeConfigBuilder;
17+
18+
public class KafkaRuntimeConfigProducerTest {
19+
20+
@Test
21+
public void testTlsConfigurationNameWithHyphens() {
22+
// Test when property is set via application.yaml as kafka.tls-configuration-name
23+
Config config = new SmallRyeConfigBuilder()
24+
.withDefaultValues(Map.of("kafka.tls-configuration-name", "my-tls-config"))
25+
.build();
26+
ApplicationConfig appConfig = mock(ApplicationConfig.class);
27+
when(appConfig.name()).thenReturn(Optional.empty());
28+
29+
KafkaRuntimeConfigProducer producer = new KafkaRuntimeConfigProducer();
30+
Map<String, Object> result = producer.createKafkaRuntimeConfig(config, appConfig);
31+
32+
assertThat(result)
33+
.containsEntry("tls-configuration-name", "my-tls-config")
34+
.containsEntry("ssl.engine.factory.class", QuarkusKafkaSslEngineFactory.class.getName());
35+
}
36+
37+
@Test
38+
public void testTlsConfigurationNameWithUnderscores() {
39+
// Test when property is set via environment variable as KAFKA_TLS_CONFIGURATION_NAME
40+
Config config = new SmallRyeConfigBuilder()
41+
.withDefaultValues(Map.of("KAFKA_TLS_CONFIGURATION_NAME", "my-tls-config"))
42+
.build();
43+
ApplicationConfig appConfig = mock(ApplicationConfig.class);
44+
when(appConfig.name()).thenReturn(Optional.empty());
45+
46+
KafkaRuntimeConfigProducer producer = new KafkaRuntimeConfigProducer();
47+
Map<String, Object> result = producer.createKafkaRuntimeConfig(config, appConfig);
48+
49+
assertThat(result)
50+
.containsEntry("tls-configuration-name", "my-tls-config")
51+
.containsEntry("ssl.engine.factory.class", QuarkusKafkaSslEngineFactory.class.getName());
52+
}
53+
54+
@Test
55+
public void testOtherKafkaProperties() {
56+
// Test that other Kafka properties are processed correctly
57+
Config config = new SmallRyeConfigBuilder()
58+
.withDefaultValues(Map.of("kafka.bootstrap.servers", "localhost:9092",
59+
"KAFKA_CONSUMER_MAX_POLL_RECORDS", "100"))
60+
.build();
61+
ApplicationConfig appConfig = mock(ApplicationConfig.class);
62+
when(appConfig.name()).thenReturn(Optional.empty());
63+
64+
KafkaRuntimeConfigProducer producer = new KafkaRuntimeConfigProducer();
65+
Map<String, Object> result = producer.createKafkaRuntimeConfig(config, appConfig);
66+
67+
assertThat(result)
68+
.containsEntry("bootstrap.servers", "localhost:9092")
69+
.containsEntry("consumer.max.poll.records", "100");
70+
}
71+
72+
@Test
73+
public void testGroupIdDefaultsToApplicationName() {
74+
SmallRyeConfig config = new SmallRyeConfigBuilder()
75+
.withDefaultValues(Map.of("kafka.bootstrap.servers", "localhost:9092"))
76+
.build();
77+
ApplicationConfig appConfig = mock(ApplicationConfig.class);
78+
when(appConfig.name()).thenReturn(Optional.of("my-app"));
79+
80+
KafkaRuntimeConfigProducer producer = new KafkaRuntimeConfigProducer();
81+
Map<String, Object> result = producer.createKafkaRuntimeConfig(config, appConfig);
82+
83+
assertThat(result)
84+
.containsEntry("bootstrap.servers", "localhost:9092")
85+
.containsEntry("group.id", "my-app");
86+
}
87+
}

0 commit comments

Comments
 (0)