|
43 | 43 | import org.springframework.kafka.listener.ContainerProperties.AckMode;
|
44 | 44 | import org.springframework.kafka.security.jaas.KafkaJaasLoginModuleInitializer;
|
45 | 45 | import org.springframework.util.CollectionUtils;
|
| 46 | +import org.springframework.util.StringUtils; |
46 | 47 | import org.springframework.util.unit.DataSize;
|
47 | 48 |
|
48 | 49 | /**
|
@@ -1427,60 +1428,67 @@ public Map<String, Object> buildProperties() {
|
1427 | 1428 |
|
1428 | 1429 | public Map<String, Object> buildProperties(SslBundles sslBundles) {
|
1429 | 1430 | validate();
|
1430 |
| - Properties properties = new Properties(); |
1431 |
| - if (getBundle() != null) { |
1432 |
| - properties.in(SslConfigs.SSL_ENGINE_FACTORY_CLASS_CONFIG) |
1433 |
| - .accept(SslBundleSslEngineFactory.class.getName()); |
1434 |
| - properties.in(SslBundle.class.getName()).accept(sslBundles.getBundle(getBundle())); |
1435 |
| - } |
1436 |
| - else { |
1437 |
| - PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); |
1438 |
| - map.from(this::getKeyPassword).to(properties.in(SslConfigs.SSL_KEY_PASSWORD_CONFIG)); |
1439 |
| - map.from(this::getKeyStoreCertificateChain) |
1440 |
| - .to(properties.in(SslConfigs.SSL_KEYSTORE_CERTIFICATE_CHAIN_CONFIG)); |
1441 |
| - map.from(this::getKeyStoreKey).to(properties.in(SslConfigs.SSL_KEYSTORE_KEY_CONFIG)); |
1442 |
| - map.from(this::getKeyStoreLocation) |
1443 |
| - .as(this::resourceToPath) |
1444 |
| - .to(properties.in(SslConfigs.SSL_KEYSTORE_LOCATION_CONFIG)); |
1445 |
| - map.from(this::getKeyStorePassword).to(properties.in(SslConfigs.SSL_KEYSTORE_PASSWORD_CONFIG)); |
1446 |
| - map.from(this::getKeyStoreType).to(properties.in(SslConfigs.SSL_KEYSTORE_TYPE_CONFIG)); |
1447 |
| - map.from(this::getTrustStoreCertificates) |
1448 |
| - .to(properties.in(SslConfigs.SSL_TRUSTSTORE_CERTIFICATES_CONFIG)); |
1449 |
| - map.from(this::getTrustStoreLocation) |
1450 |
| - .as(this::resourceToPath) |
1451 |
| - .to(properties.in(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG)); |
1452 |
| - map.from(this::getTrustStorePassword).to(properties.in(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG)); |
1453 |
| - map.from(this::getTrustStoreType).to(properties.in(SslConfigs.SSL_TRUSTSTORE_TYPE_CONFIG)); |
1454 |
| - map.from(this::getProtocol).to(properties.in(SslConfigs.SSL_PROTOCOL_CONFIG)); |
| 1431 | + String bundleName = getBundle(); |
| 1432 | + if (StringUtils.hasText(bundleName)) { |
| 1433 | + return buildPropertiesForSslBundle(sslBundles, bundleName); |
1455 | 1434 | }
|
| 1435 | + Properties properties = new Properties(); |
| 1436 | + PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); |
| 1437 | + map.from(this::getKeyPassword).to(properties.in(SslConfigs.SSL_KEY_PASSWORD_CONFIG)); |
| 1438 | + map.from(this::getKeyStoreCertificateChain) |
| 1439 | + .to(properties.in(SslConfigs.SSL_KEYSTORE_CERTIFICATE_CHAIN_CONFIG)); |
| 1440 | + map.from(this::getKeyStoreKey).to(properties.in(SslConfigs.SSL_KEYSTORE_KEY_CONFIG)); |
| 1441 | + map.from(this::getKeyStoreLocation) |
| 1442 | + .as(this::resourceToPath) |
| 1443 | + .to(properties.in(SslConfigs.SSL_KEYSTORE_LOCATION_CONFIG)); |
| 1444 | + map.from(this::getKeyStorePassword).to(properties.in(SslConfigs.SSL_KEYSTORE_PASSWORD_CONFIG)); |
| 1445 | + map.from(this::getKeyStoreType).to(properties.in(SslConfigs.SSL_KEYSTORE_TYPE_CONFIG)); |
| 1446 | + map.from(this::getTrustStoreCertificates).to(properties.in(SslConfigs.SSL_TRUSTSTORE_CERTIFICATES_CONFIG)); |
| 1447 | + map.from(this::getTrustStoreLocation) |
| 1448 | + .as(this::resourceToPath) |
| 1449 | + .to(properties.in(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG)); |
| 1450 | + map.from(this::getTrustStorePassword).to(properties.in(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG)); |
| 1451 | + map.from(this::getTrustStoreType).to(properties.in(SslConfigs.SSL_TRUSTSTORE_TYPE_CONFIG)); |
| 1452 | + map.from(this::getProtocol).to(properties.in(SslConfigs.SSL_PROTOCOL_CONFIG)); |
| 1453 | + return properties; |
| 1454 | + } |
| 1455 | + |
| 1456 | + private Map<String, Object> buildPropertiesForSslBundle(SslBundles sslBundles, String name) { |
| 1457 | + Properties properties = new Properties(); |
| 1458 | + properties.in(SslConfigs.SSL_ENGINE_FACTORY_CLASS_CONFIG).accept(SslBundleSslEngineFactory.class.getName()); |
| 1459 | + properties.in(SslBundle.class.getName()).accept(sslBundles.getBundle(name)); |
1456 | 1460 | return properties;
|
1457 | 1461 | }
|
1458 | 1462 |
|
1459 | 1463 | private void validate() {
|
1460 |
| - MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleNonNullValuesIn((entries) -> { |
| 1464 | + MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleMatchingValuesIn((entries) -> { |
1461 | 1465 | entries.put("spring.kafka.ssl.key-store-key", getKeyStoreKey());
|
1462 | 1466 | entries.put("spring.kafka.ssl.key-store-location", getKeyStoreLocation());
|
1463 |
| - }); |
1464 |
| - MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleNonNullValuesIn((entries) -> { |
| 1467 | + }, this::hasValue); |
| 1468 | + MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleMatchingValuesIn((entries) -> { |
1465 | 1469 | entries.put("spring.kafka.ssl.trust-store-certificates", getTrustStoreCertificates());
|
1466 | 1470 | entries.put("spring.kafka.ssl.trust-store-location", getTrustStoreLocation());
|
1467 |
| - }); |
1468 |
| - MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleNonNullValuesIn((entries) -> { |
| 1471 | + }, this::hasValue); |
| 1472 | + MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleMatchingValuesIn((entries) -> { |
1469 | 1473 | entries.put("spring.kafka.ssl.bundle", getBundle());
|
1470 | 1474 | entries.put("spring.kafka.ssl.key-store-key", getKeyStoreKey());
|
1471 |
| - }); |
1472 |
| - MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleNonNullValuesIn((entries) -> { |
| 1475 | + }, this::hasValue); |
| 1476 | + MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleMatchingValuesIn((entries) -> { |
1473 | 1477 | entries.put("spring.kafka.ssl.bundle", getBundle());
|
1474 | 1478 | entries.put("spring.kafka.ssl.key-store-location", getKeyStoreLocation());
|
1475 |
| - }); |
1476 |
| - MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleNonNullValuesIn((entries) -> { |
| 1479 | + }, this::hasValue); |
| 1480 | + MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleMatchingValuesIn((entries) -> { |
1477 | 1481 | entries.put("spring.kafka.ssl.bundle", getBundle());
|
1478 | 1482 | entries.put("spring.kafka.ssl.trust-store-certificates", getTrustStoreCertificates());
|
1479 |
| - }); |
1480 |
| - MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleNonNullValuesIn((entries) -> { |
| 1483 | + }, this::hasValue); |
| 1484 | + MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleMatchingValuesIn((entries) -> { |
1481 | 1485 | entries.put("spring.kafka.ssl.bundle", getBundle());
|
1482 | 1486 | entries.put("spring.kafka.ssl.trust-store-location", getTrustStoreLocation());
|
1483 |
| - }); |
| 1487 | + }, this::hasValue); |
| 1488 | + } |
| 1489 | + |
| 1490 | + private boolean hasValue(Object value) { |
| 1491 | + return (value instanceof String string) ? StringUtils.hasText(string) : value != null; |
1484 | 1492 | }
|
1485 | 1493 |
|
1486 | 1494 | private String resourceToPath(Resource resource) {
|
|
0 commit comments