-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: knqyf263 <knqyf263@gmail.com>
- Loading branch information
Showing
7 changed files
with
171 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"encoding/json" | ||
"fmt" | ||
"io" | ||
"iter" | ||
"path/filepath" | ||
|
||
"github.com/inconshreveable/log15" | ||
|
@@ -20,46 +21,63 @@ const ( | |
) | ||
|
||
// FetchUbuntuVulnList clones vuln-list and returns CVE JSONs | ||
func FetchUbuntuVulnList() (entries []models.UbuntuCVEJSON, err error) { | ||
func FetchUbuntuVulnList() (iter.Seq2[models.UbuntuCVEJSON, error], int, error) { | ||
// Clone vuln-list repository | ||
dir := filepath.Join(util.CacheDir(), "vuln-list") | ||
updatedFiles, err := git.CloneOrPull(ubuntuRepoURL, dir, ubuntuDir) | ||
if err != nil { | ||
return nil, xerrors.Errorf("error in vulnsrc clone or pull: %w", err) | ||
return nil, 0, xerrors.Errorf("error in vulnsrc clone or pull: %w", err) | ||
} | ||
|
||
// Only last_updated.json | ||
if len(updatedFiles) <= 1 { | ||
return nil, nil | ||
return nil, 0, nil | ||
} | ||
|
||
rootDir := filepath.Join(dir, ubuntuDir) | ||
targets, err := util.FilterTargets(ubuntuDir, updatedFiles) | ||
if err != nil { | ||
return nil, xerrors.Errorf("failed to filter target files: %w", err) | ||
return nil, 0, xerrors.Errorf("failed to filter target files: %w", err) | ||
} else if len(targets) == 0 { | ||
log15.Debug("Ubuntu: no update file") | ||
return nil, nil | ||
return nil, 0, nil | ||
} | ||
log15.Debug(fmt.Sprintf("Ubuntu updated files: %d", len(targets))) | ||
|
||
err = util.FileWalk(rootDir, targets, func(r io.Reader, _ string) error { | ||
content, err := io.ReadAll(r) | ||
if err != nil { | ||
return err | ||
} | ||
count, err := countUbuntuCVEs(rootDir, targets) | ||
if err != nil { | ||
return nil, 0, xerrors.Errorf("failed to count Ubuntu CVEs: %w", err) | ||
} | ||
|
||
return func(yield func(models.UbuntuCVEJSON, error) bool) { | ||
|
||
err = util.FileWalk(rootDir, targets, func(r io.Reader, _ string) error { | ||
content, err := io.ReadAll(r) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cve := models.UbuntuCVEJSON{} | ||
if err = json.Unmarshal(content, &cve); err != nil { | ||
return xerrors.Errorf("failed to decode Ubuntu JSON: %w", err) | ||
cve := models.UbuntuCVEJSON{} | ||
if err = json.Unmarshal(content, &cve); err != nil { | ||
return xerrors.Errorf("failed to decode Ubuntu JSON: %w", err) | ||
} | ||
|
||
if !yield(cve, nil) { | ||
return err | ||
} | ||
return nil | ||
}) | ||
if err != nil && !yield(models.UbuntuCVEJSON{}, xerrors.Errorf("error in Ubuntu walk: %w", err)) { | ||
return | ||
} | ||
}, count, nil | ||
} | ||
|
||
entries = append(entries, cve) | ||
func countUbuntuCVEs(rootDir string, targets map[string]struct{}) (int, error) { | ||
count := 0 | ||
err := util.FileWalk(rootDir, targets, func(r io.Reader, _ string) error { | ||
Check failure on line 78 in fetcher/ubuntu.go
|
||
count++ | ||
return nil | ||
}) | ||
if err != nil { | ||
return nil, xerrors.Errorf("error in Ubuntu walk: %w", err) | ||
} | ||
|
||
return entries, nil | ||
return count, err | ||
} |
Oops, something went wrong.