Skip to content
This repository has been archived by the owner on Jun 20, 2019. It is now read-only.

Allow use of settings.xml for credentials. #27

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions pom.xml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -48,14 +48,18 @@




<properties> <properties>
<version.junit.junit>4.11</version.junit.junit>
<version.org.jboss.as>7.1.2.Final</version.org.jboss.as> <version.org.jboss.as>7.1.2.Final</version.org.jboss.as>
<version.org.apache.maven.maven-plugin>2.2.1</version.org.apache.maven.maven-plugin> <version.org.apache.maven.maven-plugin>2.2.1</version.org.apache.maven.maven-plugin>
<version.org.apache.maven.maven-settings>3.0.3</version.org.apache.maven.maven-settings>
<version.org.apache.maven.plugin-api>3.0.4</version.org.apache.maven.plugin-api> <version.org.apache.maven.plugin-api>3.0.4</version.org.apache.maven.plugin-api>
<version.org.apache.maven.plugin-testing.maven-plugin-testing-harness>2.0-alpha-1</version.org.apache.maven.plugin-testing.maven-plugin-testing-harness>
<version.org.apache.maven.plugin-tools>3.1</version.org.apache.maven.plugin-tools> <version.org.apache.maven.plugin-tools>3.1</version.org.apache.maven.plugin-tools>
<version.org.apache.maven.plugins.plugin>3.1</version.org.apache.maven.plugins.plugin> <version.org.apache.maven.plugins.plugin>3.1</version.org.apache.maven.plugins.plugin>
<version.org.apache.maven.plugins.site-plugin>3.0</version.org.apache.maven.plugins.site-plugin> <version.org.apache.maven.plugins.site-plugin>3.0</version.org.apache.maven.plugins.site-plugin>
<version.org.codehaus.plexus.plexus-utils>3.0.1</version.org.codehaus.plexus.plexus-utils> <version.org.codehaus.plexus.plexus-utils>3.0.1</version.org.codehaus.plexus.plexus-utils>
<version.org.jboss.remoting3>3.2.12.GA</version.org.jboss.remoting3> <version.org.jboss.remoting3>3.2.12.GA</version.org.jboss.remoting3>
<version.org.mockito.mockito-all>1.9.0</version.org.mockito.mockito-all>
<version.org.sonatype.aether>1.8</version.org.sonatype.aether> <version.org.sonatype.aether>1.8</version.org.sonatype.aether>
</properties> </properties>


Expand Down Expand Up @@ -161,5 +165,39 @@
<artifactId>plexus-utils</artifactId> <artifactId>plexus-utils</artifactId>
<version>${version.org.codehaus.plexus.plexus-utils}</version> <version>${version.org.codehaus.plexus.plexus-utils}</version>
</dependency> </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${version.junit.junit}</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-settings</artifactId>
<version>${version.org.apache.maven.maven-settings}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-testing</groupId>
<artifactId>maven-plugin-testing-harness</artifactId>
<version>${version.org.apache.maven.plugin-testing.maven-plugin-testing-harness}</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-settings-builder</artifactId>
<version>${version.org.apache.maven.maven-settings}</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>${version.org.mockito.mockito-all}</version>
<scope>test</scope>
</dependency>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the versions should use a property in the version.groupId.artifactId format or something similar to the version properties that are already in the POM.

</dependencies> </dependencies>
</project> </project>
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;

import javax.security.auth.callback.CallbackHandler; import javax.security.auth.callback.CallbackHandler;


import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
import org.jboss.as.controller.client.ModelControllerClient; import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.as.controller.client.helpers.domain.DomainClient; import org.jboss.as.controller.client.helpers.domain.DomainClient;
import org.jboss.dmr.ModelNode; import org.jboss.dmr.ModelNode;
Expand All @@ -41,6 +44,14 @@
* @author Stuart Douglas * @author Stuart Douglas
*/ */
public abstract class AbstractServerConnection extends AbstractMojo implements ConnectionInfo, Closeable { public abstract class AbstractServerConnection extends AbstractMojo implements ConnectionInfo, Closeable {

public static final String DEBUG_MESSAGE_NO_CREDS = "No username and password in settings.xml file - falling back to CLI entry";
public static final String DEBUG_MESSAGE_NO_ID = "No <id> element was found in the POM - Getting credentials from CLI entry";
public static final String DEBUG_MESSAGE_NO_SERVER_SECTION = "No <server> section was found for the specified id";
public static final String DEBUG_MESSAGE_NO_SETTINGS_FILE = "No settings.xml file was found in this Mojo's execution context";
public static final String DEBUG_MESSAGE_POM_HAS_CREDS = "Getting credentials from the POM";
public static final String DEBUG_MESSAGE_SETTINGS_HAS_CREDS = "Found username and password in the settings.xml file";
public static final String DEBUG_MESSAGE_SETTINGS_HAS_ID = "Found the server's id in the settings.xml file";


protected static final Object CLIENT_LOCK = new Object(); protected static final Object CLIENT_LOCK = new Object();


Expand All @@ -59,6 +70,19 @@ public abstract class AbstractServerConnection extends AbstractMojo implements C
*/ */
@Parameter(defaultValue = "9999", property = "jboss-as.port") @Parameter(defaultValue = "9999", property = "jboss-as.port")
private int port; private int port;

/**
* Specifies the id of the server if the username and password is to be
* retrieved from the settings.xml file
*/
@Parameter(property = "jboss-as.id")
private String id;

/**
* Provides a reference to the settings file.
*/
@Parameter(property = "settings", readonly = true, required = true, defaultValue = "${settings}")
private Settings settings;


/** /**
* Specifies the username to use if prompted to authenticate by the server. * Specifies the username to use if prompted to authenticate by the server.
Expand Down Expand Up @@ -168,10 +192,39 @@ public synchronized final InetAddress getHostAddress() {
public synchronized final CallbackHandler getCallbackHandler() { public synchronized final CallbackHandler getCallbackHandler() {
CallbackHandler result = handler; CallbackHandler result = handler;
if (result == null) { if (result == null) {
if(username == null && password == null) {
if(id != null) {
getCredentialsFromSettings();
} else {
getLog().debug(DEBUG_MESSAGE_NO_ID);
}
} else {
getLog().debug(DEBUG_MESSAGE_POM_HAS_CREDS);
}
result = handler = new ClientCallbackHandler(username, password); result = handler = new ClientCallbackHandler(username, password);
} }
return result; return result;
} }

private void getCredentialsFromSettings() {
if(settings != null) {
Server server = settings.getServer(id);
if(server != null) {
getLog().debug(DEBUG_MESSAGE_SETTINGS_HAS_ID);
password = server.getPassword();
username = server.getUsername();
if(username != null && password != null) {
getLog().debug(DEBUG_MESSAGE_SETTINGS_HAS_CREDS);
} else {
getLog().debug(DEBUG_MESSAGE_NO_CREDS);
}
} else {
getLog().debug(DEBUG_MESSAGE_NO_SERVER_SECTION);
}
} else {
getLog().debug(DEBUG_MESSAGE_NO_SETTINGS_FILE);
}
}


private boolean isDomainServer(final ModelControllerClient client) { private boolean isDomainServer(final ModelControllerClient client) {
boolean result = false; boolean result = false;
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* File: MojoTestUtilities.java
* Date: Aug 5, 2011
*
* Copyright (c) 2011 by Select Systems - All rights reserved.
*
* By: Select Systems
* 111 Heritage Way, Suite S
* Boalsburg, PA 16827
*
* Revision history:
*
*
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind making the change, but this needs to have the JBoss LGPL license on it. Is that acceptable to you?


package org.jboss.as.plugin.common;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;

import org.apache.maven.plugin.Mojo;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.io.DefaultSettingsReader;
import org.apache.maven.settings.io.SettingsParseException;
import org.apache.maven.settings.io.SettingsReader;
import org.junit.After;
import org.junit.Before;

/**
*
* @author swm16 (swm16@psu.edu)
*/
public abstract class AbstractJbossMavenPluginMojoTestCase extends AbstractMojoTestCase {

@Before
public void setUp() throws Exception {
super.setUp();
}

@After
public void tearDown() throws Exception {
super.tearDown();
}

/**
* Gets a settings.xml file from the input File and prepares it to be
* attached to a pom.xml
*
* @param a file object pointing to the candidate settings file
* @return the settings object
* @throws IOException - if the settings file can't be read
*/
private Settings getSettingsFile(File userSettingsFile) throws IOException {
Map<String, ?> options = Collections.singletonMap( SettingsReader.IS_STRICT, Boolean.TRUE );
SettingsReader reader = new DefaultSettingsReader();

Settings settings = null;
try {
settings = reader.read(userSettingsFile, options);
} catch(SettingsParseException e) {

}

return settings;
}

/**
* Creates a File object from the fileName provided and verifies that it
* exists.
*
* @param fileName the path of the test file
* @return a verified File object
*/
public File getTestFileAndVerify(String fileName) {
File file = getTestFile(fileName);
assertNotNull(file);
assertTrue(file.exists());
return file;
}

/**
* Looks up the specified mojo by name, passing it the POM file that
* references it, then verifying that the lookup was successful.
*
* @param mojoName the name of the mojo being tested
* @param pomFile the pom.xml file to be used during testing
* @return the Mojo object under test
* @throws Exception if the mojo can not be found
*/
public Mojo lookupMojoAndVerify(String mojoName, File pomFile) throws Exception {
Mojo mojo = lookupMojo(mojoName, pomFile);
assertNotNull(mojo);
return mojo;
}

/**
* Looks up the specified mojo by name, passing it the POM file that
* references it and a settings file that configures it, then verifying
* that the lookup was successful.
*
* @param mojoName the name of the mojo being tested
* @param pomFile the pom.xml file to be used during testing
* @param settingsFile the settings.xml file to be used during testing
* @return the Mojo object under test
* @throws Exception if the mojo can not be found
*/
public Mojo lookupMojoVerifyAndApplySettings(String mojoName, File pomFile, File settingsFile) throws Exception {
Mojo mojo = lookupMojo(mojoName, pomFile);
assertNotNull(mojo);
setVariableValueToObject(mojo, "settings", getSettingsFile(settingsFile));
return mojo;
}

}
Loading