|
| 1 | +/* |
| 2 | + * Copyright 2012-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.pulsar; |
| 18 | + |
| 19 | +import java.io.FileNotFoundException; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Set; |
| 22 | +import java.util.stream.Collectors; |
| 23 | + |
| 24 | +import org.springframework.boot.autoconfigure.ssl.JksSslBundleProperties; |
| 25 | +import org.springframework.boot.autoconfigure.ssl.JksSslBundleProperties.Store; |
| 26 | +import org.springframework.boot.autoconfigure.ssl.PemSslBundleProperties; |
| 27 | +import org.springframework.boot.autoconfigure.ssl.SslProperties; |
| 28 | +import org.springframework.boot.context.properties.PropertyMapper; |
| 29 | +import org.springframework.boot.ssl.SslBundle; |
| 30 | +import org.springframework.boot.ssl.SslBundles; |
| 31 | +import org.springframework.boot.ssl.SslOptions; |
| 32 | +import org.springframework.boot.ssl.SslStoreBundle; |
| 33 | +import org.springframework.boot.ssl.jks.JksSslStoreBundle; |
| 34 | +import org.springframework.boot.ssl.pem.PemSslStoreBundle; |
| 35 | +import org.springframework.util.Assert; |
| 36 | +import org.springframework.util.ResourceUtils; |
| 37 | + |
| 38 | +/** |
| 39 | + * Configures the SSL settings on {@link ClientBuilderSslSettings}. |
| 40 | + * |
| 41 | + * @author Chris Bono |
| 42 | + * @since 3.2.0 |
| 43 | + */ |
| 44 | +public class ClientBuilderSslConfigurer { |
| 45 | + |
| 46 | + private final SslBundles sslBundles; |
| 47 | + |
| 48 | + private final SslProperties sslProperties; |
| 49 | + |
| 50 | + /** |
| 51 | + * Construct a configurer using the specified SSL bundle and properties. |
| 52 | + * @param sslBundles optional ssl bundles configured for the application |
| 53 | + * @param sslProperties optional ssl properties for the application |
| 54 | + */ |
| 55 | + public ClientBuilderSslConfigurer(SslBundles sslBundles, SslProperties sslProperties) { |
| 56 | + this.sslBundles = sslBundles; |
| 57 | + this.sslProperties = sslProperties; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Applies the specified SSL properties to the specified client SSL builder. |
| 62 | + * @param clientBuilderSslSettings the ssl builder |
| 63 | + * @param clientSslProperties the ssl properties |
| 64 | + */ |
| 65 | + public void applySsl(ClientBuilderSslSettings<?> clientBuilderSslSettings, |
| 66 | + SslConfigProperties clientSslProperties) { |
| 67 | + if (!clientSslProperties.isEnabled()) { |
| 68 | + return; |
| 69 | + } |
| 70 | + clientBuilderSslSettings.enableTls(true); |
| 71 | + PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); |
| 72 | + map.from(clientSslProperties::isVerifyHostname).to(clientBuilderSslSettings::enableTlsHostnameVerification); |
| 73 | + map.from(clientSslProperties::getAllowInsecureConnection) |
| 74 | + .to(clientBuilderSslSettings::allowTlsInsecureConnection); |
| 75 | + if (clientSslProperties.getBundle() == null) { |
| 76 | + return; |
| 77 | + } |
| 78 | + Assert.state(this.sslBundles != null, "SSL enabled but no SSL bundles configured"); |
| 79 | + Assert.state(this.sslProperties != null, "SSL enabled but no SSL properties configured"); |
| 80 | + String bundleName = clientSslProperties.getBundle(); |
| 81 | + SslBundle sslBundle = this.sslBundles.getBundle(bundleName); |
| 82 | + SslOptions sslOptions = sslBundle.getOptions(); |
| 83 | + if (sslOptions.getCiphers() != null) { |
| 84 | + Set<String> tlsCiphers = Arrays.stream(sslOptions.getCiphers()).collect(Collectors.toSet()); |
| 85 | + clientBuilderSslSettings.tlsCiphers(tlsCiphers); |
| 86 | + } |
| 87 | + if (sslOptions.getEnabledProtocols() != null) { |
| 88 | + Set<String> tlsProtocols = Arrays.stream(sslOptions.getEnabledProtocols()).collect(Collectors.toSet()); |
| 89 | + clientBuilderSslSettings.tlsProtocols(tlsProtocols); |
| 90 | + } |
| 91 | + SslType sslBundleType = SslType.forSslStoreBundle(sslBundle.getStores()); |
| 92 | + switch (sslBundleType) { |
| 93 | + case JKS -> applyJksSslProperties(clientBuilderSslSettings, map, bundleName); |
| 94 | + case PEM -> applyPemSslProperties(clientBuilderSslSettings, map, bundleName); |
| 95 | + case UNSUPPORTED -> throw new IllegalArgumentException( |
| 96 | + "Unsupported store bundle type %s".formatted(sslBundle.getStores().getClass().getName())); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private void applyJksSslProperties(ClientBuilderSslSettings<?> clientBuilderSslSettings, PropertyMapper map, |
| 101 | + String bundleName) { |
| 102 | + clientBuilderSslSettings.useKeyStoreTls(true); |
| 103 | + // Dip down into the ssl props for the info we need to set on the client builder |
| 104 | + JksSslBundleProperties jksProperties = this.sslProperties.getBundle().getJks().get(bundleName); |
| 105 | + Assert.state(jksProperties != null, |
| 106 | + () -> "SslStoreBundle is JKS but unable to find 'spring.ssl.bundle.jks.%s.*' properties" |
| 107 | + .formatted(bundleName)); |
| 108 | + Store trustStore = jksProperties.getTruststore(); |
| 109 | + map.from(trustStore::getType).to(clientBuilderSslSettings::tlsTrustStoreType); |
| 110 | + map.from(trustStore::getLocation).as(this::resolvePath).to(clientBuilderSslSettings::tlsTrustStorePath); |
| 111 | + map.from(trustStore::getPassword).to(clientBuilderSslSettings::tlsTrustStorePassword); |
| 112 | + map.from(trustStore::getProvider).to(clientBuilderSslSettings::sslProvider); |
| 113 | + Store keyStore = jksProperties.getKeystore(); |
| 114 | + map.from(keyStore::getType).to(clientBuilderSslSettings::tlsKeyStoreType); |
| 115 | + map.from(keyStore::getLocation).as(this::resolvePath).to(clientBuilderSslSettings::tlsKeyStorePath); |
| 116 | + map.from(keyStore::getPassword).to(clientBuilderSslSettings::tlsKeyStorePassword); |
| 117 | + // Key store provider overrides trust store provider |
| 118 | + map.from(keyStore::getProvider).to(clientBuilderSslSettings::sslProvider); |
| 119 | + } |
| 120 | + |
| 121 | + private void applyPemSslProperties(ClientBuilderSslSettings<?> clientBuilderSslSettings, PropertyMapper map, |
| 122 | + String bundleName) { |
| 123 | + // Dip down into the properties for the info we need to set on the client builder |
| 124 | + PemSslBundleProperties pemProperties = this.sslProperties.getBundle().getPem().get(bundleName); |
| 125 | + Assert.state(pemProperties != null, |
| 126 | + () -> "SslStoreBundle is PEM but unable to find 'spring.ssl.bundle.pem.%s.*' properties" |
| 127 | + .formatted(bundleName)); |
| 128 | + PemSslBundleProperties.Store trustStore = pemProperties.getTruststore(); |
| 129 | + map.from(trustStore::getCertificate).as(this::resolvePath).to(clientBuilderSslSettings::tlsTrustCertsFilePath); |
| 130 | + PemSslBundleProperties.Store keyStore = pemProperties.getKeystore(); |
| 131 | + map.from(keyStore::getPrivateKey).as(this::resolvePath).to(clientBuilderSslSettings::tlsKeyFilePath); |
| 132 | + map.from(keyStore::getCertificate).as(this::resolvePath).to(clientBuilderSslSettings::tlsCertificateFilePath); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Resolves a location into an actual path. The Pulsar client builders TLS related |
| 137 | + * methods all expect the locations passed in to be file paths. Adding this resolve |
| 138 | + * allows us to use 'classpath:' locations. |
| 139 | + * @param resourceLocation the location of the resource |
| 140 | + * @return path to the resource |
| 141 | + */ |
| 142 | + private String resolvePath(String resourceLocation) { |
| 143 | + try { |
| 144 | + return ResourceUtils.getURL(resourceLocation).getPath(); |
| 145 | + } |
| 146 | + catch (FileNotFoundException ex) { |
| 147 | + throw new RuntimeException(ex); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + enum SslType { |
| 152 | + |
| 153 | + JKS, PEM, UNSUPPORTED; |
| 154 | + |
| 155 | + static SslType forSslStoreBundle(SslStoreBundle sslStoreBundle) { |
| 156 | + if (sslStoreBundle instanceof JksSslStoreBundle) { |
| 157 | + return SslType.JKS; |
| 158 | + } |
| 159 | + if (sslStoreBundle instanceof PemSslStoreBundle) { |
| 160 | + return SslType.PEM; |
| 161 | + } |
| 162 | + return UNSUPPORTED; |
| 163 | + } |
| 164 | + |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments