2017-01-13 21:48:12 +00:00
|
|
|
// Copyright 2017 clair authors
|
2016-11-09 20:53:01 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2017-01-13 21:48:12 +00:00
|
|
|
// Package alpinerelease implements a featurens.Detector for Alpine Linux based
|
|
|
|
// container image layers.
|
2016-11-09 20:53:01 +00:00
|
|
|
package alpinerelease
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"regexp"
|
2016-12-14 23:00:05 +00:00
|
|
|
"strings"
|
2016-11-09 20:53:01 +00:00
|
|
|
|
|
|
|
"github.com/coreos/clair/database"
|
2017-01-13 21:48:12 +00:00
|
|
|
"github.com/coreos/clair/ext/featurens"
|
2017-01-03 21:00:20 +00:00
|
|
|
"github.com/coreos/clair/ext/versionfmt/dpkg"
|
2017-01-13 21:48:12 +00:00
|
|
|
"github.com/coreos/clair/pkg/tarutil"
|
2016-11-09 20:53:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
osName = "alpine"
|
2016-12-14 23:00:05 +00:00
|
|
|
alpineReleasePath = "etc/alpine-release"
|
2016-11-09 20:53:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var versionRegexp = regexp.MustCompile(`^(\d)+\.(\d)+\.(\d)+$`)
|
|
|
|
|
|
|
|
func init() {
|
2017-01-13 21:48:12 +00:00
|
|
|
featurens.RegisterDetector("alpine-release", &detector{})
|
2016-11-09 20:53:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type detector struct{}
|
|
|
|
|
2017-01-13 21:48:12 +00:00
|
|
|
func (d detector) Detect(files tarutil.FilesMap) (*database.Namespace, error) {
|
|
|
|
file, exists := files[alpineReleasePath]
|
2016-11-09 20:53:01 +00:00
|
|
|
if exists {
|
|
|
|
scanner := bufio.NewScanner(bytes.NewBuffer(file))
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
match := versionRegexp.FindStringSubmatch(line)
|
|
|
|
if len(match) > 0 {
|
2016-12-14 23:00:05 +00:00
|
|
|
versionNumbers := strings.Split(match[0], ".")
|
2017-01-03 18:15:46 +00:00
|
|
|
return &database.Namespace{
|
|
|
|
Name: osName + ":" + "v" + versionNumbers[0] + "." + versionNumbers[1],
|
2017-01-03 21:00:20 +00:00
|
|
|
VersionFormat: dpkg.ParserName,
|
2017-01-13 21:48:12 +00:00
|
|
|
}, nil
|
2016-11-09 20:53:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-13 21:48:12 +00:00
|
|
|
return nil, nil
|
2016-11-09 20:53:01 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 21:48:12 +00:00
|
|
|
func (d detector) RequiredFilenames() []string {
|
2016-11-09 20:53:01 +00:00
|
|
|
return []string{alpineReleasePath}
|
|
|
|
}
|