diff --git a/src/it/gradle-7/invoker.properties b/src/it/gradle-7/invoker.properties index a2231b0..793a71b 100644 --- a/src/it/gradle-7/invoker.properties +++ b/src/it/gradle-7/invoker.properties @@ -1,2 +1,4 @@ +# Execute first to prevent cached versions of the plugin downloaded by other tests +invoker.ordinal=999 invoker.goals.1=verify #invoker.updateSnapshots=true diff --git a/src/it/gradle-8/invoker.properties b/src/it/gradle-8/invoker.properties index a2231b0..793a71b 100644 --- a/src/it/gradle-8/invoker.properties +++ b/src/it/gradle-8/invoker.properties @@ -1,2 +1,4 @@ +# Execute first to prevent cached versions of the plugin downloaded by other tests +invoker.ordinal=999 invoker.goals.1=verify #invoker.updateSnapshots=true diff --git a/src/it/https-auth-proxy/invoker.properties b/src/it/https-auth-proxy/invoker.properties new file mode 100644 index 0000000..9c4dea7 --- /dev/null +++ b/src/it/https-auth-proxy/invoker.properties @@ -0,0 +1 @@ +invoker.goals.1=verify -U -Dhttps.proxyHost=localhost -Dhttps.proxyPort=3128 -Dhttps.proxyUser=foo -Dhttps.proxyPassword=bar diff --git a/src/it/https-auth-proxy/pom.xml b/src/it/https-auth-proxy/pom.xml new file mode 100644 index 0000000..1bd4ac3 --- /dev/null +++ b/src/it/https-auth-proxy/pom.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + com.marcnuri.plugins.it + no-action + 0.1-SNAPSHOT + Maven Integration Test :: Gradle API :: HTTPS Auth Proxy + + + + org.codehaus.groovy + groovy-all + ${version.groovy} + pom + provided + + + org.gradle + gradle-all + ${version.gradle.8} + provided + + + + + + + com.marcnuri.plugins + gradle-api-maven-plugin + @project.version@ + true + + + + diff --git a/src/it/https-auth-proxy/setup.groovy b/src/it/https-auth-proxy/setup.groovy new file mode 100644 index 0000000..57f1c34 --- /dev/null +++ b/src/it/https-auth-proxy/setup.groovy @@ -0,0 +1,2 @@ +def dockerRun = 'docker run --rm -d --name test-auth-proxy -e SQUID_USERNAME=foo -e SQUID_PASSWORD=bar -p 0.0.0.0:3128:3128 robhaswell/squid-authenticated@sha256:6a99946c96d063981b329c22efc2b9ad1ac4e90d16ddcbb9d0b2d6773a7bea2b'.execute() +dockerRun.waitForOrKill(30_000) diff --git a/src/it/https-auth-proxy/verify.groovy b/src/it/https-auth-proxy/verify.groovy new file mode 100644 index 0000000..651057e --- /dev/null +++ b/src/it/https-auth-proxy/verify.groovy @@ -0,0 +1,9 @@ +def logOut = new StringBuilder(), logErr = new StringBuilder() +def dockerLogs = 'docker exec test-auth-proxy cat /var/log/squid3/access.log'.execute() +dockerLogs.consumeProcessOutput(logOut, logErr) +dockerLogs.waitForOrKill(10_000) +def dockerStop = 'docker stop test-auth-proxy'.execute() +dockerStop.waitForOrKill(30_000) +assert logOut.toString().contains('CONNECT services.gradle.org:443') +def buildLog = new File(basedir, 'build.log').text +assert buildLog.contains('Gradle 8.2.1 download complete') diff --git a/src/main/java/com/marcnuri/plugins/gradle/api/GradleApi.java b/src/main/java/com/marcnuri/plugins/gradle/api/GradleApi.java index f7ee420..d6a6e6d 100644 --- a/src/main/java/com/marcnuri/plugins/gradle/api/GradleApi.java +++ b/src/main/java/com/marcnuri/plugins/gradle/api/GradleApi.java @@ -1,9 +1,12 @@ package com.marcnuri.plugins.gradle.api; -import org.apache.maven.plugin.logging.Log; - import java.io.IOException; import java.io.InputStream; +import java.net.Authenticator; +import java.net.PasswordAuthentication; +import java.net.Proxy; +import java.net.ProxySelector; +import java.net.URISyntaxException; import java.net.URL; import java.nio.channels.Channels; import java.nio.channels.FileChannel; @@ -53,11 +56,42 @@ private void download() { log.info("Downloading Gradle " + gradleVersion + "..."); try { final URL remoteBin = new URL(GRADLE_DISTRIBUTION_BASE_URL + gradleBinZip.toFile().getName()); + // Proxy + final Proxy proxy = ProxySelector.getDefault().select(remoteBin.toURI()) + .stream().findAny() + .map(p -> { + if (System.getProperties().get(remoteBin.getProtocol() + ".proxyUser") != null && + System.getProperty(remoteBin.getProtocol() + ".proxyPassword") != null + ) { + System.setProperty("jdk.http.auth.tunneling.disabledSchemes", ""); + Authenticator.setDefault(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication( + System.getProperty(remoteBin.getProtocol() + ".proxyUser"), + System.getProperty(remoteBin.getProtocol() + ".proxyPassword").toCharArray() + ); + } + }); + log.info("Using proxy with BASIC authentication"); + } else{ + log.info("Using proxy"); + } + return p; + }) + .orElse(null); + // Connection + final InputStream stream; + if (proxy == null) { + stream = remoteBin.openStream(); + } else { + stream = remoteBin.openConnection(proxy).getInputStream(); + } Files.createDirectories(resolveGroupDir()); - writeToFile(remoteBin.openStream(), gradleBinZip); + writeToFile(stream, gradleBinZip); writePom(GRADLE_ALL_ARTIFACT_ID, "pom"); log.info("Gradle " + gradleVersion + " download complete"); - } catch (IOException e) { + } catch (URISyntaxException | IOException e) { throw new IllegalStateException("Couldn't download Gradle " + gradleVersion, e); } }