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

Conversation

simar7
Copy link
Member

@simar7 simar7 commented Nov 10, 2023

Description

Exposes useful informational debug logs from the misconf engine when the existing --debug option is passed.

./trivy --debug config  /Users/repos/trivy-issues/5395    
2023-11-09T18:21:57.612-0700    DEBUG   Severities: ["UNKNOWN" "LOW" "MEDIUM" "HIGH" "CRITICAL"]
2023-11-09T18:21:57.624-0700    DEBUG   cache dir:  /Users/simarpreetsingh/Library/Caches/trivy
2023-11-09T18:21:57.625-0700    INFO    Misconfiguration scanning is enabled
2023-11-09T18:21:57.625-0700    DEBUG   Policies successfully loaded from disk
2023-11-09T18:21:57.649-0700    DEBUG   The nuget packages directory couldn't be found. License search disabled
2023-11-09T18:21:57.650-0700    DEBUG   Walk the file tree rooted at '/Users/simarpreetsingh/repos/trivy-issues/5395' in parallel
<snip>
2023-11-09T18:21:57.987-0700    DEBUG   [misconf] 21:57.987634000 terraform.executor               Initialised 484 rule(s).
2023-11-09T18:21:57.987-0700    DEBUG   [misconf] 21:57.987638000 terraform.executor               Created pool with 9 worker(s) to apply rules.
2023-11-09T18:21:57.989-0700    DEBUG   [misconf] 21:57.989147000 terraform.scanner.rego           Scanning 1 inputs...
2023-11-09T18:21:57.995-0700    DEBUG   [misconf] 21:57.995493000 terraform.executor               Finished applying rules.
2023-11-09T18:21:57.995-0700    DEBUG   [misconf] 21:57.995512000 terraform.executor               Applying ignores...
2023-11-09T18:21:57.995-0700    DEBUG   [misconf] 21:57.995528000 terraform.executor               Ignored 'google-gke-use-cluster-labels' at 'main.tf:2-5'.
2023-11-09T18:21:58.015-0700    DEBUG   OS is not detected.
2023-11-09T18:21:58.015-0700    INFO    Detected config files: 2
2023-11-09T18:21:58.015-0700    DEBUG   Scanned config file: .
2023-11-09T18:21:58.015-0700    DEBUG   Scanned config file: main.tf

main.tf (terraform)

Tests: 14 (SUCCESSES: 7, FAILURES: 6, EXCEPTIONS: 1)
Failures: 6 (UNKNOWN: 0, LOW: 1, MEDIUM: 3, HIGH: 2, CRITICAL: 0)

This can be useful for a variety of reasons, including knowing if a rule was ignored and why.

Related issues

Related PRs

Checklist

  • I've read the guidelines for contributing to this repository.
  • I've followed the conventions in the PR title.
  • I've added tests that prove my fix is effective or that my feature works.
  • I've updated the documentation with the relevant information (if needed).
  • I've added usage information (if the PR introduces new options)
  • I've included a "before" and "after" example to the description (if the PR is a user interface change).

@simar7 simar7 self-assigned this Nov 10, 2023
@simar7 simar7 changed the title feat(misconf): Expose debug logs with --debug option feat(misconf): Expose misconf engine debug logs with --debug option Nov 10, 2023
@knqyf263
Copy link
Collaborator

It's off-topic, but I just remember defsec didn't sync with the logging options in Trivy, like --quiet and --debug. Even when the Trivy logger is disabled with --quiet, the defesc logger is not disabled. It would be great if we support it in trivy-iac.

@simar7
Copy link
Member Author

simar7 commented Nov 10, 2023

It's off-topic, but I just remember defsec didn't sync with the logging options in Trivy, like --quiet and --debug. Even when the Trivy logger is disabled with --quiet, the defesc logger is not disabled. It would be great if we support it in trivy-iac.

Yeah you're right - I noticed that too. I think we need to better integrate the logging options between trivy and trivy-iac modules. I will create an issue for it #5551

@@ -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

@@ -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.

@nikpivkin
Copy link
Contributor

It's off-topic, but I just remember defsec didn't sync with the logging options in Trivy, like --quiet and --debug. Even when the Trivy logger is disabled with --quiet, the defesc logger is not disabled. It would be great if we support it in trivy-iac.

In this PR, the components use the Trivy global logger, so they will have the same behaviour as Trivy. I passed the -q flag and Trivy's output was clear.

@knqyf263
Copy link
Collaborator

In this PR, the components use the Trivy global logger, so they will have the same behaviour as Trivy. I passed the -q flag and Trivy's output was clear.

Great!

@@ -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

Copy link
Collaborator

@knqyf263 knqyf263 left a comment

Choose a reason for hiding this comment

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

Please be mindful that I'm planning to migrate zap to slog after bumping Go to 1.21 in Trivy. I'm not sure slog meets our requirements, though.

@knqyf263
Copy link
Collaborator

@nikpivkin Can you please review this PR?

Copy link
Contributor

@nikpivkin nikpivkin left a comment

Choose a reason for hiding this comment

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

LGTM

@knqyf263 knqyf263 added this pull request to the merge queue Nov 16, 2023
Merged via the queue into main with commit 1336223 Nov 16, 2023
12 checks passed
@knqyf263 knqyf263 deleted the misconf-debug-option branch November 16, 2023 02:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat(misconf): Improving ignore experience
3 participants