Skip to content

Commit

Permalink
chore(rocky): fix review
Browse files Browse the repository at this point in the history
  • Loading branch information
MaineK00n committed Jan 18, 2022
1 parent 14952c0 commit 38d6347
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 201 deletions.
21 changes: 7 additions & 14 deletions rocky/rocky.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import (
"os"
"path"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/aquasecurity/vuln-list-update/utils"
"github.com/cheggaaa/pb/v3"
Expand Down Expand Up @@ -149,7 +147,7 @@ func (c Config) Update() error {
for _, release := range c.releases {
for _, repo := range c.repos {
for _, arch := range c.arches {
log.Printf("Fetching Rocky Linux %s %s %s data...\n", release, repo, arch)
log.Printf("Fetching Rocky Linux %s %s %s data...", release, repo, arch)
if err := c.update(release, repo, arch); err != nil {
return xerrors.Errorf("failed to update security advisories of Rocky Linux %s %s %s: %w", release, repo, arch, err)
}
Expand All @@ -161,7 +159,7 @@ func (c Config) Update() error {

func (c Config) update(release, repo, arch string) error {
dirPath := filepath.Join(c.dir, release, repo, arch)
log.Printf("Remove Rocky Linux %s %s %s directory %s\n", release, repo, arch, dirPath)
log.Printf("Remove Rocky Linux %s %s %s directory %s", release, repo, arch, dirPath)
if err := os.RemoveAll(dirPath); err != nil {
return xerrors.Errorf("failed to remove Rocky Linux %s %s %s directory: %w", release, repo, arch, err)
}
Expand Down Expand Up @@ -190,26 +188,21 @@ func (c Config) update(release, repo, arch string) error {
if !strings.HasPrefix(rlsa.ID, "RLSA-") {
continue
}

issuedDate, err := time.Parse("2006-01-02 15:04:05", rlsa.Issued.Date)
if err != nil {
return xerrors.Errorf("failed to parse issued date: %w", err)
}
y := strconv.Itoa(issuedDate.Year())
y := strings.Split(strings.TrimPrefix(rlsa.ID, "RLSA-"), ":")[0]
secErrata[y] = append(secErrata[y], rlsa)
}

for year, errata := range secErrata {
log.Printf("Write Errata for Rocky Linux %s %s %s %s\n", release, repo, arch, year)
log.Printf("Write Errata for Rocky Linux %s %s %s %s", release, repo, arch, year)

if err := os.MkdirAll(filepath.Join(dirPath, year), os.ModePerm); err != nil {
return xerrors.Errorf("failed to mkdir: %w", err)
}

bar := pb.StartNew(len(errata))
for _, erratum := range errata {
filepath := filepath.Join(dirPath, year, fmt.Sprintf("%s.json", erratum.ID))
if err := utils.Write(filepath, erratum); err != nil {
jsonPath := filepath.Join(dirPath, year, fmt.Sprintf("%s.json", erratum.ID))
if err := utils.Write(jsonPath, erratum); err != nil {
return xerrors.Errorf("failed to write Rocky Linux CVE details: %w", err)
}
bar.Increment()
Expand Down Expand Up @@ -238,7 +231,7 @@ func fetchUpdateInfoPath(repomdURL string) (updateInfoPath string, err error) {
}
}
if updateInfoPath == "" {
return "", xerrors.New("No updateinfo field in the repomd")
return "", xerrors.New("no updateinfo field in the repomd")
}
return updateInfoPath, nil
}
Expand Down
34 changes: 19 additions & 15 deletions rocky/rocky_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,35 @@ func Test_Update(t *testing.T) {
}{
{
name: "happy path",
repomdFileName: "testdata/fixtures/repomd_valid.xml",
updateInfoFileName: "testdata/fixtures/updateinfo_valid.xml.gz",
repomdFileName: "repomd_valid.xml",
updateInfoFileName: "updateinfo_valid.xml.gz",
expectedError: nil,
},
{
name: "bad repomd response",
repomdFileName: "testdata/fixtures/repomd_invalid.xml",
repomdFileName: "repomd_invalid.xml",
expectedError: xerrors.Errorf("failed to update security advisories of Rocky Linux 8 BaseOS x86_64: %w", errors.New("failed to fetch updateInfo path from repomd.xml")),
},
{
name: "bad updateInfo response",
repomdFileName: "testdata/fixtures/repomd_valid.xml",
updateInfoFileName: "testdata/fixtures/updateinfo_invalid.xml.gz",
repomdFileName: "repomd_valid.xml",
updateInfoFileName: "updateinfo_invalid.xml.gz",
expectedError: xerrors.Errorf("failed to update security advisories of Rocky Linux 8 BaseOS x86_64: %w", errors.New("failed to fetch updateInfo")),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tsUpdateInfoURL := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case strings.HasSuffix(r.URL.Path, "repomd.xml"):
repomd, _ := os.ReadFile(tt.repomdFileName)
_, _ = w.Write(repomd)
case strings.HasSuffix(r.URL.Path, "updateinfo.xml.gz"):
buf, _ := os.ReadFile(tt.updateInfoFileName)
_, _ = w.Write(buf)
default:
assert.Fail(t, "bad URL requested: ", r.URL.Path, tt.name)
if strings.HasPrefix(r.URL.Path, "/pub/rocky/8/BaseOS/x86_64/os/repodata/") {
switch {
case strings.HasSuffix(r.URL.Path, "repomd.xml"):
r.URL.Path = filepath.Join(filepath.Dir(r.URL.Path), tt.repomdFileName)
case strings.HasSuffix(r.URL.Path, "updateinfo.xml.gz"):
r.URL.Path = filepath.Join(filepath.Dir(r.URL.Path), tt.updateInfoFileName)
}
http.StripPrefix("/pub/rocky/8/BaseOS/x86_64/os/repodata/", http.FileServer(http.Dir("testdata/fixtures")))
} else {
http.NotFound(w, r)
}
}))
defer tsUpdateInfoURL.Close()
Expand All @@ -65,6 +66,9 @@ func Test_Update(t *testing.T) {
}

err := filepath.Walk(dir, func(path string, info os.FileInfo, errfp error) error {
if errfp != nil {
return errfp
}
if info.IsDir() {
return nil
}
Expand All @@ -76,7 +80,7 @@ func Test_Update(t *testing.T) {
got, err := os.ReadFile(path)
assert.NoError(t, err, "failed to open the result file")

assert.Equal(t, string(want), string(got))
assert.JSONEq(t, string(want), string(got))

return nil
})
Expand Down
172 changes: 0 additions & 172 deletions updateinfo_valid.xml

This file was deleted.

0 comments on commit 38d6347

Please sign in to comment.