Using httputil for NVD

nvd was missed when moving to httputil, this fixes it
This commit is contained in:
Jean Michel MacKay 2018-09-06 15:41:40 -04:00
parent 089a4e0f0a
commit 49cbdd7a7c

View File

@ -24,7 +24,6 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"net/http"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -36,6 +35,7 @@ import (
"github.com/coreos/clair/database" "github.com/coreos/clair/database"
"github.com/coreos/clair/ext/vulnmdsrc" "github.com/coreos/clair/ext/vulnmdsrc"
"github.com/coreos/clair/pkg/commonerr" "github.com/coreos/clair/pkg/commonerr"
"github.com/coreos/clair/pkg/httputil"
) )
const ( const (
@ -166,11 +166,17 @@ func getDataFeeds(dataFeedHashes map[string]string, localPath string) (map[strin
} }
// Download data feed. // Download data feed.
r, err := http.Get(fmt.Sprintf(dataFeedURL, dataFeedName)) r, err := httputil.GetWithUserAgent(fmt.Sprintf(dataFeedURL, dataFeedName))
if err != nil { if err != nil {
log.WithError(err).WithField(logDataFeedName, dataFeedName).Error("could not download NVD data feed") log.WithError(err).WithField(logDataFeedName, dataFeedName).Error("could not download NVD data feed")
return dataFeedReaders, dataFeedHashes, commonerr.ErrCouldNotDownload return dataFeedReaders, dataFeedHashes, commonerr.ErrCouldNotDownload
} }
// r is closed in BuildCache()
if !httputil.Status2xx(r) {
log.WithField("StatusCode", r.StatusCode).Error("Failed to download NVD data feed")
return dataFeedReaders, dataFeedHashes, commonerr.ErrCouldNotDownload
}
// Un-gzip it. // Un-gzip it.
gr, err := gzip.NewReader(r.Body) gr, err := gzip.NewReader(r.Body)
@ -199,12 +205,16 @@ func getDataFeeds(dataFeedHashes map[string]string, localPath string) (map[strin
} }
func getHashFromMetaURL(metaURL string) (string, error) { func getHashFromMetaURL(metaURL string) (string, error) {
r, err := http.Get(metaURL) r, err := httputil.GetWithUserAgent(metaURL)
if err != nil { if err != nil {
return "", err return "", err
} }
defer r.Body.Close() defer r.Body.Close()
if !httputil.Status2xx(r) {
return "", errors.New("Unsuccesuful status code: " + string(r.StatusCode))
}
scanner := bufio.NewScanner(r.Body) scanner := bufio.NewScanner(r.Body)
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() line := scanner.Text()