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

Hotfix/fix issues count after ignoring #1875

Merged
merged 2 commits into from
Dec 31, 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
20 changes: 18 additions & 2 deletions apps/testing/src/main/java/com/akto/testing/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ public void run() {
public static void main(String[] args) throws InterruptedException {
String mongoURI = System.getenv("AKTO_MONGO_CONN");
ReadPreference readPreference = ReadPreference.secondary();
if(DashboardMode.isOnPremDeployment()){
readPreference = ReadPreference.primary();
}
WriteConcern writeConcern = WriteConcern.W1;
DaoInit.init(new ConnectionString(mongoURI), readPreference, writeConcern);

Expand Down Expand Up @@ -356,9 +359,16 @@ public void run() {
loggerMaker.infoAndAddToDb("Testing run stopped");
if (trrs != null) {
loggerMaker.infoAndAddToDb("Stopping TRRS: " + trrs.getId());

// get count issues here
Map<String,Integer> finalCountMap = Utils.finalCountIssuesMap(trrs.getId());
loggerMaker.infoAndAddToDb("Final count map calculated is " + finalCountMap.toString());
TestingRunResultSummariesDao.instance.updateOneNoUpsert(
Filters.eq(Constants.ID, trrs.getId()),
Updates.set(TestingRunResultSummary.STATE, State.STOPPED)
Updates.combine(
Updates.set(TestingRunResultSummary.STATE, State.STOPPED),
Updates.set(TestingRunResultSummary.COUNT_ISSUES, finalCountMap)
)
);
loggerMaker.infoAndAddToDb("Stopped TRRS: " + trrs.getId());
}
Expand Down Expand Up @@ -441,10 +451,16 @@ public void run() {
+ testingRunResult.getHexId() + ", TRRS_ID:" + testingRunResultSummary.getHexId() + " TR_ID:" + testingRun.getHexId(), LogDb.TESTING);

int countFailedSummaries = (int) TestingRunResultSummariesDao.instance.count(filterCountFailed);
Bson updateForSummary = Updates.set(TestingRunResultSummary.STATE, State.FAILED);
Map<String,Integer> finalCountMap = Utils.finalCountIssuesMap(testingRunResultSummary.getId());
loggerMaker.infoAndAddToDb("Final count map calculated is " + finalCountMap.toString());
Bson updateForSummary = Updates.combine(
Updates.set(TestingRunResultSummary.STATE, State.FAILED),
Updates.set(TestingRunResultSummary.COUNT_ISSUES, finalCountMap)
);
if(countFailedSummaries >= (MAX_RETRIES_FOR_FAILED_SUMMARIES - 1)){
updateForSummary = Updates.combine(
Updates.set(TestingRunResultSummary.STATE, State.COMPLETED),
Updates.set(TestingRunResultSummary.COUNT_ISSUES, finalCountMap),
Updates.set(TestingRunResultSummary.END_TIMESTAMP, Context.now())
);
loggerMaker.infoAndAddToDb("Max retries level reached for TRR_ID: " + testingRun.getHexId(), LogDb.TESTING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,13 @@ public static void updateTestSummary(ObjectId summaryId){
options.returnDocument(ReturnDocument.AFTER);

State updatedState = GetRunningTestsStatus.getRunningTests().isTestRunning(summaryId, true) ? State.COMPLETED : GetRunningTestsStatus.getRunningTests().getCurrentState(summaryId);

Map<String,Integer> finalCountMap = Utils.finalCountIssuesMap(summaryId);
loggerMaker.infoAndAddToDb("Final count map calculated is " + finalCountMap.toString());
TestingRunResultSummary testingRunResultSummary = TestingRunResultSummariesDao.instance.getMCollection().withWriteConcern(WriteConcern.W1).findOneAndUpdate(
Filters.eq(Constants.ID, summaryId),
Updates.combine(
Updates.set(TestingRunResultSummary.END_TIMESTAMP, Context.now()),
Updates.set(TestingRunResultSummary.COUNT_ISSUES, finalCountMap),
Updates.set(TestingRunResultSummary.STATE, updatedState)),
options);
GithubUtils.publishGithubComments(testingRunResultSummary);
Expand Down
53 changes: 53 additions & 0 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.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -10,22 +11,38 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.bson.conversions.Bson;
import org.bson.types.ObjectId;
import com.akto.dao.testing.TestingRunResultDao;
import com.akto.dao.testing_run_findings.TestingRunIssuesDao;
import com.akto.dto.ApiInfo.ApiInfoKey;
import com.akto.dto.CollectionConditions.ConditionsType;
import com.akto.dto.OriginalHttpRequest;
import com.akto.dto.RawApi;
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.test_run_findings.TestingIssuesId;
import com.akto.dto.test_run_findings.TestingRunIssues;
import com.akto.dto.testing.TestingRunResult;
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.testing_utils.TestingUtils;
import com.akto.util.Constants;
import com.akto.util.JSONUtils;
import com.akto.util.enums.GlobalEnums;
import com.akto.util.enums.GlobalEnums.Severity;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.Accumulators;
import com.mongodb.client.model.Aggregates;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Projections;

import okhttp3.MediaType;

Expand Down Expand Up @@ -412,5 +429,41 @@ public static void modifyHeaderOperations(OriginalHttpRequest httpRequest, List<


}

public static Map<String, Integer> finalCountIssuesMap(ObjectId testingRunResultSummaryId){
Map<String, Integer> countIssuesMap = new HashMap<>();
countIssuesMap.put(Severity.HIGH.toString(), 0);
countIssuesMap.put(Severity.MEDIUM.toString(), 0);
countIssuesMap.put(Severity.LOW.toString(), 0);

Bson projection = Projections.include(TestingRunResult.API_INFO_KEY, TestingRunResult.TEST_SUB_TYPE);
Bson filterQ = Filters.and(
Filters.eq(TestingRunResult.TEST_RUN_RESULT_SUMMARY_ID, testingRunResultSummaryId),
Filters.eq(TestingRunResult.VULNERABLE, true)
);
List<TestingRunResult> allVulResults = TestingRunResultDao.instance.findAll(filterQ, projection);

Map<TestingIssuesId, TestingRunResult> testingIssuesIdsMap = TestingUtils.
listOfIssuesIdsFromTestingRunResults(allVulResults, true, false);

Bson inQuery = Filters.and(Filters.in(Constants.ID, testingIssuesIdsMap.keySet().toArray()), Filters.eq(TestingRunIssues.TEST_RUN_ISSUES_STATUS, GlobalEnums.TestRunIssueStatus.OPEN));
List<Bson> pipeline = new ArrayList<>();
pipeline.add(Aggregates.match(inQuery));
pipeline.add(Aggregates.group(
"$" + TestingRunIssues.KEY_SEVERITY, Accumulators.sum("count",1)
));


MongoCursor<BasicDBObject> cursor = TestingRunIssuesDao.instance.getMCollection().aggregate(pipeline, BasicDBObject.class).cursor();
while(cursor.hasNext()){
BasicDBObject dbObject = cursor.next();
String id = dbObject.getString("_id");
int val = dbObject.getInt("count");

countIssuesMap.put(id, val);
}

return countIssuesMap;
}

}
Loading