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

Add https_proxy and no_proxy configurations to the Maven repository resolution #725

Merged
merged 13 commits into from
May 17, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public JFrogHttpClient(String artifactoryUrl, String accessToken, Log log) {
* @param port Proxy port
*/
public void setProxyConfiguration(String host, int port) {
setProxyConfiguration(host, port, null, null);
setProxyConfiguration(host, port, null, null,false,null);
}

/**
Expand All @@ -64,13 +64,17 @@ public void setProxyConfiguration(String host, int port) {
* @param port Proxy port
* @param username Username to authenticate with the proxy
* @param password Password to authenticate with the proxy
* @param https Proxy uses https protocol
* @param noProxyDomain No Proxy domains
*/
public void setProxyConfiguration(String host, int port, String username, String password) {
public void setProxyConfiguration(String host, int port, String username, String password, Boolean https, String noProxyDomain) {
Or-Geva marked this conversation as resolved.
Show resolved Hide resolved
ProxyConfiguration proxyConfiguration = new ProxyConfiguration();
proxyConfiguration.host = host;
proxyConfiguration.port = port;
proxyConfiguration.username = username;
proxyConfiguration.password = password;
proxyConfiguration.https = https;
proxyConfiguration.noProxyDomain = noProxyDomain;
clientBuilder.setProxyConfiguration(proxyConfiguration);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public PreemptiveHttpClientBuilder setTimeout(int timeout) {
public PreemptiveHttpClientBuilder setProxyConfiguration(ProxyConfiguration proxyConfiguration) {
this.proxyConfiguration = proxyConfiguration;
if (proxyConfiguration != null) {
this.proxy = new HttpHost(proxyConfiguration.host, proxyConfiguration.port);
this.proxy = new HttpHost(proxyConfiguration.host, proxyConfiguration.port, proxyConfiguration.https ? "https" : "http");
}
return this;
}
Or-Geva marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public class ProxyConfiguration implements Serializable {
public int port;
public String username;
public String password;
public String noProxyDomain;
public boolean https;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ public void testCredentialsMaintainedWithProxy() {
int proxyPort = 8000;
String proxyUser = "proxy-user";
String proxyPassword = "proxy-password";
boolean https = false;
String noProxyDomain = "noProxyDomain";

PreemptiveHttpClientBuilder clientBuilder = new PreemptiveHttpClientBuilder()
.setConnectionRetries(3)
.setInsecureTls(false)
.setTimeout(300)
.setLog(new NullLog())
.setProxyConfiguration(createProxyConfiguration(proxyHost, proxyPort, proxyUser, proxyPassword))
.setProxyConfiguration(createProxyConfiguration(proxyHost, proxyPort, proxyUser, proxyPassword, https, noProxyDomain))
.setUserName(rtUser)
.setPassword(rtPassword);
PreemptiveHttpClient deployClient = clientBuilder.build();
Expand All @@ -42,12 +44,14 @@ public void testCredentialsMaintainedWithProxy() {
assertEquals(portCredentials, new UsernamePasswordCredentials(proxyUser, proxyPassword));
}

private ProxyConfiguration createProxyConfiguration(String host, int port, String proxyUser, String proxyPassword) {
private ProxyConfiguration createProxyConfiguration(String host, int port, String proxyUser, String proxyPassword, boolean https, String noProxyDomain) {
ProxyConfiguration proxyConfiguration = new ProxyConfiguration();
proxyConfiguration.host = host;
proxyConfiguration.port = port;
proxyConfiguration.username = proxyUser;
proxyConfiguration.password = proxyPassword;
proxyConfiguration.https = https;
proxyConfiguration.noProxyDomain = noProxyDomain;
return proxyConfiguration;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,10 @@ private void configureProxy(ArtifactoryClientConfiguration clientConf, Artifacto
String proxyUserName = proxy.getUsername();
if (StringUtils.isNotBlank(proxyUserName)) {
log.debug("Found proxy user name '{}'", proxyUserName);
artifactoryManager.setProxyConfiguration(proxyHost, proxy.getPort(), proxyUserName, proxy.getPassword());
artifactoryManager.setProxyConfiguration(proxyHost, proxy.getPort(), proxyUserName, proxy.getPassword(), proxy.isHttps(), proxy.getNoProxyDomain());
} else {
log.debug("No proxy user name and password found, using anonymous proxy");
artifactoryManager.setProxyConfiguration(proxyHost, proxy.getPort());
artifactoryManager.setProxyConfiguration(proxyHost, proxy.getPort(), proxy.isHttps(), proxy.getNoProxyDomain());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,10 @@ protected void configureProxy(ArtifactoryClientConfiguration clientConf, Artifac
String proxyUserName = proxy.getUsername();
if (StringUtils.isNotBlank(proxyUserName)) {
buildInfoLog.debug("Found proxy user name '" + proxyUserName + "'");
artifactoryManager.setProxyConfiguration(proxyHost, proxy.getPort(), proxyUserName, proxy.getPassword());
artifactoryManager.setProxyConfiguration(proxyHost, proxy.getPort(), proxyUserName, proxy.getPassword(), proxy.isHttps(), proxy.getNoProxyDomain());
} else {
buildInfoLog.debug("No proxy user name and password found, using anonymous proxy");
artifactoryManager.setProxyConfiguration(proxyHost, proxy.getPort());
artifactoryManager.setProxyConfiguration(proxyHost, proxy.getPort(), proxy.isHttps(), proxy.getNoProxyDomain());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ private void resolveProxy(ArtifactoryClientConfiguration.ProxyHandler proxyConf,
if (StringUtils.isNotBlank(proxyUsername)) {
logResolvedProperty(ClientConfigurationFields.USERNAME, proxyUsername);
artifactoryManager.setProxyConfiguration(proxyHost, proxyConf.getPort(), proxyUsername,
proxyConf.getPassword());
proxyConf.getPassword(), proxyConf.isHttps(), proxyConf.getNoProxyDomain());
} else {
artifactoryManager.setProxyConfiguration(proxyHost, proxyConf.getPort());
artifactoryManager.setProxyConfiguration(proxyHost, proxyConf.getPort(), proxyConf.isHttps(), proxyConf.getNoProxyDomain());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
package org.jfrog.build.extractor.maven.resolver;

import org.apache.commons.lang3.StringUtils;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
import org.apache.maven.artifact.repository.MavenArtifactRepository;
import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.Logger;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.repository.Authentication;
import org.eclipse.aether.repository.Proxy;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.repository.RepositoryPolicy;
import org.eclipse.aether.util.repository.AuthenticationBuilder;

import javax.inject.Named;
import java.util.ArrayList;
Expand All @@ -30,8 +22,8 @@ public class ArtifactoryEclipseResolversHelper {
@Requirement
private Logger logger;

private List<ArtifactRepository> resolutionPluginRepositories = null;
private List<RemoteRepository> resolutionRepositories = null;
private final List<ArtifactRepository> resolutionPluginRepositories = new ArrayList<>();
private final List<RemoteRepository> resolutionRepositories = new ArrayList<>();
private RemoteRepository releaseRepository = null;
private RemoteRepository snapshotRepository = null;

Expand All @@ -48,139 +40,56 @@ void initResolutionRepositories(RepositorySystemSession session) {
* @return Snapshot and release resolution repositories
*/
List<RemoteRepository> getResolutionRepositories(RepositorySystemSession session) {
if (resolutionRepositories == null) {
List<RemoteRepository> tempRepositories = new ArrayList<>();
if (resolutionRepositories.isEmpty()) {
initResolutionHelper(session);

String releaseRepoUrl = resolutionHelper.getRepoReleaseUrl();
String snapshotRepoUrl = resolutionHelper.getRepoSnapshotUrl();

Authentication authentication = null;
if (StringUtils.isNotBlank(resolutionHelper.getRepoUsername())) {
authentication = new AuthenticationBuilder().addString("username", resolutionHelper.getRepoUsername())
.addSecret("password", resolutionHelper.getRepoPassword()).build();
ArtifactoryResolution artifactoryResolution = new ArtifactoryResolution(resolutionHelper.getRepoReleaseUrl(), resolutionHelper.getRepoSnapshotUrl(), resolutionHelper.getRepoUsername(), resolutionHelper.getRepoPassword(),
resolutionHelper.getProxyHost(), resolutionHelper.getProxyUsername(), resolutionHelper.getProxyPassword(), resolutionHelper.getProxyPort(), resolutionHelper.isHttps(),
resolutionHelper.getNoProxyDomain(), logger
);
snapshotRepository = artifactoryResolution.createSnapshotRepository();
if (snapshotRepository != null) {
resolutionRepositories.add(snapshotRepository);
}
Proxy proxy = null;
if (StringUtils.isNotBlank(resolutionHelper.getProxyHost())) {
Authentication auth = new AuthenticationBuilder()
.addString("username", resolutionHelper.getProxyUsername()).addSecret("password", resolutionHelper.getProxyPassword()).build();
proxy = new Proxy(null, resolutionHelper.getProxyHost(), resolutionHelper.getProxyPort(), auth);
}

if (StringUtils.isNotBlank(snapshotRepoUrl)) {
logger.debug("[buildinfo] Enforcing snapshot repository for resolution: " + snapshotRepoUrl);
RepositoryPolicy releasePolicy = new RepositoryPolicy(false, RepositoryPolicy.UPDATE_POLICY_DAILY, RepositoryPolicy.CHECKSUM_POLICY_WARN);
RepositoryPolicy snapshotPolicy = new RepositoryPolicy(true, RepositoryPolicy.UPDATE_POLICY_DAILY, RepositoryPolicy.CHECKSUM_POLICY_WARN);
RemoteRepository.Builder builder = new RemoteRepository.Builder("artifactory-snapshot", "default", snapshotRepoUrl);
builder.setReleasePolicy(releasePolicy);
builder.setSnapshotPolicy(snapshotPolicy);
if (authentication != null) {
logger.debug("[buildinfo] Enforcing repository authentication: " + authentication + " for snapshot resolution repository");
builder.setAuthentication(authentication);
}
if (proxy != null) {
logger.debug("[buildinfo] Enforcing proxy: " + proxy + " for snapshot resolution repository");
builder.setProxy(proxy);
}
snapshotRepository = builder.build();
tempRepositories.add(snapshotRepository);
}

if (StringUtils.isNotBlank(releaseRepoUrl)) {
logger.debug("[buildinfo] Enforcing release repository for resolution: " + releaseRepoUrl);
boolean snapshotPolicyEnabled = snapshotRepository == null;
String repositoryId = snapshotPolicyEnabled ? "artifactory-release-snapshot" : "artifactory-release";

RepositoryPolicy releasePolicy = new RepositoryPolicy(true, RepositoryPolicy.UPDATE_POLICY_DAILY, RepositoryPolicy.CHECKSUM_POLICY_WARN);
RepositoryPolicy snapshotPolicy = new RepositoryPolicy(snapshotPolicyEnabled, RepositoryPolicy.UPDATE_POLICY_DAILY, RepositoryPolicy.CHECKSUM_POLICY_WARN);
RemoteRepository.Builder builder = new RemoteRepository.Builder(repositoryId, "default", releaseRepoUrl);
builder.setReleasePolicy(releasePolicy);
builder.setSnapshotPolicy(snapshotPolicy);
if (authentication != null) {
logger.debug("[buildinfo] Enforcing repository authentication: " + authentication + " for release resolution repository");
builder.setAuthentication(authentication);
}
if (proxy != null) {
logger.debug("[buildinfo] Enforcing proxy: " + proxy + " for release resolution repository");
builder.setProxy(proxy);
}
releaseRepository = builder.build();
tempRepositories.add(releaseRepository);
releaseRepository = artifactoryResolution.createReleaseRepository();
if (releaseRepository != null) {
resolutionRepositories.add(releaseRepository);
}
resolutionRepositories = tempRepositories;
}
return resolutionRepositories;
}

private void initResolutionHelper(RepositorySystemSession session) {
if (resolutionHelper.isInitialized()) {
return;
}
Properties allMavenProps = new Properties();
allMavenProps.putAll(session.getSystemProperties());
allMavenProps.putAll(session.getUserProperties());
resolutionHelper.init(allMavenProps);
}

List<ArtifactRepository> getResolutionPluginRepositories(RepositorySystemSession session) {
if (resolutionPluginRepositories == null) {
List<ArtifactRepository> tempRepositories = new ArrayList<>();
if (resolutionPluginRepositories.isEmpty()) {
initResolutionHelper(session);

String releaseRepoUrl = resolutionHelper.getRepoReleaseUrl();
String snapshotRepoUrl = resolutionHelper.getRepoSnapshotUrl();

org.apache.maven.artifact.repository.Authentication authentication = null;
if (StringUtils.isNotBlank(resolutionHelper.getRepoUsername())) {
authentication = new org.apache.maven.artifact.repository.Authentication(resolutionHelper.getRepoUsername(), resolutionHelper.getRepoPassword());
ArtifactoryPluginResolution repositoryBuilder = new ArtifactoryPluginResolution(resolutionHelper.getRepoReleaseUrl(), resolutionHelper.getRepoSnapshotUrl(), resolutionHelper.getRepoUsername(), resolutionHelper.getRepoPassword(),
resolutionHelper.getProxyHost(), resolutionHelper.getProxyUsername(), resolutionHelper.getProxyPassword(), resolutionHelper.getProxyPort(), resolutionHelper.isHttps(),
resolutionHelper.getNoProxyDomain(), logger
);
ArtifactRepository snapshotRepository = repositoryBuilder.createSnapshotRepository();
if (snapshotRepository != null) {
resolutionPluginRepositories.add(snapshotRepository);
}
org.apache.maven.repository.Proxy proxy = null;
if (StringUtils.isNotBlank(resolutionHelper.getProxyHost())) {
proxy = new org.apache.maven.repository.Proxy();
proxy.setHost(resolutionHelper.getProxyHost());
proxy.setPort(resolutionHelper.getProxyPort());
proxy.setUserName(resolutionHelper.getProxyUsername());
proxy.setPassword(resolutionHelper.getProxyPassword());
ArtifactRepository releaseRepository = repositoryBuilder.createReleaseRepository();
if (releaseRepository != null) {
resolutionPluginRepositories.add(releaseRepository);
}

if (StringUtils.isNotBlank(snapshotRepoUrl)) {
logger.debug("[buildinfo] Enforcing snapshot repository for resolution: " + snapshotRepoUrl);
ArtifactRepositoryPolicy releasePolicy = new ArtifactRepositoryPolicy(false, ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY, ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN);
ArtifactRepositoryPolicy snapshotPolicy = new ArtifactRepositoryPolicy(true, ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY, ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN);
ArtifactRepository snapshotPluginRepository = new MavenArtifactRepository("artifactory-snapshot", snapshotRepoUrl, new DefaultRepositoryLayout(), snapshotPolicy, releasePolicy);
if (authentication != null) {
logger.debug("[buildinfo] Enforcing repository authentication: " + authentication + " for snapshot resolution repository");
snapshotPluginRepository.setAuthentication(authentication);
}

if (StringUtils.isNotBlank(resolutionHelper.getRepoUsername())) {
authentication = new org.apache.maven.artifact.repository.Authentication(resolutionHelper.getRepoUsername(), resolutionHelper.getRepoPassword());
}
if (proxy != null) {
logger.debug("[buildinfo] Enforcing proxy: " + proxy + " for snapshot resolution repository");
snapshotPluginRepository.setProxy(proxy);
}
tempRepositories.add(snapshotPluginRepository);
}

if (StringUtils.isNotBlank(releaseRepoUrl)) {
logger.debug("[buildinfo] Enforcing release repository for resolution: " + releaseRepoUrl);
boolean snapshotPolicyEnabled = tempRepositories.isEmpty();
String repositoryId = snapshotPolicyEnabled ? "artifactory-release-snapshot" : "artifactory-release";

ArtifactRepositoryPolicy releasePolicy = new ArtifactRepositoryPolicy(true, ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY, ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN);
ArtifactRepositoryPolicy snapshotPolicy = new ArtifactRepositoryPolicy(snapshotPolicyEnabled, ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY, ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN);
ArtifactRepository releasePluginRepository = new MavenArtifactRepository(repositoryId, releaseRepoUrl, new DefaultRepositoryLayout(), snapshotPolicy, releasePolicy);
if (authentication != null) {
logger.debug("[buildinfo] Enforcing repository authentication: " + authentication + " for release resolution repository");
releasePluginRepository.setAuthentication(authentication);
}
if (proxy != null) {
logger.debug("[buildinfo] Enforcing proxy: " + proxy + " for release resolution repository");
releasePluginRepository.setProxy(proxy);
}
tempRepositories.add(releasePluginRepository);
}

resolutionPluginRepositories = tempRepositories;
}
return resolutionPluginRepositories;
}

RemoteRepository getSnapshotRepository(RepositorySystemSession session) {
// Init repositories configured in the Artifactory plugin:
initResolutionRepositories(session);

if (snapshotRepository != null) {
return snapshotRepository;
}
Expand All @@ -193,14 +102,4 @@ RemoteRepository getReleaseRepository(RepositorySystemSession session) {

return releaseRepository;
}

private void initResolutionHelper(RepositorySystemSession session) {
if (!resolutionHelper.isInitialized()) {
Properties allMavenProps = new Properties();
allMavenProps.putAll(session.getSystemProperties());
allMavenProps.putAll(session.getUserProperties());
resolutionHelper.init(allMavenProps);
}

}
}
Loading