1
0
Fork 0
mirror of https://github.com/gocsaf/csaf.git synced 2025-12-22 11:55:40 +01:00

Download advisories to label/year folders

This commit is contained in:
Sascha L. Teichmann 2022-06-23 15:04:49 +02:00 committed by GitHub
parent 0b19782374
commit f8ce08a26e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 8 deletions

View file

@ -23,7 +23,9 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
"time"
"github.com/ProtonMail/gopenpgp/v2/crypto" "github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/csaf-poc/csaf_distribution/csaf" "github.com/csaf-poc/csaf_distribution/csaf"
@ -36,6 +38,14 @@ type downloader struct {
opts *options opts *options
directory string directory string
keys []*crypto.KeyRing keys []*crypto.KeyRing
eval *util.PathEval
}
func newDownloader(opts *options) *downloader {
return &downloader{
opts: opts,
eval: util.NewPathEval(),
}
} }
func (d *downloader) httpClient() util.Client { func (d *downloader) httpClient() util.Client {
@ -136,11 +146,8 @@ func (d *downloader) download(domain string) error {
return fmt.Errorf("invalid URL '%s': %v", lpmd.URL, err) return fmt.Errorf("invalid URL '%s': %v", lpmd.URL, err)
} }
eval := util.NewPathEval()
if err := d.loadOpenPGPKeys( if err := d.loadOpenPGPKeys(
d.httpClient(), d.httpClient(),
eval,
lpmd.Document, lpmd.Document,
base, base,
); err != nil { ); err != nil {
@ -149,7 +156,7 @@ func (d *downloader) download(domain string) error {
afp := csaf.NewAdvisoryFileProcessor( afp := csaf.NewAdvisoryFileProcessor(
d.httpClient(), d.httpClient(),
eval, d.eval,
lpmd.Document, lpmd.Document,
base) base)
@ -158,12 +165,11 @@ func (d *downloader) download(domain string) error {
func (d *downloader) loadOpenPGPKeys( func (d *downloader) loadOpenPGPKeys(
client util.Client, client util.Client,
eval *util.PathEval,
doc interface{}, doc interface{},
base *url.URL, base *url.URL,
) error { ) error {
src, err := eval.Eval("$.public_openpgp_keys", doc) src, err := d.eval.Eval("$.public_openpgp_keys", doc)
if err != nil { if err != nil {
// no keys. // no keys.
return nil return nil
@ -237,6 +243,12 @@ func (d *downloader) downloadFiles(label csaf.TLPLabel, files []csaf.AdvisoryFil
var lastDir string var lastDir string
lower := strings.ToLower(string(label))
var initialReleaseDate time.Time
dateExtract := util.TimeMatcher(&initialReleaseDate, time.RFC3339)
for _, file := range files { for _, file := range files {
u, err := url.Parse(file.URL()) u, err := url.Parse(file.URL())
@ -349,9 +361,15 @@ func (d *downloader) downloadFiles(label csaf.TLPLabel, files []csaf.AdvisoryFil
continue continue
} }
if err := d.eval.Extract(`$.document.tracking.initial_release_date`, dateExtract, false, doc); err != nil {
log.Printf("Cannot extract initial_release_date from advisory '%s'\n", file.URL())
initialReleaseDate = time.Now()
}
initialReleaseDate = initialReleaseDate.UTC()
// Write advisory to file // Write advisory to file
newDir := path.Join(d.directory, string(label)) newDir := path.Join(d.directory, lower, strconv.Itoa(initialReleaseDate.Year()))
if newDir != lastDir { if newDir != lastDir {
if err := os.MkdirAll(newDir, 0755); err != nil { if err := os.MkdirAll(newDir, 0755); err != nil {
return err return err

View file

@ -53,7 +53,7 @@ func main() {
return return
} }
d := downloader{opts: opts} d := newDownloader(opts)
errCheck(d.run(domains)) errCheck(d.run(domains))
} }