-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: trivy-v0.37.x deprecate flags support (#949)
* fix: trivy-v0.37.x deprecate flag support Signed-off-by: chenk <[email protected]> * fix: trivy-v0.37.x deprecate flag support Signed-off-by: chenk <[email protected]> * fix: trivy-v0.37.x deprecate flag support Signed-off-by: chenk <[email protected]> * fix: trivy-v0.37.x deprecate flag support Signed-off-by: chenk <[email protected]> * fix: trivy-v0.37.x deprecate flag support Signed-off-by: chenk <[email protected]> * fix: trivy-v0.37.x deprecate flag support Signed-off-by: chenk <[email protected]> * fix: trivy-v0.37.x deprecate flag support Signed-off-by: chenk <[email protected]> --------- Signed-off-by: chenk <[email protected]>
- Loading branch information
1 parent
e8e1b9a
commit 9c8fe92
Showing
9 changed files
with
243 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package trivy | ||
|
||
import ( | ||
"github.com/Masterminds/semver" | ||
) | ||
|
||
func compareTagVersion(currentTag string, contraint string) bool { | ||
c, err := semver.NewConstraint(contraint) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
v, err := semver.NewVersion(currentTag) | ||
if err != nil { | ||
return false | ||
} | ||
// Check if the version meets the constraints. The a variable will be true. | ||
return c.Check(v) | ||
} | ||
|
||
// Slow determine if to use the slow flag (improve memory footprint) | ||
func Slow(c Config) string { | ||
tag, err := c.GetImageTag() | ||
if err != nil { | ||
return "" | ||
} | ||
// support backward competability with older tags | ||
if compareTagVersion(tag, "< 0.35.0") { | ||
return "" | ||
} | ||
if c.GetSlow() { | ||
return "--slow" | ||
} | ||
return "" | ||
} | ||
|
||
// Scanners use scanners flag | ||
func Scanners(c Config) string { | ||
tag, err := c.GetImageTag() | ||
if err != nil { | ||
return "--scanners" | ||
} | ||
// support backward competability with older tags | ||
if compareTagVersion(tag, "< 0.37.0") { | ||
return "--security-checks" | ||
} | ||
return "--scanners" | ||
} | ||
|
||
// SkipDBUpdate skip update flag | ||
func SkipDBUpdate(c Config) string { | ||
tag, err := c.GetImageTag() | ||
if err != nil { | ||
return "--skip-db-update" | ||
} | ||
// support backward competability with older tags | ||
if compareTagVersion(tag, "< 0.37.0") { | ||
return "--skip-update" | ||
} | ||
return "--skip-db-update" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package trivy_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aquasecurity/trivy-operator/pkg/plugins/trivy" | ||
"github.com/aquasecurity/trivy-operator/pkg/trivyoperator" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSlow(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
configData trivyoperator.ConfigData | ||
want string | ||
}{{ | ||
|
||
name: "slow param set to true", | ||
configData: map[string]string{ | ||
"trivy.tag": "0.35.0", | ||
"trivy.slow": "true", | ||
}, | ||
want: "--slow", | ||
}, | ||
{ | ||
name: "slow param set to false", | ||
configData: map[string]string{ | ||
"trivy.tag": "0.35.0", | ||
"trivy.slow": "false", | ||
}, | ||
want: "", | ||
}, | ||
{ | ||
name: "slow param set to no valid value", | ||
configData: map[string]string{ | ||
"trivy.tag": "0.35.0", | ||
"trivy.slow": "false2", | ||
}, | ||
want: "--slow", | ||
}, | ||
{ | ||
name: "slow param set to true and trivy tag is less then 0.35.0", | ||
configData: map[string]string{ | ||
"trivy.slow": "true", | ||
"trivy.tag": "0.33.0", | ||
}, | ||
want: "", | ||
}, | ||
|
||
{ | ||
name: "slow param set to true and trivy tag is bigger then 0.35.0", | ||
configData: map[string]string{ | ||
"trivy.slow": "true", | ||
"trivy.tag": "0.36.0", | ||
}, | ||
want: "--slow", | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
got := trivy.Slow(trivy.Config{trivyoperator.PluginConfig{Data: tc.configData}}) | ||
assert.Equal(t, got, tc.want) | ||
}) | ||
} | ||
} | ||
|
||
func TestScanner(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
configData trivyoperator.ConfigData | ||
want string | ||
}{{ | ||
|
||
name: "scanner with trivy tag lower then v0.37.0", | ||
configData: map[string]string{ | ||
"trivy.tag": "0.36.0", | ||
}, | ||
want: "--security-checks", | ||
}, | ||
{ | ||
name: "scanner with trivy tag equal then v0.37.0", | ||
configData: map[string]string{ | ||
"trivy.tag": "0.37.0", | ||
}, | ||
want: "--scanners", | ||
}, | ||
{ | ||
name: "scanner with trivy tag higher then v0.38.0", | ||
configData: map[string]string{ | ||
"trivy.tag": "0.38.0", | ||
}, | ||
want: "--scanners", | ||
}, | ||
{ | ||
name: "scanner with no trivy tag lower", | ||
configData: map[string]string{}, | ||
want: "--scanners", | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
got := trivy.Scanners(trivy.Config{trivyoperator.PluginConfig{Data: tc.configData}}) | ||
assert.Equal(t, got, tc.want) | ||
}) | ||
} | ||
} | ||
|
||
func TestSkipDBUpdate(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
configData trivyoperator.ConfigData | ||
want string | ||
}{{ | ||
|
||
name: "skip update DB with trivy tag lower then v0.37.0", | ||
configData: map[string]string{ | ||
"trivy.tag": "0.36.0", | ||
}, | ||
want: "--skip-update", | ||
}, | ||
{ | ||
name: "skip update DB with trivy tag equal then v0.37.0", | ||
configData: map[string]string{ | ||
"trivy.tag": "0.37.0", | ||
}, | ||
want: "--skip-db-update", | ||
}, | ||
{ | ||
name: "skip update DB with trivy tag higher then v0.38.0", | ||
configData: map[string]string{ | ||
"trivy.tag": "0.38.0", | ||
}, | ||
want: "--skip-db-update", | ||
}, | ||
{ | ||
name: "skip update DB with no trivy tag lower", | ||
configData: map[string]string{}, | ||
want: "--skip-db-update", | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
got := trivy.SkipDBUpdate(trivy.Config{trivyoperator.PluginConfig{Data: tc.configData}}) | ||
assert.Equal(t, got, tc.want) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.