Skip to content

Commit

Permalink
Merge pull request #458 from spyrkob/auth_proxy
Browse files Browse the repository at this point in the history
Respect java proxy options in when resolving maven artifacts
  • Loading branch information
spyrkob authored Sep 15, 2023
2 parents 3c860f4 + 0f357da commit 678d7d1
Show file tree
Hide file tree
Showing 3 changed files with 249 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ private GalleonEnvironment(Builder builder) throws ProvisioningException, Metada
final Path sourceServerPath = builder.sourceServerPath == null? builder.installDir:builder.sourceServerPath;
MavenVersionsResolver.Factory factory;
try {
factory = new CachedVersionResolverFactory(new VersionResolverFactory(system, session), sourceServerPath, system, session);
factory = new CachedVersionResolverFactory(new VersionResolverFactory(system, session, MavenProxyHandler::addProxySettings), sourceServerPath, system, session);
} catch (IOException e) {
ProsperoLogger.ROOT_LOGGER.debug("Unable to read artifact cache, falling back to Maven resolver.", e);
factory = new VersionResolverFactory(system, session);
factory = new VersionResolverFactory(system, session, MavenProxyHandler::addProxySettings);
}
channelSession = initChannelSession(session, factory);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright 2023 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wildfly.prospero.galleon;

import org.eclipse.aether.repository.Authentication;
import org.eclipse.aether.repository.Proxy;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.util.repository.AuthenticationBuilder;
import org.jboss.logging.Logger;
import org.wildfly.channel.Repository;

import java.net.InetSocketAddress;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Optional;

import static org.wildfly.channel.maven.VersionResolverFactory.DEFAULT_REPOSITORY_POLICY;

class MavenProxyHandler {
private static final Logger LOG = Logger.getLogger(GalleonEnvironment.class.getName());

public static RemoteRepository addProxySettings(Repository r) {

final RemoteRepository.Builder builder = new RemoteRepository.Builder(r.getId(), "default", r.getUrl())
.setPolicy(DEFAULT_REPOSITORY_POLICY);
getDefinedProxy(r).ifPresent(builder::setProxy);
return builder.build();
}

private static Optional<Proxy> getDefinedProxy(Repository r) {
final URI repositoryUri;
try {
repositoryUri = new URI(r.getUrl());
if (repositoryUri.getScheme() == null || !repositoryUri.getScheme().equals("http") && !repositoryUri.getScheme().equals("https")) {
LOG.debugf("Skipping proxy configuration for %s - scheme not supported", r.getUrl());
return Optional.empty();
}
if (repositoryUri.getHost() == null) {
LOG.infof("Skipping proxy configuration for %s - unable to find host", r.getUrl());
return Optional.empty();
}
} catch (URISyntaxException e) {
LOG.infof("Skipping proxy configuration for %s - unable to parse address", r.getUrl());
return Optional.empty();
}


return ProxySelector.getDefault()
.select(repositoryUri)
.stream().filter(p -> p.type() == java.net.Proxy.Type.HTTP)
.map(java.net.Proxy::address)
.map(InetSocketAddress.class::cast)
.map(a -> {
final String username = getProperty(repositoryUri.getScheme(), "proxyUser");
final String password = getProperty(repositoryUri.getScheme(), "proxyPassword");
return createProxySettings(a, username, password);
})
.findFirst();
}

private static String getProperty(String scheme, String suffix) {
if (scheme.equals("https")) {
return System.getProperty("https." + suffix);
} else if (scheme.equals("http")) {
return System.getProperty("http." + suffix);
} else {
return null;
}
}

private static Proxy createProxySettings(InetSocketAddress proxyAddress, String username, String password) {
if (username != null && password != null) {
if (LOG.isTraceEnabled()) {
LOG.tracef("Creating authenticator for the proxy");
}
final Authentication authentication = new AuthenticationBuilder()
.addUsername(username)
.addPassword(password)
.build();
return new Proxy(null, proxyAddress.getHostName(), proxyAddress.getPort(), authentication);
} else {
return new Proxy(null, proxyAddress.getHostName(), proxyAddress.getPort());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* Copyright 2023 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wildfly.prospero.galleon;

import org.eclipse.aether.repository.RemoteRepository;
import org.junit.After;
import org.junit.Test;
import org.wildfly.channel.Repository;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

public class MavenProxyHandlerTest {

@After
public void clearProperties() {
System.clearProperty("http.proxyHost");
System.clearProperty("http.proxyPort");
System.clearProperty("http.proxyUser");
System.clearProperty("http.proxyPassword");

System.clearProperty("https.proxyHost");
System.clearProperty("https.proxyPort");
System.clearProperty("https.proxyUser");
System.clearProperty("https.proxyPassword");

System.clearProperty("http.nonProxyHosts");
}

@Test
public void noProxiesAddedIfSettingsAreNotPresent() throws Exception {
final RemoteRepository result = MavenProxyHandler.addProxySettings(new Repository("test", "http://foo.bar"));

assertNull(result.getProxy());
}

@Test
public void addUnauthenticatedHttpProxy() throws Exception {
System.setProperty("http.proxyHost", "http://proxy");
System.setProperty("http.proxyPort", "8888");
final RemoteRepository result = MavenProxyHandler.addProxySettings(new Repository("test", "http://foo.bar"));

assertThat(result.getProxy())
.hasFieldOrPropertyWithValue("host", "http://proxy")
.hasFieldOrPropertyWithValue("port", 8888);

assertNull(MavenProxyHandler.addProxySettings(new Repository("test", "https://foo.bar")).getProxy());
}

@Test
public void addUnauthenticatedHttpsProxy() throws Exception {
System.setProperty("https.proxyHost", "http://proxy");
System.setProperty("https.proxyPort", "8888");
final RemoteRepository result = MavenProxyHandler.addProxySettings(new Repository("test", "https://foo.bar"));

assertThat(result.getProxy())
.hasFieldOrPropertyWithValue("host", "http://proxy")
.hasFieldOrPropertyWithValue("port", 8888);

assertNull(MavenProxyHandler.addProxySettings(new Repository("test", "http://foo.bar")).getProxy());
}

@Test
public void addAuthenticatedHttpsProxy() throws Exception {
System.setProperty("https.proxyHost", "http://proxy");
System.setProperty("https.proxyPort", "8888");
System.setProperty("https.proxyUser", "test");
System.setProperty("https.proxyPassword", "pwd");
final RemoteRepository result = MavenProxyHandler.addProxySettings(new Repository("test", "https://foo.bar"));

assertThat(result.getProxy())
.hasFieldOrPropertyWithValue("host", "http://proxy")
.hasFieldOrPropertyWithValue("port", 8888);
assertNotNull(result.getProxy().getAuthentication());
}

@Test
public void addAuthenticatedHttpProxy() throws Exception {
System.setProperty("http.proxyHost", "http://proxy");
System.setProperty("http.proxyPort", "8888");
System.setProperty("http.proxyUser", "test");
System.setProperty("http.proxyPassword", "pwd");
final RemoteRepository result = MavenProxyHandler.addProxySettings(new Repository("test", "http://foo.bar"));

assertThat(result.getProxy())
.hasFieldOrPropertyWithValue("host", "http://proxy")
.hasFieldOrPropertyWithValue("port", 8888);
assertNotNull(result.getProxy().getAuthentication());
}

@Test
public void dontAddAuthenticationIfPartiallyConfigured() throws Exception {
System.setProperty("http.proxyHost", "http://proxy");
System.setProperty("http.proxyPort", "8888");
System.setProperty("http.proxyPassword", "pwd");
final RemoteRepository result = MavenProxyHandler.addProxySettings(new Repository("test", "http://foo.bar"));

assertThat(result.getProxy())
.hasFieldOrPropertyWithValue("host", "http://proxy")
.hasFieldOrPropertyWithValue("port", 8888);
assertNull(result.getProxy().getAuthentication());
}

@Test
public void defaultPortTo80IfNotPresent() throws Exception {
System.setProperty("http.proxyHost", "http://proxy");
final RemoteRepository result = MavenProxyHandler.addProxySettings(new Repository("test", "http://foo.bar"));

assertThat(result.getProxy())
.hasFieldOrPropertyWithValue("host", "http://proxy")
.hasFieldOrPropertyWithValue("port", 80);
}

@Test
public void skipProxyIfHostExcluded() throws Exception {
System.setProperty("http.proxyHost", "http://proxy");
System.setProperty("http.nonProxyHosts", "foo.bar");
final RemoteRepository result = MavenProxyHandler.addProxySettings(new Repository("test", "http://foo.bar"));

assertNull(result.getProxy());
}

@Test
public void skipProxyIfRepositoryUrlInvalid() throws Exception {
System.setProperty("http.proxyHost", "http://proxy");

assertNull(MavenProxyHandler.addProxySettings(new Repository("test", "http:foo.bar")).getProxy());
assertNull(MavenProxyHandler.addProxySettings(new Repository("test", "httpfoo.bar")).getProxy());
assertNull(MavenProxyHandler.addProxySettings(new Repository("test", "")).getProxy());
}

}

0 comments on commit 678d7d1

Please sign in to comment.