From 3a4dc00ec4bb7c0441b81dae1d718e631b7ee982 Mon Sep 17 00:00:00 2001 From: kkkon Date: Mon, 28 Apr 2014 02:55:17 +0900 Subject: [PATCH 1/2] support nonProxyHosts. --- .../maven/plugins/core/GitHubProjectMojo.java | 82 +++++++++++++++++-- 1 file changed, 77 insertions(+), 5 deletions(-) diff --git a/github-core/src/main/java/com/github/maven/plugins/core/GitHubProjectMojo.java b/github-core/src/main/java/com/github/maven/plugins/core/GitHubProjectMojo.java index a59a090e..d7c30b3f 100644 --- a/github-core/src/main/java/com/github/maven/plugins/core/GitHubProjectMojo.java +++ b/github-core/src/main/java/com/github/maven/plugins/core/GitHubProjectMojo.java @@ -49,6 +49,7 @@ import java.net.URL; import java.text.MessageFormat; import java.util.List; +import org.eclipse.egit.github.core.client.IGitHubConstants; /** * Base GitHub Mojo class to be extended. @@ -159,7 +160,7 @@ protected GitHubClient createClient(String host, String userName, client = createClient(); { - Proxy proxy = getProxy( settings, serverId ); + Proxy proxy = getProxy( settings, serverId, host ); if ( null != proxy ) { try @@ -404,15 +405,77 @@ protected Server getServer(final Settings settings, final String serverId) { return null; } + /** + * Check hostname that matched nonProxy setting + * + * @param proxy Maven Proxy. Must not null + * @param hostname + * @return matching result. true: match nonProxy + */ + protected boolean matchNonProxy( final Proxy proxy, final String hostname ) + { + String host = hostname; + + if ( null == hostname ) + host = IGitHubConstants.HOST_DEFAULT; // IGitHubConstants.HOST_API; + + // code from org.apache.maven.plugins.site.AbstractDeployMojo#getProxyInfo + final String nonProxyHosts = proxy.getNonProxyHosts(); + if ( null != nonProxyHosts ) + { + final String[] nonProxies = nonProxyHosts.split( "(,)|(;)|(\\|)" ); + if ( null != nonProxies ) + { + for ( final String nonProxyHost : nonProxies ) + { + //if ( StringUtils.contains( nonProxyHost, "*" ) ) + if ( null != nonProxyHost && nonProxyHost.contains( "*" ) ) + { + // Handle wildcard at the end, beginning or middle of the nonProxyHost + final int pos = nonProxyHost.indexOf( '*' ); + String nonProxyHostPrefix = nonProxyHost.substring( 0, pos ); + String nonProxyHostSuffix = nonProxyHost.substring( pos + 1 ); + // prefix* + if ( !StringUtils.isEmpty( nonProxyHostPrefix ) && host.startsWith( nonProxyHostPrefix ) + && StringUtils.isEmpty( nonProxyHostSuffix ) ) + { + return true; + } + // *suffix + if ( StringUtils.isEmpty( nonProxyHostPrefix ) && !StringUtils.isEmpty( nonProxyHostSuffix ) + && host.endsWith( nonProxyHostSuffix ) ) + { + return true; + } + // prefix*suffix + if ( !StringUtils.isEmpty( nonProxyHostPrefix ) && host.startsWith( nonProxyHostPrefix ) + && !StringUtils.isEmpty( nonProxyHostSuffix ) && host.endsWith( nonProxyHostSuffix ) ) + { + return true; + } + } + else if ( host.equals( nonProxyHost ) ) + { + return true; + } + } + } + } + + return false; + } + /** * Get proxy from settings * * @param settings * @param serverId * must be non-null and non-empty + * @param host + * hostname * @return proxy or null if none matching */ - protected Proxy getProxy(final Settings settings, final String serverId) { + protected Proxy getProxy(final Settings settings, final String serverId, final String host) { if (settings == null) return null; List proxies = settings.getProxies(); @@ -420,14 +483,17 @@ protected Proxy getProxy(final Settings settings, final String serverId) { return null; // search id match first - if ( serverId != null && serverId.isEmpty() ) { + if ( serverId != null && !serverId.isEmpty() ) { for (Proxy proxy : proxies) { if ( proxy.isActive() ) { final String proxyId = proxy.getId(); if ( proxyId != null && !proxyId.isEmpty() ) { if ( proxyId.equalsIgnoreCase(serverId) ) { if ( ( "http".equalsIgnoreCase( proxy.getProtocol() ) || "https".equalsIgnoreCase( proxy.getProtocol() ) ) ) { - return proxy; + if ( matchNonProxy( proxy, host ) ) + return null; + else + return proxy; } } } @@ -440,7 +506,13 @@ protected Proxy getProxy(final Settings settings, final String serverId) { if ( proxy.isActive() && ( "http".equalsIgnoreCase( proxy.getProtocol() ) || "https".equalsIgnoreCase( proxy.getProtocol() ) ) ) - return proxy; + { + if ( matchNonProxy( proxy, host ) ) + return null; + else + return proxy; + } + return null; } From c40647af8d57fc0e6e3fe12b1fe493767d41e7af Mon Sep 17 00:00:00 2001 From: kkkon Date: Mon, 28 Apr 2014 02:55:38 +0900 Subject: [PATCH 2/2] add test nonProxyHosts --- .../maven/plugins/core/MatchNonProxyTest.java | 328 ++++++++++++++++++ .../settings/proxy/nonproxy-github.xml | 40 +++ .../proxy/nonproxy-github_and_api.xml | 40 +++ .../proxy/nonproxy-github_wildcard.xml | 40 +++ .../nonproxy-intra_github-no_same_id.xml | 46 +++ .../settings/proxy/nonproxy-intra_github.xml | 53 +++ 6 files changed, 547 insertions(+) create mode 100644 github-core/src/test/java/com/github/maven/plugins/core/MatchNonProxyTest.java create mode 100644 github-core/src/test/resources/settings/proxy/nonproxy-github.xml create mode 100644 github-core/src/test/resources/settings/proxy/nonproxy-github_and_api.xml create mode 100644 github-core/src/test/resources/settings/proxy/nonproxy-github_wildcard.xml create mode 100644 github-core/src/test/resources/settings/proxy/nonproxy-intra_github-no_same_id.xml create mode 100644 github-core/src/test/resources/settings/proxy/nonproxy-intra_github.xml diff --git a/github-core/src/test/java/com/github/maven/plugins/core/MatchNonProxyTest.java b/github-core/src/test/java/com/github/maven/plugins/core/MatchNonProxyTest.java new file mode 100644 index 00000000..dfb76684 --- /dev/null +++ b/github-core/src/test/java/com/github/maven/plugins/core/MatchNonProxyTest.java @@ -0,0 +1,328 @@ +/* + * Copyright (c) 2012 GitHub Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ +package com.github.maven.plugins.core; + +import java.io.File; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; + +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.maven.execution.MavenSession; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.settings.Proxy; +import org.apache.maven.settings.Settings; +import org.apache.maven.settings.building.DefaultSettingsBuilderFactory; +import org.apache.maven.settings.building.DefaultSettingsBuildingRequest; +import org.apache.maven.settings.building.FileSettingsSource; +import org.apache.maven.settings.building.SettingsBuilder; +import org.apache.maven.settings.building.SettingsBuildingResult; +import org.eclipse.egit.github.core.client.GitHubClient; +import org.eclipse.egit.github.core.client.IGitHubConstants; +import org.junit.Test; + +/** + * NonProxy tests for the various configuration + * + * @author Kiyofumi Kondoh + */ +public class MatchNonProxyTest { + + private class TestMojo extends GitHubProjectMojo { + + private final AtomicReference host = new AtomicReference(); + + protected GitHubClient createClient() { + host.set(null); + return super.createClient(); + } + + protected GitHubClient createClient(String hostname) + throws MojoExecutionException { + host.set(hostname); + return super.createClient(hostname); + } + + public GitHubClient createClient(String host, String userName, + String password, String oauth2Token, String serverId, + Settings settings, MavenSession session) + throws MojoExecutionException { + return super.createClient(host, userName, password, oauth2Token, + serverId, settings, session); + } + + public void execute() throws MojoExecutionException, + MojoFailureException { + // Intentionally left blank + } + } + + /** + * matchNonProxy tests with single nonProxyHosts + */ + @Test + public void matchNonProxyWithSingle_nonPorxyHosts() throws Exception + { + SettingsBuilder builder = new DefaultSettingsBuilderFactory().newInstance(); + assertNotNull( builder ); + + DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest(); + request.setSystemProperties( System.getProperties() ); + FileSettingsSource fileSource = new FileSettingsSource( new File("src/test/resources/settings/proxy/nonproxy-github.xml").getAbsoluteFile() ); + request.setUserSettingsSource( fileSource ); + + SettingsBuildingResult result = builder.build( request ); + assertNotNull( result ); + assertNotNull( result.getEffectiveSettings() ); + + TestMojo mojo = new TestMojo(); + assertNotNull( mojo ); + + assertNotNull( result.getEffectiveSettings().getProxies() ); + for ( final Proxy proxy : result.getEffectiveSettings().getProxies() ) + { + { + final boolean isNonProxy = mojo.matchNonProxy( proxy, IGitHubConstants.HOST_DEFAULT ); + assertTrue( isNonProxy ); + } + { + final boolean isNonProxy = mojo.matchNonProxy( proxy, IGitHubConstants.HOST_API ); + assertFalse( isNonProxy ); + } + { + final boolean isNonProxy = mojo.matchNonProxy( proxy, "hoge." + IGitHubConstants.HOST_DEFAULT ); + assertFalse( isNonProxy ); + } + { + final boolean isNonProxy = mojo.matchNonProxy( proxy, "hoge" + IGitHubConstants.HOST_DEFAULT ); + assertFalse( isNonProxy ); + } + { + final boolean isNonProxy = mojo.matchNonProxy( proxy, mojo.host.get() ); + assertTrue( isNonProxy ); + } + } + } + + /** + * matchNonProxy tests with multiple nonProxyHosts + */ + @Test + public void matchNonProxyWithMultiple_nonPorxyHosts() throws Exception + { + SettingsBuilder builder = new DefaultSettingsBuilderFactory().newInstance(); + assertNotNull( builder ); + + DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest(); + request.setSystemProperties( System.getProperties() ); + FileSettingsSource fileSource = new FileSettingsSource( new File("src/test/resources/settings/proxy/nonproxy-github_and_api.xml").getAbsoluteFile() ); + request.setUserSettingsSource( fileSource ); + + SettingsBuildingResult result = builder.build( request ); + assertNotNull( result ); + assertNotNull( result.getEffectiveSettings() ); + + TestMojo mojo = new TestMojo(); + assertNotNull( mojo ); + + assertNotNull( result.getEffectiveSettings().getProxies() ); + for ( final Proxy proxy : result.getEffectiveSettings().getProxies() ) + { + { + final boolean isNonProxy = mojo.matchNonProxy( proxy, IGitHubConstants.HOST_DEFAULT ); + assertTrue( isNonProxy ); + } + { + final boolean isNonProxy = mojo.matchNonProxy( proxy, IGitHubConstants.HOST_API ); + assertTrue( isNonProxy ); + } + { + final boolean isNonProxy = mojo.matchNonProxy( proxy, "hoge." + IGitHubConstants.HOST_DEFAULT ); + assertFalse( isNonProxy ); + } + { + final boolean isNonProxy = mojo.matchNonProxy( proxy, "hoge" + IGitHubConstants.HOST_DEFAULT ); + assertFalse( isNonProxy ); + } + { + final boolean isNonProxy = mojo.matchNonProxy( proxy, mojo.host.get() ); + assertTrue( isNonProxy ); + } + } + } + + /** + * matchNonProxy tests with wildcard nonProxyHosts + */ + @Test + public void matchNonProxyWithWildcard_nonPorxyHosts() throws Exception + { + SettingsBuilder builder = new DefaultSettingsBuilderFactory().newInstance(); + assertNotNull( builder ); + + DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest(); + request.setSystemProperties( System.getProperties() ); + FileSettingsSource fileSource = new FileSettingsSource( new File("src/test/resources/settings/proxy/nonproxy-github_wildcard.xml").getAbsoluteFile() ); + request.setUserSettingsSource( fileSource ); + + SettingsBuildingResult result = builder.build( request ); + assertNotNull( result ); + assertNotNull( result.getEffectiveSettings() ); + + TestMojo mojo = new TestMojo(); + assertNotNull( mojo ); + + assertNotNull( result.getEffectiveSettings().getProxies() ); + for ( final Proxy proxy : result.getEffectiveSettings().getProxies() ) + { + { + final boolean isNonProxy = mojo.matchNonProxy( proxy, IGitHubConstants.HOST_DEFAULT ); + assertTrue( isNonProxy ); + } + { + final boolean isNonProxy = mojo.matchNonProxy( proxy, IGitHubConstants.HOST_API ); + assertTrue( isNonProxy ); + } + { + final boolean isNonProxy = mojo.matchNonProxy( proxy, "hoge." + IGitHubConstants.HOST_DEFAULT ); + assertTrue( isNonProxy ); + } + { + final boolean isNonProxy = mojo.matchNonProxy( proxy, "hoge" + IGitHubConstants.HOST_DEFAULT ); + assertTrue( isNonProxy ); + } + { + final boolean isNonProxy = mojo.matchNonProxy( proxy, mojo.host.get() ); + assertTrue( isNonProxy ); + } + } + } + + + + + /** + * getProxy tests with single nonProxyHosts + */ + @Test + public void getProxyWithSingle_nonProxyHosts() throws Exception + { + SettingsBuilder builder = new DefaultSettingsBuilderFactory().newInstance(); + assertNotNull( builder ); + + DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest(); + request.setSystemProperties( System.getProperties() ); + FileSettingsSource fileSource = new FileSettingsSource( new File("src/test/resources/settings/proxy/nonproxy-github.xml").getAbsoluteFile() ); + request.setUserSettingsSource( fileSource ); + + SettingsBuildingResult result = builder.build( request ); + assertNotNull( result ); + assertNotNull( result.getEffectiveSettings() ); + + TestMojo mojo = new TestMojo(); + assertNotNull( mojo ); + + { + Proxy proxy = mojo.getProxy( result.getEffectiveSettings(), "intra_github-test-nonproxy", mojo.host.get() ); + assertNull( proxy ); + } + { + Proxy proxy = mojo.getProxy( result.getEffectiveSettings(), "intra_github-test-nonproxy", "intra-github.com" ); + assertNotNull( proxy ); + } + } + + /** + * getProxy tests with nonProxyHosts, which have same id + */ + @Test + public void getProxyIntraWithSameId() throws Exception + { + SettingsBuilder builder = new DefaultSettingsBuilderFactory().newInstance(); + assertNotNull( builder ); + + DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest(); + request.setSystemProperties( System.getProperties() ); + FileSettingsSource fileSource = new FileSettingsSource( new File("src/test/resources/settings/proxy/nonproxy-intra_github.xml").getAbsoluteFile() ); + request.setUserSettingsSource( fileSource ); + + SettingsBuildingResult result = builder.build( request ); + assertNotNull( result ); + assertNotNull( result.getEffectiveSettings() ); + + TestMojo mojo = new TestMojo(); + assertNotNull( mojo ); + + { + Proxy proxy = mojo.getProxy( result.getEffectiveSettings(), "intra_github-test-nonproxy", mojo.host.get() ); + assertNotNull( proxy ); + } + { + Proxy proxy = mojo.getProxy( result.getEffectiveSettings(), "intra_github-test-nonproxy", "intra-github.com" ); + assertNull( proxy ); + } + { + Proxy proxy = mojo.getProxy( result.getEffectiveSettings(), "intra_github-test-nonproxy", "intra_github.com" ); + assertNotNull( proxy ); + } + } + + /** + * getProxy tests with nonProxyHosts, which doesn't have same id + */ + @Test + public void getProxyIntraNoSameId() throws Exception + { + SettingsBuilder builder = new DefaultSettingsBuilderFactory().newInstance(); + assertNotNull( builder ); + + DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest(); + request.setSystemProperties( System.getProperties() ); + FileSettingsSource fileSource = new FileSettingsSource( new File("src/test/resources/settings/proxy/nonproxy-intra_github-no_same_id.xml").getAbsoluteFile() ); + request.setUserSettingsSource( fileSource ); + + SettingsBuildingResult result = builder.build( request ); + assertNotNull( result ); + assertNotNull( result.getEffectiveSettings() ); + + TestMojo mojo = new TestMojo(); + assertNotNull( mojo ); + + { + Proxy proxy = mojo.getProxy( result.getEffectiveSettings(), "intra_github-test-nonproxy", mojo.host.get() ); + assertNotNull( proxy ); + } + { + Proxy proxy = mojo.getProxy( result.getEffectiveSettings(), "intra_github-test-nonproxy", "intra-github.com" ); + assertNull( proxy ); + } + { + Proxy proxy = mojo.getProxy( result.getEffectiveSettings(), "intra_github-test-nonproxy", "intra_github.com" ); + assertNotNull( proxy ); + } + } + + +} diff --git a/github-core/src/test/resources/settings/proxy/nonproxy-github.xml b/github-core/src/test/resources/settings/proxy/nonproxy-github.xml new file mode 100644 index 00000000..b845cb0a --- /dev/null +++ b/github-core/src/test/resources/settings/proxy/nonproxy-github.xml @@ -0,0 +1,40 @@ + + + + + + + + + github-test-nonproxy + true + http + 127.0.0.1 + 3128 + github.com + + + + diff --git a/github-core/src/test/resources/settings/proxy/nonproxy-github_and_api.xml b/github-core/src/test/resources/settings/proxy/nonproxy-github_and_api.xml new file mode 100644 index 00000000..71ac8f23 --- /dev/null +++ b/github-core/src/test/resources/settings/proxy/nonproxy-github_and_api.xml @@ -0,0 +1,40 @@ + + + + + + + + + github-test-nonproxy + true + http + 127.0.0.1 + 3128 + github.com|api.github.com + + + + diff --git a/github-core/src/test/resources/settings/proxy/nonproxy-github_wildcard.xml b/github-core/src/test/resources/settings/proxy/nonproxy-github_wildcard.xml new file mode 100644 index 00000000..4bf71506 --- /dev/null +++ b/github-core/src/test/resources/settings/proxy/nonproxy-github_wildcard.xml @@ -0,0 +1,40 @@ + + + + + + + + + github-test-nonproxy + true + http + 127.0.0.1 + 3128 + *github.com + + + + diff --git a/github-core/src/test/resources/settings/proxy/nonproxy-intra_github-no_same_id.xml b/github-core/src/test/resources/settings/proxy/nonproxy-intra_github-no_same_id.xml new file mode 100644 index 00000000..dcf72526 --- /dev/null +++ b/github-core/src/test/resources/settings/proxy/nonproxy-intra_github-no_same_id.xml @@ -0,0 +1,46 @@ + + + + + + + + + github-test-nonproxy + true + http + 127.0.0.1 + 8080 + intra-github.com + + + + + + intra_github-test-nonproxy + + + + diff --git a/github-core/src/test/resources/settings/proxy/nonproxy-intra_github.xml b/github-core/src/test/resources/settings/proxy/nonproxy-intra_github.xml new file mode 100644 index 00000000..0a3d1741 --- /dev/null +++ b/github-core/src/test/resources/settings/proxy/nonproxy-intra_github.xml @@ -0,0 +1,53 @@ + + + + + + + + + github-test-nonproxy + true + http + 127.0.0.1 + 8080 + + + intra_github-test-nonproxy + true + http + 127.0.0.1 + 3128 + intra-github.com + + + + + + intra_github-test-nonproxy + + + +