Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
daynewlee committed Nov 13, 2024
1 parent 3a5dce7 commit 07cef76
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions enricher/epss/epss.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (e *Enricher) FetchEnrichment(ctx context.Context, fingerprint driver.Finge
newUUID := uuid.New()
hint := driver.Fingerprint(newUUID.String())
zlog.Info(ctx).Str("hint", string(hint)).Msg("starting fetch")

out, err := tmp.NewFile("", "enricher.epss.*.json")
if err != nil {
return nil, hint, err
Expand All @@ -92,9 +93,11 @@ func (e *Enricher) FetchEnrichment(ctx context.Context, fingerprint driver.Finge
}
}
}()

if e.feedPath == "" || !strings.HasSuffix(e.feedPath, ".gz") {
e.sourceURL()
}

resp, err := http.Get(e.feedPath)
if err != nil {
return nil, "", fmt.Errorf("failed to fetch file from %s: %w", e.feedPath, err)
Expand All @@ -112,11 +115,14 @@ func (e *Enricher) FetchEnrichment(ctx context.Context, fingerprint driver.Finge
defer gzipReader.Close()

csvReader := csv.NewReader(gzipReader)
headers, err := csvReader.Read() // read headers
headers, err := csvReader.Read() // Column names
if err != nil {
return nil, "", fmt.Errorf("failed to read CSV headers: %w", err)
}

enc := json.NewEncoder(out)
totalCVEs := 0

for {
record, err := csvReader.Read()
if err == io.EOF {
Expand All @@ -126,28 +132,32 @@ func (e *Enricher) FetchEnrichment(ctx context.Context, fingerprint driver.Finge
return nil, "", fmt.Errorf("failed to read CSV row: %w", err)
}

// Convert CSV row to a JSON object
jsonObject := make(map[string]string)
item := make(map[string]string)
for i, value := range record {
jsonObject[headers[i]] = value
item[headers[i]] = value
}

jsonLine, err := json.Marshal(jsonObject)
if err != nil {
return nil, "", fmt.Errorf("failed to encode JSON: %w", err)
// filter fields
e := make(map[string]string)
e["epss"] = item["epss"]
e["percentile"] = item["percentile"]
enrichment, err := json.Marshal(e)

r := driver.EnrichmentRecord{
Tags: []string{item["cve"]},
Enrichment: enrichment,
}

if _, err := out.Write(jsonLine); err != nil {
return nil, "", fmt.Errorf("failed to write JSON line to file: %w", err)
}
if _, err := out.WriteString("\n"); err != nil { // newline for each JSON object
return nil, "", fmt.Errorf("failed to write newline to file: %w", err)
if err = enc.Encode(&r); err != nil {
return nil, "", fmt.Errorf("encoding enrichment: %w", err)
}
totalCVEs++
}

zlog.Info(ctx).Int("totalCVEs", totalCVEs).Msg("processed CVEs")
if _, err := out.Seek(0, io.SeekStart); err != nil {
return nil, hint, fmt.Errorf("unable to reset file pointer: %w", err)
}
success = true

return out, hint, nil
}
Expand Down

0 comments on commit 07cef76

Please sign in to comment.