clair/ext/featurens/aptsources/aptsources.go

91 lines
2.3 KiB
Go
Raw Normal View History

2017-01-13 21:48:12 +00:00
// Copyright 2017 clair authors
2015-11-13 19:11:28 +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 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
2015-11-13 19:11:28 +00:00
import (
"bufio"
"strings"
"github.com/coreos/clair/database"
2017-01-13 21:48:12 +00:00
"github.com/coreos/clair/ext/featurens"
"github.com/coreos/clair/ext/versionfmt/dpkg"
2017-01-13 21:48:12 +00:00
"github.com/coreos/clair/pkg/tarutil"
2015-11-13 19:11:28 +00:00
)
2017-01-13 21:48:12 +00:00
type detector struct{}
2015-11-13 19:11:28 +00:00
func init() {
2017-01-13 21:48:12 +00:00
featurens.RegisterDetector("apt-sources", &detector{})
2015-11-13 19:11:28 +00:00
}
2017-01-13 21:48:12 +00:00
func (d detector) Detect(files tarutil.FilesMap) (*database.Namespace, error) {
f, hasFile := files["etc/apt/sources.list"]
2015-11-13 19:11:28 +00:00
if !hasFile {
2017-01-13 21:48:12 +00:00
return nil, nil
2015-11-13 19:11:28 +00:00
}
var OS, version string
2015-11-13 19:11:28 +00:00
scanner := bufio.NewScanner(strings.NewReader(string(f)))
for scanner.Scan() {
// Format: man sources.list | https://wiki.debian.org/SourcesList)
// deb uri distribution component1 component2 component3
// deb-src uri distribution component1 component2 component3
line := strings.Split(scanner.Text(), " ")
if len(line) > 3 {
// Only consider main component
isMainComponent := false
for _, component := range line[3:] {
if component == "main" {
isMainComponent = true
break
}
}
if !isMainComponent {
continue
}
var found bool
version, found = database.DebianReleasesMapping[line[2]]
if found {
OS = "debian"
break
}
version, found = database.UbuntuReleasesMapping[line[2]]
if found {
OS = "ubuntu"
break
}
}
}
if OS != "" && version != "" {
return &database.Namespace{
Name: OS + ":" + version,
VersionFormat: dpkg.ParserName,
2017-01-13 21:48:12 +00:00
}, nil
}
2017-01-13 21:48:12 +00:00
return nil, nil
2015-11-13 19:11:28 +00:00
}
2017-01-13 21:48:12 +00:00
func (d detector) RequiredFilenames() []string {
2015-11-13 19:11:28 +00:00
return []string{"etc/apt/sources.list"}
}