2015-12-11 11:37:25 +00:00
|
|
|
// Copyright 2015 clair authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// Package detectors exposes functions to register and use container
|
|
|
|
// information extractors.
|
|
|
|
package detectors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2016-02-09 21:55:56 +00:00
|
|
|
"math"
|
2015-12-11 11:37:25 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
cerrors "github.com/coreos/clair/utils/errors"
|
2016-02-09 21:55:56 +00:00
|
|
|
"github.com/coreos/pkg/capnslog"
|
2015-12-11 11:37:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// The DataDetector interface defines a way to detect the required data from input path
|
|
|
|
type DataDetector interface {
|
|
|
|
//Support check if the input path and format are supported by the underling detector
|
|
|
|
Supported(path string, format string) bool
|
|
|
|
// Detect detects the required data from input path
|
|
|
|
Detect(layerReader io.ReadCloser, toExtract []string, maxFileSize int64) (data map[string][]byte, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
dataDetectorsLock sync.Mutex
|
|
|
|
dataDetectors = make(map[string]DataDetector)
|
2016-02-09 21:55:56 +00:00
|
|
|
|
|
|
|
log = capnslog.NewPackageLogger("github.com/coreos/clair", "detectors")
|
2016-02-24 20:39:49 +00:00
|
|
|
|
|
|
|
// ErrCouldNotFindLayer is returned when we could not download or open the layer file.
|
|
|
|
ErrCouldNotFindLayer = cerrors.NewBadRequestError("could not find layer")
|
2015-12-11 11:37:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RegisterDataDetector provides a way to dynamically register an implementation of a
|
|
|
|
// DataDetector.
|
|
|
|
//
|
|
|
|
// If RegisterDataDetector is called twice with the same name if DataDetector is nil,
|
|
|
|
// or if the name is blank, it panics.
|
|
|
|
func RegisterDataDetector(name string, f DataDetector) {
|
|
|
|
if name == "" {
|
|
|
|
panic("Could not register a DataDetector with an empty name")
|
|
|
|
}
|
|
|
|
if f == nil {
|
|
|
|
panic("Could not register a nil DataDetector")
|
|
|
|
}
|
|
|
|
|
|
|
|
dataDetectorsLock.Lock()
|
|
|
|
defer dataDetectorsLock.Unlock()
|
|
|
|
|
|
|
|
if _, alreadyExists := dataDetectors[name]; alreadyExists {
|
|
|
|
panic(fmt.Sprintf("Detector '%s' is already registered", name))
|
|
|
|
}
|
|
|
|
dataDetectors[name] = f
|
|
|
|
}
|
|
|
|
|
|
|
|
// DetectData finds the Data of the layer by using every registered DataDetector
|
2016-05-05 17:48:10 +00:00
|
|
|
func DetectData(format, path string, headers map[string]string, toExtract []string, maxFileSize int64) (data map[string][]byte, err error) {
|
2015-12-11 11:37:25 +00:00
|
|
|
var layerReader io.ReadCloser
|
|
|
|
if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") {
|
2016-05-05 17:48:10 +00:00
|
|
|
// Create a new HTTP request object.
|
2016-05-04 19:47:14 +00:00
|
|
|
request, err := http.NewRequest("GET", path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrCouldNotFindLayer
|
|
|
|
}
|
2016-05-05 17:48:10 +00:00
|
|
|
|
|
|
|
// Set any provided HTTP Headers.
|
|
|
|
if headers != nil {
|
|
|
|
for k, v := range headers {
|
|
|
|
request.Header.Set(k, v)
|
|
|
|
}
|
2016-05-04 19:47:14 +00:00
|
|
|
}
|
2016-05-05 17:48:10 +00:00
|
|
|
|
|
|
|
// Send the request and handle the response.
|
2016-05-04 19:47:14 +00:00
|
|
|
r, err := http.DefaultClient.Do(request)
|
2015-12-11 11:37:25 +00:00
|
|
|
if err != nil {
|
2016-02-12 21:25:37 +00:00
|
|
|
log.Warningf("could not download layer: %s", err)
|
2016-02-24 20:39:49 +00:00
|
|
|
return nil, ErrCouldNotFindLayer
|
2016-02-09 21:55:56 +00:00
|
|
|
}
|
2016-05-05 17:48:10 +00:00
|
|
|
|
|
|
|
// Fail if we don't receive a 2xx HTTP status code.
|
2016-02-09 21:55:56 +00:00
|
|
|
if math.Floor(float64(r.StatusCode/100)) != 2 {
|
|
|
|
log.Warningf("could not download layer: got status code %d, expected 2XX", r.StatusCode)
|
2016-02-24 20:39:49 +00:00
|
|
|
return nil, ErrCouldNotFindLayer
|
2016-02-09 21:55:56 +00:00
|
|
|
}
|
2016-05-05 17:48:10 +00:00
|
|
|
|
2015-12-11 11:37:25 +00:00
|
|
|
layerReader = r.Body
|
|
|
|
} else {
|
|
|
|
layerReader, err = os.Open(path)
|
|
|
|
if err != nil {
|
2016-02-24 20:39:49 +00:00
|
|
|
return nil, ErrCouldNotFindLayer
|
2015-12-11 11:37:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
defer layerReader.Close()
|
|
|
|
|
|
|
|
for _, detector := range dataDetectors {
|
|
|
|
if detector.Supported(path, format) {
|
2016-02-10 04:25:48 +00:00
|
|
|
data, err = detector.Detect(layerReader, toExtract, maxFileSize)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-12-11 11:37:25 +00:00
|
|
|
}
|
2016-02-10 04:25:48 +00:00
|
|
|
return data, nil
|
2015-12-11 11:37:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-25 21:20:47 +00:00
|
|
|
return nil, cerrors.NewBadRequestError(fmt.Sprintf("unsupported image format '%s'", format))
|
2015-12-11 11:37:25 +00:00
|
|
|
}
|