Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
[MCHANGES-379] [REGRESSION] Authentication does not work after Upgrade
Browse files Browse the repository at this point in the history
Using built-in SettingsDecrypter component to decrypt the password in the settings.

git-svn-id: https://svn.apache.org/repos/asf/maven/plugins/trunk@1784412 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Guillaume Boué committed Feb 26, 2017
1 parent 99784be commit 7adac66
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 24 deletions.
6 changes: 6 additions & 0 deletions maven-changes-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,12 @@ under the License.
<version>3.8.2</version>
<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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
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;
Expand Down Expand Up @@ -234,6 +235,12 @@ public class AnnouncementMojo
@Component( role = VelocityComponent.class, hint = "maven-changes-plugin" )
private VelocityComponent velocity;

/**
* Component used to decrypt server information.
*/
@Component
private SettingsDecrypter settingsDecrypter;

/**
* Version of the artifact.
*/
Expand Down Expand Up @@ -845,7 +852,7 @@ protected List<Release> getGitHubReleases()
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() );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
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;
Expand Down Expand Up @@ -212,7 +215,8 @@ public List<Issue> getIssueList()
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;

Expand All @@ -222,6 +226,12 @@ public void configureAuthentication( String githubAPIServerId, Settings settings
{
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 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;
Expand All @@ -36,6 +37,7 @@
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.
Expand Down Expand Up @@ -66,6 +68,12 @@ public class GitHubMojo
githubColumns.put( "Updated", IssuesReportHelper.COLUMN_UPDATED );
}

/**
* 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.
Expand Down Expand Up @@ -179,7 +187,7 @@ protected void executeReport( Locale locale )
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();

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 );
Expand All @@ -53,4 +73,76 @@ public void testCreateIssue()
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;
}

}

0 comments on commit 7adac66

Please sign in to comment.