Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.

Conversation

simar7
Copy link
Member

@simar7 simar7 commented Apr 14, 2023

This adds the SetUseEmbeddedLibraries option for scanners. It is needed in cases where we only want to load libraries but not policies.

Signed-off-by: Simar [email protected]

@simar7 simar7 force-pushed the enable-embedded-libraries-option branch from c606912 to d885472 Compare April 14, 2023 20:24
@simar7 simar7 marked this pull request as ready for review April 14, 2023 20:55
@simar7 simar7 requested a review from giorod3 as a code owner April 14, 2023 20:55
@simar7 simar7 requested a review from chen-keinan April 14, 2023 22:48
@simar7
Copy link
Member Author

simar7 commented Apr 14, 2023

@chen-keinan could you help me review this?

I have made a PR in trivy to adapt to this change: aquasecurity/trivy#4070 - are there other users of defsec that you know that will need a change?

@simar7 simar7 force-pushed the enable-embedded-libraries-option branch from d885472 to 7e92a22 Compare April 17, 2023 22:35
chen-keinan
chen-keinan previously approved these changes Apr 18, 2023
regoScanner := rego.NewScanner(types.SourceCloud, s.scannerOptions...)
regoScanner.SetParentDebugLogger(s.debug)
if err := regoScanner.LoadPolicies(s.loadEmbedded, srcFS, s.policyDirs, s.policyReaders); err != nil {
if err := regoScanner.LoadPolicies(s.loadEmbeddedLibraries, s.loadEmbeddedPolicies, srcFS, s.policyDirs, s.policyReaders); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: maybe pass s (Scanner) to LoadPolicies method as there are too many params

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah that's a good idea, however s could be any type of Scanner. I tried to implement this with generics but quickly ran into a dead end that a method on a receiver where the base (in this case rego scanner) isn't generic cannot accept generic parameters. https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md#No-parameterized-methods

I will revisit this again. It will be a lot cleaner if we only passed s since all function inputs (except srcFS) come from s.

regoScanner := rego.NewScanner(types.SourceCloud, s.options...)
regoScanner.SetParentDebugLogger(s.debug)
if err := regoScanner.LoadPolicies(s.useEmbedded, srcFS, s.policyDirs, s.policyReaders); err != nil {
if err := regoScanner.LoadPolicies(s.loadEmbeddedLibraries, s.loadEmbeddedPolicies, srcFS, s.policyDirs, s.policyReaders); err != nil {
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 comment

regoScanner := rego.NewScanner(types.SourceCloud, s.options...)
regoScanner.SetParentDebugLogger(s.debug)
if err := regoScanner.LoadPolicies(s.loadEmbedded, srcFS, s.policyDirs, s.policyReaders); err != nil {
if err := regoScanner.LoadPolicies(s.loadEmbeddedLibraries, s.loadEmbeddedPolicies, srcFS, s.policyDirs, s.policyReaders); err != nil {
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

regoScanner := rego.NewScanner(types.SourceDockerfile, s.options...)
regoScanner.SetParentDebugLogger(s.debug)
if err := regoScanner.LoadPolicies(s.loadEmbedded, srcFS, s.policyDirs, s.policyReaders); err != nil {
if err := regoScanner.LoadPolicies(s.loadEmbeddedLibraries, s.loadEmbeddedPolicies, srcFS, s.policyDirs, s.policyReaders); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

+1

policyFS = s.policyFS
}
if err := regoScanner.LoadPolicies(s.loadEmbedded, policyFS, s.policyDirs, s.policyReaders); err != nil {
if err := regoScanner.LoadPolicies(s.loadEmbeddedLibraries, s.loadEmbeddedPolicies, policyFS, s.policyDirs, s.policyReaders); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

+1

regoScanner := rego.NewScanner(types.SourceKubernetes, s.options...)
regoScanner.SetParentDebugLogger(s.debug)
if err := regoScanner.LoadPolicies(s.loadEmbedded, srcFS, s.policyDirs, s.policyReaders); err != nil {
if err := regoScanner.LoadPolicies(s.loadEmbeddedLibraries, s.loadEmbeddedPolicies, srcFS, s.policyDirs, s.policyReaders); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

+1

regoScanner := rego.NewScanner(types.SourceTOML, s.options...)
regoScanner.SetParentDebugLogger(s.debug)
if err := regoScanner.LoadPolicies(s.loadEmbedded, srcFS, s.policyDirs, s.policyReaders); err != nil {
if err := regoScanner.LoadPolicies(s.loadEmbeddedLibraries, s.loadEmbeddedPolicies, srcFS, s.policyDirs, s.policyReaders); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

+1

regoScanner := rego.NewScanner(types.SourceYAML, s.options...)
regoScanner.SetParentDebugLogger(s.debug)
if err := regoScanner.LoadPolicies(s.loadEmbedded, srcFS, s.policyDirs, s.policyReaders); err != nil {
if err := regoScanner.LoadPolicies(s.loadEmbeddedLibraries, s.loadEmbeddedPolicies, srcFS, s.policyDirs, s.policyReaders); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

+1

require.NoError(t, err)

scanner := kubernetes.NewScanner(options.ScannerWithEmbeddedPolicies(true))
scanner := kubernetes.NewScanner(options.ScannerWithEmbeddedPolicies(true), options.ScannerWithEmbeddedLibraries(true), options.ScannerWithEmbeddedLibraries(true))
Copy link
Contributor

Choose a reason for hiding this comment

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

+1

`

results, err := NewScanner(options.ScannerWithEmbeddedPolicies(true)).ScanReader(context.TODO(), "k8s.yaml", strings.NewReader(file))
results, err := NewScanner(options.ScannerWithEmbeddedPolicies(true), options.ScannerWithEmbeddedLibraries(true), options.ScannerWithEmbeddedLibraries(true)).ScanReader(context.TODO(), "k8s.yaml", strings.NewReader(file))
Copy link
Contributor

Choose a reason for hiding this comment

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

nit:

Suggested change
results, err := NewScanner(options.ScannerWithEmbeddedPolicies(true), options.ScannerWithEmbeddedLibraries(true), options.ScannerWithEmbeddedLibraries(true)).ScanReader(context.TODO(), "k8s.yaml", strings.NewReader(file))
results, err := NewScanner(
options.ScannerWithEmbeddedPolicies(true),
options.ScannerWithEmbeddedLibraries(true),
options.ScannerWithEmbeddedLibraries(true)).ScanReader(context.TODO(), "k8s.yaml", strings.NewReader(file))

@chen-keinan
Copy link
Contributor

@simar7 look good, I have added few nit comments , its up to you (no blocker)

@simar7 simar7 force-pushed the enable-embedded-libraries-option branch from 7e92a22 to d0fadf8 Compare April 19, 2023 01:28
@simar7 simar7 force-pushed the enable-embedded-libraries-option branch from ba33c6f to 220ede3 Compare April 19, 2023 05:58
@simar7 simar7 merged commit 97ff1b4 into master Apr 20, 2023
@simar7 simar7 deleted the enable-embedded-libraries-option branch April 20, 2023 04:15
simar7 added a commit that referenced this pull request Apr 27, 2023
simar7 added a commit that referenced this pull request Apr 28, 2023
simar7 added a commit that referenced this pull request Jun 23, 2023
simar7 added a commit that referenced this pull request Jul 5, 2023
simar7 added a commit that referenced this pull request Jul 8, 2023
simar7 added a commit that referenced this pull request Jul 11, 2023
simar7 added a commit that referenced this pull request Jul 16, 2023
simar7 added a commit that referenced this pull request Jul 16, 2023
…d policies (#1280)" (#1298)" (#1357)

* Revert "Revert "feat(scanner): Break out options for enabling libs and policies (#1280)" (#1298)"

This reverts commit 63a8b4f.

* add loadembedded for terraformplan

Signed-off-by: Simar <[email protected]>

* fix tests

Signed-off-by: Simar <[email protected]>

---------

Signed-off-by: Simar <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants