Skip to content
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

feat: Support HTTP proxies with credentials. #2176

Merged
merged 4 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ object Dependencies {
//endregion ktor

const val LOGBACK = "ch.qos.logback:logback-classic:${Versions.LOGBACK}"
const val JBOSS_LOGGING = "org.jboss.logging:commons-logging-jboss-logging:${Versions.JBOSS_LOGGING}"

const val WOODSTOX = "com.fasterxml.woodstox:woodstox-core:${Versions.WOODSTOX}"

Expand Down
5 changes: 5 additions & 0 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ object Versions {
// https://commons.apache.org/proper/commons-text/
const val COMMON_TEXT = "1.9"

// https://github.com/jboss-logging/commons-logging-jboss-logging
// JBoss logging dep is used by the Google Auth library when configuring a proxy.
// https://github.com/googleapis/google-auth-library-java#configuring-a-proxy
const val JBOSS_LOGGING = "1.0.0.Final"

// https://github.com/fusesource/jansi/releases
const val JANSI = "2.4.0"

Expand Down
6 changes: 6 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,13 @@ and flank's example [gradle-export-api](https://github.com/Flank/flank/tree/mast
6) > How can I find project id?

Please check the [firebase documentation](https://firebase.google.com/docs/projects/learn-more?hl=en#find_the_project_id) about finding the project id

7) > How do I run Flank with a proxy?

`java -Dhttps.proxyHost=localhost -Dhttps.proxyPort=8080 -Dhttps.proxyUser=user -Dhttps.proxyPassword=pass -jar ./test_runner/build/libs/flank.jar firebase test android run`

See [google-auth-library-java](https://github.com/googleapis/google-auth-library-java#configuring-a-proxy) for details.


# Resources

Expand Down
1 change: 1 addition & 0 deletions test_runner/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ dependencies {
implementation(Dependencies.KOTLIN_REFLECT)

implementation(Dependencies.LOGBACK)
implementation(Dependencies.JBOSS_LOGGING)

implementation(Dependencies.PICOCLI)
annotationProcessor(Dependencies.PICOCLI_CODEGEN)
Expand Down
40 changes: 39 additions & 1 deletion test_runner/src/main/kotlin/ftl/client/google/Credentials.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package ftl.client.google

import com.google.api.client.http.GoogleApiLogger
import com.google.api.client.http.HttpRequestInitializer
import com.google.api.client.http.apache.v2.ApacheHttpTransport
import com.google.auth.http.HttpTransportFactory
import com.google.auth.oauth2.AccessToken
import com.google.auth.oauth2.GoogleCredentials
import com.google.auth.oauth2.ServiceAccountCredentials
Expand All @@ -10,6 +12,12 @@ import flank.common.isWindows
import ftl.config.FtlConstants
import ftl.http.HttpTimeoutIncrease
import ftl.run.exception.FlankGeneralError
import org.apache.http.HttpHost
import org.apache.http.auth.AuthScope
import org.apache.http.auth.UsernamePasswordCredentials
import org.apache.http.impl.client.BasicCredentialsProvider
import org.apache.http.impl.client.ProxyAuthenticationStrategy
import org.apache.http.impl.conn.DefaultProxyRoutePlanner
import java.io.IOException
import java.util.Date

Expand All @@ -19,7 +27,12 @@ val credential: GoogleCredentials by lazy {
else ->
runCatching {
GoogleApiLogger.silenceComputeEngine()
ServiceAccountCredentials.getApplicationDefault()
val httpTransportFactory = getHttpTransportFactory()
if (httpTransportFactory != null) {
ServiceAccountCredentials.getApplicationDefault(httpTransportFactory)
} else {
ServiceAccountCredentials.getApplicationDefault()
}
}.getOrElse {
when {
isWindows -> loadCredentialsWindows()
Expand All @@ -29,6 +42,31 @@ val credential: GoogleCredentials by lazy {
}
}

private fun getHttpTransportFactory(): HttpTransportFactory? {
val proxyHost = System.getProperty("http.proxyHost") ?: return null
val proxyPort = System.getProperty("http.proxyPort")?.toInt() ?: return null
val proxyPassword = System.getProperty("http.proxyPassword") ?: return null
val proxyUsername = System.getProperty("http.proxyUser") ?: return null

val proxyHostDetails = HttpHost(proxyHost, proxyPort)
val httpRoutePlanner = DefaultProxyRoutePlanner(proxyHostDetails)

val credentialsProvider = BasicCredentialsProvider()
credentialsProvider.setCredentials(
AuthScope(proxyHostDetails.getHostName(), proxyHostDetails.getPort()),
UsernamePasswordCredentials(proxyUsername, proxyPassword)
)

val httpClient = ApacheHttpTransport.newDefaultHttpClientBuilder()
.setRoutePlanner(httpRoutePlanner)
.setProxyAuthenticationStrategy(ProxyAuthenticationStrategy.INSTANCE)
.setDefaultCredentialsProvider(credentialsProvider)
.build()

val httpTransport = ApacheHttpTransport(httpClient)
return HttpTransportFactory { httpTransport }
}

private fun loadCredentialsWindows() = runCatching {
loadGoogleAccountCredentials()
}.getOrElse {
Expand Down