2017-01-13 21:48:12 +00:00
|
|
|
// Copyright 2017 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 featurens exposes functions to dynamically register methods for
|
|
|
|
// determining a namespace for features present in an image layer.
|
|
|
|
package featurens
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2017-05-04 17:21:25 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-01-13 21:48:12 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/coreos/clair/database"
|
|
|
|
"github.com/coreos/clair/pkg/tarutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
detectorsM sync.RWMutex
|
2018-09-19 18:31:15 +00:00
|
|
|
detectors = make(map[string]detector)
|
2017-01-13 21:48:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Detector represents an ability to detect a namespace used for organizing
|
|
|
|
// features present in an image layer.
|
|
|
|
type Detector interface {
|
|
|
|
// Detect attempts to determine a Namespace from a FilesMap of an image
|
|
|
|
// layer.
|
|
|
|
Detect(tarutil.FilesMap) (*database.Namespace, error)
|
|
|
|
|
2017-01-13 23:49:17 +00:00
|
|
|
// RequiredFilenames returns the list of files required to be in the FilesMap
|
2017-01-13 21:48:12 +00:00
|
|
|
// provided to the Detect method.
|
2017-01-13 23:49:17 +00:00
|
|
|
//
|
|
|
|
// Filenames must not begin with "/".
|
2017-01-13 21:48:12 +00:00
|
|
|
RequiredFilenames() []string
|
|
|
|
}
|
|
|
|
|
2018-09-19 18:31:15 +00:00
|
|
|
type detector struct {
|
|
|
|
Detector
|
|
|
|
|
|
|
|
info database.Detector
|
|
|
|
}
|
|
|
|
|
2017-01-13 21:48:12 +00:00
|
|
|
// RegisterDetector makes a detector available by the provided name.
|
|
|
|
//
|
|
|
|
// If called twice with the same name, the name is blank, or if the provided
|
|
|
|
// Detector is nil, this function panics.
|
2018-09-19 18:31:15 +00:00
|
|
|
func RegisterDetector(name string, version string, d Detector) {
|
|
|
|
if name == "" || version == "" {
|
|
|
|
panic("namespace: could not register a Detector with an empty name or version")
|
2017-01-13 21:48:12 +00:00
|
|
|
}
|
|
|
|
if d == nil {
|
|
|
|
panic("namespace: could not register a nil Detector")
|
|
|
|
}
|
|
|
|
|
|
|
|
detectorsM.Lock()
|
|
|
|
defer detectorsM.Unlock()
|
|
|
|
|
2018-09-19 18:31:15 +00:00
|
|
|
if _, ok := detectors[name]; ok {
|
2017-01-13 21:48:12 +00:00
|
|
|
panic("namespace: RegisterDetector called twice for " + name)
|
|
|
|
}
|
|
|
|
|
2018-09-19 18:31:15 +00:00
|
|
|
detectors[name] = detector{d, database.NewNamespaceDetector(name, version)}
|
2017-01-13 21:48:12 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 18:31:15 +00:00
|
|
|
// Detect uses detectors specified to retrieve the detect result.
|
|
|
|
func Detect(files tarutil.FilesMap, toUse []database.Detector) ([]database.LayerNamespace, error) {
|
2017-01-13 21:48:12 +00:00
|
|
|
detectorsM.RLock()
|
|
|
|
defer detectorsM.RUnlock()
|
2018-09-19 18:31:15 +00:00
|
|
|
|
|
|
|
namespaces := []database.LayerNamespace{}
|
|
|
|
for _, d := range toUse {
|
|
|
|
// Only use the detector with the same type
|
|
|
|
if d.DType != database.NamespaceDetectorType {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if detector, ok := detectors[d.Name]; ok {
|
2017-07-26 23:22:29 +00:00
|
|
|
namespace, err := detector.Detect(files)
|
|
|
|
if err != nil {
|
2018-09-19 18:31:15 +00:00
|
|
|
log.WithError(err).WithField("detector", d).Warning("failed while attempting to detect namespace")
|
2017-07-26 23:22:29 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if namespace != nil {
|
2018-09-19 18:31:15 +00:00
|
|
|
log.WithFields(log.Fields{"detector": d, "namespace": namespace.Name}).Debug("detected namespace")
|
|
|
|
namespaces = append(namespaces, database.LayerNamespace{
|
|
|
|
Namespace: *namespace,
|
|
|
|
By: detector.info,
|
|
|
|
})
|
2017-07-26 23:22:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-09-19 18:31:15 +00:00
|
|
|
log.WithField("detector", d).Fatal("unknown namespace detector")
|
2017-01-13 21:48:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 18:31:15 +00:00
|
|
|
return namespaces, nil
|
2017-01-13 21:48:12 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 18:31:15 +00:00
|
|
|
// RequiredFilenames returns all files required by the give extensions. Any
|
|
|
|
// extension metadata that has non namespace-detector type will be skipped.
|
|
|
|
func RequiredFilenames(toUse []database.Detector) (files []string) {
|
2017-01-13 23:49:17 +00:00
|
|
|
detectorsM.RLock()
|
|
|
|
defer detectorsM.RUnlock()
|
|
|
|
|
2018-09-19 18:31:15 +00:00
|
|
|
for _, d := range toUse {
|
|
|
|
if d.DType != database.NamespaceDetectorType {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
files = append(files, detectors[d.Name].RequiredFilenames()...)
|
2017-01-13 21:48:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-19 18:31:15 +00:00
|
|
|
// ListDetectors returns the info of all registered namespace detectors.
|
|
|
|
func ListDetectors() []database.Detector {
|
|
|
|
r := make([]database.Detector, 0, len(detectors))
|
|
|
|
for _, d := range detectors {
|
|
|
|
r = append(r, d.info)
|
2017-07-26 23:22:29 +00:00
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2017-01-13 23:49:17 +00:00
|
|
|
// TestData represents the data used to test an implementation of Detector.
|
2017-01-13 21:48:12 +00:00
|
|
|
type TestData struct {
|
|
|
|
Files tarutil.FilesMap
|
|
|
|
ExpectedNamespace *database.Namespace
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestDetector runs a Detector on each provided instance of TestData and
|
|
|
|
// asserts the output to be equal to the expected output.
|
|
|
|
func TestDetector(t *testing.T, d Detector, testData []TestData) {
|
|
|
|
for _, td := range testData {
|
|
|
|
namespace, err := d.Detect(td.Files)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
if namespace == nil {
|
|
|
|
assert.Equal(t, td.ExpectedNamespace, namespace)
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, td.ExpectedNamespace.Name, namespace.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|