Skip to content

Commit

Permalink
Merge pull request #1876 from akto-api-security/hotfix/fix_apis_group…
Browse files Browse the repository at this point in the history
…_count

Fixing count of apis
  • Loading branch information
notshivansh authored Dec 31, 2024
2 parents b83edbf + bc84246 commit 0847057
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ public String getEndpointsListFromConditions() {
InventoryAction inventoryAction = new InventoryAction();
inventoryAction.attachAPIInfoListInResponse(list,-1);
this.setResponse(inventoryAction.getResponse());
response.put("apiCount", ApiCollectionUsers.getApisCountFromConditions(conditions, new ArrayList<>(deactivatedCollections)));
response.put("apiCount", ApiCollectionUsers.getApisCountFromConditionsWithStis(conditions, new ArrayList<>(deactivatedCollections)));
return SUCCESS.toUpperCase();
}
public String getEndpointsFromConditions(){
Expand Down
7 changes: 7 additions & 0 deletions libs/dao/src/main/java/com/akto/dao/SingleTypeInfoDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -828,4 +828,11 @@ public List<BasicDBObject> fetchRecentEndpoints(int startTimestamp, int endTimes
return endpoints;
}

public static BasicDBObject getApiInfoGroupedId() {
BasicDBObject groupedId =
new BasicDBObject("apiCollectionId", "$apiCollectionId")
.append("url", "$url")
.append("method", "$method");
return groupedId;
}
}
38 changes: 38 additions & 0 deletions libs/dao/src/main/java/com/akto/dto/ApiCollectionUsers.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.akto.dao.context.Context;
import com.akto.dao.demo.VulnerableRequestForTemplateDao;
import com.akto.dao.testing_run_findings.TestingRunIssuesDao;
import com.akto.dto.rbac.UsersCollectionsList;
import com.akto.dto.testing.CustomTestingEndpoints;
import com.akto.dto.testing.SensitiveDataEndpoints;
import com.akto.dto.testing.TestingEndpoints;
Expand All @@ -35,6 +36,7 @@
import com.akto.util.Constants;
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;
Expand Down Expand Up @@ -94,6 +96,42 @@ public static int getApisCountFromConditions(List<TestingEndpoints> conditions,
return (int) ApiInfoDao.instance.count(apiInfoFilters);
}

public static int getApisCountFromConditionsWithStis(List<TestingEndpoints> conditions, List<Integer> deactivatedCollections){
if(conditions == null || conditions.isEmpty()){
return 0;
}

Bson stiFiltes = getFilters(conditions, CollectionType.ApiCollectionId);
List<Bson> pipeLine = new ArrayList<>();
pipeLine.add(Aggregates.match(stiFiltes));

try {
List<Integer> collectionIds = UsersCollectionsList.getCollectionsIdForUser(Context.userId.get(), Context.accountId.get());
if(collectionIds != null) {
pipeLine.add(Aggregates.match(Filters.in(SingleTypeInfo._COLLECTION_IDS, collectionIds)));
}
} catch(Exception e){
}

pipeLine.add(Aggregates.match(Filters.and(
Filters.eq(SingleTypeInfo._RESPONSE_CODE, -1),
Filters.eq(SingleTypeInfo._IS_HEADER, true)
)));
BasicDBObject groupedId = SingleTypeInfoDao.getApiInfoGroupedId();
pipeLine.add(Aggregates.group(groupedId, Accumulators.sum("count", 1)));
pipeLine.add(Aggregates.count("finalCount"));

int ansCount = 0;

MongoCursor<BasicDBObject> countCursor = SingleTypeInfoDao.instance.getMCollection().aggregate(pipeLine, BasicDBObject.class).cursor();
while(countCursor.hasNext()){
BasicDBObject dbObject = countCursor.next();
ansCount = dbObject.getInt("finalCount");
}

return ansCount;
}

public static void updateApiCollection(List<TestingEndpoints> conditions, int id) {

ApiCollectionsDao.instance.updateOne(
Expand Down

0 comments on commit 0847057

Please sign in to comment.