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

feat: enable literal scrubbed user query logging #26

Merged
merged 1 commit into from
Aug 18, 2023
Merged
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
77 changes: 51 additions & 26 deletions cmd/api/src/queries/graph.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
// 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 queries

//go:generate go run go.uber.org/mock/mockgen -copyright_file=../../../../LICENSE.header -destination=./mocks/graph.go -package=mocks . Graph

import (
"bytes"
"context"
"fmt"
"net/http"
Expand Down Expand Up @@ -140,18 +141,22 @@ type Graph interface {
}

type GraphQuery struct {
Graph graph.Database
Cache cache.Cache
SlowQueryThreshold int64 // Threshold in milliseconds
DisableCypherQC bool
Graph graph.Database
Cache cache.Cache
SlowQueryThreshold int64 // Threshold in milliseconds
DisableCypherQC bool
cypherEmitter frontend.Emitter
strippedCypherEmitter frontend.Emitter
}

func NewGraphQuery(graphDB graph.Database, cache cache.Cache, slowQueryThreshold int64, disableCypherQC bool) *GraphQuery {
return &GraphQuery{
Graph: graphDB,
Cache: cache,
SlowQueryThreshold: slowQueryThreshold,
DisableCypherQC: disableCypherQC,
Graph: graphDB,
Cache: cache,
SlowQueryThreshold: slowQueryThreshold,
DisableCypherQC: disableCypherQC,
cypherEmitter: frontend.NewCypherEmitter(false),
strippedCypherEmitter: frontend.NewCypherEmitter(true),
}
}

Expand Down Expand Up @@ -344,35 +349,55 @@ func (s *GraphQuery) SearchNodesByName(ctx context.Context, nodeKinds graph.Kind
}

type preparedQuery struct {
cypher string
complexity *analyzer.ComplexityMeasure
cypher string
strippedCypher string
complexity *analyzer.ComplexityMeasure
}

func prepareQuery(rawCypher string, disableCypherQC bool) (preparedQuery, error) {
parseCtx := frontend.DefaultCypherContext()
func (s *GraphQuery) prepareGraphQuery(rawCypher string, disableCypherQC bool) (preparedQuery, error) {
var (
parseCtx = frontend.DefaultCypherContext()
buffer = &bytes.Buffer{}
graphQuery preparedQuery
)

if queryModel, err := frontend.ParseCypher(parseCtx, rawCypher); err != nil {
return preparedQuery{}, newQueryError(err)
return graphQuery, newQueryError(err)
} else if complexityMeasure, err := analyzer.QueryComplexity(queryModel); err != nil {
return preparedQuery{}, newQueryError(err)
return graphQuery, newQueryError(err)
} else if !disableCypherQC && complexityMeasure.Weight > MaxQueryComplexityWeightAllowed {
return preparedQuery{}, newQueryError(ErrCypherQueryToComplex)
} else if cypherQuery, err := frontend.FormatRegularQuery(queryModel); err != nil {
return preparedQuery{}, newQueryError(err)
return graphQuery, newQueryError(ErrCypherQueryToComplex)
} else {
return preparedQuery{
cypher: cypherQuery,
complexity: complexityMeasure,
}, nil
graphQuery.complexity = complexityMeasure

if err := s.cypherEmitter.Write(queryModel, buffer); err != nil {
return graphQuery, newQueryError(err)
} else {
graphQuery.cypher = buffer.String()
}

buffer.Reset()

if err := s.strippedCypherEmitter.Write(queryModel, buffer); err != nil {
return graphQuery, newQueryError(err)
} else {
graphQuery.strippedCypher = buffer.String()
}
}

return graphQuery, nil
}

func (s *GraphQuery) RawCypherSearch(ctx context.Context, rawCypher string) (model.UnifiedGraph, error) {
graphResponse := model.NewUnifiedGraph()

if preparedQuery, err := prepareQuery(rawCypher, s.DisableCypherQC); err != nil {
if preparedQuery, err := s.prepareGraphQuery(rawCypher, s.DisableCypherQC); err != nil {
return graphResponse, err
} else {
logEvent := log.WithLevel(log.LevelInfo)
logEvent.Str("query", preparedQuery.strippedCypher)
logEvent.Msg("Executing user cypher query")

return graphResponse, s.Graph.ReadTransaction(ctx, func(tx graph.Transaction) error {
if pathSet, err := ops.FetchPathSetByQuery(tx, preparedQuery.cypher); err != nil {
return err
Expand Down
Loading