Skip to content

Commit

Permalink
Merge pull request #15 from Cognifide/feature/instances-username-pass…
Browse files Browse the repository at this point in the history
…word-credentials

Feature/instances username password credentials
  • Loading branch information
andrzejkrej authored Sep 22, 2016
2 parents 189168c + 31092e6 commit 0996a6b
Show file tree
Hide file tree
Showing 16 changed files with 125 additions and 139 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ JAR package will be available as `target/secure-aem-VERSION-cli.jar`.

Usage is simple:

java -jar secure-aem-VERSION.jar [-a AUTHOR_URL] [-p PUBLISH_URL] [-d DISPATCHER_URL]
java -jar secure-aem-VERSION.jar [-a AUTHOR_URL] [-aCredentials AUTHOR_LOGIN:AUTHOR_PASSWORD] [-p PUBLISH_URL] [-pCredentials PUBLISH_LOGIN:PUBLISH_PASSWORD] [-d DISPATCHER_URL]

Enter at least one URL to test given instance, eg.:

java -jar secure-aem-VERSION.jar -a https://localhost:4502
java -jar secure-aem-VERSION.jar -a https://localhost:4502 -aCredentials admin:admin

to invoke author tests on the localhost or

java -jar secure-aem-VERSION.jar -a 192.168.35.105:4502 -p 192.168.35.105:4503 -d 192.168.35.105
java -jar secure-aem-VERSION.jar -a 192.168.35.105:4502 -aCredentials admin:admin -p 192.168.35.105:4503 -pCredentials admin:admin-d 192.168.35.105

to invoke author, publish and dispatcher-related tests. You may skip the starting `https://`, *SecureAEM* uses HTTP protocol by default.

Expand All @@ -62,7 +62,7 @@ By default *SecureAEM* runs full test set defined in:

to override it use maven -suite parameter

java -jar secure-aem-VERSION.jar -a https://localhost:4502 -suite /home/myComputer/test_suite.properties
java -jar secure-aem-VERSION.jar -a https://localhost:4502 -aCredentials admin:admin -suite /home/myComputer/test_suite.properties

## Writing own tests

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@
title="Test configuration"
xtype="panel">
<items jcr:primaryType="cq:WidgetCollection">
<title
jcr:primaryType="cq:Widget"
allowBlank="false"
fieldLabel="Credentials"
name="./testComponent/users"
xtype="textfield"/>
<items
jcr:primaryType="cq:Widget"
fieldDescription="Press + to add more items"
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@
title="Test configuration"
xtype="panel">
<items jcr:primaryType="cq:WidgetCollection">
<title
jcr:primaryType="cq:Widget"
allowBlank="false"
fieldLabel="Credentials"
name="./testComponent/users"
xtype="textfield"/>
<items
jcr:primaryType="cq:Widget"
fieldDescription="Press + to add more items"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,27 @@
fieldLabel="Author URL"
name="./author"
xtype="textfield"/>
<authorCredentials
jcr:primaryType="cq:Widget"
allowBlank="false"
fieldLabel="Author Credentials"
name="./authorCredentials"
defaultValue="admin:admin"
xtype="textfield"/>
<publish
jcr:primaryType="cq:Widget"
allowBlank="true"
fieldLabel="Publish URL"
name="./publish"
xtype="textfield"/>
<publishCredentials
jcr:primaryType="cq:Widget"
allowBlank="false"
fieldLabel="Publish Credentials"
name="./publishCredentials"
defaultValue="admin:admin"
xtype="textfield"/>

</items>
</configuration>
</items>
Expand Down
35 changes: 19 additions & 16 deletions src/main/java/com/cognifide/secureaem/AbstractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ public void test() throws IOException {

/**
* Perform test.
*
* @param url URL of the instance to test.
*
* @param url URL of the instance to test.
* @param instanceName Name of the instance (eg. author, publish or dispatcher).
* @return true if the test succeeded
* @throws Exception If you throw an exception, test result will be set to "Exception". You may throw
* special {@link AbstractTest.InvalidConfigurationException} with message if the test configuration isn't set
* correctly.
* special {@link AbstractTest.InvalidConfigurationException} with message if the test
* configuration isn't set correctly.
*/
protected abstract boolean doTest(String url, String instanceName) throws Exception;

Expand Down Expand Up @@ -100,9 +100,9 @@ private TestResult doTest() throws Exception {

/**
* Add information message, it'll be shown to the user.
*
*
* @param message Message can contain standard {@code String.format()} placeholders
* @param params Values to fill the placeholders.
* @param params Values to fill the placeholders.
*/
protected void addInfoMessage(String message, Object... params) {
String formatted = String.format(message, params);
Expand All @@ -111,27 +111,30 @@ protected void addInfoMessage(String message, Object... params) {

/**
* Add error message, it'll be shown to the user.
*
*
* @param message Message can contain standard {@code String.format()} placeholders
* @param params Values to fill the placeholders.
* @param params Values to fill the placeholders.
*/
protected void addErrorMessage(String message, Object... params) {
String formatted = String.format(message, params);
errorMessages.add(formatted);
}

/**
* Creates {@code UsernamePasswordCredentials} instance from component configuration.
* Creates {@code UsernamePasswordCredentials} instance from configuration.
* @param instance - instance name
* @return UsernamePasswordCredentials
* @throws IllegalArgumentException if credentials are not configured properly.
*/
protected UsernamePasswordCredentials getUserNamePasswordCredentials() {
String[] users = config.getStringList("users");
if (ArrayUtils.isEmpty(users)) {
throw new IllegalArgumentException("Invalid configuration");
protected UsernamePasswordCredentials getUsernamePasswordCredentials(String instance) {
UsernamePasswordCredentials credentials = null;
if (AuthorTest.ENVIRONMENT_NAME.equals(instance)) {
credentials = new UsernamePasswordCredentials(config.getAuthorLogin(),
config.getAuthorPassword());
} else if (PublishTest.ENVIRONMENT_NAME.equals(instance)) {
credentials = new UsernamePasswordCredentials(config.getPublishLogin(),
config.getPublishPassword());
}
String[] userInfo = UserHelper.splitUser(users[0]);
return new UsernamePasswordCredentials(userInfo[0], userInfo[1]);
return credentials;
}

public List<String> getInfoMessages() {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/cognifide/secureaem/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@ public interface Configuration {

String getAuthor();

String getAuthorLogin();

String getAuthorPassword();

String getPublish();

String getPublishLogin();

String getPublishPassword();

String getStringValue(String name, String defaultValue);

String[] getStringList(String name);
Expand Down
49 changes: 36 additions & 13 deletions src/main/java/com/cognifide/secureaem/cli/CliConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,65 @@

public class CliConfiguration implements Configuration {

public static final String DEFAULT_USER = "admin";

private final XmlConfigurationReader xmlConfigReader;

private final CommandLine cmdLine;

public CliConfiguration(XmlConfigurationReader xmlConfigReader, CommandLine cmdLine) throws IOException,
ParserConfigurationException, SAXException, URISyntaxException {
public CliConfiguration(XmlConfigurationReader xmlConfigReader, CommandLine cmdLine)
throws IOException, ParserConfigurationException, SAXException, URISyntaxException {
this.xmlConfigReader = xmlConfigReader;
this.cmdLine = cmdLine;
}

@Override
public String getDispatcherUrl() {
@Override public String getDispatcherUrl() {
return makeUrl(cmdLine.getOptionValue("d"));
}

@Override
public String getAuthor() {
@Override public String getAuthor() {
return makeUrl(cmdLine.getOptionValue("a"));
}

@Override
public String getPublish() {
@Override public String getAuthorLogin() {
return getCredentialsParameter("aCredentials", 0);
}

@Override public String getAuthorPassword() {
return getCredentialsParameter("aCredentials", 1);
}

@Override public String getPublish() {
return makeUrl(cmdLine.getOptionValue("p"));
}

@Override
public String getStringValue(String name, String defaultValue) {
@Override public String getPublishLogin() {
return getCredentialsParameter("pCredentials", 0);
}

@Override public String getPublishPassword() {
return getCredentialsParameter("pCredentials", 1);
}

@Override public String getStringValue(String name, String defaultValue) {
return StringUtils.defaultIfEmpty(xmlConfigReader.getValue(name), defaultValue);
}

@Override
public String[] getStringList(String name) {
@Override public String[] getStringList(String name) {
return xmlConfigReader.getValueList(name);
}

public static String makeUrl(String url) {
private String getCredentialsParameter(String credentialName, int parameterIndex) {
if (cmdLine.hasOption(credentialName)) {
String[] parameters = cmdLine.getOptionValue(credentialName).split(":");
if (parameters.length == 2) {
return parameters[parameterIndex];
}
}
return DEFAULT_USER;
}

private static String makeUrl(String url) {
if (StringUtils.isBlank(url)) {
return url;
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/cognifide/secureaem/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ private static CommandLine createOptions(String args[]) throws ParseException {
options.addOption("p", true, "publish URL");
options.addOption("d", true, "dispatcher URL");
options.addOption("suite", true, "test suite");
options.addOption("aCredentials", true, "author credentials");
options.addOption("pCredentials", true, "publish credentials");

CommandLineParser parser = new PosixParser();
return parser.parse(options, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.slf4j.LoggerFactory;

import com.cognifide.secureaem.Configuration;
import com.cognifide.secureaem.cli.CliConfiguration;

public class ResourceConfiguration implements Configuration {
private static final Logger LOG = LoggerFactory.getLogger(DefaultConfigurationProvider.class);
Expand Down Expand Up @@ -37,11 +38,27 @@ public String getAuthor() {
return StringUtils.removeEnd(getGlobalConfig("author"), "/");
}

@Override public String getAuthorLogin() {
return getCredentialsParameter("authorCredentials", 0);
}

@Override public String getAuthorPassword() {
return getCredentialsParameter("authorCredentials", 1);
}

@Override
public String getPublish() {
return StringUtils.removeEnd(getGlobalConfig("publish"), "/");
}

@Override public String getPublishLogin() {
return getCredentialsParameter("publishCredentials", 0);
}

@Override public String getPublishPassword() {
return getCredentialsParameter("publishCredentials", 1);
}

@Override
public String getStringValue(String name, String defaultValue) {
return getLocalConfig(name, defaultValue);
Expand All @@ -52,6 +69,14 @@ public String[] getStringList(String name) {
return getLocalConfig(name, ArrayUtils.EMPTY_STRING_ARRAY);
}

private String getCredentialsParameter(String credentialName, int parameterIndex) {
String[] parameters = getGlobalConfig(credentialName).split(":");
if(parameters.length == 2) {
return parameters[parameterIndex];
}
return CliConfiguration.DEFAULT_USER;
}

private String getGlobalConfig(String name) {
if (globalConfig == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public BundlesTest(Configuration config) {
}

@Override public boolean doTest(String url, String instanceName) throws Exception {
UsernamePasswordCredentials credentials = getUserNamePasswordCredentials();
UsernamePasswordCredentials credentials = getUsernamePasswordCredentials(instanceName);

String agentUrl = url + "/system/console/bundles.json";
HttpUriRequest request = new HttpGet(agentUrl);
Expand Down
Loading

0 comments on commit 0996a6b

Please sign in to comment.