Skip to content

Commit

Permalink
fix(models/redhat): fix collectRedHatPacks (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
masahiro331 authored May 24, 2023
1 parent 1fd1251 commit eb88caf
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 17 deletions.
2 changes: 1 addition & 1 deletion commands/fetch-redhat.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func fetchRedHat(_ *cobra.Command, args []string) (err error) {
root := models.Root{
Family: c.RedHat,
OSVersion: v,
Definitions: redhat.ConvertToModel(roots),
Definitions: redhat.ConvertToModel(v, roots),
Timestamp: time.Now(),
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/htcat/htcat v1.0.2
github.com/inconshreveable/log15 v3.0.0-testing.5+incompatible
github.com/k0kubun/pp v3.0.1+incompatible
github.com/knqyf263/go-rpm-version v0.0.0-20220614171824-631e686d1075
github.com/labstack/echo/v4 v4.10.2
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.7.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q
github.com/k0kubun/pp v3.0.1+incompatible h1:3tqvf7QgUnZ5tXO6pNAZlrvHgl6DvifjDrd9g2S9Z40=
github.com/k0kubun/pp v3.0.1+incompatible/go.mod h1:GWse8YhT0p8pT4ir3ZgBbfZild3tgzSScAn6HmfYukg=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/knqyf263/go-rpm-version v0.0.0-20220614171824-631e686d1075 h1:aC6MEAs3PE3lWD7lqrJfDxHd6hcced9R4JTZu85cJwU=
github.com/knqyf263/go-rpm-version v0.0.0-20220614171824-631e686d1075/go.mod h1:i4sF0l1fFnY1aiw08QQSwVAFxHEm311Me3WsU/X7nL0=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
Expand Down
30 changes: 26 additions & 4 deletions models/redhat/redhat.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package redhat

import (
"fmt"
"strings"
"time"

version "github.com/knqyf263/go-rpm-version"
"github.com/spf13/viper"
"golang.org/x/exp/maps"

Expand All @@ -12,7 +14,7 @@ import (
)

// ConvertToModel Convert OVAL to models
func ConvertToModel(roots []Root) []models.Definition {
func ConvertToModel(v string, roots []Root) []models.Definition {
defs := map[string]models.Definition{}
for _, root := range roots {
for _, d := range root.Definitions.Definitions {
Expand Down Expand Up @@ -74,7 +76,7 @@ func ConvertToModel(roots []Root) []models.Definition {
Updated: updated,
},
Debian: nil,
AffectedPacks: collectRedHatPacks(d.Criteria),
AffectedPacks: collectRedHatPacks(v, d.Criteria),
References: rs,
}

Expand All @@ -97,11 +99,31 @@ func ConvertToModel(roots []Root) []models.Definition {
return maps.Values(defs)
}

func collectRedHatPacks(cri Criteria) []models.Package {
func collectRedHatPacks(v string, cri Criteria) []models.Package {
ps := walkRedHat(cri, []models.Package{}, "")
pkgs := map[string]models.Package{}
for _, p := range ps {
pkgs[p.Name] = p
// OVALv1 includes definitions other than the target RHEL version
if !strings.Contains(p.Version, ".el"+v) && !strings.Contains(p.Version, ".module+el"+v) {
continue
}

n := p.Name
if p.ModularityLabel != "" {
n = fmt.Sprintf("%s::%s", p.ModularityLabel, p.Name)
}

// since different versions are defined for the same package, the newer version is adopted
// example: OVALv2: oval:com.redhat.rhsa:def:20111349, oval:com.redhat.rhsa:def:20120451
if base, ok := pkgs[n]; ok {
v1 := version.NewVersion(base.Version)
v2 := version.NewVersion(p.Version)
if v1.GreaterThan(v2) {
p = base
}
}

pkgs[n] = p
}
return maps.Values(pkgs)
}
Expand Down
116 changes: 104 additions & 12 deletions models/redhat/redhat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (

func TestWalkRedHat(t *testing.T) {
var tests = []struct {
version string
cri Criteria
expected []models.Package
}{
// 0
{
version: "6",
cri: Criteria{
Criterions: []Criterion{
{Comment: "kernel-headers is earlier than 0:2.6.32-71.7.1.el6"},
Expand All @@ -29,8 +30,8 @@ func TestWalkRedHat(t *testing.T) {
},
},
},
// 1
{
version: "6",
cri: Criteria{
Criterias: []Criteria{
{
Expand All @@ -56,8 +57,8 @@ func TestWalkRedHat(t *testing.T) {
},
},
},
// 2
{
version: "6",
cri: Criteria{
Criterias: []Criteria{
{
Expand Down Expand Up @@ -106,8 +107,82 @@ func TestWalkRedHat(t *testing.T) {
},
},
},
// 3 dnf module
{
version: "6",
cri: Criteria{
Criterias: []Criteria{
{
Criterias: []Criteria{
{
Criterions: []Criterion{
{Comment: "rpm is earlier than 0:4.8.0-12.el6_0.2"},
},
},
},
Criterions: []Criterion{
{Comment: "Red Hat Enterprise Linux 6 is installed"},
},
},
{
Criterias: []Criteria{
{
Criterions: []Criterion{
{Comment: "rpm is earlier than 0:4.8.0-19.el6_2.1"},
},
},
},
Criterions: []Criterion{
{Comment: "Red Hat Enterprise Linux 6 is installed"},
},
},
},
},
expected: []models.Package{
{
Name: "rpm",
Version: "0:4.8.0-19.el6_2.1",
},
},
},
{
version: "6",
cri: Criteria{
Criterias: []Criteria{
{
Criterias: []Criteria{
{
Criterions: []Criterion{
{Comment: "rpm is earlier than 0:4.8.0-12.el6_0.2"},
},
},
},
Criterions: []Criterion{
{Comment: "Red Hat Enterprise Linux 6 is installed"},
},
},
{
Criterias: []Criteria{
{
Criterions: []Criterion{
{Comment: "rpm is earlier than 0:4.8.0-19.el7_0.1"},
},
},
},
Criterions: []Criterion{
{Comment: "Red Hat Enterprise Linux 7 is installed"},
},
},
},
},
expected: []models.Package{
{
Name: "rpm",
Version: "0:4.8.0-12.el6_0.2",
},
},
},
{
version: "8",
cri: Criteria{
Criterias: []Criteria{
{
Expand All @@ -130,51 +205,68 @@ func TestWalkRedHat(t *testing.T) {
},
},
},
// 4
{
version: "8",
cri: Criteria{
Criterias: []Criteria{
{
Criterias: []Criteria{
{
Criterions: []Criterion{
{Comment: "rpm is earlier than 0:4.8.0-12.el6_0.2"},
{Comment: "libvirt is earlier than 0:4.5.0-42.module+el8.2.0+6024+15a2423f"},
{Comment: "libvirt is signed with Red Hat redhatrelease2 key"},
},
},
},
Criterions: []Criterion{
{Comment: "Red Hat Enterprise Linux 6 is installed"},
{Comment: "Module virt:rhel is enabled"},
},
},
{
Criterias: []Criteria{
{
Criterions: []Criterion{
{Comment: "rpm is earlier than 0:4.8.0-19.el6_2.1"},
{Comment: "libvirt is earlier than 0:4.5.0-42.module+el8.2.0+6024+15a2423f"},
{Comment: "libvirt is signed with Red Hat redhatrelease2 key"},
},
},
},
Criterions: []Criterion{
{Comment: "Red Hat Enterprise Linux 6 is installed"},
{Comment: "Module virt-devel:rhel is enabled"},
},
},
},
Criterions: []Criterion{
{Comment: "Red Hat Enterprise Linux 8 is installed"},
},
},
expected: []models.Package{
{
Name: "rpm",
Version: "0:4.8.0-19.el6_2.1",
Name: "libvirt",
Version: "0:4.5.0-42.module+el8.2.0+6024+15a2423f",
ModularityLabel: "virt:rhel",
},
{
Name: "libvirt",
Version: "0:4.5.0-42.module+el8.2.0+6024+15a2423f",
ModularityLabel: "virt-devel:rhel",
},
},
},
}

for i, tt := range tests {
actual := collectRedHatPacks(tt.cri)
actual := collectRedHatPacks(tt.version, tt.cri)
sort.Slice(actual, func(i, j int) bool {
if actual[i].Name == actual[j].Name {
return actual[i].ModularityLabel < actual[j].ModularityLabel
}
return actual[i].Name < actual[j].Name
})
sort.Slice(tt.expected, func(i, j int) bool {
if tt.expected[i].Name == tt.expected[j].Name {
return tt.expected[i].ModularityLabel < tt.expected[j].ModularityLabel
}
return tt.expected[i].Name < tt.expected[j].Name
})

Expand Down

0 comments on commit eb88caf

Please sign in to comment.