Skip to content

Commit

Permalink
Support no-proxy for Artifacts deployment (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
yahavi authored Aug 23, 2024
1 parent c72469e commit c95669e
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 9 deletions.
12 changes: 12 additions & 0 deletions src/functionalTest/resources/gradle-example-publish/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,16 @@ artifactory {
publishIvy = true // Publish generated Ivy descriptor files to Artifactory (true by default)
}
}

// Configure a proxy with invalid settings and bypass it for a specific host using noProxy
proxy {
// Set proxy host to a non-functional proxy
host = "bad-proxy"

// Set proxy port to an arbitrary value
port = 666

// Bypass the proxy for the Artifactory host
noProxy = new URL(System.getenv("BITESTS_PLATFORM_URL")).getHost()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.jfrog.build.client.ArtifactoryUploadResponse;
import org.jfrog.build.extractor.Proxy;
import org.jfrog.build.extractor.ProxySelector;
import org.jfrog.build.extractor.ci.BuildInfo;
import org.jfrog.build.extractor.clientConfiguration.ArtifactoryClientConfiguration;
import org.jfrog.build.extractor.clientConfiguration.IncludeExcludePatterns;
Expand Down Expand Up @@ -116,21 +118,26 @@ private static void configureArtifactoryManager(ArtifactoryClientConfiguration a
configureProxy(accRoot.proxy, artifactoryManager);
}

private static void configureProxy(ArtifactoryClientConfiguration.ProxyHandler proxy, ArtifactoryManager artifactoryManager) {
static void configureProxy(ArtifactoryClientConfiguration.ProxyHandler proxy, ArtifactoryManager artifactoryManager) {
// If no proxy is configured, return
String proxyHost = proxy.getHost();
Integer proxyPort = proxy.getPort();
if (StringUtils.isBlank(proxyHost) || proxyPort == null) {
return;
}
log.debug("Found proxy host '{}' in port '{}'", proxyHost, proxyPort);
String proxyUserName = proxy.getUsername();
if (StringUtils.isNotBlank(proxyUserName)) {
log.debug("Found proxy user name '{}'", proxyUserName);
artifactoryManager.setProxyConfiguration(proxyHost, proxy.getPort(), proxyUserName, proxy.getPassword());
} else {
log.debug("No proxy user name and password found, using anonymous proxy");
artifactoryManager.setProxyConfiguration(proxyHost, proxy.getPort());

// If the Artifactory URL is in the no proxy list, return
ProxySelector proxySelector = new ProxySelector(proxyHost, proxyPort, proxy.getUsername(), proxy.getPassword(),
proxyHost, proxyPort, proxy.getUsername(), proxy.getPassword(), proxy.getNoProxy()
);
Proxy proxyHandler = proxySelector.getProxy(artifactoryManager.getUrl());
if (proxyHandler == null) {
return;
}

// Configure the proxy
log.debug("Found proxy host '{}' in port '{}'", proxyHost, proxyPort);
artifactoryManager.setProxyConfiguration(proxyHandler.getHost(), proxyHandler.getPort(), proxyHandler.getUsername(), proxyHandler.getPassword());
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.jfrog.gradle.plugin.artifactory.utils;

import org.jfrog.build.api.util.NullLog;
import org.jfrog.build.client.ProxyConfiguration;
import org.jfrog.build.extractor.clientConfiguration.ArtifactoryClientConfiguration;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.ArtifactoryManager;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import static org.jfrog.gradle.plugin.artifactory.utils.DeployUtils.configureProxy;
import static org.testng.Assert.*;

public class DeployUtilsTest {
private static final String artifactoryUrl = "https://acme.jfrog.io/artifactory";
private static final String proxyUrl = "https://acme-proxy.jfrog.io";
private static final int proxyPort = 1234;

@DataProvider
public Object[][] proxies() {
return new Object[][]{
{"", null, "", false},
{proxyUrl, null, "", false},
{"", proxyPort, "", false},
{proxyUrl, proxyPort, "", true},
{proxyUrl, proxyPort, "acme.jfrog.io", false},
{proxyUrl, proxyPort, "acme2.jfrog.io", true},
};
}

@Test(dataProvider = "proxies")
void testConfigureProxy(String proxyUrl, Integer proxyPort, String noProxy, boolean expectedProxy) {
// Configure proxy with the given parameters
ArtifactoryClientConfiguration.ProxyHandler proxyHandler = createProxyHandler(proxyUrl, proxyPort, noProxy);
ArtifactoryManager artifactoryManager = new ArtifactoryManager(artifactoryUrl, new NullLog());
configureProxy(proxyHandler, artifactoryManager);

// Verify the proxy configuration
ProxyConfiguration actualProxyConfiguration = artifactoryManager.getProxyConfiguration();
if (expectedProxy) {
// Verify the proxy configuration
assertNotNull(actualProxyConfiguration);
assertEquals(actualProxyConfiguration.host, proxyUrl);
assertEquals(actualProxyConfiguration.port, proxyPort);
} else {
// Verify no proxy configuration if no proxy is set or the host is in the no proxy list
assertNull(actualProxyConfiguration);
}
}

/**
* Create a proxy handler with the given parameters.
*
* @param proxyUrl The proxy URL
* @param proxyPort The proxy port
* @param noProxy The no proxy
* @return The created proxy handler.
*/
private ArtifactoryClientConfiguration.ProxyHandler createProxyHandler(String proxyUrl, Integer proxyPort, String noProxy) {
ArtifactoryClientConfiguration clientConfiguration = new ArtifactoryClientConfiguration(new NullLog());
ArtifactoryClientConfiguration.ProxyHandler proxyHandler = clientConfiguration.proxy;
proxyHandler.setHost(proxyUrl);
proxyHandler.setPort(proxyPort);
proxyHandler.setNoProxy(noProxy);
return proxyHandler;
}
}

0 comments on commit c95669e

Please sign in to comment.