-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #458 from spyrkob/auth_proxy
Respect java proxy options in when resolving maven artifacts
- Loading branch information
Showing
3 changed files
with
249 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
prospero-common/src/main/java/org/wildfly/prospero/galleon/MavenProxyHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
147 changes: 147 additions & 0 deletions
147
prospero-common/src/test/java/org/wildfly/prospero/galleon/MavenProxyHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |