2015-11-13 19:11:28 +00:00
|
|
|
// Copyright 2015 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.
|
|
|
|
|
2015-12-28 20:03:29 +00:00
|
|
|
package aptsources
|
2015-11-13 19:11:28 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/coreos/clair/database"
|
|
|
|
"github.com/coreos/clair/worker/detectors"
|
|
|
|
)
|
|
|
|
|
2015-12-28 20:03:29 +00:00
|
|
|
// AptSourcesNamespaceDetector implements NamespaceDetector and detects the Namespace from the
|
2015-11-13 19:11:28 +00:00
|
|
|
// /etc/apt/sources.list file.
|
2015-12-28 20:03:29 +00:00
|
|
|
//
|
|
|
|
// This detector is necessary to determine precise Debian version when it is
|
|
|
|
// an unstable version for instance.
|
|
|
|
type AptSourcesNamespaceDetector struct{}
|
2015-11-13 19:11:28 +00:00
|
|
|
|
|
|
|
func init() {
|
2015-12-28 20:03:29 +00:00
|
|
|
detectors.RegisterNamespaceDetector("apt-sources", &AptSourcesNamespaceDetector{})
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
2015-12-28 20:03:29 +00:00
|
|
|
func (detector *AptSourcesNamespaceDetector) Detect(data map[string][]byte) *database.Namespace {
|
2015-11-13 19:11:28 +00:00
|
|
|
f, hasFile := data["etc/apt/sources.list"]
|
|
|
|
if !hasFile {
|
2015-12-28 20:03:29 +00:00
|
|
|
return nil
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
2015-12-28 20:03:29 +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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-28 20:03:29 +00:00
|
|
|
if OS != "" && version != "" {
|
2017-01-03 18:15:46 +00:00
|
|
|
return &database.Namespace{
|
|
|
|
Name: OS + ":" + version,
|
|
|
|
VersionFormat: "dpkg",
|
|
|
|
}
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
return nil
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
2015-12-28 20:03:29 +00:00
|
|
|
func (detector *AptSourcesNamespaceDetector) GetRequiredFiles() []string {
|
2015-11-13 19:11:28 +00:00
|
|
|
return []string{"etc/apt/sources.list"}
|
|
|
|
}
|