diff --git a/ext/vulnmdsrc/nvd/nvd.go b/ext/vulnmdsrc/nvd/nvd.go index a36dec83..750559fe 100644 --- a/ext/vulnmdsrc/nvd/nvd.go +++ b/ext/vulnmdsrc/nvd/nvd.go @@ -24,7 +24,6 @@ import ( "fmt" "io" "io/ioutil" - "net/http" "os" "path/filepath" "strconv" @@ -36,6 +35,7 @@ import ( "github.com/coreos/clair/database" "github.com/coreos/clair/ext/vulnmdsrc" "github.com/coreos/clair/pkg/commonerr" + "github.com/coreos/clair/pkg/httputil" ) const ( @@ -166,11 +166,17 @@ func getDataFeeds(dataFeedHashes map[string]string, localPath string) (map[strin } // Download data feed. - r, err := http.Get(fmt.Sprintf(dataFeedURL, dataFeedName)) + r, err := httputil.GetWithUserAgent(fmt.Sprintf(dataFeedURL, dataFeedName)) if err != nil { log.WithError(err).WithField(logDataFeedName, dataFeedName).Error("could not download NVD data feed") 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. 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) { - r, err := http.Get(metaURL) + r, err := httputil.GetWithUserAgent(metaURL) if err != nil { return "", err } defer r.Body.Close() + if !httputil.Status2xx(r) { + return "", errors.New("Unsuccesuful status code: " + string(r.StatusCode)) + } + scanner := bufio.NewScanner(r.Body) for scanner.Scan() { line := scanner.Text()