Skip to content

Commit

Permalink
ext: namespace detector -> featurens
Browse files Browse the repository at this point in the history
  • Loading branch information
jzelinskie committed Jan 23, 2017
1 parent d9be34c commit fb193e1
Show file tree
Hide file tree
Showing 15 changed files with 283 additions and 285 deletions.
11 changes: 5 additions & 6 deletions cmd/clair/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ import (
"github.com/coreos/clair/config"

// Register extensions.
_ "github.com/coreos/clair/ext/featurens/alpinerelease"
_ "github.com/coreos/clair/ext/featurens/aptsources"
_ "github.com/coreos/clair/ext/featurens/lsbrelease"
_ "github.com/coreos/clair/ext/featurens/osrelease"
_ "github.com/coreos/clair/ext/featurens/redhatrelease"
_ "github.com/coreos/clair/ext/imagefmt/aci"
_ "github.com/coreos/clair/ext/imagefmt/docker"
_ "github.com/coreos/clair/ext/notification/webhook"
Expand All @@ -40,12 +45,6 @@ import (
_ "github.com/coreos/clair/worker/detectors/feature/dpkg"
_ "github.com/coreos/clair/worker/detectors/feature/rpm"

_ "github.com/coreos/clair/worker/detectors/namespace/alpinerelease"
_ "github.com/coreos/clair/worker/detectors/namespace/aptsources"
_ "github.com/coreos/clair/worker/detectors/namespace/lsbrelease"
_ "github.com/coreos/clair/worker/detectors/namespace/osrelease"
_ "github.com/coreos/clair/worker/detectors/namespace/redhatrelease"

_ "github.com/coreos/clair/database/pgsql"
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 clair authors
// 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.
Expand All @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package alpinerelease implements a featurens.Detector for Alpine Linux based
// container image layers.
package alpinerelease

import (
Expand All @@ -21,8 +23,9 @@ import (
"strings"

"github.com/coreos/clair/database"
"github.com/coreos/clair/ext/featurens"
"github.com/coreos/clair/ext/versionfmt/dpkg"
"github.com/coreos/clair/worker/detectors"
"github.com/coreos/clair/pkg/tarutil"
)

const (
Expand All @@ -33,15 +36,13 @@ const (
var versionRegexp = regexp.MustCompile(`^(\d)+\.(\d)+\.(\d)+$`)

func init() {
detectors.RegisterNamespaceDetector("alpine-release", &detector{})
featurens.RegisterDetector("alpine-release", &detector{})
}

// detector implements NamespaceDetector by reading the current version of
// Alpine Linux from /etc/alpine-release.
type detector struct{}

func (d *detector) Detect(data map[string][]byte) *database.Namespace {
file, exists := data[alpineReleasePath]
func (d detector) Detect(files tarutil.FilesMap) (*database.Namespace, error) {
file, exists := files[alpineReleasePath]
if exists {
scanner := bufio.NewScanner(bytes.NewBuffer(file))
for scanner.Scan() {
Expand All @@ -52,14 +53,14 @@ func (d *detector) Detect(data map[string][]byte) *database.Namespace {
return &database.Namespace{
Name: osName + ":" + "v" + versionNumbers[0] + "." + versionNumbers[1],
VersionFormat: dpkg.ParserName,
}
}, nil
}
}
}

return nil
return nil, nil
}

func (d *detector) GetRequiredFiles() []string {
func (d detector) RequiredFilenames() []string {
return []string{alpineReleasePath}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 clair authors
// 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.
Expand All @@ -18,34 +18,35 @@ import (
"testing"

"github.com/coreos/clair/database"
"github.com/coreos/clair/worker/detectors/namespace"
"github.com/coreos/clair/ext/featurens"
"github.com/coreos/clair/pkg/tarutil"
)

func TestAlpineReleaseNamespaceDetection(t *testing.T) {
testData := []namespace.TestData{
func TestDetector(t *testing.T) {
testData := []featurens.TestData{
{
ExpectedNamespace: &database.Namespace{Name: "alpine:v3.3"},
Data: map[string][]byte{"etc/alpine-release": []byte(`3.3.4`)},
Files: tarutil.FilesMap{"etc/alpine-release": []byte(`3.3.4`)},
},
{
ExpectedNamespace: &database.Namespace{Name: "alpine:v3.4"},
Data: map[string][]byte{"etc/alpine-release": []byte(`3.4.0`)},
Files: tarutil.FilesMap{"etc/alpine-release": []byte(`3.4.0`)},
},
{
ExpectedNamespace: &database.Namespace{Name: "alpine:v0.3"},
Data: map[string][]byte{"etc/alpine-release": []byte(`0.3.4`)},
Files: tarutil.FilesMap{"etc/alpine-release": []byte(`0.3.4`)},
},
{
ExpectedNamespace: &database.Namespace{Name: "alpine:v0.3"},
Data: map[string][]byte{"etc/alpine-release": []byte(`
Files: tarutil.FilesMap{"etc/alpine-release": []byte(`
0.3.4
`)},
},
{
ExpectedNamespace: nil,
Data: map[string][]byte{},
Files: tarutil.FilesMap{},
},
}

namespace.TestDetector(t, &detector{}, testData)
featurens.TestDetector(t, &detector{}, testData)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015 clair authors
// 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.
Expand All @@ -12,32 +12,33 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package aptsources implements a featurens.Detector for apt based container
// image layers.
//
// This detector is necessary to determine the precise Debian version when it
// is an unstable version for instance.
package aptsources

import (
"bufio"
"strings"

"github.com/coreos/clair/database"
"github.com/coreos/clair/ext/featurens"
"github.com/coreos/clair/ext/versionfmt/dpkg"
"github.com/coreos/clair/worker/detectors"
"github.com/coreos/clair/pkg/tarutil"
)

// AptSourcesNamespaceDetector implements NamespaceDetector and detects the Namespace from the
// /etc/apt/sources.list file.
//
// This detector is necessary to determine precise Debian version when it is
// an unstable version for instance.
type AptSourcesNamespaceDetector struct{}
type detector struct{}

func init() {
detectors.RegisterNamespaceDetector("apt-sources", &AptSourcesNamespaceDetector{})
featurens.RegisterDetector("apt-sources", &detector{})
}

func (detector *AptSourcesNamespaceDetector) Detect(data map[string][]byte) *database.Namespace {
f, hasFile := data["etc/apt/sources.list"]
func (d detector) Detect(files tarutil.FilesMap) (*database.Namespace, error) {
f, hasFile := files["etc/apt/sources.list"]
if !hasFile {
return nil
return nil, nil
}

var OS, version string
Expand Down Expand Up @@ -79,11 +80,11 @@ func (detector *AptSourcesNamespaceDetector) Detect(data map[string][]byte) *dat
return &database.Namespace{
Name: OS + ":" + version,
VersionFormat: dpkg.ParserName,
}
}, nil
}
return nil
return nil, nil
}

func (detector *AptSourcesNamespaceDetector) GetRequiredFiles() []string {
func (d detector) RequiredFilenames() []string {
return []string{"etc/apt/sources.list"}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 clair authors
// 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.
Expand All @@ -18,14 +18,15 @@ import (
"testing"

"github.com/coreos/clair/database"
"github.com/coreos/clair/worker/detectors/namespace"
"github.com/coreos/clair/ext/featurens"
"github.com/coreos/clair/pkg/tarutil"
)

func TestAptSourcesNamespaceDetector(t *testing.T) {
testData := []namespace.TestData{
func TestDetector(t *testing.T) {
testData := []featurens.TestData{
{
ExpectedNamespace: &database.Namespace{Name: "debian:unstable"},
Data: map[string][]byte{
Files: tarutil.FilesMap{
"etc/os-release": []byte(
`PRETTY_NAME="Debian GNU/Linux stretch/sid"
NAME="Debian GNU/Linux"
Expand All @@ -38,5 +39,5 @@ BUG_REPORT_URL="https://bugs.debian.org/"`),
},
}

namespace.TestDetector(t, &AptSourcesNamespaceDetector{}, testData)
featurens.TestDetector(t, &detector{}, testData)
}
124 changes: 124 additions & 0 deletions ext/featurens/driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// 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"

"github.com/coreos/pkg/capnslog"
"github.com/stretchr/testify/assert"

"github.com/coreos/clair/database"
"github.com/coreos/clair/pkg/tarutil"
)

var (
log = capnslog.NewPackageLogger("github.com/coreos/clair", "ext/featurens")

detectorsM sync.RWMutex
detectors = make(map[string]Detector)
)

// 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)

// RequireFilenames returns the list of files required to be in the FilesMap
// provided to the Detect method.
// TODO(jzelinskie): strip "/" prefix
RequiredFilenames() []string
}

// 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.
func RegisterDetector(name string, d Detector) {
if name == "" {
panic("namespace: could not register a Detector with an empty name")
}
if d == nil {
panic("namespace: could not register a nil Detector")
}

detectorsM.Lock()
defer detectorsM.Unlock()

if _, dup := detectors[name]; dup {
panic("namespace: RegisterDetector called twice for " + name)
}

detectors[name] = d
}

// Detect iterators through all registered Detectors and returns the first
// non-nil detected namespace.
func Detect(files tarutil.FilesMap) (*database.Namespace, error) {
detectorsM.RLock()
defer detectorsM.RUnlock()

for name, detector := range detectors {
namespace, err := detector.Detect(files)
if err != nil {
log.Warningf("failed while attempting to detect namespace %s: %s", name, err)
return nil, err
}

if namespace != nil {
log.Debugf("detected namespace %s: %#v", name, namespace)
return namespace, nil
}
}

return nil, nil
}

// RequiredFilenames returns the total list of files required for all
// registered Detectors.
func RequiredFilenames() (files []string) {
for _, detector := range detectors {
files = append(files, detector.RequiredFilenames()...)
}

return
}

// TestData represents the data used to test an implementation of
// NameSpaceDetector.
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)
}
}
}
Loading

0 comments on commit fb193e1

Please sign in to comment.