Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public class ApiAttackTool implements Callable<Integer> {
@Option(names = {"-ph", "--proxyHost"}, description = "Specify the proxy host to send the requests via a proxy")
private String proxyHost;

@Option(names = {"-rpp", "--requestsPerParameter"}, description = "Specify the number of requests to send per the swagger's parameters (Strings excluded)")
private Integer numOfRequestsPerParameter;

@Option(names = {"-pp", "--proxyPort"}, description = "The proxy port")
private Integer proxyPort;

Expand Down Expand Up @@ -63,6 +66,10 @@ public Integer getProxyPort() {
return proxyPort;
}

public Integer getNumOfRequestsPerParameter() {
return numOfRequestsPerParameter;
}

public List<Integer> getUserProvidedPositiveResponseCodes() {
return userProvidedPositiveResponseCodes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class NegativeSinglePropertyScenarioTestDriver extends AbstractTestDriver implements TestDriver {
private Fuzzer firstStepFuzzer;
Expand All @@ -36,15 +38,25 @@ public NegativeSinglePropertyScenarioTestDriver(Fuzzer firstStepFuzzer,
}

@Override
public List<HttpRequestWrapper> getHttpRequestList(String resourceFileName) {
public List<HttpRequestWrapper> getHttpRequestList(String resourceFileName, int numOfRequestsPerParameter) {
List<EndpointModel> endpointModelList = parseSwagger(resourceFileName);
if (endpointModelList.isEmpty()) {
return Collections.emptyList();
}

List<EndpointValuedModel> endpointValuedModelList = getModelToValueConverter().endpointModelToEndpointValuedModel(endpointModelList);
List<EndpointValuedModel> modelsWithPolicyEnforced = getPolicyEnforcer().enforcePolicyOn(endpointValuedModelList);

List<EndpointValuedModel> fuzzedModelsWithPositiveValues = getBeforeMainEndpointModelProcessor().process(modelsWithPolicyEnforced, firstStepFuzzer);
// Generate more data based on the value numOfRequestsPerParameter
for (int i = 0; i < numOfRequestsPerParameter - 1; i++) {
fuzzedModelsWithPositiveValues = Stream.concat(
fuzzedModelsWithPositiveValues.stream(),
getMainEndpointModelProcessor().process(modelsWithPolicyEnforced, getFuzzer()).stream())
.collect(Collectors.toList()
);
}

List<EndpointValuedModel> fuzzedEndpointValuedModelList = getMainEndpointModelProcessor().process(fuzzedModelsWithPositiveValues, getFuzzer());
List<EndpointTestRequestData> endpointTestRequestDataList = getTestRequestDataConverter().processList(fuzzedEndpointValuedModelList);
List<HttpRequestWrapper> httpRequestWrapperList = getHttpRequestGenerator().generateFrom(endpointTestRequestDataList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static Object[][] negativeBadPropertyScenarioDataProvider(ITestContext co
private static Object[][] getEndpointTestRequestData(TestDriver testDriver) {
List<HttpRequestWrapper> httpRequestWrapperList;
try {
httpRequestWrapperList = testDriver.getHttpRequestList(TestConfiguration.getSpecFilePath());
httpRequestWrapperList = testDriver.getHttpRequestList(TestConfiguration.getSpecFilePath(), TestConfiguration.getNumOfRequestsPerParameter());
} catch (Exception anyException) {
logger.error("Failed to get httpRequestList, for file: {}", TestConfiguration.getSpecFilePath(), anyException);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class SingleValueScenarioTestDriver extends AbstractTestDriver implements TestDriver {

Expand All @@ -32,15 +34,25 @@ public SingleValueScenarioTestDriver(Fuzzer fuzzer,
}

@Override
public List<HttpRequestWrapper> getHttpRequestList(String resourceFileName) {
public List<HttpRequestWrapper> getHttpRequestList(String resourceFileName, int numOfRequestsPerParameter) {
List<EndpointModel> endpointModelList = parseSwagger(resourceFileName);
if (endpointModelList.isEmpty()) {
return Collections.emptyList();
}

List<EndpointValuedModel> endpointValuedModelList = getModelToValueConverter().endpointModelToEndpointValuedModel(endpointModelList);
List<EndpointValuedModel> modelsWithPolicyEnforced = getPolicyEnforcer().enforcePolicyOn(endpointValuedModelList);

List<EndpointValuedModel> fuzzedEndpointValuedModelList = getMainEndpointModelProcessor().process(modelsWithPolicyEnforced, getFuzzer());
// Generate more data based on the value numOfRequestsPerParameter
for (int i = 0; i < numOfRequestsPerParameter - 1; i++) {
fuzzedEndpointValuedModelList = Stream.concat(
fuzzedEndpointValuedModelList.stream(),
getMainEndpointModelProcessor().process(modelsWithPolicyEnforced, getFuzzer()).stream())
.collect(Collectors.toList()
);
}

List<EndpointTestRequestData> endpointTestRequestDataList = getTestRequestDataConverter().processList(fuzzedEndpointValuedModelList);
List<HttpRequestWrapper> httpRequestWrapperList = getHttpRequestGenerator().generateFrom(endpointTestRequestDataList);
return httpRequestWrapperList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class TestConfiguration {

private static final int DEFAULT_PROXY_PORT = 80;
private static final int DEFAULT_HOST_PORT = 443;
private static final int DEFAULT_NUM_OF_REQUESTS_PER_PARAMETER = 1;

private static String specFilePath = System.getProperty("specFile", null);

Expand All @@ -27,6 +28,8 @@ public class TestConfiguration {

private static Integer proxyPort = getIntegerFieldFromProperty("proxyPort", DEFAULT_PROXY_PORT);

private static Integer numOfRequestsPerParameter = getIntegerFieldFromProperty("numOfRequestsPerParameter", DEFAULT_NUM_OF_REQUESTS_PER_PARAMETER);

private static Collection<Integer> userProvidedPositiveResponseCodes = getIntegerListFromProperty("addPositiveRC", Collections.emptyList());

private static Collection<Integer> userProvidedNegativeResponseCodes = getIntegerListFromProperty("addNegativeRC", Collections.emptyList());
Expand All @@ -38,6 +41,7 @@ public static void initFrom(ApiAttackTool apiAttackToolOptions) {
hostPort = apiAttackToolOptions.getHostPort();
proxyHost = apiAttackToolOptions.getProxyHost();
proxyPort = apiAttackToolOptions.getProxyPort();
numOfRequestsPerParameter = apiAttackToolOptions.getNumOfRequestsPerParameter();
userProvidedPositiveResponseCodes =
apiAttackToolOptions.getUserProvidedPositiveResponseCodes() == null
? Collections.emptyList()
Expand All @@ -52,6 +56,10 @@ public static String getSpecFilePath() {
return specFilePath;
}

public static int getNumOfRequestsPerParameter() {
return numOfRequestsPerParameter == null ? DEFAULT_NUM_OF_REQUESTS_PER_PARAMETER : numOfRequestsPerParameter;
}

public static String getHostScheme() {
return hostScheme;
}
Expand Down Expand Up @@ -86,8 +94,8 @@ public static Collection<Integer> getUserProvidedNegativeResponseCodes() {

public static String getWorkingConfigurationString() {
return "API Spec file path: " + specFilePath + "\n"
+ "Host: (" + hostScheme + ") " + hostName + " : " + getHostPort() + "\n"
+ (isProxyDefined()
+ "Host: (" + hostScheme + ") " + hostName + " : " + getHostPort() + "\n"
+ (isProxyDefined()
? "Proxy Host: " + proxyHost + " : " + getProxyPort() + "\n"
: "");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
import java.util.List;

public interface TestDriver {
List<HttpRequestWrapper> getHttpRequestList(String resourceFileName);
List<HttpRequestWrapper> getHttpRequestList(String resourceFileName, int numOfRequestsPerParameter);
}