Skip to content

Commit

Permalink
fix(redhat): possibility of false positives on RHEL (#1115)
Browse files Browse the repository at this point in the history
  • Loading branch information
kotakanbe committed Jan 6, 2021
1 parent b13f93a commit 4359503
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
13 changes: 8 additions & 5 deletions oval/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,6 @@ func isOvalDefAffected(def ovalmodels.Definition, req request, family string, ru
return false, false, ""
}

var centosVerPattern = regexp.MustCompile(`\.[es]l(\d+)(?:_\d+)?(?:\.centos)?`)
var esVerPattern = regexp.MustCompile(`\.el(\d+)(?:_\d+)?`)

func lessThan(family, newVer string, packInOVAL ovalmodels.Package) (bool, error) {
switch family {
case config.Debian,
Expand Down Expand Up @@ -416,12 +413,18 @@ func lessThan(family, newVer string, packInOVAL ovalmodels.Package) (bool, error

case config.RedHat,
config.CentOS:
vera := rpmver.NewVersion(centosVerPattern.ReplaceAllString(newVer, ".el$1"))
verb := rpmver.NewVersion(esVerPattern.ReplaceAllString(packInOVAL.Version, ".el$1"))
vera := rpmver.NewVersion(centOSVersionToRHEL(newVer))
verb := rpmver.NewVersion(packInOVAL.Version)
return vera.LessThan(verb), nil

default:
util.Log.Errorf("Not implemented yet: %s", family)
}
return false, xerrors.Errorf("Package version comparison not supported: %s", family)
}

var centosVerPattern = regexp.MustCompile(`\.[es]l(\d+)(?:_\d+)?(?:\.centos)?`)

func centOSVersionToRHEL(ver string) string {
return centosVerPattern.ReplaceAllString(ver, ".el$1")
}
33 changes: 33 additions & 0 deletions oval/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1193,3 +1193,36 @@ func Test_major(t *testing.T) {
}
}
}

func Test_centOSVersionToRHEL(t *testing.T) {
type args struct {
ver string
}
tests := []struct {
name string
args args
want string
}{
{
name: "remove centos.",
args: args{
ver: "grub2-tools-2.02-0.80.el7.centos.x86_64",
},
want: "grub2-tools-2.02-0.80.el7.x86_64",
},
{
name: "noop",
args: args{
ver: "grub2-tools-2.02-0.80.el7.x86_64",
},
want: "grub2-tools-2.02-0.80.el7.x86_64",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := centOSVersionToRHEL(tt.args.ver); got != tt.want {
t.Errorf("centOSVersionToRHEL() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 4359503

Please sign in to comment.