worker: detect the status code when downloading a layer and expect 2XX.

This commit is contained in:
Quentin Machu 2016-02-09 16:55:56 -05:00
parent 6aa501f18e
commit 343ce39865

View File

@ -19,12 +19,14 @@ package detectors
import ( import (
"fmt" "fmt"
"io" "io"
"math"
"net/http" "net/http"
"os" "os"
"strings" "strings"
"sync" "sync"
cerrors "github.com/coreos/clair/utils/errors" cerrors "github.com/coreos/clair/utils/errors"
"github.com/coreos/pkg/capnslog"
) )
// The DataDetector interface defines a way to detect the required data from input path // The DataDetector interface defines a way to detect the required data from input path
@ -38,6 +40,8 @@ type DataDetector interface {
var ( var (
dataDetectorsLock sync.Mutex dataDetectorsLock sync.Mutex
dataDetectors = make(map[string]DataDetector) dataDetectors = make(map[string]DataDetector)
log = capnslog.NewPackageLogger("github.com/coreos/clair", "detectors")
) )
// RegisterDataDetector provides a way to dynamically register an implementation of a // RegisterDataDetector provides a way to dynamically register an implementation of a
@ -70,6 +74,14 @@ func DetectData(path string, format string, toExtract []string, maxFileSize int6
if err != nil { if err != nil {
return nil, cerrors.ErrCouldNotDownload return nil, cerrors.ErrCouldNotDownload
} }
if err != nil {
log.Warningf("could not download layer: %s", err)
return nil, cerrors.ErrCouldNotDownload
}
if math.Floor(float64(r.StatusCode/100)) != 2 {
log.Warningf("could not download layer: got status code %d, expected 2XX", r.StatusCode)
return nil, cerrors.ErrCouldNotDownload
}
layerReader = r.Body layerReader = r.Body
} else { } else {
layerReader, err = os.Open(path) layerReader, err = os.Open(path)