Skip to content

Commit

Permalink
rename PackageOverrides
Browse files Browse the repository at this point in the history
  • Loading branch information
josieang committed Jun 6, 2024
1 parent 95efa12 commit cf09e19
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 42 deletions.
4 changes: 2 additions & 2 deletions fixtures/testdatainner/osv-scanner.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ id = "GO-2022-1059"
# ignoreUntil = 2022-11-09 # Optional exception expiry date
# reason = "" # Optional reason

[[PackageVersions]]
[[PackageOverrides]]
name = "lib"
version = "1.0.0"
ecosystem = "Go"
ignore = true
# effectiveUntil = 2022-11-09 # Optional exception expiry date
reason = "abc"

[[PackageVersions]]
[[PackageOverrides]]
name = "my-pkg"
version = "1.0.0"
ecosystem = "Go"
Expand Down
21 changes: 11 additions & 10 deletions internal/sourceanalysis/__snapshots__/integration_test.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

[Test_runGovulncheck - 1]
{
"GO-2021-0053": [
Expand Down Expand Up @@ -124,7 +125,7 @@
"position": {
"filename": "\u003cAny value\u003e",
"offset": -1,
"line": 839,
"line": 840,
"column": 21
}
},
Expand All @@ -137,7 +138,7 @@
"position": {
"filename": "\u003cAny value\u003e",
"offset": -1,
"line": 1038,
"line": 1039,
"column": 24
}
},
Expand All @@ -149,7 +150,7 @@
"position": {
"filename": "\u003cAny value\u003e",
"offset": -1,
"line": 430,
"line": 429,
"column": 21
}
},
Expand All @@ -161,7 +162,7 @@
"position": {
"filename": "\u003cAny value\u003e",
"offset": -1,
"line": 389,
"line": 388,
"column": 19
}
},
Expand All @@ -174,7 +175,7 @@
"position": {
"filename": "\u003cAny value\u003e",
"offset": -1,
"line": 1002,
"line": 1003,
"column": 19
}
},
Expand All @@ -187,7 +188,7 @@
"position": {
"filename": "\u003cAny value\u003e",
"offset": -1,
"line": 1670,
"line": 1675,
"column": 17
}
},
Expand All @@ -200,7 +201,7 @@
"position": {
"filename": "\u003cAny value\u003e",
"offset": -1,
"line": 2015,
"line": 2045,
"column": 18
}
},
Expand All @@ -213,7 +214,7 @@
"position": {
"filename": "\u003cAny value\u003e",
"offset": -1,
"line": 3086,
"line": 3285,
"column": 3
}
},
Expand All @@ -226,7 +227,7 @@
"position": {
"filename": "\u003cAny value\u003e",
"offset": -1,
"line": 2985,
"line": 3184,
"column": 18
}
},
Expand All @@ -238,7 +239,7 @@
"position": {
"filename": "\u003cAny value\u003e",
"offset": -1,
"line": 3239,
"line": 3438,
"column": 30
}
},
Expand Down
26 changes: 13 additions & 13 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ type ConfigManager struct {
}

type Config struct {
IgnoredVulns []IgnoreEntry `toml:"IgnoredVulns"`
PackageVersions []PackageVersionEntry `toml:"PackageVersions"`
LoadPath string `toml:"LoadPath"`
GoVersionOverride string `toml:"GoVersionOverride"`
IgnoredVulns []IgnoreEntry `toml:"IgnoredVulns"`
PackageOverrides []PackageOverrideEntry `toml:"PackageOverrides"`
LoadPath string `toml:"LoadPath"`
GoVersionOverride string `toml:"GoVersionOverride"`
}

type IgnoreEntry struct {
Expand All @@ -35,7 +35,7 @@ type IgnoreEntry struct {
Reason string `toml:"reason"`
}

type PackageVersionEntry struct {
type PackageOverrideEntry struct {
Name string `toml:"name"`
// If the version is empty, the entry applies to all versions.
Version string `toml:"version"`
Expand All @@ -60,30 +60,30 @@ func (c *Config) ShouldIgnore(vulnID string) (bool, IgnoreEntry) {
return shouldIgnoreTimestamp(ignoredLine.IgnoreUntil), ignoredLine
}

func (c *Config) filterPackageVersionEntries(name string, version string, ecosystem string, condition func(PackageVersionEntry) bool) (bool, PackageVersionEntry) {
index := slices.IndexFunc(c.PackageVersions, func(e PackageVersionEntry) bool {
func (c *Config) filterPackageVersionEntries(name string, version string, ecosystem string, condition func(PackageOverrideEntry) bool) (bool, PackageOverrideEntry) {
index := slices.IndexFunc(c.PackageOverrides, func(e PackageOverrideEntry) bool {
if ecosystem != e.Ecosystem || name != e.Name {
return false
}

return (version == e.Version || e.Version == "") && condition(e)
})
if index == -1 {
return false, PackageVersionEntry{}
return false, PackageOverrideEntry{}
}
ignoredLine := c.PackageVersions[index]
ignoredLine := c.PackageOverrides[index]

return shouldIgnoreTimestamp(ignoredLine.EffectiveUntil), ignoredLine
}

func (c *Config) ShouldIgnorePackageVersion(name, version, ecosystem string) (bool, PackageVersionEntry) {
return c.filterPackageVersionEntries(name, version, ecosystem, func(e PackageVersionEntry) bool {
func (c *Config) ShouldIgnorePackageVersion(name, version, ecosystem string) (bool, PackageOverrideEntry) {
return c.filterPackageVersionEntries(name, version, ecosystem, func(e PackageOverrideEntry) bool {
return e.Ignore
})
}

func (c *Config) ShouldOverridePackageVersionLicense(name, version, ecosystem string) (bool, PackageVersionEntry) {
return c.filterPackageVersionEntries(name, version, ecosystem, func(e PackageVersionEntry) bool {
func (c *Config) ShouldOverridePackageVersionLicense(name, version, ecosystem string) (bool, PackageOverrideEntry) {
return c.filterPackageVersionEntries(name, version, ecosystem, func(e PackageOverrideEntry) bool {
return len(e.License.Override) > 0
})
}
Expand Down
32 changes: 16 additions & 16 deletions pkg/config/config_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestTryLoadConfig(t *testing.T) {
ID: "GO-2022-1059",
},
},
PackageVersions: []PackageVersionEntry{
PackageOverrides: []PackageOverrideEntry{
{
Name: "lib",
Version: "1.0.0",
Expand Down Expand Up @@ -88,7 +88,7 @@ func TestTryLoadConfig(t *testing.T) {
if !cmp.Equal(config.IgnoredVulns, testData.config.IgnoredVulns) {
t.Errorf("Configs not equal: %+v != %+v", config, testData.config)
}
if !cmp.Equal(config.PackageVersions, testData.config.PackageVersions) {
if !cmp.Equal(config.PackageOverrides, testData.config.PackageOverrides) {
t.Errorf("Configs not equal: %+v != %+v", config, testData.config)
}
if testData.configHasErr {
Expand Down Expand Up @@ -227,12 +227,12 @@ func TestConfig_ShouldIgnorePackageVersion(t *testing.T) {
config Config
args args
wantOk bool
wantEntry PackageVersionEntry
wantEntry PackageOverrideEntry
}{
{
name: "Version-level entry exists",
config: Config{
PackageVersions: []PackageVersionEntry{
PackageOverrides: []PackageOverrideEntry{
{
Name: "lib1",
Version: "1.0.0",
Expand All @@ -249,7 +249,7 @@ func TestConfig_ShouldIgnorePackageVersion(t *testing.T) {
ecosystem: "Go",
},
wantOk: true,
wantEntry: PackageVersionEntry{
wantEntry: PackageOverrideEntry{
Name: "lib1",
Version: "1.0.0",
Ecosystem: "Go",
Expand All @@ -261,7 +261,7 @@ func TestConfig_ShouldIgnorePackageVersion(t *testing.T) {
{
name: "Package-level entry exists",
config: Config{
PackageVersions: []PackageVersionEntry{
PackageOverrides: []PackageOverrideEntry{
{
Name: "lib1",
Ecosystem: "Go",
Expand All @@ -277,7 +277,7 @@ func TestConfig_ShouldIgnorePackageVersion(t *testing.T) {
ecosystem: "Go",
},
wantOk: true,
wantEntry: PackageVersionEntry{
wantEntry: PackageOverrideEntry{
Name: "lib1",
Ecosystem: "Go",
Ignore: true,
Expand All @@ -288,7 +288,7 @@ func TestConfig_ShouldIgnorePackageVersion(t *testing.T) {
{
name: "Entry doesn't exist",
config: Config{
PackageVersions: []PackageVersionEntry{
PackageOverrides: []PackageOverrideEntry{
{
Name: "lib1",
Version: "2.0.0",
Expand All @@ -313,7 +313,7 @@ func TestConfig_ShouldIgnorePackageVersion(t *testing.T) {
ecosystem: "Go",
},
wantOk: false,
wantEntry: PackageVersionEntry{},
wantEntry: PackageOverrideEntry{},
},
}

Expand Down Expand Up @@ -346,12 +346,12 @@ func TestConfig_ShouldOverridePackageVersionLicense(t *testing.T) {
config Config
args args
wantOk bool
wantEntry PackageVersionEntry
wantEntry PackageOverrideEntry
}{
{
name: "Exact version entry exists",
config: Config{
PackageVersions: []PackageVersionEntry{
PackageOverrides: []PackageOverrideEntry{
{
Name: "lib1",
Version: "1.0.0",
Expand All @@ -369,7 +369,7 @@ func TestConfig_ShouldOverridePackageVersionLicense(t *testing.T) {
ecosystem: "Go",
},
wantOk: true,
wantEntry: PackageVersionEntry{
wantEntry: PackageOverrideEntry{
Name: "lib1",
Version: "1.0.0",
Ecosystem: "Go",
Expand All @@ -382,7 +382,7 @@ func TestConfig_ShouldOverridePackageVersionLicense(t *testing.T) {
{
name: "Version entry doesn't exist",
config: Config{
PackageVersions: []PackageVersionEntry{
PackageOverrides: []PackageOverrideEntry{
{
Name: "lib1",
Version: "1.0.0",
Expand All @@ -400,12 +400,12 @@ func TestConfig_ShouldOverridePackageVersionLicense(t *testing.T) {
ecosystem: "Go",
},
wantOk: false,
wantEntry: PackageVersionEntry{},
wantEntry: PackageOverrideEntry{},
},
{
name: "Name matches",
config: Config{
PackageVersions: []PackageVersionEntry{
PackageOverrides: []PackageOverrideEntry{
{
Name: "lib1",
Ecosystem: "Go",
Expand All @@ -422,7 +422,7 @@ func TestConfig_ShouldOverridePackageVersionLicense(t *testing.T) {
ecosystem: "Go",
},
wantOk: true,
wantEntry: PackageVersionEntry{
wantEntry: PackageOverrideEntry{
Name: "lib1",
Ecosystem: "Go",
License: License{
Expand Down
2 changes: 1 addition & 1 deletion pkg/osvscanner/vulnerability_result_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func Test_assembleResult(t *testing.T) {
},
config: &config.ConfigManager{
OverrideConfig: &config.Config{
PackageVersions: []config.PackageVersionEntry{
PackageOverrides: []config.PackageOverrideEntry{
{
Name: "pkg-3",
Ecosystem: "npm",
Expand Down

0 comments on commit cf09e19

Please sign in to comment.