|
23 | 23 | import org.zowe.apiml.zaasclient.exception.ZaasConfigurationErrorCodes; |
24 | 24 | import org.zowe.apiml.zaasclient.exception.ZaasConfigurationException; |
25 | 25 |
|
26 | | -import javax.net.ssl.*; |
| 26 | +import javax.net.ssl.HostnameVerifier; |
| 27 | +import javax.net.ssl.KeyManagerFactory; |
| 28 | +import javax.net.ssl.SSLContext; |
| 29 | +import javax.net.ssl.TrustManagerFactory; |
27 | 30 | import java.io.FileInputStream; |
28 | 31 | import java.io.IOException; |
29 | 32 | import java.io.InputStream; |
|
35 | 38 |
|
36 | 39 | @AllArgsConstructor |
37 | 40 | class ZaasHttpsClientProvider implements CloseableClientProvider { |
| 41 | + |
38 | 42 | private static final int REQUEST_TIMEOUT = 30 * 1000; |
39 | 43 |
|
40 | 44 | private final RequestConfig requestConfig; |
41 | 45 |
|
42 | 46 | private static final Pattern KEYRING_PATTERN = Pattern.compile("^(safkeyring[^:]*):/{2,4}([^/]+)/([^/]+)$"); |
43 | 47 |
|
| 48 | + private ConfigProperties configProperties; |
| 49 | + |
44 | 50 | private TrustManagerFactory tmf; |
45 | 51 | private KeyManagerFactory kmf; |
46 | 52 |
|
47 | | - private final char[] keyStorePassword; |
48 | | - private final String keyStoreType; |
49 | | - private final String keyStorePath; |
50 | 53 | private final HostnameVerifier hostnameVerifier; |
51 | 54 |
|
52 | 55 | private final CookieStore cookieStore = new BasicCookieStore(); |
53 | 56 |
|
54 | 57 | private CloseableHttpClient httpsClient; |
55 | 58 |
|
56 | 59 | public ZaasHttpsClientProvider(ConfigProperties configProperties) throws ZaasConfigurationException { |
57 | | - this.requestConfig = this.buildCustomRequestConfig(); |
58 | | - |
59 | 60 | if (configProperties.getTrustStorePath() == null) { |
60 | 61 | throw new ZaasConfigurationException(ZaasConfigurationErrorCodes.TRUST_STORE_NOT_PROVIDED); |
61 | 62 | } |
| 63 | + this.configProperties = configProperties; |
62 | 64 |
|
| 65 | + this.requestConfig = this.buildCustomRequestConfig(); |
63 | 66 | initializeTrustManagerFactory(configProperties.getTrustStorePath(), configProperties.getTrustStoreType(), configProperties.getTrustStorePassword()); |
64 | 67 | this.hostnameVerifier = configProperties.isNonStrictVerifySslCertificatesOfServices() ? new NoopHostnameVerifier() : SSLConnectionSocketFactory.getDefaultHostnameVerifier(); |
65 | | - this.keyStorePath = configProperties.getKeyStorePath(); |
66 | | - this.keyStorePassword = configProperties.getKeyStorePassword(); |
67 | | - this.keyStoreType = configProperties.getKeyStoreType(); |
68 | 68 | } |
69 | 69 |
|
70 | 70 | static boolean isKeyring(String input) { |
@@ -114,14 +114,14 @@ private void initializeTrustManagerFactory(String trustStorePath, String trustSt |
114 | 114 | private void initializeKeyStoreManagerFactory() throws ZaasConfigurationException { |
115 | 115 | try { |
116 | 116 | KeyStore keyStore; |
117 | | - if (keyStorePath != null) { |
118 | | - keyStore = getKeystore(keyStorePath, keyStoreType, keyStorePassword); |
| 117 | + if (configProperties.getKeyStorePath() != null) { |
| 118 | + keyStore = getKeystore(configProperties.getKeyStorePath(), configProperties.getKeyStoreType(), configProperties.getKeyStorePassword()); |
119 | 119 | } else { |
120 | 120 | keyStore = getEmptyKeystore(); |
121 | 121 | } |
122 | 122 |
|
123 | 123 | kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); |
124 | | - kmf.init(keyStore, keyStorePassword); |
| 124 | + kmf.init(keyStore, configProperties.getKeyStorePassword()); |
125 | 125 | } catch (NoSuchAlgorithmException | CertificateException | UnrecoverableKeyException | KeyStoreException e) { |
126 | 126 | throw new ZaasConfigurationException(ZaasConfigurationErrorCodes.WRONG_CRYPTO_CONFIGURATION, e); |
127 | 127 | } catch (IOException e) { |
@@ -155,14 +155,12 @@ private InputStream getCorrectInputStream(String uri) throws IOException { |
155 | 155 |
|
156 | 156 | private SSLContext getSSLContext() throws ZaasConfigurationException { |
157 | 157 | try { |
158 | | - SSLContext sslContext = SSLContext.getInstance("TLSv1.2"); |
| 158 | + SSLContext sslContext = SSLContext.getInstance(configProperties.getProtocol()); |
159 | 159 | sslContext.init( |
160 | 160 | kmf != null ? kmf.getKeyManagers() : null, |
161 | 161 | tmf.getTrustManagers(), |
162 | 162 | new SecureRandom() |
163 | 163 | ); |
164 | | - HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); |
165 | | - HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier); |
166 | 164 | return sslContext; |
167 | 165 | } catch (NoSuchAlgorithmException | KeyManagementException e) { |
168 | 166 | throw new ZaasConfigurationException(ZaasConfigurationErrorCodes.WRONG_CRYPTO_CONFIGURATION, e); |
@@ -192,4 +190,5 @@ private RequestConfig buildCustomRequestConfig() { |
192 | 190 | builder.setConnectTimeout(REQUEST_TIMEOUT); |
193 | 191 | return builder.build(); |
194 | 192 | } |
| 193 | + |
195 | 194 | } |
0 commit comments