Skip to content

Commit ef43160

Browse files
committed
Merge branch '3.3.x'
Closes gh-43563
2 parents 809d6f3 + ba916cb commit ef43160

File tree

4 files changed

+77
-41
lines changed

4 files changed

+77
-41
lines changed

Diff for: spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/kafka/KafkaProperties.java

+45-37
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.springframework.kafka.listener.ContainerProperties.AckMode;
4545
import org.springframework.kafka.security.jaas.KafkaJaasLoginModuleInitializer;
4646
import org.springframework.util.CollectionUtils;
47+
import org.springframework.util.StringUtils;
4748
import org.springframework.util.unit.DataSize;
4849

4950
/**
@@ -1399,60 +1400,67 @@ public Map<String, Object> buildProperties() {
13991400

14001401
public Map<String, Object> buildProperties(SslBundles sslBundles) {
14011402
validate();
1402-
Properties properties = new Properties();
1403-
if (getBundle() != null) {
1404-
properties.in(SslConfigs.SSL_ENGINE_FACTORY_CLASS_CONFIG)
1405-
.accept(SslBundleSslEngineFactory.class.getName());
1406-
properties.in(SslBundle.class.getName()).accept(sslBundles.getBundle(getBundle()));
1407-
}
1408-
else {
1409-
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
1410-
map.from(this::getKeyPassword).to(properties.in(SslConfigs.SSL_KEY_PASSWORD_CONFIG));
1411-
map.from(this::getKeyStoreCertificateChain)
1412-
.to(properties.in(SslConfigs.SSL_KEYSTORE_CERTIFICATE_CHAIN_CONFIG));
1413-
map.from(this::getKeyStoreKey).to(properties.in(SslConfigs.SSL_KEYSTORE_KEY_CONFIG));
1414-
map.from(this::getKeyStoreLocation)
1415-
.as(this::resourceToPath)
1416-
.to(properties.in(SslConfigs.SSL_KEYSTORE_LOCATION_CONFIG));
1417-
map.from(this::getKeyStorePassword).to(properties.in(SslConfigs.SSL_KEYSTORE_PASSWORD_CONFIG));
1418-
map.from(this::getKeyStoreType).to(properties.in(SslConfigs.SSL_KEYSTORE_TYPE_CONFIG));
1419-
map.from(this::getTrustStoreCertificates)
1420-
.to(properties.in(SslConfigs.SSL_TRUSTSTORE_CERTIFICATES_CONFIG));
1421-
map.from(this::getTrustStoreLocation)
1422-
.as(this::resourceToPath)
1423-
.to(properties.in(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG));
1424-
map.from(this::getTrustStorePassword).to(properties.in(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG));
1425-
map.from(this::getTrustStoreType).to(properties.in(SslConfigs.SSL_TRUSTSTORE_TYPE_CONFIG));
1426-
map.from(this::getProtocol).to(properties.in(SslConfigs.SSL_PROTOCOL_CONFIG));
1403+
String bundleName = getBundle();
1404+
if (StringUtils.hasText(bundleName)) {
1405+
return buildPropertiesForSslBundle(sslBundles, bundleName);
14271406
}
1407+
Properties properties = new Properties();
1408+
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
1409+
map.from(this::getKeyPassword).to(properties.in(SslConfigs.SSL_KEY_PASSWORD_CONFIG));
1410+
map.from(this::getKeyStoreCertificateChain)
1411+
.to(properties.in(SslConfigs.SSL_KEYSTORE_CERTIFICATE_CHAIN_CONFIG));
1412+
map.from(this::getKeyStoreKey).to(properties.in(SslConfigs.SSL_KEYSTORE_KEY_CONFIG));
1413+
map.from(this::getKeyStoreLocation)
1414+
.as(this::resourceToPath)
1415+
.to(properties.in(SslConfigs.SSL_KEYSTORE_LOCATION_CONFIG));
1416+
map.from(this::getKeyStorePassword).to(properties.in(SslConfigs.SSL_KEYSTORE_PASSWORD_CONFIG));
1417+
map.from(this::getKeyStoreType).to(properties.in(SslConfigs.SSL_KEYSTORE_TYPE_CONFIG));
1418+
map.from(this::getTrustStoreCertificates).to(properties.in(SslConfigs.SSL_TRUSTSTORE_CERTIFICATES_CONFIG));
1419+
map.from(this::getTrustStoreLocation)
1420+
.as(this::resourceToPath)
1421+
.to(properties.in(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG));
1422+
map.from(this::getTrustStorePassword).to(properties.in(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG));
1423+
map.from(this::getTrustStoreType).to(properties.in(SslConfigs.SSL_TRUSTSTORE_TYPE_CONFIG));
1424+
map.from(this::getProtocol).to(properties.in(SslConfigs.SSL_PROTOCOL_CONFIG));
1425+
return properties;
1426+
}
1427+
1428+
private Map<String, Object> buildPropertiesForSslBundle(SslBundles sslBundles, String name) {
1429+
Properties properties = new Properties();
1430+
properties.in(SslConfigs.SSL_ENGINE_FACTORY_CLASS_CONFIG).accept(SslBundleSslEngineFactory.class.getName());
1431+
properties.in(SslBundle.class.getName()).accept(sslBundles.getBundle(name));
14281432
return properties;
14291433
}
14301434

14311435
private void validate() {
1432-
MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleNonNullValuesIn((entries) -> {
1436+
MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleMatchingValuesIn((entries) -> {
14331437
entries.put("spring.kafka.ssl.key-store-key", getKeyStoreKey());
14341438
entries.put("spring.kafka.ssl.key-store-location", getKeyStoreLocation());
1435-
});
1436-
MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleNonNullValuesIn((entries) -> {
1439+
}, this::hasValue);
1440+
MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleMatchingValuesIn((entries) -> {
14371441
entries.put("spring.kafka.ssl.trust-store-certificates", getTrustStoreCertificates());
14381442
entries.put("spring.kafka.ssl.trust-store-location", getTrustStoreLocation());
1439-
});
1440-
MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleNonNullValuesIn((entries) -> {
1443+
}, this::hasValue);
1444+
MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleMatchingValuesIn((entries) -> {
14411445
entries.put("spring.kafka.ssl.bundle", getBundle());
14421446
entries.put("spring.kafka.ssl.key-store-key", getKeyStoreKey());
1443-
});
1444-
MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleNonNullValuesIn((entries) -> {
1447+
}, this::hasValue);
1448+
MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleMatchingValuesIn((entries) -> {
14451449
entries.put("spring.kafka.ssl.bundle", getBundle());
14461450
entries.put("spring.kafka.ssl.key-store-location", getKeyStoreLocation());
1447-
});
1448-
MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleNonNullValuesIn((entries) -> {
1451+
}, this::hasValue);
1452+
MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleMatchingValuesIn((entries) -> {
14491453
entries.put("spring.kafka.ssl.bundle", getBundle());
14501454
entries.put("spring.kafka.ssl.trust-store-certificates", getTrustStoreCertificates());
1451-
});
1452-
MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleNonNullValuesIn((entries) -> {
1455+
}, this::hasValue);
1456+
MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleMatchingValuesIn((entries) -> {
14531457
entries.put("spring.kafka.ssl.bundle", getBundle());
14541458
entries.put("spring.kafka.ssl.trust-store-location", getTrustStoreLocation());
1455-
});
1459+
}, this::hasValue);
1460+
}
1461+
1462+
private boolean hasValue(Object value) {
1463+
return (value instanceof String string) ? StringUtils.hasText(string) : value != null;
14561464
}
14571465

14581466
private String resourceToPath(Resource resource) {

Diff for: spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/client/ClientsConfiguredCondition.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

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

+14
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,20 @@ void sslPemConfiguration() {
8787
"-----BEGINchain");
8888
}
8989

90+
@Test
91+
void sslPemConfigurationWithEmptyBundle() {
92+
KafkaProperties properties = new KafkaProperties();
93+
properties.getSsl().setKeyStoreKey("-----BEGINkey");
94+
properties.getSsl().setTrustStoreCertificates("-----BEGINtrust");
95+
properties.getSsl().setKeyStoreCertificateChain("-----BEGINchain");
96+
properties.getSsl().setBundle("");
97+
Map<String, Object> consumerProperties = properties.buildConsumerProperties();
98+
assertThat(consumerProperties).containsEntry(SslConfigs.SSL_KEYSTORE_KEY_CONFIG, "-----BEGINkey");
99+
assertThat(consumerProperties).containsEntry(SslConfigs.SSL_TRUSTSTORE_CERTIFICATES_CONFIG, "-----BEGINtrust");
100+
assertThat(consumerProperties).containsEntry(SslConfigs.SSL_KEYSTORE_CERTIFICATE_CHAIN_CONFIG,
101+
"-----BEGINchain");
102+
}
103+
90104
@Test
91105
void sslBundleConfiguration() {
92106
KafkaProperties properties = new KafkaProperties();

Diff for: spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/MutuallyExclusiveConfigurationPropertiesException.java

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,8 +20,10 @@
2020
import java.util.LinkedHashMap;
2121
import java.util.LinkedHashSet;
2222
import java.util.Map;
23+
import java.util.Objects;
2324
import java.util.Set;
2425
import java.util.function.Consumer;
26+
import java.util.function.Predicate;
2527
import java.util.stream.Collectors;
2628

2729
import org.springframework.util.Assert;
@@ -96,11 +98,23 @@ private static String buildMessage(Set<String> mutuallyExclusiveNames, Set<Strin
9698
* @param entries a consumer used to populate the entries to check
9799
*/
98100
public static void throwIfMultipleNonNullValuesIn(Consumer<Map<String, Object>> entries) {
99-
Map<String, Object> map = new LinkedHashMap<>();
101+
throwIfMultipleMatchingValuesIn(entries, Objects::nonNull);
102+
}
103+
104+
/**
105+
* Throw a new {@link MutuallyExclusiveConfigurationPropertiesException} if multiple
106+
* values are defined in a set of entries that match the given predicate.
107+
* @param <V> the value type
108+
* @param entries a consumer used to populate the entries to check
109+
* @param predicate the predicate used to check for matching values
110+
* @since 3.3.7
111+
*/
112+
public static <V> void throwIfMultipleMatchingValuesIn(Consumer<Map<String, V>> entries, Predicate<V> predicate) {
113+
Map<String, V> map = new LinkedHashMap<>();
100114
entries.accept(map);
101115
Set<String> configuredNames = map.entrySet()
102116
.stream()
103-
.filter((entry) -> entry.getValue() != null)
117+
.filter((entry) -> predicate.test(entry.getValue()))
104118
.map(Map.Entry::getKey)
105119
.collect(Collectors.toCollection(LinkedHashSet::new));
106120
if (configuredNames.size() > 1) {

0 commit comments

Comments
 (0)