Skip to content

Commit b661898

Browse files
committed
chore: cleanup
1 parent 1a4b855 commit b661898

11 files changed

Lines changed: 135 additions & 164 deletions

File tree

cmd/api/src/api/v2/azure.go

Lines changed: 73 additions & 69 deletions
Large diffs are not rendered by default.

cmd/api/src/queries/graph.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,6 @@ func (s *GraphQuery) runListQuery(ctx context.Context, primaryDisplayKinds graph
10131013
}
10141014

10151015
func (s *GraphQuery) runCountQuery(ctx context.Context, node *graph.Node, params EntityQueryParameters, cacheEnabled bool) (any, int, error) {
1016-
params.RequestedType = model.DataTypeList
10171016
result, err := s.runMaybeCachedEntityQuery(ctx, node, params, cacheEnabled)
10181017
return nil, result.Len(), err
10191018
}

packages/go/analysis/azure/azure_integration_test.go

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,39 +1022,6 @@ func TestFetchEntityDescendentPaths_MultiHopPathToRoot(t *testing.T) {
10221022
require.NoError(t, err)
10231023
}
10241024

1025-
// TestFetchEntityDescendentPaths_DifferentTenantExcluded verifies that nodes sharing the same kind
1026-
// but belonging to a different Azure tenant are not included in the path set.
1027-
func TestFetchEntityDescendentPaths_DifferentTenantExcluded(t *testing.T) {
1028-
t.Parallel()
1029-
1030-
suite := setupIntegrationTestSuite(t)
1031-
defer teardownIntegrationTestSuite(t, &suite)
1032-
1033-
var (
1034-
tenantID = integration.RandomObjectID(t)
1035-
otherTenantID = integration.RandomObjectID(t)
1036-
tenantNode = NewAzureTenant(t, &suite, tenantID)
1037-
ownApp = NewAzureApplication(t, &suite, "OwnApp", integration.RandomObjectID(t), tenantID)
1038-
foreignApp = NewAzureApplication(t, &suite, "ForeignApp", integration.RandomObjectID(t), otherTenantID)
1039-
)
1040-
1041-
NewRelationship(t, &suite, tenantNode, ownApp, graphAzure.Contains)
1042-
// foreignApp is deliberately not connected; it belongs to a different tenant.
1043-
1044-
err := suite.GraphDB.ReadTransaction(suite.Context, func(tx graph.Transaction) error {
1045-
paths, err := azure.FetchEntityDescendentPaths(tx, tenantNode, graphAzure.App)
1046-
require.NoError(t, err)
1047-
1048-
nodeIDs := paths.AllNodes().IDs()
1049-
require.Contains(t, nodeIDs, tenantNode.ID)
1050-
require.Contains(t, nodeIDs, ownApp.ID)
1051-
require.NotContains(t, nodeIDs, foreignApp.ID)
1052-
1053-
return nil
1054-
})
1055-
require.NoError(t, err)
1056-
}
1057-
10581025
// TestFetchEntityDescendentPaths_NoTerminalsReturnsEmpty verifies that an empty path set is
10591026
// returned when no nodes of the requested kind exist within the tenant.
10601027
func TestFetchEntityDescendentPaths_NoTerminalsReturnsEmpty(t *testing.T) {

packages/go/analysis/azure/db_ops.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,12 @@ func ListEntityDescendentPaths(ctx context.Context, db graph.Database, relatedEn
7373
return ErrInvalidRelatedEntityType
7474
}
7575

76-
if validEndKinds, ok := containsValidEndKinds[sourceKind]; ok && validEndKinds.ContainsOneOf(targetKind) {
76+
if isDirectDescendent(sourceKind, targetKind) {
7777
paths, err = FetchDirectDescendentPaths(tx, node, targetKind)
78-
return err
7978
} else {
8079
paths, err = FetchEntityDescendentPaths(tx, node, targetKind)
81-
return err
8280
}
83-
81+
return err
8482
}
8583
})
8684
}
@@ -133,14 +131,12 @@ func ListEntityDescendents(ctx context.Context, db graph.Database, relatedEntity
133131
return ErrInvalidRelatedEntityType
134132
}
135133

136-
if validEndKinds, ok := containsValidEndKinds[sourceKind]; ok && validEndKinds.ContainsOneOf(targetKind) {
137-
nodes, err = FetchDirectEntityDescendents(tx, node, skip, limit, targetKind)
138-
return err
134+
if isDirectDescendent(sourceKind, targetKind) {
135+
nodes, err = FetchDirectEntityDescendents(tx, node, targetKind)
139136
} else {
140-
nodes, err = FetchEntityDescendents(tx, node, skip, limit, targetKind)
141-
return err
137+
nodes, err = FetchEntityDescendents(tx, node, targetKind)
142138
}
143-
139+
return err
144140
}
145141
})
146142
}

packages/go/analysis/azure/management_group.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func ManagementGroupEntityDetails(ctx context.Context, db graph.Database, primar
4545
func PopulateManagementGroupEntityDetailsCounts(tx graph.Transaction, node *graph.Node, details ManagementGroupDetails) (ManagementGroupDetails, error) {
4646
var descendentKinds = GetDescendentKinds(azure.ManagementGroup)
4747

48-
if descendents, err := FetchEntityDescendentCounts(tx, node, 0, 0, descendentKinds...); err != nil {
48+
if descendents, err := FetchEntityDescendentCounts(tx, node, descendentKinds...); err != nil {
4949
return details, err
5050
} else {
5151
details.Descendents = descendents

packages/go/analysis/azure/queries.go

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package azure
1818

1919
import (
2020
"context"
21-
"net/url"
2221

2322
"log/slog"
2423
"strings"
@@ -822,6 +821,13 @@ var containsValidEndKinds = map[graph.Kind]graph.Kinds{
822821
},
823822
}
824823

824+
func isDirectDescendent(sourceKind, targetKind graph.Kind) bool {
825+
if validEndKinds, ok := containsValidEndKinds[sourceKind]; ok && validEndKinds.ContainsOneOf(targetKind) {
826+
return true
827+
}
828+
return false
829+
}
830+
825831
// nodeAzureTenantID returns the Azure tenant ID for a node. For Tenant nodes the tenant ID is
826832
// stored in the common.ObjectID property; for all other Azure nodes it is stored in azure.TenantID.
827833
func nodeAzureTenantID(node *graph.Node) (string, error) {
@@ -832,22 +838,16 @@ func nodeAzureTenantID(node *graph.Node) (string, error) {
832838
return node.Properties.Get(azure.TenantID.String()).String()
833839
}
834840

835-
// FetchDirectDescendentPaths fetches the direct AZContains relationships from the root node. Both
836-
// the valid start/end node kind pairing and the tenant-id match are enforced
841+
// FetchDirectDescendentPaths fetches the direct AZContains relationships from the root node
837842
func FetchDirectDescendentPaths(tx graph.Transaction, root *graph.Node, descendentKind ...graph.Kind) (graph.PathSet, error) {
838-
if tenantID, err := nodeAzureTenantID(root); err != nil {
839-
return nil, err
840-
} else {
841-
return ops.FetchPathSet(tx.Relationships().Filter(query.And(
842-
query.Equals(query.StartID(), root.ID),
843-
query.Kind(query.Relationship(), azure.Contains),
844-
query.KindIn(query.End(), descendentKind...),
845-
query.Equals(query.EndProperty(azure.TenantID.String()), tenantID),
846-
)))
847-
}
843+
return ops.FetchPathSet(tx.Relationships().Filter(query.And(
844+
query.Equals(query.StartID(), root.ID),
845+
query.Kind(query.Relationship(), azure.Contains),
846+
query.KindIn(query.End(), descendentKind...),
847+
)))
848848
}
849849

850-
func FetchDirectEntityDescendents(tx graph.Transaction, root *graph.Node, skip, limit int, descendentKind graph.Kind) (graph.NodeSet, error) {
850+
func FetchDirectEntityDescendents(tx graph.Transaction, root *graph.Node, descendentKind graph.Kind) (graph.NodeSet, error) {
851851
if paths, err := FetchDirectDescendentPaths(tx, root, descendentKind); err != nil {
852852
return nil, err
853853
} else {
@@ -857,7 +857,7 @@ func FetchDirectEntityDescendents(tx graph.Transaction, root *graph.Node, skip,
857857
}
858858
}
859859

860-
func FetchDescendentKindByTenantID(tx graph.Transaction, root *graph.Node, skip, limit int, descendentKind ...graph.Kind) (graph.NodeSet, error) {
860+
func FetchDescendentKindByTenantID(tx graph.Transaction, root *graph.Node, descendentKind ...graph.Kind) (graph.NodeSet, error) {
861861
if tenantID, err := nodeAzureTenantID(root); err != nil {
862862
return nil, err
863863
} else if nodes, err := ops.FetchNodeSet(tx.Nodes().Filter(query.And(
@@ -883,7 +883,7 @@ func FetchEntityDescendentPaths(tx graph.Transaction, root *graph.Node, descende
883883
// Pre-populate the visited bitmap with root so that any traversal halts when it reaches it
884884
visitedBitmap.Add(root.ID.Uint64())
885885

886-
terminalNodes, err := FetchDescendentKindByTenantID(tx, root, 0, 0, descendentKind...)
886+
terminalNodes, err := FetchDescendentKindByTenantID(tx, root, descendentKind...)
887887
if err != nil {
888888
return nil, err
889889
}
@@ -918,7 +918,7 @@ func FetchEntityDescendentPaths(tx graph.Transaction, root *graph.Node, descende
918918
return pathSet, nil
919919
}
920920

921-
func FetchEntityDescendents(tx graph.Transaction, root *graph.Node, skip, limit int, descendentKind graph.Kind) (graph.NodeSet, error) {
921+
func FetchEntityDescendents(tx graph.Transaction, root *graph.Node, descendentKind graph.Kind) (graph.NodeSet, error) {
922922
if paths, err := FetchEntityDescendentPaths(tx, root, descendentKind); err != nil {
923923
return nil, err
924924
} else {
@@ -928,7 +928,7 @@ func FetchEntityDescendents(tx graph.Transaction, root *graph.Node, skip, limit
928928
}
929929
}
930930

931-
func FetchDirectDescendentCounts(tx graph.Transaction, root *graph.Node, skip, limit int, descendentKinds ...graph.Kind) (Descendents, error) {
931+
func FetchDirectDescendentCounts(tx graph.Transaction, root *graph.Node, descendentKinds ...graph.Kind) (Descendents, error) {
932932
if paths, err := FetchDirectDescendentPaths(tx, root, descendentKinds...); err != nil {
933933
return Descendents{}, err
934934
} else {
@@ -946,7 +946,7 @@ func FetchDirectDescendentCounts(tx graph.Transaction, root *graph.Node, skip, l
946946
}
947947
}
948948

949-
func FetchEntityDescendentCounts(tx graph.Transaction, root *graph.Node, skip, limit int, descendentKinds ...graph.Kind) (Descendents, error) {
949+
func FetchEntityDescendentCounts(tx graph.Transaction, root *graph.Node, descendentKinds ...graph.Kind) (Descendents, error) {
950950
if paths, err := FetchEntityDescendentPaths(tx, root, descendentKinds...); err != nil {
951951
return Descendents{}, err
952952
} else {
@@ -980,14 +980,10 @@ func fetchRolesTraversalPlan(root *graph.Node) ops.TraversalPlan {
980980

981981
// FetchEntityByObjectID pulls a node by its ObjectID. It requires a kind to perform index lookups against.
982982
func FetchEntityByObjectID(tx graph.Transaction, objectID string) (*graph.Node, error) {
983-
if decodedObjectID, err := url.QueryUnescape(objectID); err != nil {
984-
return nil, err
985-
} else {
986-
return tx.Nodes().Filterf(func() graph.Criteria {
987-
return query.And(
988-
query.Kind(query.Node(), azure.Entity),
989-
query.Equals(query.NodeProperty(common.ObjectID.String()), decodedObjectID),
990-
)
991-
}).First()
992-
}
983+
return tx.Nodes().Filterf(func() graph.Criteria {
984+
return query.And(
985+
query.Kind(query.Node(), azure.Entity),
986+
query.Equals(query.NodeProperty(common.ObjectID.String()), objectID),
987+
)
988+
}).First()
993989
}

packages/go/analysis/azure/resource_group.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func ResourceGroupEntityDetails(ctx context.Context, db graph.Database, primaryD
4343
func PopulateResourceGroupEntityDetailsCounts(tx graph.Transaction, node *graph.Node, details ResourceGroupDetails) (ResourceGroupDetails, error) {
4444
var descendentKinds = GetDescendentKinds(azure.ResourceGroup)
4545

46-
if descendents, err := FetchDirectDescendentCounts(tx, node, 0, 0, descendentKinds...); err != nil {
46+
if descendents, err := FetchDirectDescendentCounts(tx, node, descendentKinds...); err != nil {
4747
return details, err
4848
} else {
4949
details.Descendents = descendents

packages/go/analysis/azure/subscription.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func SubscriptionEntityDetails(ctx context.Context, db graph.Database, primaryDi
4343
func PopulateSubscriptionEntityDetailsCounts(tx graph.Transaction, node *graph.Node, details SubscriptionDetails) (SubscriptionDetails, error) {
4444
var descendentKinds = GetDescendentKinds(azure.Subscription)
4545

46-
if descendents, err := FetchEntityDescendentCounts(tx, node, 0, 0, descendentKinds...); err != nil {
46+
if descendents, err := FetchEntityDescendentCounts(tx, node, descendentKinds...); err != nil {
4747
return details, err
4848
} else {
4949
details.Descendents = descendents

packages/go/analysis/azure/tenant.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TenantEntityDetails(ctx context.Context, db graph.Database, primaryDisplayK
4848
func PopulateTenantEntityDetailsCounts(tx graph.Transaction, node *graph.Node, details TenantDetails) (TenantDetails, error) {
4949
descendentKinds := GetDescendentKinds(azure.Tenant)
5050

51-
if descendents, err := FetchEntityDescendentCounts(tx, node, 0, 0, descendentKinds...); err != nil {
51+
if descendents, err := FetchEntityDescendentCounts(tx, node, descendentKinds...); err != nil {
5252
return details, err
5353
} else {
5454
details.Descendents = descendents

packages/javascript/bh-shared-ui/src/components/EntityInfoDataTableGraphed/EntityInfoDataTableGraphed.tsx

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414
//
1515
// SPDX-License-Identifier: Apache-2.0
16+
import { PaginatedResponse } from 'js-client-library';
1617
import { useQuery } from 'react-query';
1718
import { NODE_GRAPH_RENDER_LIMIT } from '../../constants';
1819
import { useExploreParams } from '../../hooks';
@@ -21,6 +22,28 @@ import { EntityInfoDataTableProps, entityRelationshipEndpoints } from '../../uti
2122
import EntityInfoCollapsibleSection from '../EntityInfo/EntityInfoCollapsibleSection';
2223
import InfiniteScrollingTable from '../InfiniteScrollingTable';
2324

25+
function getCount<T>(
26+
queryData: Array<PromiseFulfilledResult<PaginatedResponse<T>>> | PaginatedResponse<T> | undefined,
27+
countLabel: string | undefined
28+
): number | undefined {
29+
if (Array.isArray(queryData)) {
30+
const fulfilledData = queryData.filter((result) => result.status === 'fulfilled').map((result) => result.value);
31+
32+
if (countLabel !== undefined) {
33+
fulfilledData.forEach((sectionData: any) => {
34+
if (sectionData?.countLabel === countLabel) return sectionData.count;
35+
});
36+
} else {
37+
return fulfilledData.reduce((acc, val) => {
38+
const sectionCount = val?.count ?? 0;
39+
return acc + sectionCount;
40+
}, 0);
41+
}
42+
} else if (queryData) {
43+
return queryData?.count ?? 0;
44+
}
45+
}
46+
2447
export const EntityInfoDataTableGraphed: React.FC<EntityInfoDataTableProps> = ({
2548
id,
2649
label,
@@ -35,13 +58,13 @@ export const EntityInfoDataTableGraphed: React.FC<EntityInfoDataTableProps> = ({
3558
const isExpandedPanelSection = (expandedPanelSections as string[]).includes(label);
3659

3760
const countQuery = useQuery(
38-
['relatedCount', label, id],
61+
['relatedCount', label, id, sections],
3962
() => {
4063
if (endpoint) {
4164
return endpoint({ id, skip: 0, limit: 128 });
4265
}
4366
if (sections)
44-
return Promise.all(
67+
return Promise.allSettled(
4568
sections.map((section: EntityInfoDataTableProps) => {
4669
const endpoint = section.queryType ? entityRelationshipEndpoints[section.queryType] : undefined;
4770
return endpoint ? endpoint({ id, skip: 0, limit: 128 }) : Promise.resolve();
@@ -104,21 +127,7 @@ export const EntityInfoDataTableGraphed: React.FC<EntityInfoDataTableProps> = ({
104127
setNodeSearchParams(item);
105128
};
106129

107-
let count: number | undefined;
108-
if (Array.isArray(countQuery.data)) {
109-
if (countLabel !== undefined) {
110-
countQuery.data.forEach((sectionData: any) => {
111-
if (sectionData.countLabel === countLabel) count = sectionData.count;
112-
});
113-
} else {
114-
count = countQuery.data.reduce((acc, val) => {
115-
const count = val?.count ?? 0;
116-
return acc + count;
117-
}, 0);
118-
}
119-
} else if (countQuery.data) {
120-
count = countQuery.data?.count ?? 0;
121-
}
130+
const count = getCount(countQuery.data, countLabel);
122131

123132
return (
124133
<EntityInfoCollapsibleSection

0 commit comments

Comments
 (0)