Author: gboue Date: Sun Feb 26 00:33:12 2017 New Revision: 1784412 URL: http://svn.apache.org/viewvc?rev=1784412&view=rev Log: [MCHANGES-379] [REGRESSION] Authentication does not work after Upgrade
Using built-in SettingsDecrypter component to decrypt the password in the settings. Modified: maven/plugins/trunk/maven-changes-plugin/pom.xml maven/plugins/trunk/maven-changes-plugin/src/main/java/org/apache/maven/plugins/announcement/AnnouncementMojo.java maven/plugins/trunk/maven-changes-plugin/src/main/java/org/apache/maven/plugins/github/GitHubDownloader.java maven/plugins/trunk/maven-changes-plugin/src/main/java/org/apache/maven/plugins/github/GitHubMojo.java maven/plugins/trunk/maven-changes-plugin/src/test/java/org/apache/maven/plugins/github/GitHubDownloaderTestCase.java Modified: maven/plugins/trunk/maven-changes-plugin/pom.xml URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-changes-plugin/pom.xml?rev=1784412&r1=1784411&r2=1784412&view=diff ============================================================================== --- maven/plugins/trunk/maven-changes-plugin/pom.xml (original) +++ maven/plugins/trunk/maven-changes-plugin/pom.xml Sun Feb 26 00:33:12 2017 @@ -382,6 +382,12 @@ under the License. <scope>test</scope> </dependency> <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <version>1.9.5</version> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.apache.maven.plugin-testing</groupId> <artifactId>maven-plugin-testing-harness</artifactId> <version>2.1</version> Modified: maven/plugins/trunk/maven-changes-plugin/src/main/java/org/apache/maven/plugins/announcement/AnnouncementMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-changes-plugin/src/main/java/org/apache/maven/plugins/announcement/AnnouncementMojo.java?rev=1784412&r1=1784411&r2=1784412&view=diff ============================================================================== --- maven/plugins/trunk/maven-changes-plugin/src/main/java/org/apache/maven/plugins/announcement/AnnouncementMojo.java (original) +++ maven/plugins/trunk/maven-changes-plugin/src/main/java/org/apache/maven/plugins/announcement/AnnouncementMojo.java Sun Feb 26 00:33:12 2017 @@ -49,6 +49,7 @@ import org.apache.maven.plugins.trac.Tra import org.apache.maven.plugins.trac.TracIssueManagmentSystem; import org.apache.maven.project.MavenProject; import org.apache.maven.settings.Settings; +import org.apache.maven.settings.crypto.SettingsDecrypter; import org.apache.velocity.Template; import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.context.Context; @@ -235,6 +236,12 @@ public class AnnouncementMojo private VelocityComponent velocity; /** + * Component used to decrypt server information. + */ + @Component + private SettingsDecrypter settingsDecrypter; + + /** * Version of the artifact. */ @Parameter( property = "changes.version", defaultValue = "${project.version}", required = true ) @@ -845,7 +852,7 @@ public class AnnouncementMojo GitHubDownloader issueDownloader = new GitHubDownloader( project, githubAPIScheme, githubAPIPort, includeOpenIssues, true ); - issueDownloader.configureAuthentication( githubAPIServerId, settings, getLog() ); + issueDownloader.configureAuthentication( settingsDecrypter, githubAPIServerId, settings, getLog() ); return getReleases( issueDownloader.getIssueList(), new GitHubIssueManagementSystem() ); } Modified: maven/plugins/trunk/maven-changes-plugin/src/main/java/org/apache/maven/plugins/github/GitHubDownloader.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-changes-plugin/src/main/java/org/apache/maven/plugins/github/GitHubDownloader.java?rev=1784412&r1=1784411&r2=1784412&view=diff ============================================================================== --- maven/plugins/trunk/maven-changes-plugin/src/main/java/org/apache/maven/plugins/github/GitHubDownloader.java (original) +++ maven/plugins/trunk/maven-changes-plugin/src/main/java/org/apache/maven/plugins/github/GitHubDownloader.java Sun Feb 26 00:33:12 2017 @@ -24,7 +24,10 @@ import org.apache.maven.plugins.issues.I import org.apache.maven.project.MavenProject; import org.apache.maven.settings.Server; import org.apache.maven.settings.Settings; - +import org.apache.maven.settings.building.SettingsProblem; +import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest; +import org.apache.maven.settings.crypto.SettingsDecrypter; +import org.apache.maven.settings.crypto.SettingsDecryptionResult; import org.eclipse.egit.github.core.Label; import org.eclipse.egit.github.core.client.GitHubClient; import org.eclipse.egit.github.core.service.IssueService; @@ -212,7 +215,8 @@ public class GitHubDownloader return issueList; } - public void configureAuthentication( String githubAPIServerId, Settings settings, Log log ) + public void configureAuthentication( SettingsDecrypter decrypter, String githubAPIServerId, Settings settings, + Log log ) { boolean configured = false; @@ -222,6 +226,12 @@ public class GitHubDownloader { if ( server.getId().equals( githubAPIServerId ) ) { + SettingsDecryptionResult result = decrypter.decrypt( new DefaultSettingsDecryptionRequest( server ) ); + for ( SettingsProblem problem : result.getProblems() ) + { + log.error( problem.getMessage(), problem.getException() ); + } + server = result.getServer(); String user = server.getUsername(); String password = server.getPassword(); this.client.setCredentials( user, password ); Modified: maven/plugins/trunk/maven-changes-plugin/src/main/java/org/apache/maven/plugins/github/GitHubMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-changes-plugin/src/main/java/org/apache/maven/plugins/github/GitHubMojo.java?rev=1784412&r1=1784411&r2=1784412&view=diff ============================================================================== --- maven/plugins/trunk/maven-changes-plugin/src/main/java/org/apache/maven/plugins/github/GitHubMojo.java (original) +++ maven/plugins/trunk/maven-changes-plugin/src/main/java/org/apache/maven/plugins/github/GitHubMojo.java Sun Feb 26 00:33:12 2017 @@ -26,6 +26,7 @@ import java.util.Locale; import java.util.Map; import java.util.ResourceBundle; +import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.changes.AbstractChangesReport; @@ -36,6 +37,7 @@ import org.apache.maven.plugins.issues.I import org.apache.maven.plugins.issues.IssuesReportHelper; import org.apache.maven.reporting.MavenReportException; import org.apache.maven.settings.Settings; +import org.apache.maven.settings.crypto.SettingsDecrypter; /** * Goal which downloads issues from GitHub and generates a report. @@ -67,6 +69,12 @@ public class GitHubMojo } /** + * Component used to decrypt server information. + */ + @Component + private SettingsDecrypter settingsDecrypter; + + /** * Sets the column names that you want to show in the report. The columns will appear in the report in the same * order as you specify them here. Multiple values can be separated by commas. * <p> @@ -179,7 +187,7 @@ public class GitHubMojo GitHubDownloader issueDownloader = new GitHubDownloader( project, githubAPIScheme, githubAPIPort, includeOpenIssues, onlyMilestoneIssues ); - issueDownloader.configureAuthentication( githubAPIServerId, settings, getLog() ); + issueDownloader.configureAuthentication( settingsDecrypter, githubAPIServerId, settings, getLog() ); List<Issue> issueList = issueDownloader.getIssueList(); Modified: maven/plugins/trunk/maven-changes-plugin/src/test/java/org/apache/maven/plugins/github/GitHubDownloaderTestCase.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-changes-plugin/src/test/java/org/apache/maven/plugins/github/GitHubDownloaderTestCase.java?rev=1784412&r1=1784411&r2=1784412&view=diff ============================================================================== --- maven/plugins/trunk/maven-changes-plugin/src/test/java/org/apache/maven/plugins/github/GitHubDownloaderTestCase.java (original) +++ maven/plugins/trunk/maven-changes-plugin/src/test/java/org/apache/maven/plugins/github/GitHubDownloaderTestCase.java Sun Feb 26 00:33:12 2017 @@ -1,26 +1,52 @@ +package org.apache.maven.plugins.github; + /* - * Licensed to Elasticsearch under one or more contributor license agreements. See the NOTICE file distributed with this - * work for additional information regarding copyright ownership. Elasticsearch licenses this file to you 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 + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * 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. + * 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.apache.maven.plugins.github; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.any; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; -import junit.framework.TestCase; import org.apache.maven.model.IssueManagement; -import org.apache.maven.plugins.github.GitHubDownloader; +import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugins.issues.Issue; import org.apache.maven.project.MavenProject; +import org.apache.maven.settings.Server; +import org.apache.maven.settings.Settings; +import org.apache.maven.settings.building.DefaultSettingsProblem; +import org.apache.maven.settings.building.SettingsProblem; +import org.apache.maven.settings.building.SettingsProblem.Severity; +import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest; +import org.apache.maven.settings.crypto.SettingsDecrypter; +import org.apache.maven.settings.crypto.SettingsDecryptionRequest; +import org.apache.maven.settings.crypto.SettingsDecryptionResult; import org.eclipse.egit.github.core.User; +import org.mockito.ArgumentCaptor; -import java.io.IOException; +import junit.framework.TestCase; public class GitHubDownloaderTestCase extends TestCase @@ -29,14 +55,8 @@ public class GitHubDownloaderTestCase public void testCreateIssue() throws IOException { - - MavenProject mavenProject = new MavenProject(); - IssueManagement issueManagement = new IssueManagement(); - issueManagement.setSystem( "GitHub" ); - issueManagement.setUrl( "https://github.com/dadoonet/spring-elasticsearch/issues/" ); - mavenProject.setIssueManagement( issueManagement ); - - GitHubDownloader gitHubDownloader = new GitHubDownloader( mavenProject, "https", 80, true, false ); + IssueManagement issueManagement = newGitHubIssueManagement(); + GitHubDownloader gitHubDownloader = newGitHubDownloader( issueManagement ); org.eclipse.egit.github.core.Issue githubIssue = new org.eclipse.egit.github.core.Issue(); githubIssue.setNumber( 1 ); @@ -53,4 +73,76 @@ public class GitHubDownloaderTestCase assertEquals( githubIssue.getTitle(), issue.getSummary() ); assertEquals( issueManagement.getUrl() + githubIssue.getNumber(), issue.getLink() ); } + + public void testConfigureAuthenticationWithProblems() + throws Exception + { + IssueManagement issueManagement = newGitHubIssueManagement(); + GitHubDownloader gitHubDownloader = newGitHubDownloader( issueManagement ); + Settings settings = new Settings(); + Server server = newServer( "github-server" ); + settings.addServer( server ); + SettingsDecrypter decrypter = mock( SettingsDecrypter.class ); + SettingsDecryptionResult result = mock( SettingsDecryptionResult.class ); + Log log = mock( Log.class ); + when( result.getProblems() ).thenReturn( Arrays.<SettingsProblem>asList( new DefaultSettingsProblem( "Ups " + + server.getId(), Severity.ERROR, null, -1, -1, null ) ) ); + when( result.getServer() ).thenReturn( server ); + when( decrypter.decrypt( any( SettingsDecryptionRequest.class ) ) ).thenReturn( result ); + + gitHubDownloader.configureAuthentication( decrypter, "github-server", settings, log ); + + verify( log ).error( "Ups github-server", null ); + ArgumentCaptor<SettingsDecryptionRequest> argument = ArgumentCaptor.forClass( SettingsDecryptionRequest.class ); + verify( decrypter ).decrypt( argument.capture() ); + List<Server> servers = ( (DefaultSettingsDecryptionRequest) argument.getValue() ).getServers(); + assertEquals( 1, servers.size() ); + assertSame( server, servers.get( 0 ) ); + } + + public void testConfigureAuthenticationWithNoServer() + throws Exception + { + IssueManagement issueManagement = newGitHubIssueManagement(); + GitHubDownloader gitHubDownloader = newGitHubDownloader( issueManagement ); + Settings settings = new Settings(); + Server server = newServer( "not-the-right-one" ); + settings.addServer( server ); + SettingsDecrypter decrypter = mock( SettingsDecrypter.class ); + SettingsDecryptionResult result = mock( SettingsDecryptionResult.class ); + Log log = mock( Log.class ); + when( result.getProblems() ).thenReturn( Collections.<SettingsProblem>emptyList() ); + when( result.getServer() ).thenReturn( server ); + when( decrypter.decrypt( new DefaultSettingsDecryptionRequest( server ) ) ).thenReturn( result ); + + gitHubDownloader.configureAuthentication( decrypter, "github-server", settings, log ); + + verify( log ).warn( "Can't find server id [github-server] configured in githubAPIServerId." ); + } + + private Server newServer( String id ) + { + Server server = new Server(); + server.setId( id ); + server.setUsername( "some-user" ); + server.setPassword( "Sup3rSecret" ); + return server; + } + + private GitHubDownloader newGitHubDownloader( IssueManagement issueManagement ) + throws MalformedURLException + { + MavenProject mavenProject = new MavenProject(); + mavenProject.setIssueManagement( issueManagement ); + return new GitHubDownloader( mavenProject, "https", 80, true, false ); + } + + private IssueManagement newGitHubIssueManagement() + { + IssueManagement issueManagement = new IssueManagement(); + issueManagement.setSystem( "GitHub" ); + issueManagement.setUrl( "https://github.com/dadoonet/spring-elasticsearch/issues/" ); + return issueManagement; + } + }