Skip to content

Commit

Permalink
Support HTTP proxies with credentials.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdm-square committed Nov 4, 2021
1 parent 04eb0fc commit 3ab3210
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
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
3 changes: 3 additions & 0 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ object Versions {
// https://commons.apache.org/proper/commons-text/
const val COMMON_TEXT = "1.9"

// https://github.com/jboss-logging/commons-logging-jboss-logging
const val JBOSS_LOGGING = "1.0.0.Final"

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

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

0 comments on commit 3ab3210

Please sign in to comment.