Skip to content

Commit 0d06a28

Browse files
committed
Polish "Support PEM format for Kafka SSL certs and private key"
See gh-28123
1 parent 7a98364 commit 0d06a28

File tree

1 file changed

+38
-0
lines changed
  • spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/kafka

1 file changed

+38
-0
lines changed

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/kafka/KafkaPropertiesTests.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,27 @@
1616

1717
package org.springframework.boot.autoconfigure.kafka;
1818

19+
import java.util.Map;
20+
21+
import org.apache.kafka.common.config.SslConfigs;
1922
import org.junit.jupiter.api.Test;
2023

2124
import org.springframework.boot.autoconfigure.kafka.KafkaProperties.Cleanup;
2225
import org.springframework.boot.autoconfigure.kafka.KafkaProperties.IsolationLevel;
2326
import org.springframework.boot.autoconfigure.kafka.KafkaProperties.Listener;
27+
import org.springframework.boot.context.properties.source.MutuallyExclusiveConfigurationPropertiesException;
28+
import org.springframework.core.io.ClassPathResource;
2429
import org.springframework.kafka.core.CleanupConfig;
2530
import org.springframework.kafka.listener.ContainerProperties;
2631

2732
import static org.assertj.core.api.Assertions.assertThat;
33+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2834

2935
/**
3036
* Tests for {@link KafkaProperties}.
3137
*
3238
* @author Stephane Nicoll
39+
* @author Madhura Bhave
3340
*/
3441
class KafkaPropertiesTests {
3542

@@ -52,6 +59,37 @@ void listenerDefaultValuesAreConsistent() {
5259
assertThat(listenerProperties.isMissingTopicsFatal()).isEqualTo(container.isMissingTopicsFatal());
5360
}
5461

62+
@Test
63+
void sslPemConfiguration() {
64+
KafkaProperties properties = new KafkaProperties();
65+
properties.getSsl().setKeyStoreKey("-----BEGINkey");
66+
properties.getSsl().setTrustStoreCertificates("-----BEGINtrust");
67+
properties.getSsl().setKeyStoreCertificateChain("-----BEGINchain");
68+
Map<String, Object> consumerProperties = properties.buildConsumerProperties();
69+
assertThat(consumerProperties.get(SslConfigs.SSL_KEYSTORE_KEY_CONFIG)).isEqualTo("-----BEGINkey");
70+
assertThat(consumerProperties.get(SslConfigs.SSL_TRUSTSTORE_CERTIFICATES_CONFIG)).isEqualTo("-----BEGINtrust");
71+
assertThat(consumerProperties.get(SslConfigs.SSL_KEYSTORE_CERTIFICATE_CHAIN_CONFIG))
72+
.isEqualTo("-----BEGINchain");
73+
}
74+
75+
@Test
76+
void sslPropertiesWhenKeyStoreLocationAndKeySetShouldThrowException() {
77+
KafkaProperties properties = new KafkaProperties();
78+
properties.getSsl().setKeyStoreKey("-----BEGIN");
79+
properties.getSsl().setKeyStoreLocation(new ClassPathResource("ksLoc"));
80+
assertThatExceptionOfType(MutuallyExclusiveConfigurationPropertiesException.class)
81+
.isThrownBy(properties::buildConsumerProperties);
82+
}
83+
84+
@Test
85+
void sslPropertiesWhenTrustStoreLocationAndCertificatesSetShouldThrowException() {
86+
KafkaProperties properties = new KafkaProperties();
87+
properties.getSsl().setTrustStoreLocation(new ClassPathResource("tsLoc"));
88+
properties.getSsl().setTrustStoreCertificates("-----BEGIN");
89+
assertThatExceptionOfType(MutuallyExclusiveConfigurationPropertiesException.class)
90+
.isThrownBy(properties::buildConsumerProperties);
91+
}
92+
5593
@Test
5694
void cleanupConfigDefaultValuesAreConsistent() {
5795
CleanupConfig cleanupConfig = new CleanupConfig();

0 commit comments

Comments
 (0)