|
44 | 44 | import org.springframework.kafka.listener.ContainerProperties.AckMode;
|
45 | 45 | import org.springframework.kafka.security.jaas.KafkaJaasLoginModuleInitializer;
|
46 | 46 | import org.springframework.util.CollectionUtils;
|
| 47 | +import org.springframework.util.StringUtils; |
47 | 48 | import org.springframework.util.unit.DataSize;
|
48 | 49 |
|
49 | 50 | /**
|
@@ -1399,60 +1400,67 @@ public Map<String, Object> buildProperties() {
|
1399 | 1400 |
|
1400 | 1401 | public Map<String, Object> buildProperties(SslBundles sslBundles) {
|
1401 | 1402 | 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); |
1427 | 1406 | }
|
| 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)); |
1428 | 1432 | return properties;
|
1429 | 1433 | }
|
1430 | 1434 |
|
1431 | 1435 | private void validate() {
|
1432 |
| - MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleNonNullValuesIn((entries) -> { |
| 1436 | + MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleMatchingValuesIn((entries) -> { |
1433 | 1437 | entries.put("spring.kafka.ssl.key-store-key", getKeyStoreKey());
|
1434 | 1438 | entries.put("spring.kafka.ssl.key-store-location", getKeyStoreLocation());
|
1435 |
| - }); |
1436 |
| - MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleNonNullValuesIn((entries) -> { |
| 1439 | + }, this::hasValue); |
| 1440 | + MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleMatchingValuesIn((entries) -> { |
1437 | 1441 | entries.put("spring.kafka.ssl.trust-store-certificates", getTrustStoreCertificates());
|
1438 | 1442 | entries.put("spring.kafka.ssl.trust-store-location", getTrustStoreLocation());
|
1439 |
| - }); |
1440 |
| - MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleNonNullValuesIn((entries) -> { |
| 1443 | + }, this::hasValue); |
| 1444 | + MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleMatchingValuesIn((entries) -> { |
1441 | 1445 | entries.put("spring.kafka.ssl.bundle", getBundle());
|
1442 | 1446 | entries.put("spring.kafka.ssl.key-store-key", getKeyStoreKey());
|
1443 |
| - }); |
1444 |
| - MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleNonNullValuesIn((entries) -> { |
| 1447 | + }, this::hasValue); |
| 1448 | + MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleMatchingValuesIn((entries) -> { |
1445 | 1449 | entries.put("spring.kafka.ssl.bundle", getBundle());
|
1446 | 1450 | entries.put("spring.kafka.ssl.key-store-location", getKeyStoreLocation());
|
1447 |
| - }); |
1448 |
| - MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleNonNullValuesIn((entries) -> { |
| 1451 | + }, this::hasValue); |
| 1452 | + MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleMatchingValuesIn((entries) -> { |
1449 | 1453 | entries.put("spring.kafka.ssl.bundle", getBundle());
|
1450 | 1454 | entries.put("spring.kafka.ssl.trust-store-certificates", getTrustStoreCertificates());
|
1451 |
| - }); |
1452 |
| - MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleNonNullValuesIn((entries) -> { |
| 1455 | + }, this::hasValue); |
| 1456 | + MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleMatchingValuesIn((entries) -> { |
1453 | 1457 | entries.put("spring.kafka.ssl.bundle", getBundle());
|
1454 | 1458 | 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; |
1456 | 1464 | }
|
1457 | 1465 |
|
1458 | 1466 | private String resourceToPath(Resource resource) {
|
|
0 commit comments