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

refactor(misconf): simplify k8s scanner #7717

Merged
merged 4 commits into from
Oct 17, 2024

Conversation

nikpivkin
Copy link
Contributor

@nikpivkin nikpivkin commented Oct 11, 2024

Description

In this PR:

  • k8s scanner uses a generic scanner
  • k8s parser simplified, now it's just 1 function
  • duplicate tests were removed, large checks were replaced by compact ones

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

@nikpivkin nikpivkin marked this pull request as ready for review October 11, 2024 10:15
@nikpivkin nikpivkin requested a review from simar7 as a code owner October 11, 2024 10:15
Comment on lines -30 to -67
func (p *Parser) ParseFS(ctx context.Context, target fs.FS, path string) (map[string][]any, error) {
files := make(map[string][]any)
if err := fs.WalkDir(target, filepath.ToSlash(path), func(path string, entry fs.DirEntry, err error) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
if err != nil {
return err
}
if entry.IsDir() {
return nil
}

parsed, err := p.ParseFile(ctx, target, path)
if err != nil {
p.logger.Error("Parse error", log.FilePath(path), log.Err(err))
return nil
}

files[path] = parsed
return nil
}); err != nil {
return nil, err
}
return files, nil
}

// ParseFile parses Kubernetes manifest from the provided filesystem path.
func (p *Parser) ParseFile(_ context.Context, fsys fs.FS, path string) ([]any, error) {
f, err := fsys.Open(filepath.ToSlash(path))
if err != nil {
return nil, err
}
defer func() { _ = f.Close() }()
return p.Parse(f, path)
}
Copy link
Member

Choose a reason for hiding this comment

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

Were they not used? I'm a little skeptical because they are exported funcs which may have been used elsewhere (outside of the project).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This parser is specific to Trivy, I don't think it can be used anywhere else.

Copy link
Member

Choose a reason for hiding this comment

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

OK - I guess only one way to find out 😺

@simar7 simar7 self-requested a review October 17, 2024 21:50
@simar7 simar7 added this pull request to the merge queue Oct 17, 2024
Merged via the queue into aquasecurity:main with commit cd44bb4 Oct 17, 2024
15 checks passed
@nikpivkin nikpivkin deleted the k8s-scanner branch October 18, 2024 06:21
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.

2 participants