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

chore: slices cleanup #361

Merged
merged 7 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
12 changes: 6 additions & 6 deletions cmd/api/src/api/agi.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
// Copyright 2023 Specter Ops, Inc.
//
//
// Licensed under the Apache License, Version 2.0
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
// SPDX-License-Identifier: Apache-2.0

package api

import (
"fmt"
"slices"
"sort"
"strconv"

"github.com/specterops/bloodhound/src/model"
"github.com/specterops/bloodhound/slices"
)

type AssetGroupMember struct {
Expand Down Expand Up @@ -82,7 +82,7 @@ func (s AssetGroupMembers) SortBy(columns []string) (AssetGroupMembers, error) {
}

if descending {
s = slices.Reverse(s)
slices.Reverse(s)
}
}
return s, nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/api/src/api/v2/agi.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"net/http"
"net/url"
"slices"
"strconv"
"strings"

Expand All @@ -31,7 +32,6 @@ import (
"github.com/specterops/bloodhound/graphschema/common"
"github.com/specterops/bloodhound/headers"
"github.com/specterops/bloodhound/log"
"github.com/specterops/bloodhound/slices"
"github.com/specterops/bloodhound/src/api"
"github.com/specterops/bloodhound/src/ctx"
"github.com/specterops/bloodhound/src/model"
Expand Down
2 changes: 1 addition & 1 deletion cmd/api/src/api/v2/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package v2
import (
"fmt"
"net/http"
"slices"
"strings"
"time"

"github.com/specterops/bloodhound/slices"
"github.com/specterops/bloodhound/src/api"
"github.com/specterops/bloodhound/src/model"
)
Expand Down
15 changes: 7 additions & 8 deletions cmd/api/src/api/v2/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ import (
"fmt"
"io"
"net/http"
"slices"
"strconv"
"strings"
"time"

"github.com/crewjam/saml"
"github.com/crewjam/saml/samlsp"
"github.com/gofrs/uuid"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/pquerna/otp/totp"
"github.com/specterops/bloodhound/crypto"
"github.com/specterops/bloodhound/log"
"github.com/specterops/bloodhound/slices"
"github.com/specterops/bloodhound/src/api"
v2 "github.com/specterops/bloodhound/src/api/v2"
"github.com/specterops/bloodhound/src/auth"
Expand All @@ -40,13 +46,6 @@ import (
"github.com/specterops/bloodhound/src/serde"
"github.com/specterops/bloodhound/src/utils"
"github.com/specterops/bloodhound/src/utils/validation"

"github.com/crewjam/saml"
"github.com/crewjam/saml/samlsp"
"github.com/gofrs/uuid"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/pquerna/otp/totp"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion cmd/api/src/api/v2/file_uploads.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import (
"errors"
"fmt"
"net/http"
"slices"
"strconv"
"strings"

"github.com/gorilla/mux"
"github.com/specterops/bloodhound/log"
"github.com/specterops/bloodhound/slices"
"github.com/specterops/bloodhound/src/api"
"github.com/specterops/bloodhound/src/auth"
"github.com/specterops/bloodhound/src/ctx"
Expand Down
8 changes: 4 additions & 4 deletions cmd/api/src/api/v2/pathfinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/specterops/bloodhound/graphschema/ad"
"github.com/specterops/bloodhound/graphschema/azure"
"github.com/specterops/bloodhound/params"
"github.com/specterops/bloodhound/slices"
slicesext "github.com/specterops/bloodhound/slicesext"
superlinkx marked this conversation as resolved.
Show resolved Hide resolved
"github.com/specterops/bloodhound/src/api"
"github.com/specterops/bloodhound/src/api/bloodhoundgraph"
"github.com/specterops/bloodhound/src/model"
Expand Down Expand Up @@ -62,11 +62,11 @@ func writeShortestPathsResult(paths graph.PathSet, response http.ResponseWriter,
graphResponse.Nodes[n.ID.String()] = model.FromDAWGSNode(n, false)
}

edges := slices.FlatMap(paths, func(path graph.Path) []model.UnifiedEdge {
return slices.Map(path.Edges, model.FromDAWGSRelationship(false))
edges := slicesext.FlatMap(paths, func(path graph.Path) []model.UnifiedEdge {
return slicesext.Map(path.Edges, model.FromDAWGSRelationship(false))
})

graphResponse.Edges = slices.UniqueBy(edges, func(edge model.UnifiedEdge) string {
graphResponse.Edges = slicesext.UniqueBy(edges, func(edge model.UnifiedEdge) string {
return edge.Source + edge.Kind + edge.Target
})

Expand Down
10 changes: 5 additions & 5 deletions cmd/api/src/model/filter.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// Copyright 2023 Specter Ops, Inc.
//
//
// Licensed under the Apache License, Version 2.0
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
// SPDX-License-Identifier: Apache-2.0

package model
Expand All @@ -20,14 +20,14 @@ import (
"fmt"
"net/http"
"regexp"
"slices"
"strconv"
"strings"

"github.com/specterops/bloodhound/dawgs/graph"
"github.com/specterops/bloodhound/dawgs/query"

"github.com/specterops/bloodhound/errors"
"github.com/specterops/bloodhound/slices"
)

type FilterOperator string
Expand Down
10 changes: 5 additions & 5 deletions cmd/api/src/model/search.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// Copyright 2023 Specter Ops, Inc.
//
//
// Licensed under the Apache License, Version 2.0
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
// SPDX-License-Identifier: Apache-2.0

package model
Expand All @@ -20,12 +20,12 @@ import (
"fmt"
"net/http"
"net/url"
"slices"

"github.com/specterops/bloodhound/dawgs/graph"
"github.com/specterops/bloodhound/dawgs/query"
"github.com/specterops/bloodhound/graphschema/ad"
"github.com/specterops/bloodhound/graphschema/azure"
"github.com/specterops/bloodhound/slices"
)

type DomainSelector struct {
Expand Down
2 changes: 1 addition & 1 deletion go.work
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ use (
./packages/go/mediatypes
./packages/go/params
./packages/go/schemagen
./packages/go/slices
./packages/go/slicesext
./packages/go/stbernard
)
2 changes: 1 addition & 1 deletion packages/go/analysis/ad/esc3.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ package ad

import (
"context"
"slices"

"github.com/specterops/bloodhound/analysis"
"github.com/specterops/bloodhound/dawgs/graph"
"github.com/specterops/bloodhound/dawgs/util/channels"
"github.com/specterops/bloodhound/graphschema/ad"
"github.com/specterops/bloodhound/log"
"github.com/specterops/bloodhound/slices"
)

func PostEnrollOnBehalfOf(certTemplates []*graph.Node, operation analysis.StatTrackedOperation[analysis.CreatePostRelationshipJob]) error {
Expand Down
4 changes: 2 additions & 2 deletions packages/go/analysis/ad/esc_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/specterops/bloodhound/dawgs/util/channels"
"github.com/specterops/bloodhound/graphschema/ad"
"github.com/specterops/bloodhound/log"
"github.com/specterops/bloodhound/slices"
slicesext "github.com/specterops/bloodhound/slicesext"
superlinkx marked this conversation as resolved.
Show resolved Hide resolved
)

func PostTrustedForNTAuth(ctx context.Context, db graph.Database, operation analysis.StatTrackedOperation[analysis.CreatePostRelationshipJob]) error {
Expand Down Expand Up @@ -150,7 +150,7 @@ func processCertChainParent(node *graph.Node, tx graph.Transaction) ([]analysis.
if targetNodes, err := findNodesByCertThumbprint(parentCert, tx, ad.EnterpriseCA, ad.RootCA); err != nil {
return []analysis.CreatePostRelationshipJob{}, err
} else {
return slices.Map(targetNodes, func(nodeId graph.ID) analysis.CreatePostRelationshipJob {
return slicesext.Map(targetNodes, func(nodeId graph.ID) analysis.CreatePostRelationshipJob {
return analysis.CreatePostRelationshipJob{
FromID: node.ID,
ToID: nodeId,
Expand Down
16 changes: 5 additions & 11 deletions packages/go/analysis/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package analysis
import (
"context"
"fmt"
"slices"
"strings"

"github.com/specterops/bloodhound/dawgs/graph"
Expand All @@ -28,7 +29,7 @@ import (
"github.com/specterops/bloodhound/graphschema/azure"
"github.com/specterops/bloodhound/graphschema/common"
"github.com/specterops/bloodhound/log"
"github.com/specterops/bloodhound/slices"
slicesext "github.com/specterops/bloodhound/slicesext"
)

const (
Expand Down Expand Up @@ -110,17 +111,10 @@ func ClearSystemTags(ctx context.Context, db graph.Database) error {

func ValidKinds() []graph.Kind {
var (
lenCalc = len(ad.Nodes()) + len(ad.Relationships()) + len(azure.NodeKinds()) + len(azure.Relationships())
kinds = make([]graph.Kind, 0, lenCalc)
metaKinds = []graph.Kind{metaKind, metaDetailKind}
)

kinds = append(kinds, ad.Nodes()...)
kinds = append(kinds, ad.Relationships()...)
kinds = append(kinds, azure.NodeKinds()...)
kinds = append(kinds, azure.Relationships()...)
kinds = append(kinds, metaKind, metaDetailKind)

return kinds
return slicesext.Concat(ad.Nodes(), ad.Relationships(), azure.NodeKinds(), azure.Relationships(), metaKinds)
}

func ParseKind(rawKind string) (graph.Kind, error) {
Expand All @@ -138,7 +132,7 @@ func ParseKinds(rawKinds ...string) (graph.Kinds, error) {
return graph.Kinds{ad.Entity, azure.Entity}, nil
}

return slices.MapWithErr(rawKinds, ParseKind)
return slicesext.MapWithErr(rawKinds, ParseKind)
}

func nodeByIndexedKindProperty(property, value string, kind graph.Kind) graph.Criteria {
Expand Down
13 changes: 2 additions & 11 deletions packages/go/analysis/analysis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/specterops/bloodhound/dawgs/graph"
"github.com/specterops/bloodhound/graphschema/ad"
"github.com/specterops/bloodhound/graphschema/azure"
slicesext "github.com/specterops/bloodhound/slicesext"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -46,17 +47,7 @@ func (s kindStr) Is(others ...graph.Kind) bool {
}

func validKinds() graph.Kinds {
var (
lenCalc = len(ad.NodeKinds()) + len(ad.Relationships()) + len(azure.NodeKinds()) + len(azure.Relationships())
kinds = make(graph.Kinds, 0, lenCalc)
)

kinds = append(kinds, ad.NodeKinds()...)
kinds = append(kinds, ad.Relationships()...)
kinds = append(kinds, azure.NodeKinds()...)
kinds = append(kinds, azure.Relationships()...)

return kinds
return slicesext.Concat(ad.NodeKinds(), ad.Relationships(), azure.NodeKinds(), azure.Relationships())
}

func validKindStrings() []string {
Expand Down
5 changes: 2 additions & 3 deletions packages/go/analysis/azure/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,20 @@ package azure
import (
"context"
"fmt"
"slices"
"strings"

"github.com/RoaringBitmap/roaring"
"github.com/RoaringBitmap/roaring/roaring64"
"github.com/bloodhoundad/azurehound/v2/constants"
"github.com/specterops/bloodhound/analysis"
"github.com/specterops/bloodhound/dawgs/graph"
"github.com/specterops/bloodhound/dawgs/ops"
"github.com/specterops/bloodhound/dawgs/query"
"github.com/specterops/bloodhound/dawgs/util/channels"
"github.com/specterops/bloodhound/graphschema/azure"
"github.com/specterops/bloodhound/graphschema/common"
"github.com/specterops/bloodhound/log"
"github.com/specterops/bloodhound/slices"

"github.com/specterops/bloodhound/analysis"
)

func AddMemberAllGroupsTargetRoles() []string {
Expand Down
6 changes: 3 additions & 3 deletions packages/go/ein/ad.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/specterops/bloodhound/dawgs/graph"
"github.com/specterops/bloodhound/graphschema/ad"
"github.com/specterops/bloodhound/log"
"github.com/specterops/bloodhound/slices"
slicesext "github.com/specterops/bloodhound/slicesext"
)

func ConvertSessionObject(session Session) IngestibleSession {
Expand Down Expand Up @@ -510,7 +510,7 @@ func handleEnterpriseCAEnrollmentAgentRestrictions(enterpriseCA EnterpriseCA, re

func handleEnterpriseCASecurity(enterpriseCA EnterpriseCA, relationships []IngestibleRelationship) []IngestibleRelationship {
if enterpriseCA.CARegistryData.CASecurity.Collected {
caSecurityData := slices.Filter(enterpriseCA.CARegistryData.CASecurity.Data, func(s ACE) bool {
caSecurityData := slicesext.Filter(enterpriseCA.CARegistryData.CASecurity.Data, func(s ACE) bool {
if s.PrincipalType == ad.LocalGroup.String() {
return false
}
Expand All @@ -521,7 +521,7 @@ func handleEnterpriseCASecurity(enterpriseCA EnterpriseCA, relationships []Inges
}
})

filteredACES := slices.Filter(enterpriseCA.Aces, func(s ACE) bool {
filteredACES := slicesext.Filter(enterpriseCA.Aces, func(s ACE) bool {
if s.PrincipalSID == enterpriseCA.HostingComputer {
return true
} else {
Expand Down
Loading
Loading