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 1 commit
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
10 changes: 5 additions & 5 deletions pkg/cloud/aws/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,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(&DebugLogger{}))
Copy link
Contributor

Choose a reason for hiding this comment

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

How about passing a named logger as a field? Zap will take care of adding the path to the logger name itself

Suggested change
scannerOpts = append(scannerOpts, options.ScannerWithDebug(&DebugLogger{}))
scannerOpts = append(scannerOpts, options.ScannerWithDebug(&DebugLogger{log.Logger.Named("aws")}))

Copy link
Member Author

Choose a reason for hiding this comment

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

log.Logger.Named() returns a sugaredlogger which doesn't satisfy the interface conditions.

Instead I refactored the DebugLogger into the log package which would help us re-use it across other packages and added a name as a field as you mentioned.

}

if option.Trace {
scannerOpts = append(scannerOpts, options.ScannerWithTrace(&defsecLogger{}))
scannerOpts = append(scannerOpts, options.ScannerWithTrace(&DebugLogger{}))
}

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

type defsecLogger struct {
type DebugLogger struct {
}

func (d *defsecLogger) Write(p []byte) (n int, err error) {
log.Logger.Debug("[defsec] " + strings.TrimSpace(string(p)))
func (d *DebugLogger) Write(p []byte) (n int, err error) {
log.Logger.Debug("[aws] " + strings.TrimSpace(string(p)))
return len(p), nil
}
func addPolicyNamespaces(namespaces []string, scannerOpts []options.ScannerOption) []options.ScannerOption {
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
13 changes: 13 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 All @@ -67,6 +68,14 @@ func (o *ScannerOption) Sort() {
sort.Strings(o.DataPaths)
}

type DebugLogger struct {
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above


func (d *DebugLogger) Write(p []byte) (n int, err error) {
log.Logger.Debug("[misconf] " + strings.TrimSpace(string(p)))
return len(p), nil
}

type Scanner struct {
fileType detection.FileType
scanner scanners.FSScanner
Expand Down Expand Up @@ -257,6 +266,10 @@ func scannerOptions(t detection.FileType, opt ScannerOption) ([]options.ScannerO
options.ScannerWithDataFilesystem(dataFS),
)

if opt.Debug {
opts = append(opts, options.ScannerWithDebug(&DebugLogger{}))
}

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