clair/updater/fetchers/debian/debian.go

255 lines
7.1 KiB
Go
Raw Normal View History

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.
package debian
2015-11-13 19:11:28 +00:00
import (
"crypto/sha1"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/coreos/clair/database"
"github.com/coreos/clair/updater"
cerrors "github.com/coreos/clair/utils/errors"
2015-11-13 19:11:28 +00:00
"github.com/coreos/clair/utils/types"
2016-01-20 00:17:08 +00:00
"github.com/coreos/pkg/capnslog"
2015-11-13 19:11:28 +00:00
)
const (
url = "https://security-tracker.debian.org/tracker/data/json"
cveURLPrefix = "https://security-tracker.debian.org/tracker"
updaterFlag = "debianUpdater"
2015-11-13 19:11:28 +00:00
)
2016-01-20 00:17:08 +00:00
var log = capnslog.NewPackageLogger("github.com/coreos/clair", "updater/fetchers/debian")
2015-11-13 19:11:28 +00:00
type jsonData map[string]map[string]jsonVuln
type jsonVuln struct {
Description string `json:"description"`
Releases map[string]jsonRel `json:"releases"`
}
type jsonRel struct {
FixedVersion string `json:"fixed_version"`
Status string `json:"status"`
Urgency string `json:"urgency"`
}
// DebianFetcher implements updater.Fetcher for the Debian Security Tracker
// (https://security-tracker.debian.org).
type DebianFetcher struct{}
func init() {
updater.RegisterFetcher("debian", &DebianFetcher{})
2015-11-13 19:11:28 +00:00
}
// FetchUpdate fetches vulnerability updates from the Debian Security Tracker.
2016-01-20 00:17:08 +00:00
func (fetcher *DebianFetcher) FetchUpdate(datastore database.Datastore) (resp updater.FetcherResponse, err error) {
2016-02-23 17:07:41 +00:00
log.Info("fetching Debian vulnerabilities")
2015-11-13 19:11:28 +00:00
// Download JSON.
r, err := http.Get(url)
if err != nil {
log.Errorf("could not download Debian's update: %s", err)
return resp, cerrors.ErrCouldNotDownload
}
// Get the SHA-1 of the latest update's JSON data
latestHash, err := datastore.GetKeyValue(updaterFlag)
2015-11-13 19:11:28 +00:00
if err != nil {
return resp, err
}
// Parse the JSON.
resp, err = buildResponse(r.Body, latestHash)
if err != nil {
return resp, err
}
return resp, nil
}
func buildResponse(jsonReader io.Reader, latestKnownHash string) (resp updater.FetcherResponse, err error) {
hash := latestKnownHash
// Defer the addition of flag information to the response.
defer func() {
if err == nil {
resp.FlagName = updaterFlag
2015-11-13 19:11:28 +00:00
resp.FlagValue = hash
}
}()
// Create a TeeReader so that we can unmarshal into JSON and write to a SHA-1
// digest at the same time.
jsonSHA := sha1.New()
teedJSONReader := io.TeeReader(jsonReader, jsonSHA)
// Unmarshal JSON.
var data jsonData
err = json.NewDecoder(teedJSONReader).Decode(&data)
if err != nil {
log.Errorf("could not unmarshal Debian's JSON: %s", err)
2016-01-20 00:17:08 +00:00
return resp, cerrors.ErrCouldNotParse
2015-11-13 19:11:28 +00:00
}
// Calculate the hash and skip updating if the hash has been seen before.
hash = hex.EncodeToString(jsonSHA.Sum(nil))
if latestKnownHash == hash {
log.Debug("no Debian update")
return resp, nil
}
// Extract vulnerability data from Debian's JSON schema.
var unknownReleases map[string]struct{}
2016-01-20 00:17:08 +00:00
resp.Vulnerabilities, unknownReleases = parseDebianJSON(&data)
2015-11-13 19:11:28 +00:00
// Log unknown releases
for k := range unknownReleases {
note := fmt.Sprintf("Debian %s is not mapped to any version number (eg. Jessie->8). Please update me.", k)
resp.Notes = append(resp.Notes, note)
log.Warning(note)
}
return resp, nil
}
2016-01-20 00:17:08 +00:00
func parseDebianJSON(data *jsonData) (vulnerabilities []database.Vulnerability, unknownReleases map[string]struct{}) {
mvulnerabilities := make(map[string]*database.Vulnerability)
2015-11-13 19:11:28 +00:00
unknownReleases = make(map[string]struct{})
for pkgName, pkgNode := range *data {
for vulnName, vulnNode := range pkgNode {
for releaseName, releaseNode := range vulnNode.Releases {
// Attempt to detect the release number.
if _, isReleaseKnown := database.DebianReleasesMapping[releaseName]; !isReleaseKnown {
unknownReleases[releaseName] = struct{}{}
continue
}
// Skip if the status is not determined or the vulnerability is a temporary one.
if !strings.HasPrefix(vulnName, "CVE-") || releaseNode.Status == "undetermined" {
2015-11-13 19:11:28 +00:00
continue
}
// Get or create the vulnerability.
2016-01-25 19:50:48 +00:00
vulnerability, vulnerabilityAlreadyExists := mvulnerabilities[vulnName]
2015-11-13 19:11:28 +00:00
if !vulnerabilityAlreadyExists {
vulnerability = &database.Vulnerability{
2016-01-20 00:17:08 +00:00
Name: vulnName,
2015-11-13 19:11:28 +00:00
Link: strings.Join([]string{cveURLPrefix, "/", vulnName}, ""),
2016-01-20 00:17:08 +00:00
Severity: types.Unknown,
2015-11-13 19:11:28 +00:00
Description: vulnNode.Description,
}
}
// Set the priority of the vulnerability.
// In the JSON, a vulnerability has one urgency per package it affects.
// The highest urgency should be the one set.
2016-01-20 00:17:08 +00:00
urgency := urgencyToSeverity(releaseNode.Urgency)
if urgency.Compare(vulnerability.Severity) > 0 {
vulnerability.Severity = urgency
2015-11-13 19:11:28 +00:00
}
// Determine the version of the package the vulnerability affects.
var version types.Version
var err error
2016-01-20 00:17:08 +00:00
if releaseNode.FixedVersion == "0" {
// This means that the package is not affected by this vulnerability.
version = types.MinVersion
} else if releaseNode.Status == "open" {
2015-11-13 19:11:28 +00:00
// Open means that the package is currently vulnerable in the latest
// version of this Debian release.
version = types.MaxVersion
} else if releaseNode.Status == "resolved" {
// Resolved means that the vulnerability has been fixed in
// "fixed_version" (if affected).
version, err = types.NewVersion(releaseNode.FixedVersion)
if err != nil {
log.Warningf("could not parse package version '%s': %s. skipping", releaseNode.FixedVersion, err.Error())
continue
}
}
2016-01-20 00:17:08 +00:00
// Create and add the feature version.
pkg := database.FeatureVersion{
2016-01-25 19:50:48 +00:00
Feature: database.Feature{
Name: pkgName,
Namespace: database.Namespace{
Name: "debian:" + database.DebianReleasesMapping[releaseName],
},
},
2015-11-13 19:11:28 +00:00
Version: version,
}
2016-01-20 00:17:08 +00:00
vulnerability.FixedIn = append(vulnerability.FixedIn, pkg)
2015-11-13 19:11:28 +00:00
// Store the vulnerability.
2016-01-25 19:50:48 +00:00
mvulnerabilities[vulnName] = vulnerability
2015-11-13 19:11:28 +00:00
}
}
}
// Convert the vulnerabilities map to a slice
for _, v := range mvulnerabilities {
2016-01-20 00:17:08 +00:00
vulnerabilities = append(vulnerabilities, *v)
}
2015-11-13 19:11:28 +00:00
return
}
2016-01-20 00:17:08 +00:00
func urgencyToSeverity(urgency string) types.Priority {
2015-11-13 19:11:28 +00:00
switch urgency {
case "not yet assigned":
return types.Unknown
case "end-of-life":
fallthrough
case "unimportant":
return types.Negligible
case "low":
fallthrough
case "low*":
fallthrough
case "low**":
return types.Low
case "medium":
fallthrough
case "medium*":
fallthrough
case "medium**":
return types.Medium
case "high":
fallthrough
case "high*":
fallthrough
case "high**":
return types.High
default:
log.Warningf("could not determine vulnerability priority from: %s", urgency)
return types.Unknown
}
}
// Clean deletes any allocated resources.
func (fetcher *DebianFetcher) Clean() {}