Skip to content

feat: Provide HttpTransport support for google certificates with apache HTTP client v5 (#2497) #2503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add checkstyle to apache 5 module + fixed lint issues
  • Loading branch information
ldetmer committed Aug 22, 2024
commit 9cdab695981e87ec697df6a8139cb8b69acd0877
26 changes: 21 additions & 5 deletions google-api-client-apache-v5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
<configuration>
<instructions>
<Bundle-DocURL>https://developers.google.com/api-client-library/java/</Bundle-DocURL>
<Bundle-Description>Google HTTP transport wrapper for the Apache Http Client.</Bundle-Description>
<Bundle-Description>Google HTTP transport wrapper for the Apache 5 Http Client.</Bundle-Description>
<Bundle-SymbolicName>com.google.api.client.googleapis.apache</Bundle-SymbolicName>
</instructions>
</configuration>
Expand All @@ -103,10 +103,26 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- These tests require an Env Var to be set. Use -PenvVarTest to ONLY run these tests -->
<test>!AbstractGoogleClientTest#testGoogleClientBuilder_noCustomUniverseDomain_universeDomainEnvVar+testGoogleClientBuilder_customUniverseDomain_universeDomainEnvVar</test>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<executions>
<execution>
<id>validate-google-style</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
<configLocation>google_checks.xml</configLocation>
<suppressionsLocation>src/checkstyle/checkstyle-suppressions.xml</suppressionsLocation>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
<failOnViolation>true</failOnViolation>
<violationSeverity>warning</violationSeverity>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
"-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
"https://checkstyle.org/dtds/suppressions_1_2.dtd">
<suppressions>
<!-- Test checkstyle suppressions -->
<suppress checks="MissingJavadocType" files="/src/test/"/>
<suppress checks="AbbreviationAsWordInName" files="/src/test/"/>
<suppress checks="VariableDeclarationUsageDistance" files="/src/test/"/>
</suppressions>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Google LLC
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand All @@ -19,32 +19,31 @@
import com.google.api.client.googleapis.mtls.MtlsUtils;
import com.google.api.client.googleapis.util.Utils;
import com.google.api.client.http.apache.v5.Apache5HttpTransport;
import com.google.common.annotations.Beta;
import com.google.api.client.util.SslUtils;
import com.google.common.annotations.Beta;
import com.google.common.annotations.VisibleForTesting;
import java.io.IOException;
import java.net.ProxySelector;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLContext;
import org.apache.hc.client5.http.config.ConnectionConfig;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.socket.LayeredConnectionSocketFactory;
import org.apache.hc.client5.http.socket.PlainConnectionSocketFactory;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.client5.http.impl.routing.SystemDefaultRoutePlanner;
import org.apache.hc.client5.http.socket.ConnectionSocketFactory;
import org.apache.hc.client5.http.socket.LayeredConnectionSocketFactory;
import org.apache.hc.client5.http.socket.PlainConnectionSocketFactory;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.apache.hc.core5.http.config.Registry;
import org.apache.hc.core5.http.config.RegistryBuilder;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.net.ProxySelector;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.util.concurrent.TimeUnit;

/**
* Utilities for Google APIs based on {@link ApacheHttpTransport}.
*
* @since 1.31
* @since 2.6.1
*/
public final class GoogleApache5HttpTransport {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Google LLC
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Google LLC
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand All @@ -14,16 +14,15 @@

package com.google.api.client.googleapis.apache.v5;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import com.google.api.client.googleapis.mtls.MtlsProvider;
import com.google.api.client.googleapis.mtls.MtlsTransportBaseTest;
import com.google.api.client.http.HttpTransport;
import org.junit.Test;


import java.io.IOException;
import java.security.GeneralSecurityException;

import static org.junit.Assert.*;
import org.junit.Test;

public class GoogleApache5HttpTransportTest extends MtlsTransportBaseTest {
@Override
Expand All @@ -34,8 +33,10 @@ protected HttpTransport buildTrustedTransport(MtlsProvider mtlsProvider)

@Test
public void socketFactoryRegistryHandlerTest() throws GeneralSecurityException, IOException {
MtlsProvider mtlsProvider = new TestMtlsProvider(true, createTestMtlsKeyStore(), "", false);
GoogleApache5HttpTransport.SocketFactoryRegistryHandler handler = new GoogleApache5HttpTransport.SocketFactoryRegistryHandler(mtlsProvider);
MtlsProvider mtlsProvider = new TestMtlsProvider(true, createTestMtlsKeyStore(),
"", false);
GoogleApache5HttpTransport.SocketFactoryRegistryHandler handler =
new GoogleApache5HttpTransport.SocketFactoryRegistryHandler(mtlsProvider);
assertNotNull(handler.getSocketFactoryRegistry().lookup("http"));
assertNotNull(handler.getSocketFactoryRegistry().lookup("https"));
assertTrue(handler.isMtls());
Expand Down
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The format checker will probably complain for a missing license header in this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you're looking at old version of the file?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, indeed! Please disregard.

Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.api.client.googleapis.apache.v5;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

import com.google.api.client.http.apache.v5.Apache5HttpTransport;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.junit.Test;
import javax.net.ssl.SSLHandshakeException;
import java.io.IOException;
import java.security.GeneralSecurityException;
import javax.net.ssl.SSLHandshakeException;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
import org.junit.Test;
import org.mockserver.integration.ClientAndServer;
import org.mockserver.socket.PortFactory;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;

public class ITGoogleApache5HttpTransportTest {

@Test
Expand All @@ -25,19 +41,28 @@ public void testHttpRequestFailsWhenMakingRequestToSiteWithoutGoogleCerts()
ClientAndServer mockServer = ClientAndServer.startClientAndServer(port);
Apache5HttpTransport apache5HttpTransport = GoogleApache5HttpTransport.newTrustedTransport();
HttpGet httpGet = new HttpGet("https://localhost:" + port + "/");
Exception exception = null;
try {
apache5HttpTransport
.getHttpClient()
.execute(
httpGet,
new HttpClientResponseHandler<Void>() {
@Override
public Void handleResponse(ClassicHttpResponse response) {
fail(
"Should not have been able to complete SSL request on non google site."
);
return null;
}
});
fail("Expected SSLHandshakeException was not thrown");
} catch (SSLHandshakeException e) {
exception = e;
}

assertThrows(
SSLHandshakeException.class,
() ->
apache5HttpTransport
.getHttpClient()
.execute(
httpGet,
response -> {
fail(
"Should not have been able to complete SSL request to site without google certificates.");
return null;
}));
assertNotNull(exception);
assertEquals(exception.getClass(), SSLHandshakeException.class);

mockServer.stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
* Provider interface for mutual TLS. It is used in {@link
* GoogleApacheHttpTransport#newTrustedTransport(MtlsProvider)}, {@link
* GoogleApache5HttpTransport#newTrustedTransport(MtlsProvider)} and {@link
* GoogleNetHttpTransport#newTrustedTransport(MtlsProvider)}
* to configure the mutual TLS in the transport.
* GoogleNetHttpTransport#newTrustedTransport(MtlsProvider)} to configure the mutual TLS in the
* transport.
*
* @since 1.31
*/
Expand Down
Loading