|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.google.api.client.googleapis.apache.v5; |
| 16 | + |
| 17 | +import com.google.api.client.googleapis.GoogleUtils; |
| 18 | +import com.google.api.client.googleapis.mtls.MtlsProvider; |
| 19 | +import com.google.api.client.googleapis.mtls.MtlsUtils; |
| 20 | +import com.google.api.client.googleapis.util.Utils; |
| 21 | +import com.google.api.client.http.apache.v5.Apache5HttpTransport; |
| 22 | +import com.google.api.client.util.SslUtils; |
| 23 | +import com.google.common.annotations.Beta; |
| 24 | +import com.google.common.annotations.VisibleForTesting; |
| 25 | +import java.io.IOException; |
| 26 | +import java.net.ProxySelector; |
| 27 | +import java.security.GeneralSecurityException; |
| 28 | +import java.security.KeyStore; |
| 29 | +import java.util.concurrent.TimeUnit; |
| 30 | +import javax.net.ssl.SSLContext; |
| 31 | +import org.apache.hc.client5.http.config.ConnectionConfig; |
| 32 | +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; |
| 33 | +import org.apache.hc.client5.http.impl.classic.HttpClients; |
| 34 | +import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; |
| 35 | +import org.apache.hc.client5.http.impl.routing.SystemDefaultRoutePlanner; |
| 36 | +import org.apache.hc.client5.http.socket.ConnectionSocketFactory; |
| 37 | +import org.apache.hc.client5.http.socket.LayeredConnectionSocketFactory; |
| 38 | +import org.apache.hc.client5.http.socket.PlainConnectionSocketFactory; |
| 39 | +import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory; |
| 40 | +import org.apache.hc.core5.http.config.Registry; |
| 41 | +import org.apache.hc.core5.http.config.RegistryBuilder; |
| 42 | + |
| 43 | +/** |
| 44 | + * Utilities for Google APIs based on {@link Apache5HttpTransport}. |
| 45 | + * |
| 46 | + * @since 2.6.1 |
| 47 | + */ |
| 48 | +public final class GoogleApache5HttpTransport { |
| 49 | + |
| 50 | + /** |
| 51 | + * Returns a new instance of {@link Apache5HttpTransport} that uses {@link |
| 52 | + * GoogleUtils#getCertificateTrustStore()} for the trusted certificates. If |
| 53 | + * `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "true", and the default |
| 54 | + * client certificate key store from {@link Utils#loadDefaultMtlsKeyStore()} is not null, then the |
| 55 | + * transport uses the default client certificate and is mutual TLS. |
| 56 | + */ |
| 57 | + public static Apache5HttpTransport newTrustedTransport() |
| 58 | + throws GeneralSecurityException, IOException { |
| 59 | + return newTrustedTransport(MtlsUtils.getDefaultMtlsProvider()); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * {@link Beta} <br> |
| 64 | + * Returns a new instance of {@link Apache5HttpTransport} that uses {@link |
| 65 | + * GoogleUtils#getCertificateTrustStore()} for the trusted certificates. mtlsProvider can be used |
| 66 | + * to configure mutual TLS for the transport. |
| 67 | + * |
| 68 | + * @param mtlsProvider MtlsProvider to configure mutual TLS for the transport |
| 69 | + */ |
| 70 | + @Beta |
| 71 | + public static Apache5HttpTransport newTrustedTransport(MtlsProvider mtlsProvider) |
| 72 | + throws GeneralSecurityException, IOException { |
| 73 | + |
| 74 | + SocketFactoryRegistryHandler handler = new SocketFactoryRegistryHandler(mtlsProvider); |
| 75 | + |
| 76 | + PoolingHttpClientConnectionManager connectionManager = |
| 77 | + new PoolingHttpClientConnectionManager(handler.getSocketFactoryRegistry()); |
| 78 | + connectionManager.setMaxTotal(200); |
| 79 | + connectionManager.setDefaultMaxPerRoute(20); |
| 80 | + connectionManager.setDefaultConnectionConfig( |
| 81 | + ConnectionConfig.custom() |
| 82 | + .setTimeToLive(-1, TimeUnit.MILLISECONDS) |
| 83 | + .setValidateAfterInactivity(-1L, TimeUnit.MILLISECONDS) |
| 84 | + .build()); |
| 85 | + |
| 86 | + CloseableHttpClient client = |
| 87 | + HttpClients.custom() |
| 88 | + .useSystemProperties() |
| 89 | + .setConnectionManager(connectionManager) |
| 90 | + .setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault())) |
| 91 | + .disableRedirectHandling() |
| 92 | + .disableAutomaticRetries() |
| 93 | + .build(); |
| 94 | + |
| 95 | + return new Apache5HttpTransport(client, handler.isMtls()); |
| 96 | + } |
| 97 | + |
| 98 | + @VisibleForTesting |
| 99 | + static class SocketFactoryRegistryHandler { |
| 100 | + private final Registry<ConnectionSocketFactory> socketFactoryRegistry; |
| 101 | + private final boolean isMtls; |
| 102 | + |
| 103 | + public SocketFactoryRegistryHandler(MtlsProvider mtlsProvider) |
| 104 | + throws GeneralSecurityException, IOException { |
| 105 | + KeyStore mtlsKeyStore = null; |
| 106 | + String mtlsKeyStorePassword = null; |
| 107 | + if (mtlsProvider.useMtlsClientCertificate()) { |
| 108 | + mtlsKeyStore = mtlsProvider.getKeyStore(); |
| 109 | + mtlsKeyStorePassword = mtlsProvider.getKeyStorePassword(); |
| 110 | + } |
| 111 | + |
| 112 | + // Use the included trust store |
| 113 | + KeyStore trustStore = GoogleUtils.getCertificateTrustStore(); |
| 114 | + SSLContext sslContext = SslUtils.getTlsSslContext(); |
| 115 | + |
| 116 | + if (mtlsKeyStore != null && mtlsKeyStorePassword != null) { |
| 117 | + this.isMtls = true; |
| 118 | + SslUtils.initSslContext( |
| 119 | + sslContext, |
| 120 | + trustStore, |
| 121 | + SslUtils.getPkixTrustManagerFactory(), |
| 122 | + mtlsKeyStore, |
| 123 | + mtlsKeyStorePassword, |
| 124 | + SslUtils.getDefaultKeyManagerFactory()); |
| 125 | + } else { |
| 126 | + this.isMtls = false; |
| 127 | + SslUtils.initSslContext(sslContext, trustStore, SslUtils.getPkixTrustManagerFactory()); |
| 128 | + } |
| 129 | + LayeredConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext); |
| 130 | + |
| 131 | + this.socketFactoryRegistry = |
| 132 | + RegistryBuilder.<ConnectionSocketFactory>create() |
| 133 | + .register("http", PlainConnectionSocketFactory.getSocketFactory()) |
| 134 | + .register("https", socketFactory) |
| 135 | + .build(); |
| 136 | + } |
| 137 | + |
| 138 | + public Registry<ConnectionSocketFactory> getSocketFactoryRegistry() { |
| 139 | + return this.socketFactoryRegistry; |
| 140 | + } |
| 141 | + |
| 142 | + public boolean isMtls() { |
| 143 | + return this.isMtls; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private GoogleApache5HttpTransport() {} |
| 148 | +} |
0 commit comments