Skip to content

perf(misconf): retrieve check metadata from annotations once #8478

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

Merged
merged 7 commits into from
Mar 11, 2025

Conversation

nikpivkin
Copy link
Contributor

@nikpivkin nikpivkin commented Mar 3, 2025

Description

Right now, Trivy retrieves the static metadata of a check each time it is run. This PR retrieves static metadata from annotations after checks are loaded. For dynamic metadata that is specified in the legacy way via the ___rego_metadata__ rule, the behavior has not changed, as they can use input data.
Also for checks that use the __rego_metadata__ and __rego_input__ rules, a warning is issued that this is a deprecated way of specifying metadata and input options and it is recommended to use annotations.

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 March 6, 2025 08:58
@nikpivkin nikpivkin requested a review from simar7 as a code owner March 6, 2025 08:58
Comment on lines 56 to 59
log.Warn("Failed to retrieve metadata",
log.String("package", module.Package.String()), log.Err(err))
continue
} else if metadata == nil {
Copy link
Member

Choose a reason for hiding this comment

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

can we add a sad test case for these two code paths?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have removed these changes as they are unnecessary.

Comment on lines -69 to -71
if !metadata.Deprecated {
regoCheckIDs.Append(metadata.AVDID)
}
Copy link
Member

Choose a reason for hiding this comment

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

this is interesting, should we add a linter check to ensure a variable gets flagged if it's only set and never used?

Comment on lines 86 to 88
Filtered(paths, func(abspath string, info fs.FileInfo, _ int) bool {
return isNotRegoFile(info) || isOptionalChecks(abspath)
})
Copy link
Member

Choose a reason for hiding this comment

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

can we add some test cases where .Filtered() can return true, IOW files that are not valid rego?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added tests de66947

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I put the old way of loading back in, since we use the slash as a path separator regardless of OS, and OPA uses the filepath package which is OS dependent.

Comment on lines +99 to +100
func isOptionalChecks(path string) bool {
return strings.HasSuffix(filepath.Dir(filepath.ToSlash(path)), filepath.Join("advanced", "optional"))
Copy link
Member

Choose a reason for hiding this comment

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

Is this still needed after the recent k8s checks re-org?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's look at this in another PR when updating trivy-checks? For now, trivy works with the old trivy-checks structure.

}
}

// moduleHasLegacyMetadataFormat checks if the module has an legacy metadata format.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// moduleHasLegacyMetadataFormat checks if the module has an legacy metadata format.
// moduleHasLegacyMetadataFormat checks if the module has a legacy metadata format.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed 3ee7477

})
}

// moduleHasLegacyInputFormat checks if the module has an legacy input format.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// moduleHasLegacyInputFormat checks if the module has an legacy input format.
// moduleHasLegacyInputFormat checks if the module has a legacy input format.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed 3ee7477


if len(metadata.InputOptions.Selectors) == 0 && !metadata.Library {
s.logger.Warn(
"Module has no input selectors - it will be loaded for all inputs!",
Copy link
Member

Choose a reason for hiding this comment

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

I recall reading somewhere on the Golang website about Go best practices for logging avoiding exclamation marks but I can't find it now.

Suggested change
"Module has no input selectors - it will be loaded for all inputs!",
"Module has no input selectors - it will be loaded for all inputs",

It also talked about case but my memory fails me as to what the recommendation was.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated 3ee7477

@@ -276,6 +285,38 @@ func (m StaticMetadata) ToRule() scan.Rule {
}
}

func MetadataFromAnnotations(module *ast.Module) (*StaticMetadata, error) {
Copy link
Member

@simar7 simar7 Mar 8, 2025

Choose a reason for hiding this comment

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

I see that it is tested with higher level tests but I feel we can write some tests for these function in isolation as it seems to critical and potentially re-usable as it's exported.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added tests eb72e1d

@simar7 simar7 added this pull request to the merge queue Mar 11, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to a conflict with the base branch Mar 11, 2025
@nikpivkin nikpivkin added this pull request to the merge queue Mar 11, 2025
Merged via the queue into aquasecurity:main with commit 7b96351 Mar 11, 2025
12 checks passed
@nikpivkin nikpivkin deleted the perf/metadata branch March 11, 2025 05:09
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