-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
fix(oracle): handle advisories contain ksplice versions #1209
Conversation
Improve a handling of advisories contain ksplice versions: * when one of them doesn't have ksplice, we'll also skip it * extract kspliceX and compare it with kspliceY in advisories * if kspliceX and kspliceY are different, we will skip the advisory. Fixes aquasecurity#1205
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Thanks. I suggested some ideas to simplify code.
pkg/detector/ospkg/oracle/oracle.go
Outdated
func extractKspliceNumber(v string) string { | ||
subs := strings.Split(strings.ToLower(v), "ksplice") | ||
if len(subs) != 2 { | ||
return "" | ||
} | ||
ns := strings.Split(subs[1], ".") | ||
if len(ns) == 0 { | ||
return "" | ||
} | ||
return ns[0] | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we can assume ksplice is separated by dot, how about this?
func extractKspliceNumber(v string) string { | |
subs := strings.Split(strings.ToLower(v), "ksplice") | |
if len(subs) != 2 { | |
return "" | |
} | |
ns := strings.Split(subs[1], ".") | |
if len(ns) == 0 { | |
return "" | |
} | |
return ns[0] | |
} | |
func extractKspliceNumber(v string) string { | |
subs := strings.Split(strings.ToLower(v), ".") | |
for _, s := range subs { | |
if strings.HasPrefix(v, "ksplice") { | |
return s | |
} | |
} | |
return "" | |
} | |
pkg/detector/ospkg/oracle/oracle.go
Outdated
if isKSlice := strings.Contains(adv.FixedVersion, ".ksplice"); isKSlice != strings.Contains(pkg.Release, ".ksplice") { | ||
continue | ||
} else if isKSlice { | ||
releaseV := extractKspliceNumber(adv.FixedVersion) | ||
fixedV := extractKspliceNumber(pkg.Release) | ||
if releaseV != fixedV { | ||
continue | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can simplify it.
if isKSlice := strings.Contains(adv.FixedVersion, ".ksplice"); isKSlice != strings.Contains(pkg.Release, ".ksplice") { | |
continue | |
} else if isKSlice { | |
releaseV := extractKspliceNumber(adv.FixedVersion) | |
fixedV := extractKspliceNumber(pkg.Release) | |
if releaseV != fixedV { | |
continue | |
} | |
if extractKspliceNumber(adv.FixedVersion) != extractKspliceNumber(pkg.Release) { | |
continue | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, your was is better!
{ | ||
name: "the correct installed version without ksplice2", | ||
fixtures: []string{"testdata/fixtures/oracle8.yaml"}, | ||
args: args{ | ||
osVer: "8", | ||
pkgs: []ftypes.Package{ | ||
{ | ||
Name: "glibc", | ||
Version: "2.28", | ||
Release: "151.0.1.el8", | ||
Arch: "x86_64", | ||
SrcName: "glibc", | ||
SrcVersion: "2.28", | ||
SrcRelease: "151.0.1.el8", | ||
}, | ||
}, | ||
}, | ||
want: nil, | ||
}, | ||
{ | ||
name: "the correct installed version with ksplice2", | ||
fixtures: []string{"testdata/fixtures/oracle8.yaml"}, | ||
args: args{ | ||
osVer: "8", | ||
pkgs: []ftypes.Package{ | ||
{ | ||
Name: "glibc", | ||
Epoch: 2, | ||
Version: "2.28", | ||
Release: "151.0.1.ksplice2.el8", | ||
Arch: "x86_64", | ||
SrcEpoch: 2, | ||
SrcName: "glibc", | ||
SrcVersion: "2.28", | ||
SrcRelease: "151.0.1.ksplice2.el8", | ||
}, | ||
}, | ||
}, | ||
want: nil, | ||
}, | ||
{ | ||
name: "the installed version has ksplice1", | ||
fixtures: []string{"testdata/fixtures/oracle8.yaml"}, | ||
args: args{ | ||
osVer: "8", | ||
pkgs: []ftypes.Package{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have the same test for ksplice1. These tests are duplicate. We should add only 1 test where the installed version has ksplice2.
func TestGetKspliceNumber(t *testing.T) { | ||
tests := []struct { | ||
in string | ||
res string | ||
}{ | ||
{"", ""}, | ||
{"2:2.17-157.el7_3.4", ""}, | ||
{"2:2.17-157.ksplice1.el7_3.4", "1"}, | ||
{"2:2.28-151.0.1.ksplice2.el8", "2"}, | ||
{"2:2.28-151.0.1.ksplice2", "2"}, | ||
{"2:2.28-151.0.1.KSplice2", "2"}, | ||
} | ||
for _, test := range tests { | ||
if got := extractKspliceNumber(test.in); got != test.res { | ||
t.Errorf("getKspliceNumber(%q) got %q, want %q", test.in, got, test.res) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can test this method through TestScanner_Detect
simplify code and remove duplicated tests Fixes aquasecurity#1205
* fix(oracle): handle advisories contain ksplice versions Improve a handling of advisories contain ksplice versions: * when one of them doesn't have ksplice, we'll also skip it * extract kspliceX and compare it with kspliceY in advisories * if kspliceX and kspliceY are different, we will skip the advisory. Fixes #1205 * fix(oracle): handle advisories contain ksplice versions simplify code and remove duplicated tests Fixes #1205 * run go fmt
Improve a handling of advisories contain ksplice versions:
Fixes #1205