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

Feature/add script calculation #1723

Merged
merged 5 commits into from
Nov 24, 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
Expand Up @@ -16,18 +16,8 @@
import com.akto.dto.test_editor.YamlTemplate;
import com.akto.dto.test_run_findings.TestingIssuesId;
import com.akto.dto.test_run_findings.TestingRunIssues;
import com.akto.dto.testing.AccessMatrixTaskInfo;
import com.akto.dto.testing.AccessMatrixUrlToRole;
import com.akto.dto.testing.CollectionWiseTestingEndpoints;
import com.akto.dto.testing.CustomTestingEndpoints;
import com.akto.dto.testing.EndpointLogicalGroup;
import com.akto.dto.testing.TestRoles;
import com.akto.dto.testing.TestingRun;
import com.akto.dto.testing.TestingRunConfig;
import com.akto.dto.testing.TestingRunResult;
import com.akto.dto.testing.TestingRunResultSummary;
import com.akto.dto.testing.WorkflowTest;
import com.akto.dto.testing.WorkflowTestResult;
import com.akto.dto.testing.*;
import com.akto.dto.testing.config.TestScript;
import com.akto.dto.testing.sources.TestSourceConfig;
import com.akto.dto.traffic.SampleData;
import com.akto.dto.traffic.TrafficInfo;
Expand Down Expand Up @@ -86,6 +76,7 @@ public class DbAction extends ActionSupport {
List<BulkUpdates> writesForTrafficMetrics;
List<BulkUpdates> writesForTestingRunIssues;
List<DependencyNode> dependencyNodeList;
TestScript testScript;

public List<BulkUpdates> getWritesForTestingRunIssues() {
return writesForTestingRunIssues;
Expand Down Expand Up @@ -1698,6 +1689,16 @@ public String createCollectionForHostAndVpc() {
return Action.SUCCESS.toUpperCase();
}

public String fetchTestScript() {
try {
testScript = DbLayer.fetchTestScript();
return SUCCESS.toUpperCase();
} catch (Exception e) {
System.out.println("Error in fetchTestScript " + e.toString());
return Action.ERROR.toUpperCase();
}
}

public String countTestingRunResultSummaries() {
count = DbLayer.countTestingRunResultSummaries(filter);
return Action.SUCCESS.toUpperCase();
Expand Down Expand Up @@ -2613,6 +2614,10 @@ public void setVpcId(String vpcId) {
this.vpcId = vpcId;
}

public TestScript getTestScript() {
return testScript;
}

public void setFilter(Bson filter) {
this.filter = filter;
}
Expand Down
11 changes: 11 additions & 0 deletions apps/database-abstractor/src/main/resources/struts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,17 @@
</result>
</action>

<action name="api/fetchTestScript" class="com.akto.action.DbAction" method="fetchTestScript">
<interceptor-ref name="json"/>
<interceptor-ref name="defaultStack" />
<result name="SUCCESS" type="json"/>
<result name="ERROR" type="json">
<param name="statusCode">422</param>
<param name="ignoreHierarchy">false</param>
<param name="includeProperties">^actionErrors.*</param>
</result>
</action>

<action name="api/countTestingRunResultSummaries" class="com.akto.action.DbAction" method="countTestingRunResultSummaries">
<interceptor-ref name="json"/>
<interceptor-ref name="defaultStack" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.akto.dao.testing.config;

import com.akto.dao.*;
import com.akto.dto.testing.config.TestScript;
import com.mongodb.BasicDBObject;

public class TestScriptsDao extends AccountsContextDao<TestScript> {

public static final TestScriptsDao instance = new TestScriptsDao();

private TestScriptsDao() {}

public TestScript fetchTestScript() {
return TestScriptsDao.instance.findOne(new BasicDBObject());
}

@Override
public String getCollName() {
return "test_collection_properties";
}

@Override
public Class<TestScript> getClassT() {
return TestScript.class;
}
}
13 changes: 9 additions & 4 deletions libs/dao/src/main/java/com/akto/dto/OriginalHttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,16 @@ public static String extractAktoUUid(String message) {
}

public String getPath(){
String path = URI.create(this.url).getPath();
if (path == null || path.isEmpty()) {
return "/";
try {
String path = URI.create(this.url).getPath();
if (path == null || path.isEmpty()) {
return "/";
}
return path;
} catch (Exception e) {
String strippedUrl = this.url.replaceAll("^(https?://[^/]+)", "");
return strippedUrl.isEmpty() ? "/" : strippedUrl;
}
return path;
}

@Override
Expand Down
122 changes: 122 additions & 0 deletions libs/dao/src/main/java/com/akto/dto/testing/config/TestScript.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package com.akto.dto.testing.config;
import java.util.Objects;

import org.bson.codecs.pojo.annotations.BsonId;

import com.akto.dao.context.Context;

public class TestScript {


public enum Type {
PRE_TEST, PRE_REQUEST, POST_REQUEST, POST_TEST
}

public static final String ID = "_id";
@BsonId
private String id;

public static final String JAVASCRIPT = "javascript";
private String javascript;

private Type type;

public static final String AUTHOR = "author";
private String author;

private int lastCreatedAt;

public static final String LAST_UPDATED_AT = "lastUpdatedAt";
private int lastUpdatedAt;


public TestScript() {
}

public TestScript(String id, String javascript, Type type, String author, int lastUpdatedAt) {
this.id = id;
this.javascript = javascript;
this.type = type;
this.author = author;
this.lastCreatedAt = Context.now();
this.lastUpdatedAt = lastUpdatedAt;
}

public String getJavascript() {
return this.javascript;
}

public void setJavascript(String javascript) {
this.javascript = javascript;
}

public Type getType() {
return this.type;
}

public void setType(Type type) {
this.type = type;
}

public String getAuthor() {
return this.author;
}

public void setAuthor(String author) {
this.author = author;
}

public int getLastCreatedAt() {
return this.lastCreatedAt;
}

public void setLastCreatedAt(int lastCreatedAt) {
this.lastCreatedAt = lastCreatedAt;
}

public int getLastUpdatedAt() {
return this.lastUpdatedAt;
}

public void setLastUpdatedAt(int lastUpdatedAt) {
this.lastUpdatedAt = lastUpdatedAt;
}

public String getId() {
return this.id;
}

public void setId(String id) {
this.id = id;
}

@Override
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof TestScript)) {
return false;
}
TestScript testScript = (TestScript) o;
return Objects.equals(javascript, testScript.javascript) && Objects.equals(type, testScript.type) && Objects.equals(author, testScript.author) && lastCreatedAt == testScript.lastCreatedAt && lastUpdatedAt == testScript.lastUpdatedAt;
}

@Override
public int hashCode() {
return Objects.hash(javascript, type, author, lastCreatedAt, lastUpdatedAt);
}

@Override
public String toString() {
return "{" +
" id='" + getId() + "'" +
", javascript='" + getJavascript() + "'" +
", type='" + getType() + "'" +
", author='" + getAuthor() + "'" +
", lastCreatedAt='" + getLastCreatedAt() + "'" +
", lastUpdatedAt='" + getLastUpdatedAt() + "'" +
"}";
}


}
30 changes: 29 additions & 1 deletion libs/utils/src/main/java/com/akto/data_actor/ClientActor.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.akto.dto.testing.TestingRunResultSummary;
import com.akto.dto.testing.WorkflowTest;
import com.akto.dto.testing.WorkflowTestResult;
import com.akto.dto.testing.config.TestScript;
import com.akto.dto.testing.sources.TestSourceConfig;
import com.akto.dto.traffic.SampleData;
import com.akto.dto.type.SingleTypeInfo;
Expand Down Expand Up @@ -3442,7 +3443,7 @@ public long countTestingRunResultSummaries(Bson filter) {
Map<String, List<String>> headers = buildHeaders();
OriginalHttpRequest request = new OriginalHttpRequest(url + "/countTestingRunResultSummaries", "", "POST", obj.toString(), headers, "");
try {
OriginalHttpResponse response = ApiExecutor.sendRequestBackOff(request, true, null, false, null);
OriginalHttpResponse response = ApiExecutor.sendRequest(request, true, null, false, null);
String responsePayload = response.getBody();
if (response.getStatusCode() != 200 || responsePayload == null) {
loggerMaker.errorAndAddToDb("non 2xx response in countTestingRunResultSummaries", LoggerMaker.LogDb.TESTING);
Expand All @@ -3462,4 +3463,31 @@ public long countTestingRunResultSummaries(Bson filter) {
}
}

public TestScript fetchTestScript(){
TestScript testScript = null;

Map<String, List<String>> headers = buildHeaders();
OriginalHttpRequest request = new OriginalHttpRequest(url + "/fetchTestScript", "", "GET", null, headers, "");
try {
OriginalHttpResponse response = ApiExecutor.sendRequest(request, true, null, false, null);
String responsePayload = response.getBody();
if (response.getStatusCode() != 200 || responsePayload == null) {
loggerMaker.errorAndAddToDb("invalid response in fetchTestScript", LoggerMaker.LogDb.RUNTIME);
return testScript;
}
BasicDBObject payloadObj;

try {
payloadObj = BasicDBObject.parse(responsePayload);
BasicDBObject testScriptObj = (BasicDBObject) payloadObj.get("testScript");
testScript = objectMapper.readValue(testScriptObj.toJson(), TestScript.class);
} catch (Exception e) {
loggerMaker.errorAndAddToDb("error extracting response in fetchTestScript" + e, LoggerMaker.LogDb.RUNTIME);
}
} catch (Exception e) {
loggerMaker.errorAndAddToDb("error in fetchTestScript" + e, LoggerMaker.LogDb.RUNTIME);
}
return testScript;
}

}
3 changes: 3 additions & 0 deletions libs/utils/src/main/java/com/akto/data_actor/DataActor.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.akto.dto.testing.TestingRunResultSummary;
import com.akto.dto.testing.WorkflowTest;
import com.akto.dto.testing.WorkflowTestResult;
import com.akto.dto.testing.config.TestScript;
import com.akto.dto.testing.sources.TestSourceConfig;
import com.akto.dto.traffic.SampleData;
import com.akto.dto.type.SingleTypeInfo;
Expand Down Expand Up @@ -261,4 +262,6 @@ public abstract class DataActor {

public abstract long countTestingRunResultSummaries(Bson filter);

public abstract TestScript fetchTestScript();

}
5 changes: 5 additions & 0 deletions libs/utils/src/main/java/com/akto/data_actor/DbActor.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.akto.dto.testing.TestRoles;
import com.akto.dto.testing.TestingRun;
import com.akto.dto.testing.TestingRun.State;
import com.akto.dto.testing.config.TestScript;
import com.akto.dto.testing.TestingRunConfig;
import com.akto.dto.testing.TestingRunResult;
import com.akto.dto.testing.TestingRunResultSummary;
Expand Down Expand Up @@ -542,4 +543,8 @@ public long countTestingRunResultSummaries(Bson filter){
return DbLayer.countTestingRunResultSummaries(filter);
}

public TestScript fetchTestScript(){
return DbLayer.fetchTestScript();
}

}
8 changes: 8 additions & 0 deletions libs/utils/src/main/java/com/akto/data_actor/DbLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import com.akto.dao.testing.TestingRunResultSummariesDao;
import com.akto.dao.testing.WorkflowTestResultsDao;
import com.akto.dao.testing.WorkflowTestsDao;
import com.akto.dao.testing.config.TestCollectionPropertiesDao;
import com.akto.dao.testing.config.TestScriptsDao;
import com.akto.dao.testing.sources.TestSourceConfigsDao;
import com.akto.dao.testing_run_findings.TestingRunIssuesDao;
import com.akto.dao.traffic_metrics.TrafficMetricsDao;
Expand All @@ -62,6 +64,7 @@
import com.akto.dto.testing.WorkflowTest;
import com.akto.dto.testing.WorkflowTestResult;
import com.akto.dto.testing.TestingRun.State;
import com.akto.dto.testing.config.TestScript;
import com.akto.dto.testing.sources.TestSourceConfig;
import com.akto.dto.traffic.SampleData;
import com.akto.dto.traffic.TrafficInfo;
Expand Down Expand Up @@ -1014,4 +1017,9 @@ public static long countTestingRunResultSummaries(Bson filter){
return TestingRunResultSummariesDao.instance.count(filter);
}


public static TestScript fetchTestScript(){
return TestScriptsDao.instance.fetchTestScript();
}

}
Loading
Loading