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/custom mini testing selection dashboard #2065

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
@@ -1,15 +1,20 @@
package com.akto.action.testing;

import com.akto.DaoInit;
import com.akto.action.ExportSampleDataAction;
import com.akto.action.AccountAction;
import com.akto.action.UserAction;
import com.akto.dao.RBACDao;
import com.akto.dao.AccountsDao;
import com.akto.dao.context.Context;
import com.akto.dao.test_editor.YamlTemplateDao;
import com.akto.dao.testing.sources.TestSourceConfigsDao;
import com.akto.dao.testing_run_findings.TestingRunIssuesDao;
import com.akto.dao.testing.*;
import com.akto.dto.Account;
import com.akto.dto.testing.config.EditableTestingRunConfig;
import com.akto.dto.ApiInfo;
import com.akto.dto.MiniTestingServiceHeartbeat;
import com.akto.dto.User;
import com.akto.dto.ApiToken.Utility;
import com.akto.dto.RBAC;
Expand All @@ -28,11 +33,13 @@
import com.akto.log.LoggerMaker.LogDb;
import com.akto.util.Constants;
import com.akto.util.DashboardMode;
import com.akto.util.Pair;
import com.akto.util.enums.GlobalEnums;
import com.akto.util.enums.GlobalEnums.TestErrorSource;
import com.akto.utils.DeleteTestRunUtils;
import com.akto.utils.Utils;
import com.google.gson.Gson;
import com.mongodb.ConnectionString;
import com.mongodb.client.model.*;
import com.mongodb.client.result.InsertOneResult;
import com.opensymphony.xwork2.Action;
Expand Down Expand Up @@ -114,7 +121,7 @@ private static List<ObjectId> getTestsWithSeverity(List<String> severities) {
private boolean sendSlackAlert = false;
private boolean sendMsTeamsAlert = false;

private TestingRun createTestingRun(int scheduleTimestamp, int periodInSeconds) {
private TestingRun createTestingRun(int scheduleTimestamp, int periodInSeconds, String miniTestingServiceName) {
User user = getSUser();

if (!StringUtils.isEmpty(this.overriddenTestAppUrl)) {
Expand Down Expand Up @@ -170,9 +177,10 @@ private TestingRun createTestingRun(int scheduleTimestamp, int periodInSeconds)

return new TestingRun(scheduleTimestamp, user.getLogin(),
testingEndpoints, testIdConfig, State.SCHEDULED, periodInSeconds, testName, this.testRunTime,
this.maxConcurrentRequests, this.sendSlackAlert, this.sendMsTeamsAlert);
this.maxConcurrentRequests, this.sendSlackAlert, this.sendMsTeamsAlert, miniTestingServiceName);
}

String selectedMiniTestingServiceName;
private List<String> selectedTests;
private List<TestConfigsAdvancedSettings> testConfigsAdvancedSettings;

Expand Down Expand Up @@ -213,7 +221,7 @@ public String startTest() {
}
if (localTestingRun == null) {
try {
localTestingRun = createTestingRun(scheduleTimestamp, this.recurringDaily ? 86400 : 0);
localTestingRun = createTestingRun(scheduleTimestamp, this.recurringDaily ? 86400 : 0, selectedMiniTestingServiceName);
// pass boolean from ui, which will tell if testing is coniinuous on new endpoints
if (this.continuousTesting) {
localTestingRun.setPeriodInSeconds(-1);
Expand Down Expand Up @@ -252,15 +260,15 @@ public String startTest() {
Updates.set(TestingRun.STATE, TestingRun.State.COMPLETED),
Updates.set(TestingRun.END_TIMESTAMP, Context.now())));
}

}

if (this.overriddenTestAppUrl != null || this.selectedTests != null) {
int id = UUID.randomUUID().hashCode() & 0xfffffff;
TestingRunConfig testingRunConfig = new TestingRunConfig(id, null, this.selectedTests, null, this.overriddenTestAppUrl, this.testRoleId);
this.testIdConfig = testingRunConfig.getId();
TestingRunConfigDao.instance.insertOne(testingRunConfig);
}
}

}

Expand Down Expand Up @@ -654,7 +662,7 @@ public String fetchTestRunResultsCount() {
return ERROR.toUpperCase();
}


int accountId = Context.accountId.get();

testCountMap = new HashMap<>();
Expand All @@ -681,9 +689,9 @@ public String fetchTestRunResultsCount() {
} catch (Exception e) {
e.printStackTrace();
}

}

} catch (Exception e) {
e.printStackTrace();
return ERROR.toUpperCase();
Expand Down Expand Up @@ -816,9 +824,9 @@ public String fetchVulnerableTestRunResults() {
}else{
testingRunResultList = TestingRunResultDao.instance.findAll(filters, skip, 50, null);
}





// Map<String, String> sampleDataVsCurlMap = new HashMap<>();
// for (TestingRunResult runResult: testingRunResultList) {
// WorkflowTest workflowTest = runResult.getWorkflowTest();
Expand Down Expand Up @@ -1171,31 +1179,31 @@ public String modifyTestingRunConfig(){
if (editableTestingRunConfig.getConfigsAdvancedSettings() != null && !editableTestingRunConfig.getConfigsAdvancedSettings().equals(existingTestingRunConfig.getConfigsAdvancedSettings())) {
updates.add(Updates.set(TestingRunConfig.TEST_CONFIGS_ADVANCED_SETTINGS, editableTestingRunConfig.getConfigsAdvancedSettings()));
}

if (editableTestingRunConfig.getTestSubCategoryList() != null &&
!editableTestingRunConfig.getTestSubCategoryList().equals(existingTestingRunConfig.getTestSubCategoryList())) {
updates.add(Updates.set(TestingRunConfig.TEST_SUBCATEGORY_LIST, editableTestingRunConfig.getTestSubCategoryList()));
}

if (editableTestingRunConfig.getTestRoleId() != null && !editableTestingRunConfig.getTestRoleId().equals(existingTestingRunConfig.getTestRoleId())) {
updates.add(Updates.set(TestingRunConfig.TEST_ROLE_ID, editableTestingRunConfig.getTestRoleId()));
}

if (editableTestingRunConfig.getOverriddenTestAppUrl() != null && !editableTestingRunConfig.getOverriddenTestAppUrl().equals(existingTestingRunConfig.getOverriddenTestAppUrl())) {
updates.add(Updates.set(TestingRunConfig.OVERRIDDEN_TEST_APP_URL, editableTestingRunConfig.getOverriddenTestAppUrl()));
}

if (!updates.isEmpty()) {
TestingRunConfigDao.instance.updateOne(
Filters.eq(Constants.ID, this.testingRunConfigId),
Updates.combine(updates)
Updates.combine(updates)
);
}

}

if (editableTestingRunConfig.getTestingRunHexId() != null) {

TestingRun existingTestingRun = TestingRunDao.instance.findOne(Filters.eq(Constants.ID, new ObjectId(editableTestingRunConfig.getTestingRunHexId())));

if (existingTestingRun != null) {
Expand Down Expand Up @@ -1234,15 +1242,15 @@ public String modifyTestingRunConfig(){
if (existingTestingRun.getPeriodInSeconds() != periodInSeconds) {
updates.add(Updates.set(TestingRun.PERIOD_IN_SECONDS, periodInSeconds));
}

if (!updates.isEmpty()) {
TestingRunDao.instance.updateOne(
Filters.eq(Constants.ID,new ObjectId(editableTestingRunConfig.getTestingRunHexId())),
Updates.combine(updates)
Updates.combine(updates)
);
}
}

}


Expand Down Expand Up @@ -1327,7 +1335,7 @@ public void run() {
Updates.set(TestingRunResult.IS_IGNORED_RESULT, true)
);
}

} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -1337,6 +1345,32 @@ public void run() {
return SUCCESS.toUpperCase();
}

private Set<String> miniTestingServiceNames;
private boolean isHybridTestingEnabled;
public String fetchMiniTestingServiceNames() {
int accountId = Context.accountId.get();
Account account = AccountsDao.instance.findOne(Filters.eq("_id", accountId), Projections.include(Account.HYBRID_TESTING_ENABLED, Account.MINI_TESTING_HEARTBEAT));
if(account == null || !account.getHybridTestingEnabled() || account.getMiniTestingHeartbeat() == null) {
return SUCCESS.toUpperCase();
}

List<MiniTestingServiceHeartbeat> miniTestingHeartbeats = account.getMiniTestingHeartbeat();
isHybridTestingEnabled = account.getHybridTestingEnabled();

miniTestingServiceNames = new HashSet<>();

for(MiniTestingServiceHeartbeat miniTestingHeartbeat : miniTestingHeartbeats) {
String miniTestingServiceName = miniTestingHeartbeat.getMiniTestingServiceName();
int miniTestingLastHeartbeat = miniTestingHeartbeat.getLastHeartbeatTimestamp();

if(Math.abs(Context.now() - miniTestingLastHeartbeat) <= 300) {
miniTestingServiceNames.add(miniTestingServiceName);
}
}

return SUCCESS.toUpperCase();
}


public void setType(TestingEndpoints.Type type) {
this.type = type;
Expand Down Expand Up @@ -1719,4 +1753,24 @@ public boolean getSendMsTeamsAlert() {
public void setSendMsTeamsAlert(boolean sendMsTeamsAlert) {
this.sendMsTeamsAlert = sendMsTeamsAlert;
}

public Set<String> getMiniTestingServiceNames() {
return miniTestingServiceNames;
}

public void setMiniTestingServiceNames(Set<String> miniTestingServiceNames) {
this.miniTestingServiceNames = miniTestingServiceNames;
}

public boolean getIsHybridTestingEnabled() {
return isHybridTestingEnabled;
}

public void setIsHybridTestingEnabled(boolean isHybridTestingEnabled) {
this.isHybridTestingEnabled = isHybridTestingEnabled;
}

public void setSelectedMiniTestingServiceName(String selectedMiniTestingServiceName) {
this.selectedMiniTestingServiceName = selectedMiniTestingServiceName;
}
}
21 changes: 21 additions & 0 deletions apps/dashboard/src/main/resources/struts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8126,6 +8126,27 @@
</result>
</action>

<action name="api/fetchMiniTestingServiceNames" class="com.akto.action.testing.StartTestAction" method="fetchMiniTestingServiceNames">
<interceptor-ref name="json"/>
<interceptor-ref name="defaultStack" />
<interceptor-ref name="roleAccessInterceptor">
<param name="featureLabel">TEST_RESULTS</param>
<param name="accessType">READ</param>
</interceptor-ref>

<result name="FORBIDDEN" type="json">
<param name="statusCode">403</param>
<param name="ignoreHierarchy">false</param>
<param name="includeProperties">^actionErrors.*</param>
</result>
<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>

</package>

</struts>
Original file line number Diff line number Diff line change
Expand Up @@ -573,20 +573,20 @@ export default {
data: {}
})
},
scheduleTestForCollection(apiCollectionId, startTimestamp, recurringDaily, selectedTests, testName, testRunTime, maxConcurrentRequests, overriddenTestAppUrl, testRoleId, continuousTesting, sendSlackAlert, sendMsTeamsAlert, testConfigsAdvancedSettings, cleanUpTestingResources) {
scheduleTestForCollection(apiCollectionId, startTimestamp, recurringDaily, selectedTests, testName, testRunTime, maxConcurrentRequests, overriddenTestAppUrl, testRoleId, continuousTesting, sendSlackAlert, sendMsTeamsAlert, testConfigsAdvancedSettings, cleanUpTestingResources, selectedMiniTestingServiceName) {
return request({
url: '/api/startTest',
method: 'post',
data: { apiCollectionId, type: "COLLECTION_WISE", startTimestamp, recurringDaily, selectedTests, testName, testRunTime, maxConcurrentRequests, overriddenTestAppUrl, testRoleId, continuousTesting, sendSlackAlert, sendMsTeamsAlert, testConfigsAdvancedSettings, cleanUpTestingResources}
data: { apiCollectionId, type: "COLLECTION_WISE", startTimestamp, recurringDaily, selectedTests, testName, testRunTime, maxConcurrentRequests, overriddenTestAppUrl, testRoleId, continuousTesting, sendSlackAlert, sendMsTeamsAlert, testConfigsAdvancedSettings, cleanUpTestingResources, selectedMiniTestingServiceName}
}).then((resp) => {
return resp
})
},
scheduleTestForCustomEndpoints(apiInfoKeyList, startTimestamp, recurringDaily, selectedTests, testName, testRunTime, maxConcurrentRequests, overriddenTestAppUrl, source, testRoleId, continuousTesting, sendSlackAlert, sendMsTeamsAlert, testConfigsAdvancedSettings, cleanUpTestingResources) {
scheduleTestForCustomEndpoints(apiInfoKeyList, startTimestamp, recurringDaily, selectedTests, testName, testRunTime, maxConcurrentRequests, overriddenTestAppUrl, source, testRoleId, continuousTesting, sendSlackAlert, sendMsTeamsAlert, testConfigsAdvancedSettings, cleanUpTestingResources, selectedMiniTestingServiceName) {
return request({
url: '/api/startTest',
method: 'post',
data: {apiInfoKeyList, type: "CUSTOM", startTimestamp, recurringDaily, selectedTests, testName, testRunTime, maxConcurrentRequests, overriddenTestAppUrl, source, testRoleId, continuousTesting, sendSlackAlert, sendMsTeamsAlert, testConfigsAdvancedSettings, cleanUpTestingResources}
data: {apiInfoKeyList, type: "CUSTOM", startTimestamp, recurringDaily, selectedTests, testName, testRunTime, maxConcurrentRequests, overriddenTestAppUrl, source, testRoleId, continuousTesting, sendSlackAlert, sendMsTeamsAlert, testConfigsAdvancedSettings, cleanUpTestingResources, selectedMiniTestingServiceName}
}).then((resp) => {
return resp
})
Expand Down
Loading
Loading