googleapis / google-api-java-client

Google APIs Client Library for Java
Apache License 2.0
1.34k stars 698 forks source link

Proxy support? #1247

Open bootstraponline opened 5 years ago

bootstraponline commented 5 years ago

I'm using the Firebase Test Lab Java client which uses google-api-java-client under the hood. A bug report came in that the API calls aren't respecting proxies. When using the gcloud CLI, proxy support is automatically detected with no end user configuration. Is it possible to achive something similar with google-api-java-client?

Environment details

Steps to reproduce

import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport
import com.google.api.client.googleapis.util.Utils
import com.google.api.services.toolresults.ToolResults
import com.google.auth.http.HttpCredentialsAdapter
import com.google.auth.oauth2.UserCredentials
import java.io.FileInputStream
import java.io.ObjectInputStream
import java.nio.file.Paths

fun main() {
    val httpCredential = HttpCredentialsAdapter(ServiceAccountCredentials.getApplicationDefault().createScoped(listOf("https://www.googleapis.com/auth/cloud-platform")))
    val httpTransport = GoogleNetHttpTransport.newTrustedTransport()
    val jsonFactory = Utils.getDefaultJsonFactory()

    val service = ToolResults.Builder(httpTransport, jsonFactory, httpCredential).setApplicationName("test").build()
    service.Projects().initializeSettings("1234").execute()
}

Stacktrace

Exception in thread "main" java.net.ConnectException: Connection refused (Connection refused)
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:666)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)
    at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:264)
    at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:367)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1156)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1334)
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1309)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:259)
    at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:113)
    at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:84)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1040)
    at com.google.auth.oauth2.UserCredentials.refreshAccessToken(UserCredentials.java:227)
    at com.google.auth.oauth2.OAuth2Credentials.refresh(OAuth2Credentials.java:181)
    at com.google.auth.oauth2.OAuth2Credentials.getRequestMetadata(OAuth2Credentials.java:167)
    at com.google.auth.http.HttpCredentialsAdapter.initialize(HttpCredentialsAdapter.java:96)
    at com.google.api.client.http.HttpRequestFactory.buildRequest(HttpRequestFactory.java:93)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.buildHttpRequest(AbstractGoogleClientRequest.java:397)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:515)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:448)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:565)
    at DebugKt.main(Debug.kt:22)
    at DebugKt.main(Debug.kt)

External references such as API reference guides used

leinardi commented 5 years ago

I'm currently evaluating for the company I work for if it would make sense to run our CI's Espresso tests using Firebase Test Lab instead of using the standard Android Emulator on our local build servers. This issue is blocking us because FTL doesn't provide any sharding feature and Flank is the best solution out there but cannot be used because of the missing proxy support of google-api-client.

HarryEuro commented 4 years ago

I'm also having similar issues because i'm behind a corporate proxy with a self signed certificate. The proxy url and port can be specified with VM options (-Dhttp.proxyHost=... -Dhttp.proxyPort=...).

The big problem is the self signed certificate. The google-api-java-client uses it's own Java KeyStore (src/main/resources/com/google/api/client/googleapis/google.jks) and not the official from Java ($JAVA_HOME/jre/lib/security/cacerts). So our self signed certificate cannot be verified and the request will fail.

Writing a custom pluggable transport is also not possible since i'm not using the google-api-java-client directly. I'm using Apache Beam v2.16.0 which uses google-api-java-client 1.27.0 to stage files. The workaround that i come up with was to add our self signed certificate to the google.jks file located in google-api-client-1.27.0.jar which is of course not a good solution. But then everything worked behind our proxy. Also the Magnitude Simba drivers for BigQuery are not working for the same reason behind our proxy. They are needed so BigQuery can be used with JetBrains DataGrip.

The same problems are also described in https://github.com/googleapis/google-api-java-client/issues/1114 but with no solution. So is anyone working on this problem?