Skip to content

Commit

Permalink
feat(misconf): Expose misconf engine debug logs with --debug option (
Browse files Browse the repository at this point in the history
…#5550)

Signed-off-by: Simar <simar@linux.com>
  • Loading branch information
simar7 committed Nov 16, 2023
1 parent 7105186 commit 1336223
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
12 changes: 2 additions & 10 deletions pkg/cloud/aws/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io/fs"
"strings"

"golang.org/x/xerrors"

Expand Down Expand Up @@ -44,11 +43,11 @@ func (s *AWSScanner) Scan(ctx context.Context, option flag.Options) (scan.Result
}

if option.Debug {
scannerOpts = append(scannerOpts, options.ScannerWithDebug(&defsecLogger{}))
scannerOpts = append(scannerOpts, options.ScannerWithDebug(&log.PrefixedLogger{Name: "aws"}))
}

if option.Trace {
scannerOpts = append(scannerOpts, options.ScannerWithTrace(&defsecLogger{}))
scannerOpts = append(scannerOpts, options.ScannerWithTrace(&log.PrefixedLogger{Name: "aws"}))
}

if option.Region != "" {
Expand Down Expand Up @@ -160,13 +159,6 @@ func createState(freshState *state.State, awsCache *cache.Cache) (*state.State,
return fullState, nil
}

type defsecLogger struct {
}

func (d *defsecLogger) Write(p []byte) (n int, err error) {
log.Logger.Debug("[defsec] " + strings.TrimSpace(string(p)))
return len(p), nil
}
func addPolicyNamespaces(namespaces []string, scannerOpts []options.ScannerOption) []options.ScannerOption {
if len(namespaces) > 0 {
scannerOpts = append(
Expand Down
1 change: 1 addition & 0 deletions pkg/commands/artifact/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ func initScannerConfig(opts flag.Options, cacheClient cache.Cache) (ScannerConfi
disableEmbedded = true
}
configScannerOptions = misconf.ScannerOption{
Debug: opts.Debug,
Trace: opts.Trace,
Namespaces: append(opts.PolicyNamespaces, defaultPolicyNamespaces...),
PolicyPaths: append(opts.PolicyPaths, downloadedPolicyPaths...),
Expand Down
10 changes: 10 additions & 0 deletions pkg/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package log
import (
"os"
"runtime"
"strings"

xlog "github.com/masahiro331/go-xfs-filesystem/log"
"go.uber.org/zap"
Expand Down Expand Up @@ -121,3 +122,12 @@ func String(key, val string) zap.Field {
}
return zap.String(key, val)
}

type PrefixedLogger struct {
Name string
}

func (d *PrefixedLogger) Write(p []byte) (n int, err error) {
Logger.Debugf("[%s] %s", d.Name, strings.TrimSpace(string(p)))
return len(p), nil
}
5 changes: 5 additions & 0 deletions pkg/misconf/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var enabledDefsecTypes = map[detection.FileType]types.ConfigType{
}

type ScannerOption struct {
Debug bool
Trace bool
RegoOnly bool
Namespaces []string
Expand Down Expand Up @@ -225,6 +226,10 @@ func scannerOptions(t detection.FileType, opt ScannerOption) ([]options.ScannerO
options.ScannerWithDataFilesystem(dataFS),
)

if opt.Debug {
opts = append(opts, options.ScannerWithDebug(&log.PrefixedLogger{Name: "misconf"}))
}

if opt.Trace {
opts = append(opts, options.ScannerWithPerResultTracing(true))
}
Expand Down

0 comments on commit 1336223

Please sign in to comment.