Skip to content

Commit

Permalink
Make Decryption postprocessor fail sample on exception
Browse files Browse the repository at this point in the history
  • Loading branch information
tilln committed Nov 12, 2017
1 parent c0607d8 commit 8c50e40
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
package nz.co.breakpoint.jmeter.modifiers;

import javax.xml.parsers.ParserConfigurationException;
import org.apache.jmeter.assertions.AssertionResult;
import org.apache.jmeter.processor.PostProcessor;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.testbeans.TestBean;
import org.apache.jmeter.testelement.AbstractTestElement;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
import java.io.StringReader;
import java.util.Properties;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.wss4j.common.ext.WSSecurityException;
import org.apache.wss4j.common.util.XMLUtils;
import org.apache.wss4j.dom.message.WSSecHeader;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

/**
* Abstract base class for any postprocessor that validates/decrypts a SOAP WSS header in the sampler response.
Expand All @@ -26,6 +19,8 @@ public abstract class AbstractWSSecurityPostProcessor extends AbstractXMLTestEle

private static final Logger log = LoggingManager.getLoggerForClass();

static final String FAIL_ON_WSS_EXCEPTION = "jmeter.wssecurity.failSamplerOnWSSException";

public AbstractWSSecurityPostProcessor() throws ParserConfigurationException {
super();
}
Expand Down Expand Up @@ -57,6 +52,13 @@ public void process() {
}
catch (Exception e) {
log.error("Processing failed! ", e);
if (e instanceof WSSecurityException && JMeterUtils.getPropDefault(FAIL_ON_WSS_EXCEPTION, true)) {
AssertionResult assertionResult = new AssertionResult("WSSecurityException").setResultForFailure(e.getMessage());
assertionResult.setError(true);
assertionResult.setFailure(true);
prev.addAssertionResult(assertionResult);
prev.setSuccessful(false);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,13 @@
import org.apache.jmeter.processor.PreProcessor;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.testbeans.TestBean;
import org.apache.jmeter.testelement.AbstractTestElement;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
import java.io.StringReader;
import java.util.Properties;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.wss4j.common.ext.WSSecurityException;
import org.apache.wss4j.common.util.XMLUtils;
import org.apache.wss4j.dom.message.WSSecHeader;
import org.apache.wss4j.dom.message.WSSecBase;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

/**
* Abstract base class for any preprocessor that creates/modifies a SOAP WSS header in the sampler payload.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ public abstract class CryptoWSSecurityPostProcessor extends AbstractWSSecurityPo

private static final Logger log = LoggingManager.getLoggerForClass();

private final Properties cryptoProps; // Holds configured attributes for crypto instance
private final Properties cryptoProps = new Properties(); // Holds configured attributes for crypto instance

private String certPassword;

public CryptoWSSecurityPostProcessor() throws ParserConfigurationException {
super();
cryptoProps = new Properties();
}

protected Crypto getCrypto() throws WSSecurityException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import static org.apache.wss4j.common.crypto.Merlin.PREFIX;
import static org.apache.wss4j.common.crypto.Merlin.KEYSTORE_FILE;
import static org.apache.wss4j.common.crypto.Merlin.KEYSTORE_PASSWORD;
import static org.apache.wss4j.common.crypto.Merlin.KEYSTORE_TYPE;

/**
* Abstract parent class of any preprocessors that perform crypto operations (e.g. signature or encryption).
Expand All @@ -24,7 +23,7 @@ public abstract class CryptoWSSecurityPreProcessor extends AbstractWSSecurityPre

private static final Logger log = LoggingManager.getLoggerForClass();

private final Properties cryptoProps; // Holds configured attributes for crypto instance
private final Properties cryptoProps = new Properties(); // Holds configured attributes for crypto instance

private List<SecurityPart> partsToSecure; // Holds the names of XML elements to secure (e.g. SOAP Body)

Expand All @@ -43,9 +42,6 @@ public abstract class CryptoWSSecurityPreProcessor extends AbstractWSSecurityPre

public CryptoWSSecurityPreProcessor() throws ParserConfigurationException {
super();
cryptoProps = new Properties();
cryptoProps.setProperty("org.apache.wss4j.crypto.provider", "org.apache.wss4j.common.crypto.Merlin");
cryptoProps.setProperty(PREFIX+KEYSTORE_TYPE, "jks");
}

/* Reverse lookup for above keyIdentifierMap. Mainly used for populating the GUI dropdown.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package nz.co.breakpoint.jmeter.modifiers;

import java.beans.PropertyDescriptor;
import org.apache.wss4j.dom.WSConstants;

public class WSSDecryptionPostProcessorBeanInfo extends CryptoWSSecurityPostProcessorBeanInfo {

public WSSDecryptionPostProcessorBeanInfo() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nz.co.breakpoint.jmeter.modifiers;

import javax.xml.parsers.ParserConfigurationException;
import org.apache.wss4j.common.crypto.Crypto;
import org.apache.wss4j.common.ext.WSSecurityException;
import org.apache.wss4j.dom.WSConstants;
import org.apache.wss4j.dom.message.WSSecBase;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nz.co.breakpoint.jmeter.modifiers;

import java.beans.PropertyDescriptor;
import org.apache.wss4j.dom.WSConstants;

public class WSSEncryptionPreProcessorBeanInfo extends CryptoWSSecurityPreProcessorBeanInfo {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nz.co.breakpoint.jmeter.modifiers;

import javax.xml.parsers.ParserConfigurationException;
import org.apache.wss4j.common.crypto.Crypto;
import org.apache.wss4j.common.ext.WSSecurityException;
import org.apache.wss4j.dom.WSConstants;
import org.apache.wss4j.dom.message.WSSecBase;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nz.co.breakpoint.jmeter.modifiers;

import java.beans.PropertyDescriptor;
import org.apache.wss4j.dom.WSConstants;

public class WSSSignaturePreProcessorBeanInfo extends CryptoWSSecurityPreProcessorBeanInfo {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import javax.xml.parsers.ParserConfigurationException;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
import org.apache.wss4j.common.crypto.Crypto;
import org.apache.wss4j.common.ext.WSSecurityException;
import org.apache.wss4j.dom.message.WSSecBase;
import org.apache.wss4j.dom.message.WSSecHeader;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package nz.co.breakpoint.jmeter.modifiers;

import javax.xml.parsers.ParserConfigurationException;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
import org.apache.jmeter.protocol.jms.sampler.JMSSampler;
import org.apache.jmeter.threads.JMeterContext;
import org.apache.jmeter.threads.JMeterContextService;
import org.apache.wss4j.common.ext.WSSecurityException;
import org.apache.wss4j.dom.message.WSSecHeader;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package nz.co.breakpoint.jmeter.modifiers;

import java.beans.PropertyDescriptor;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerFactory;
import org.apache.jmeter.protocol.jms.sampler.JMSSampler;
import org.apache.jmeter.util.JMeterUtils;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class TestSamplerPayloadAccessor {
@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
package nz.co.breakpoint.jmeter.modifiers;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.hamcrest.CoreMatchers.containsString;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.apache.jmeter.assertions.AssertionResult;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.threads.JMeterContext;
import org.apache.jmeter.threads.JMeterContextService;
import org.apache.jmeter.util.JMeterUtils;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class TestWSSDecryptionPostProcessor {
private WSSDecryptionPostProcessor mod = null;
private JMeterContext context = null;
private SampleResult result = null;

@BeforeClass
public static void setUpClass() throws IOException {
File propsFile = File.createTempFile("jmeter-wssecurity-test-", ".properties");
propsFile.deleteOnExit();
JMeterUtils.loadJMeterProperties(propsFile.getAbsolutePath());
}

@Before
public void setUp() throws Exception {
Expand All @@ -23,15 +39,57 @@ public void setUp() throws Exception {
mod.setKeystoreFile("src/test/resources/keystore.jks");
mod.setKeystorePassword("changeit");
mod.setCertPassword("changeit");
result = new SampleResult();
result.setSuccessful(true);
}

@Test
public void testDecryption() throws Exception {
SampleResult result = new SampleResult();
result.setResponseData(Files.readAllBytes(Paths.get("src/test/resources/encrypted-body.xml")));
context.setPreviousResult(result);
mod.process();
String decrypted = result.getResponseDataAsString();
assertThat(decrypted, containsString("<add xmlns=\"http://ws.apache.org/counter/counter_port_type\">"));
}

@Test
public void testFailureOnWSSException() throws Exception {
result.setResponseData("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" >"
+ "<SOAP-ENV:Header>"
+ "<wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" >"
+ "<ds:Signature xmlns:ds=\"http://www.w3.org/2000/09/xmldsig#\" >"
+ "<ds:KeyInfo />"
+ "</ds:Signature>"
+ "</wsse:Security>"
+ "</SOAP-ENV:Header>"
+ "<SOAP-ENV:Body />"
+"</SOAP-ENV:Envelope>",
"UTF-8");
context.setPreviousResult(result);

JMeterUtils.setProperty(AbstractWSSecurityPostProcessor.FAIL_ON_WSS_EXCEPTION, "false");
mod.process();
assertTrue(result.isSuccessful());
assertEquals(0, result.getAssertionResults().length);

JMeterUtils.setProperty(AbstractWSSecurityPostProcessor.FAIL_ON_WSS_EXCEPTION, "true");
mod.process();
assertFalse(result.isSuccessful());
AssertionResult[] assertionResults = result.getAssertionResults();
assertEquals(1, assertionResults.length);
assertEquals("WSSecurityException", assertionResults[0].getName());
assertTrue(assertionResults[0].isError());
assertThat(assertionResults[0].getFailureMessage(), containsString("Any SIG_KEY_INFO MUST contain exactly one child element"));
}

@Test
public void testNoFailureOnOtherException() throws Exception {
JMeterUtils.setProperty(AbstractWSSecurityPostProcessor.FAIL_ON_WSS_EXCEPTION, "true");
result.setResponseData("<dummy />", "UTF-8");
context.setPreviousResult(result);
mod.process();
assertTrue(result.isSuccessful());
AssertionResult[] assertionResults = result.getAssertionResults();
assertEquals(0, assertionResults.length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static org.hamcrest.CoreMatchers.containsString;

import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
import org.apache.jmeter.threads.JMeterContext;
import org.apache.jmeter.threads.JMeterContextService;
import org.junit.Before;
import org.junit.Test;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package nz.co.breakpoint.jmeter.modifiers;

import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.containsString;

import org.apache.jmeter.protocol.http.sampler.HTTPSamplerFactory;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
import org.apache.jmeter.threads.JMeterContext;
import org.apache.jmeter.threads.JMeterContextService;
import org.junit.Before;
import org.junit.Test;

public class TestWSSSecurityPreProcessorBase {
protected JMeterContext context = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static org.hamcrest.CoreMatchers.containsString;

import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
import org.apache.jmeter.threads.JMeterContext;
import org.apache.jmeter.threads.JMeterContextService;
import org.junit.Before;
import org.junit.Test;
Expand Down

0 comments on commit 8c50e40

Please sign in to comment.