Skip to content

Commit

Permalink
instances username and passwords credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
mariuszkubis committed Sep 21, 2016
1 parent ef83b3c commit e32b2b4
Show file tree
Hide file tree
Showing 15 changed files with 138 additions and 142 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 http://localhost:4502
java -jar secure-aem-VERSION.jar -a http://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 `http://`, *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 http://localhost:4502 -suite /home/myComputer/test_suite.properties
java -jar secure-aem-VERSION.jar -a http://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
34 changes: 26 additions & 8 deletions src/main/java/com/cognifide/secureaem/AbstractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -54,13 +55,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 @@ -98,9 +99,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 @@ -109,15 +110,32 @@ 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 configuration.
* @param instance - instance name
* @return UsernamePasswordCredentials
*/
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());
}
return credentials;
}

public List<String> getInfoMessages() {
return infoMessages;
}
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 @@ -17,38 +17,61 @@ public class CliConfiguration implements Configuration {

private final CommandLine cmdLine;

public CliConfiguration(XmlConfigurationReader xmlConfigReader, CommandLine cmdLine) throws IOException,
ParserConfigurationException, SAXException, URISyntaxException {
private static final String DEFAULT_USER = "admin";

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 @@ -37,11 +37,43 @@ public String getAuthor() {
return StringUtils.removeEnd(getGlobalConfig("author"), "/");
}

@Override public String getAuthorLogin() {
String[] parameters = getGlobalConfig("authorCredentials").split(":");
if(parameters.length == 2) {
return parameters[0];
}
return StringUtils.EMPTY;
}

@Override public String getAuthorPassword() {
String[] parameters = getGlobalConfig("authorCredentials").split(":");
if(parameters.length == 2) {
return parameters[1];
}
return StringUtils.EMPTY;
}

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

@Override public String getPublishLogin() {
String[] parameters = getGlobalConfig("publishCredentials").split(":");
if(parameters.length == 2) {
return parameters[0];
}
return StringUtils.EMPTY;
}

@Override public String getPublishPassword() {
String[] parameters = getGlobalConfig("publishCredentials").split(":");
if(parameters.length == 2) {
return parameters[1];
}
return StringUtils.EMPTY;
}

@Override
public String getStringValue(String name, String defaultValue) {
return getLocalConfig(name, defaultValue);
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/com/cognifide/secureaem/tests/BundlesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,7 @@ public BundlesTest(Configuration config) {
}

@Override public boolean doTest(String url, String instanceName) throws Exception {
String[] users = config.getStringList("users");
if (ArrayUtils.isEmpty(users)) {
throw new IllegalArgumentException("Invalid configuration");
}
String[] userInfo = UserHelper.splitUser(users[0]);
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userInfo[0], userInfo[1]);
UsernamePasswordCredentials credentials = getUsernamePasswordCredentials(instanceName);

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

0 comments on commit e32b2b4

Please sign in to comment.