2017-01-13 23:49:02 +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 featurefmt exposes functions to dynamically register methods for
|
|
|
|
// determining the features present in an image layer.
|
|
|
|
package featurefmt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2017-06-22 18:01:41 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2017-01-13 23:49:02 +00:00
|
|
|
"github.com/coreos/clair/database"
|
|
|
|
"github.com/coreos/clair/pkg/tarutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-09-19 18:31:15 +00:00
|
|
|
listersM sync.RWMutex
|
|
|
|
listers = make(map[string]lister)
|
2017-01-13 23:49:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Lister represents an ability to list the features present in an image layer.
|
|
|
|
type Lister interface {
|
2017-07-26 23:22:29 +00:00
|
|
|
// ListFeatures produces a list of Features present in an image layer.
|
2019-03-06 09:24:14 +00:00
|
|
|
ListFeatures(tarutil.FilesMap) ([]database.LayerFeature, error)
|
2017-01-13 23:49:02 +00:00
|
|
|
|
2019-03-22 10:13:24 +00:00
|
|
|
// RequiredFilenames returns a list of patterns for filenames that will
|
|
|
|
// be in the FilesMap provided to the ListFeatures method.
|
2017-01-13 23:49:02 +00:00
|
|
|
//
|
2019-03-22 10:13:24 +00:00
|
|
|
// The patterns are expressed as regexps, and will be matched against
|
|
|
|
// full paths that do not include the leading "/".
|
2017-01-13 23:49:02 +00:00
|
|
|
RequiredFilenames() []string
|
|
|
|
}
|
|
|
|
|
2018-09-19 18:31:15 +00:00
|
|
|
type lister struct {
|
|
|
|
Lister
|
|
|
|
|
|
|
|
info database.Detector
|
|
|
|
}
|
|
|
|
|
2017-01-13 23:49:02 +00:00
|
|
|
// RegisterLister makes a Lister available by the provided name.
|
|
|
|
//
|
|
|
|
// If called twice with the same name, the name is blank, or if the provided
|
|
|
|
// Lister is nil, this function panics.
|
2018-09-19 18:31:15 +00:00
|
|
|
func RegisterLister(name string, version string, l Lister) {
|
|
|
|
if name == "" || version == "" {
|
|
|
|
panic("featurefmt: could not register a Lister with an empty name or version")
|
2017-01-13 23:49:02 +00:00
|
|
|
}
|
|
|
|
if l == nil {
|
|
|
|
panic("featurefmt: could not register a nil Lister")
|
|
|
|
}
|
|
|
|
|
|
|
|
listersM.Lock()
|
|
|
|
defer listersM.Unlock()
|
|
|
|
|
|
|
|
if _, dup := listers[name]; dup {
|
|
|
|
panic("featurefmt: RegisterLister called twice for " + name)
|
|
|
|
}
|
|
|
|
|
2018-09-19 18:31:15 +00:00
|
|
|
listers[name] = lister{l, database.NewFeatureDetector(name, version)}
|
2017-01-13 23:49:02 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
// ListFeatures produces the list of Features in an image layer using
|
2017-01-13 23:49:02 +00:00
|
|
|
// every registered Lister.
|
2018-09-19 18:31:15 +00:00
|
|
|
func ListFeatures(files tarutil.FilesMap, toUse []database.Detector) ([]database.LayerFeature, error) {
|
2017-01-13 23:49:02 +00:00
|
|
|
listersM.RLock()
|
|
|
|
defer listersM.RUnlock()
|
|
|
|
|
2018-09-19 18:31:15 +00:00
|
|
|
features := []database.LayerFeature{}
|
|
|
|
for _, d := range toUse {
|
|
|
|
// Only use the detector with the same type
|
|
|
|
if d.DType != database.FeatureDetectorType {
|
|
|
|
continue
|
|
|
|
}
|
2017-06-22 18:01:41 +00:00
|
|
|
|
2018-09-19 18:31:15 +00:00
|
|
|
if lister, ok := listers[d.Name]; ok {
|
|
|
|
fs, err := lister.ListFeatures(files)
|
2017-07-26 23:22:29 +00:00
|
|
|
if err != nil {
|
2018-09-19 18:31:15 +00:00
|
|
|
return nil, err
|
2017-07-26 23:22:29 +00:00
|
|
|
}
|
2018-09-19 18:31:15 +00:00
|
|
|
|
2019-03-06 09:24:14 +00:00
|
|
|
for i := range fs {
|
|
|
|
fs[i].By = lister.info
|
2018-09-19 18:31:15 +00:00
|
|
|
}
|
2019-03-06 09:24:14 +00:00
|
|
|
features = append(features, fs...)
|
2018-09-19 18:31:15 +00:00
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
} else {
|
2018-09-19 18:31:15 +00:00
|
|
|
log.WithField("Name", d).Fatal("unknown feature detector")
|
2017-01-13 23:49:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 18:31:15 +00:00
|
|
|
return features, nil
|
2017-01-13 23:49:02 +00:00
|
|
|
}
|
|
|
|
|
2019-03-22 10:13:24 +00:00
|
|
|
// RequiredFilenames returns all file patterns that will be passed to the
|
|
|
|
// given extensions. These patterns are expressed as regexps. Any extension
|
|
|
|
// metadata that has non feature-detector type will be skipped.
|
2018-09-19 18:31:15 +00:00
|
|
|
func RequiredFilenames(toUse []database.Detector) (files []string) {
|
2017-01-13 23:49:02 +00:00
|
|
|
listersM.RLock()
|
|
|
|
defer listersM.RUnlock()
|
|
|
|
|
2018-09-19 18:31:15 +00:00
|
|
|
for _, d := range toUse {
|
|
|
|
if d.DType != database.FeatureDetectorType {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
files = append(files, listers[d.Name].RequiredFilenames()...)
|
2017-01-13 23:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 23:22:29 +00:00
|
|
|
// ListListers returns the names of all the registered feature listers.
|
2018-09-19 18:31:15 +00:00
|
|
|
func ListListers() []database.Detector {
|
|
|
|
r := []database.Detector{}
|
|
|
|
for _, d := range listers {
|
|
|
|
r = append(r, d.info)
|
2017-07-26 23:22:29 +00:00
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|