-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1112 from akto-api-security/develop
Develop
- Loading branch information
Showing
11 changed files
with
416 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
apps/dashboard/src/main/java/com/akto/utils/RiskScoreTestingEndpointsUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.akto.utils; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import com.akto.dao.context.Context; | ||
import com.akto.dto.ApiCollectionUsers; | ||
import com.akto.dto.ApiInfo; | ||
import com.akto.dto.testing.RiskScoreTestingEndpoints; | ||
import com.akto.dto.testing.TestingEndpoints; | ||
import com.akto.log.LoggerMaker; | ||
import com.akto.log.LoggerMaker.LogDb; | ||
|
||
public class RiskScoreTestingEndpointsUtils { | ||
private static final LoggerMaker loggerMaker = new LoggerMaker(RiskScoreTestingEndpointsUtils.class); | ||
|
||
private Map<RiskScoreTestingEndpoints.RiskScoreGroupType, List<ApiInfo>> removeApisFromRiskScoreGroupMap = new HashMap<RiskScoreTestingEndpoints.RiskScoreGroupType, List<ApiInfo>>() {{ | ||
put(RiskScoreTestingEndpoints.RiskScoreGroupType.LOW, new ArrayList<>()); | ||
put(RiskScoreTestingEndpoints.RiskScoreGroupType.MEDIUM, new ArrayList<>()); | ||
put(RiskScoreTestingEndpoints.RiskScoreGroupType.HIGH, new ArrayList<>()); | ||
}}; | ||
|
||
|
||
private Map<RiskScoreTestingEndpoints.RiskScoreGroupType, List<ApiInfo>> addApisToRiskScoreGroupMap = new HashMap<RiskScoreTestingEndpoints.RiskScoreGroupType, List<ApiInfo>>() {{ | ||
put(RiskScoreTestingEndpoints.RiskScoreGroupType.LOW, new ArrayList<>()); | ||
put(RiskScoreTestingEndpoints.RiskScoreGroupType.MEDIUM, new ArrayList<>()); | ||
put(RiskScoreTestingEndpoints.RiskScoreGroupType.HIGH, new ArrayList<>()); | ||
}}; | ||
|
||
private static final ExecutorService executorService = Executors.newFixedThreadPool(1); | ||
|
||
public RiskScoreTestingEndpointsUtils() { | ||
} | ||
|
||
public void updateApiRiskScoreGroup(ApiInfo apiInfo, float updatedRiskScore) { | ||
float oldRiskScore = apiInfo.getRiskScore(); | ||
|
||
RiskScoreTestingEndpoints.RiskScoreGroupType removeRiskScoreGroupType = RiskScoreTestingEndpoints.calculateRiskScoreGroup(oldRiskScore); | ||
removeApisFromRiskScoreGroupMap.get(removeRiskScoreGroupType).add(apiInfo); | ||
|
||
RiskScoreTestingEndpoints.RiskScoreGroupType addRiskScoreGroupType = RiskScoreTestingEndpoints.calculateRiskScoreGroup(updatedRiskScore); | ||
addApisToRiskScoreGroupMap.get(addRiskScoreGroupType).add(apiInfo); | ||
} | ||
|
||
private void updateRiskScoreApiGroups() { | ||
try { | ||
for(RiskScoreTestingEndpoints.RiskScoreGroupType riskScoreGroupType: RiskScoreTestingEndpoints.RiskScoreGroupType.values()) { | ||
RiskScoreTestingEndpoints riskScoreTestingEndpoints = new RiskScoreTestingEndpoints(riskScoreGroupType); | ||
|
||
List<TestingEndpoints> testingEndpoints = new ArrayList<>(); | ||
testingEndpoints.add(riskScoreTestingEndpoints); | ||
int apiCollectionId = RiskScoreTestingEndpoints.getApiCollectionId(riskScoreGroupType); | ||
|
||
// Remove APIs from the original risk score group | ||
List<ApiInfo> removeApisFromRiskScoreGroupList = removeApisFromRiskScoreGroupMap.get(riskScoreGroupType); | ||
loggerMaker.infoAndAddToDb("Removing " + removeApisFromRiskScoreGroupList.size() + " APIs from risk score group - " + riskScoreGroupType, LogDb.DASHBOARD); | ||
for (int start = 0; start < removeApisFromRiskScoreGroupList.size(); start += RiskScoreTestingEndpoints.BATCH_SIZE) { | ||
int end = Math.min(start + RiskScoreTestingEndpoints.BATCH_SIZE, removeApisFromRiskScoreGroupList.size()); | ||
|
||
List<ApiInfo> batch = removeApisFromRiskScoreGroupList.subList(start, end); | ||
|
||
riskScoreTestingEndpoints.setFilterRiskScoreGroupApis(batch); | ||
ApiCollectionUsers.removeFromCollectionsForCollectionId(testingEndpoints, apiCollectionId); | ||
} | ||
|
||
// Add APIs to the new risk score group | ||
List<ApiInfo> addApisToRiskScoreGroupList = addApisToRiskScoreGroupMap.get(riskScoreGroupType); | ||
loggerMaker.infoAndAddToDb("Adding " + addApisToRiskScoreGroupList.size() + " APIs to risk score group - " + riskScoreGroupType, LogDb.DASHBOARD); | ||
for (int start = 0; start < addApisToRiskScoreGroupList.size(); start += RiskScoreTestingEndpoints.BATCH_SIZE) { | ||
int end = Math.min(start + RiskScoreTestingEndpoints.BATCH_SIZE, addApisToRiskScoreGroupList.size()); | ||
|
||
List<ApiInfo> batch = addApisToRiskScoreGroupList.subList(start, end); | ||
|
||
riskScoreTestingEndpoints.setFilterRiskScoreGroupApis(batch); | ||
ApiCollectionUsers.addToCollectionsForCollectionId(testingEndpoints, apiCollectionId); | ||
} | ||
} | ||
} catch (Exception e) { | ||
loggerMaker.errorAndAddToDb("Error updating risk score group APIs - " + e.getMessage(), LogDb.DASHBOARD); | ||
} | ||
} | ||
|
||
public void syncRiskScoreGroupApis() { | ||
int accountId = Context.accountId.get(); | ||
|
||
try { | ||
executorService.submit(() -> { | ||
Context.accountId.set(accountId); | ||
loggerMaker.infoAndAddToDb("Updating risk score API groups", LogDb.DASHBOARD); | ||
updateRiskScoreApiGroups(); | ||
}); | ||
} catch (Exception e) { | ||
loggerMaker.errorAndAddToDb("Error syncing risk score group APIs - " + e.getMessage(), LogDb.DASHBOARD); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.