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(misconf): Expose misconf engine debug logs with --debug option #5550

Merged
merged 4 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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: 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.DebugLogger{Name: "aws"}))
}

if option.Trace {
scannerOpts = append(scannerOpts, options.ScannerWithTrace(&defsecLogger{}))
scannerOpts = append(scannerOpts, options.ScannerWithTrace(&log.DebugLogger{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 DebugLogger struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks more like PrefixedLogger, doesn't it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed: d9389f7

Name string
}

func (d *DebugLogger) 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 @@ -257,6 +258,10 @@ func scannerOptions(t detection.FileType, opt ScannerOption) ([]options.ScannerO
options.ScannerWithDataFilesystem(dataFS),
)

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

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