Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving advanced test configs to mini testing #1788

Merged
merged 1 commit into from
Dec 2, 2024
Merged
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
@@ -0,0 +1,43 @@
package com.akto.dto.CollectionConditions;

import java.util.Set;

public class ConditionsType {

private String key;
private String value;
private Set<String> urlsList;


public ConditionsType () {}

public ConditionsType (String key, String value, Set<String> urlsList) {
this.key = key;
this.value = value;
this.urlsList = urlsList ;
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public Set<String> getUrlsList() {
return urlsList;
}

public void setUrlsList(Set<String> urlsList) {
this.urlsList = urlsList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.akto.dto.CollectionConditions;

import java.util.List;

public class TestConfigsAdvancedSettings {

private String operatorType;
private List<ConditionsType> operationsGroupList;

public TestConfigsAdvancedSettings(){}

public TestConfigsAdvancedSettings(String operatorType, List<ConditionsType> operationsGroupList){
this.operationsGroupList = operationsGroupList;
this.operatorType = operatorType;
}

public String getOperatorType() {
return operatorType;
}

public void setOperatorType(String operatorType) {
this.operatorType = operatorType;
}

public List<ConditionsType> getOperationsGroupList() {
return operationsGroupList;
}

public void setOperationsGroupList(List<ConditionsType> operationsGroupList) {
this.operationsGroupList = operationsGroupList;
}

}
11 changes: 11 additions & 0 deletions libs/dao/src/main/java/com/akto/dto/testing/TestingRunConfig.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.akto.dto.testing;

import com.akto.dto.ApiInfo;
import com.akto.dto.CollectionConditions.TestConfigsAdvancedSettings;

import org.bson.codecs.pojo.annotations.BsonId;
import org.bson.codecs.pojo.annotations.BsonIgnore;
import org.bson.types.ObjectId;
Expand All @@ -21,6 +23,8 @@ public class TestingRunConfig {

private String testRoleId;
private String overriddenTestAppUrl;

private List<TestConfigsAdvancedSettings> configsAdvancedSettings;
public TestingRunConfig() {}
public TestingRunConfig(int id, Map<Integer, List<ApiInfo.ApiInfoKey>> collectionWiseApiInfoKey,
List<String> testSubCategoryList,
Expand Down Expand Up @@ -117,4 +121,11 @@ public void setStrAuthMechanismId(String strAuthMechanismId) {
this.strAuthMechanismId = strAuthMechanismId;
}

public List<TestConfigsAdvancedSettings> getConfigsAdvancedSettings() {
return configsAdvancedSettings;
}
public void setConfigsAdvancedSettings(List<TestConfigsAdvancedSettings> configsAdvancedSettings) {
this.configsAdvancedSettings = configsAdvancedSettings;
}

}
33 changes: 33 additions & 0 deletions libs/utils/src/main/java/com/akto/testing/ApiExecutor.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.akto.testing;

import com.akto.dao.context.Context;
import com.akto.dao.test_editor.TestEditorEnums;
import com.akto.dao.testing.config.TestScriptsDao;
import com.akto.data_actor.DataActor;
import com.akto.data_actor.DataActorFactory;
import com.akto.dto.OriginalHttpRequest;
import com.akto.dto.OriginalHttpResponse;
import com.akto.dto.CollectionConditions.ConditionsType;
import com.akto.dto.CollectionConditions.TestConfigsAdvancedSettings;
import com.akto.dto.testing.TestingRunConfig;
import com.akto.dto.testing.TestingRunResult;
import com.akto.dto.testing.config.TestScript;
Expand Down Expand Up @@ -285,6 +288,10 @@ public static OriginalHttpResponse sendRequest(OriginalHttpRequest request, bool
boolean executeScript = testingRunConfig != null;
calculateHashAndAddAuth(request, executeScript);

if(testingRunConfig.getConfigsAdvancedSettings() != null && !testingRunConfig.getConfigsAdvancedSettings().isEmpty()){
calculateFinalRequestFromAdvancedSettings(request, testingRunConfig.getConfigsAdvancedSettings());
}

boolean nonTestingContext = false;
if (testingRunConfig == null) {
nonTestingContext = true;
Expand Down Expand Up @@ -392,6 +399,32 @@ public void writeTo(BufferedSink sink) throws IOException {

}

private static void calculateFinalRequestFromAdvancedSettings(OriginalHttpRequest originalHttpRequest, List<TestConfigsAdvancedSettings> advancedSettings){
Map<String,List<ConditionsType>> headerConditions = new HashMap<>();
Map<String,List<ConditionsType>> payloadConditions = new HashMap<>();

for(TestConfigsAdvancedSettings settings: advancedSettings){
if(settings.getOperatorType().toLowerCase().contains("header")){
headerConditions.put(settings.getOperatorType(), settings.getOperationsGroupList());
}else{
payloadConditions.put(settings.getOperatorType(), settings.getOperationsGroupList());
}
}
List<ConditionsType> emptyList = new ArrayList<>();

Utils.modifyHeaderOperations(originalHttpRequest,
headerConditions.getOrDefault(TestEditorEnums.NonTerminalExecutorDataOperands.MODIFY_HEADER.name(), emptyList),
headerConditions.getOrDefault(TestEditorEnums.NonTerminalExecutorDataOperands.ADD_HEADER.name(), emptyList),
headerConditions.getOrDefault(TestEditorEnums.TerminalExecutorDataOperands.DELETE_HEADER.name(), emptyList)
);

Utils.modifyBodyOperations(originalHttpRequest,
payloadConditions.getOrDefault(TestEditorEnums.NonTerminalExecutorDataOperands.MODIFY_BODY_PARAM.name(), emptyList),
payloadConditions.getOrDefault(TestEditorEnums.NonTerminalExecutorDataOperands.ADD_BODY_PARAM.name(), emptyList),
payloadConditions.getOrDefault(TestEditorEnums.TerminalExecutorDataOperands.DELETE_BODY_PARAM.name(), emptyList)
);
}

private static void calculateHashAndAddAuth(OriginalHttpRequest originalHttpRequest, boolean executeScript) {
if (!executeScript) {
return;
Expand Down
88 changes: 86 additions & 2 deletions libs/utils/src/main/java/com/akto/testing/Utils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.akto.testing;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand All @@ -15,22 +16,27 @@

import com.akto.dto.ApiInfo.ApiInfoKey;
import com.akto.dto.OriginalHttpRequest;
import com.akto.dto.OriginalHttpResponse;
import com.akto.dto.RawApi;
import com.akto.dto.CollectionConditions.ConditionsType;
import com.akto.dto.test_editor.DataOperandsFilterResponse;
import com.akto.dto.test_editor.FilterNode;
import com.akto.dto.test_editor.Util;
import com.akto.dto.testing.WorkflowUpdatedSampleData;
import com.akto.dto.type.RequestTemplate;
import com.akto.log.LoggerMaker;
import com.akto.log.LoggerMaker.LogDb;
import com.akto.test_editor.filter.Filter;
import com.akto.test_editor.filter.data_operands_impl.ValidationResult;
import com.akto.util.JSONUtils;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;

import okhttp3.MediaType;

import static com.akto.runtime.RuntimeUtil.extractAllValuesFromPayload;;
import static com.akto.runtime.RuntimeUtil.extractAllValuesFromPayload;
import static com.akto.test_editor.Utils.deleteKeyFromPayload;
import static com.akto.test_editor.execution.Operations.deleteCookie;
import static com.akto.test_editor.execution.Operations.modifyCookie;

public class Utils {

Expand Down Expand Up @@ -368,5 +374,83 @@ private static ValidationResult validate(FilterNode node, RawApi rawApi, RawApi
DataOperandsFilterResponse dataOperandsFilterResponse = filter.isEndpointValid(node, rawApi, testRawApi, apiInfoKey, null, null , false,context, varMap, logId, false);
return new ValidationResult(dataOperandsFilterResponse.getResult(), dataOperandsFilterResponse.getValidationReason());
}

public static void modifyBodyOperations(OriginalHttpRequest httpRequest, List<ConditionsType> modifyOperations, List<ConditionsType> addOperations, List<ConditionsType> deleteOperations){
String oldReqBody = httpRequest.getBody();
if(oldReqBody == null || oldReqBody.isEmpty()){
return ;
}
BasicDBObject payload;

if (oldReqBody != null && oldReqBody.startsWith("[")) {
oldReqBody = "{\"json\": "+oldReqBody+"}";
}
try {
payload = BasicDBObject.parse(oldReqBody);
} catch (Exception e) {
payload = new BasicDBObject();
}

if(!modifyOperations.isEmpty()){
for(ConditionsType condition : modifyOperations){
Util.modifyValueInPayload(payload, null, condition.getKey(), condition.getValue());
}
}
if(!addOperations.isEmpty()){
for(ConditionsType condition : addOperations){
payload.put(condition.getKey(), condition.getValue());
}
}

if(!deleteOperations.isEmpty()){
for(ConditionsType condition : deleteOperations){
deleteKeyFromPayload(payload, null, condition.getKey());
}
}

String payloadStr = payload.toJson();

if (payload.size() == 1 && payload.containsKey("json")) {
Object jsonValue = payload.get("json");
if (jsonValue instanceof BasicDBList) {
payloadStr = payload.get("json").toString();
}
}


httpRequest.setBody(payloadStr);
}

public static void modifyHeaderOperations(OriginalHttpRequest httpRequest, List<ConditionsType> modifyOperations, List<ConditionsType> addOperations, List<ConditionsType> deleteOperations){
Map<String, List<String>> reqHeaders = httpRequest.getHeaders();

if(!addOperations.isEmpty()){
for(ConditionsType condition : addOperations){
List<String> valList = Collections.singletonList(condition.getValue());
reqHeaders.put(condition.getKey(), valList);
}
}

if(!deleteOperations.isEmpty()){
for(ConditionsType condition : deleteOperations){
String key = condition.getKey();
deleteCookie(reqHeaders, key, null);
if (reqHeaders.containsKey(key)) {
reqHeaders.remove(key);
}
}
}

if(!modifyOperations.isEmpty()){
for(ConditionsType condition : modifyOperations){
String key = condition.getKey();
modifyCookie(reqHeaders, key, condition.getValue());
List<String> valList = Collections.singletonList(condition.getValue());
reqHeaders.put(condition.getKey(), valList);
}
}


}

}
Loading