Skip to content

Commit

Permalink
feat: add support for proxy basic authentication
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa committed Oct 30, 2023
1 parent e039ca5 commit 118510b
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/it/gradle-7/invoker.properties
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions src/it/gradle-8/invoker.properties
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src/it/https-auth-proxy/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals.1=verify -U -Dhttps.proxyHost=localhost -Dhttps.proxyPort=3128 -Dhttps.proxyUser=foo -Dhttps.proxyPassword=bar -Djdk.http.auth.tunneling.disabledSchemes=
37 changes: 37 additions & 0 deletions src/it/https-auth-proxy/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.marcnuri.plugins.it</groupId>
<artifactId>no-action</artifactId>
<version>0.1-SNAPSHOT</version>
<name>Maven Integration Test :: Gradle API :: HTTPS Auth Proxy</name>

<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${version.groovy}</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gradle</groupId>
<artifactId>gradle-all</artifactId>
<version>${version.gradle.8}</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.marcnuri.plugins</groupId>
<artifactId>gradle-api-maven-plugin</artifactId>
<version>@project.version@</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</project>
2 changes: 2 additions & 0 deletions src/it/https-auth-proxy/setup.groovy
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 9 additions & 0 deletions src/it/https-auth-proxy/verify.groovy
Original file line number Diff line number Diff line change
@@ -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')
41 changes: 37 additions & 4 deletions src/main/java/com/marcnuri/plugins/gradle/api/GradleApi.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -53,11 +56,41 @@ 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
) {
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);
}
}
Expand Down

0 comments on commit 118510b

Please sign in to comment.