2017-06-05 14:37:29 +00:00
|
|
|
// 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 clairpb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/coreos/clair/database"
|
|
|
|
"github.com/coreos/clair/ext/versionfmt"
|
|
|
|
)
|
|
|
|
|
2018-09-20 19:39:10 +00:00
|
|
|
// DatabaseDetectorTypeMapping maps the database detector type to the integer
|
|
|
|
// enum proto.
|
2018-10-08 18:12:00 +00:00
|
|
|
var DatabaseDetectorTypeMapping = map[database.DetectorType]Detector_DType{
|
|
|
|
database.NamespaceDetectorType: Detector_DType(1),
|
|
|
|
database.FeatureDetectorType: Detector_DType(2),
|
2018-09-20 19:39:10 +00:00
|
|
|
}
|
|
|
|
|
2017-07-12 21:04:05 +00:00
|
|
|
// PagedVulnerableAncestriesFromDatabaseModel converts database
|
|
|
|
// PagedVulnerableAncestries to api PagedVulnerableAncestries and assigns
|
|
|
|
// indexes to ancestries.
|
|
|
|
func PagedVulnerableAncestriesFromDatabaseModel(dbVuln *database.PagedVulnerableAncestries) (*PagedVulnerableAncestries, error) {
|
|
|
|
if dbVuln == nil {
|
|
|
|
return nil, nil
|
2017-06-05 14:37:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-12 21:04:05 +00:00
|
|
|
vuln, err := VulnerabilityFromDatabaseModel(dbVuln.Vulnerability)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
next := ""
|
|
|
|
if !dbVuln.End {
|
|
|
|
next = string(dbVuln.Next)
|
2017-06-05 14:37:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-12 21:04:05 +00:00
|
|
|
vulnAncestry := PagedVulnerableAncestries{
|
|
|
|
Vulnerability: vuln,
|
|
|
|
CurrentPage: string(dbVuln.Current),
|
|
|
|
NextPage: next,
|
|
|
|
Limit: int32(dbVuln.Limit),
|
2017-06-05 14:37:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-12 21:04:05 +00:00
|
|
|
for index, ancestryName := range dbVuln.Affected {
|
2018-04-05 21:02:13 +00:00
|
|
|
indexedAncestry := PagedVulnerableAncestries_IndexedAncestryName{
|
2017-07-12 21:04:05 +00:00
|
|
|
Name: ancestryName,
|
|
|
|
Index: int32(index),
|
|
|
|
}
|
|
|
|
vulnAncestry.Ancestries = append(vulnAncestry.Ancestries, &indexedAncestry)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &vulnAncestry, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotificationFromDatabaseModel converts database notification, old and new
|
|
|
|
// vulnerabilities' paged vulnerable ancestries to be api notification.
|
2018-04-05 21:02:13 +00:00
|
|
|
func NotificationFromDatabaseModel(dbNotification database.VulnerabilityNotificationWithVulnerable) (*GetNotificationResponse_Notification, error) {
|
2017-07-12 21:04:05 +00:00
|
|
|
var (
|
2018-04-05 21:02:13 +00:00
|
|
|
noti GetNotificationResponse_Notification
|
2017-07-12 21:04:05 +00:00
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
noti.Name = dbNotification.Name
|
2017-06-05 14:37:29 +00:00
|
|
|
if !dbNotification.Created.IsZero() {
|
2017-07-12 21:04:05 +00:00
|
|
|
noti.Created = fmt.Sprintf("%d", dbNotification.Created.Unix())
|
2017-06-05 14:37:29 +00:00
|
|
|
}
|
2017-07-12 21:04:05 +00:00
|
|
|
|
2017-06-05 14:37:29 +00:00
|
|
|
if !dbNotification.Notified.IsZero() {
|
2017-07-12 21:04:05 +00:00
|
|
|
noti.Notified = fmt.Sprintf("%d", dbNotification.Notified.Unix())
|
2017-06-05 14:37:29 +00:00
|
|
|
}
|
2017-07-12 21:04:05 +00:00
|
|
|
|
2017-06-05 14:37:29 +00:00
|
|
|
if !dbNotification.Deleted.IsZero() {
|
2017-07-12 21:04:05 +00:00
|
|
|
noti.Deleted = fmt.Sprintf("%d", dbNotification.Deleted.Unix())
|
|
|
|
}
|
2017-06-05 14:37:29 +00:00
|
|
|
|
2017-07-12 21:04:05 +00:00
|
|
|
noti.Old, err = PagedVulnerableAncestriesFromDatabaseModel(dbNotification.Old)
|
2017-06-05 14:37:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-07-12 21:04:05 +00:00
|
|
|
noti.New, err = PagedVulnerableAncestriesFromDatabaseModel(dbNotification.New)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ¬i, nil
|
2017-06-05 14:37:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-12 21:04:05 +00:00
|
|
|
func VulnerabilityFromDatabaseModel(dbVuln database.Vulnerability) (*Vulnerability, error) {
|
2017-06-05 14:37:29 +00:00
|
|
|
metaString := ""
|
|
|
|
if dbVuln.Metadata != nil {
|
|
|
|
metadataByte, err := json.Marshal(dbVuln.Metadata)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
metaString = string(metadataByte)
|
|
|
|
}
|
|
|
|
|
2017-07-12 21:04:05 +00:00
|
|
|
return &Vulnerability{
|
2017-06-05 14:37:29 +00:00
|
|
|
Name: dbVuln.Name,
|
|
|
|
NamespaceName: dbVuln.Namespace.Name,
|
|
|
|
Description: dbVuln.Description,
|
|
|
|
Link: dbVuln.Link,
|
|
|
|
Severity: string(dbVuln.Severity),
|
|
|
|
Metadata: metaString,
|
2017-07-12 21:04:05 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2017-06-05 14:37:29 +00:00
|
|
|
|
2017-07-12 21:04:05 +00:00
|
|
|
func VulnerabilityWithFixedInFromDatabaseModel(dbVuln database.VulnerabilityWithFixedIn) (*Vulnerability, error) {
|
|
|
|
vuln, err := VulnerabilityFromDatabaseModel(dbVuln.Vulnerability)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-06-05 14:37:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-12 21:04:05 +00:00
|
|
|
vuln.FixedBy = dbVuln.FixedInVersion
|
|
|
|
return vuln, nil
|
2017-06-05 14:37:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-12 21:04:05 +00:00
|
|
|
// NamespacedFeatureFromDatabaseModel converts database namespacedFeature to api Feature.
|
2018-09-20 19:39:10 +00:00
|
|
|
func NamespacedFeatureFromDatabaseModel(feature database.AncestryFeature) *Feature {
|
2017-07-12 21:04:05 +00:00
|
|
|
version := feature.Feature.Version
|
2017-06-05 14:37:29 +00:00
|
|
|
if version == versionfmt.MaxVersion {
|
|
|
|
version = "None"
|
|
|
|
}
|
|
|
|
|
2017-07-12 21:04:05 +00:00
|
|
|
return &Feature{
|
2018-09-20 19:39:10 +00:00
|
|
|
Name: feature.Feature.Name,
|
|
|
|
Namespace: &Namespace{
|
|
|
|
Name: feature.Namespace.Name,
|
|
|
|
Detector: DetectorFromDatabaseModel(feature.NamespaceBy),
|
|
|
|
},
|
2017-07-12 21:04:05 +00:00
|
|
|
VersionFormat: feature.Namespace.VersionFormat,
|
|
|
|
Version: version,
|
2018-09-20 19:39:10 +00:00
|
|
|
Detector: DetectorFromDatabaseModel(feature.FeatureBy),
|
2017-06-05 14:37:29 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-20 19:39:10 +00:00
|
|
|
|
|
|
|
func DetectorFromDatabaseModel(detector database.Detector) *Detector {
|
|
|
|
return &Detector{
|
|
|
|
Name: detector.Name,
|
|
|
|
Version: detector.Version,
|
2018-10-08 18:12:00 +00:00
|
|
|
Dtype: DatabaseDetectorTypeMapping[detector.DType],
|
2018-09-20 19:39:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func DetectorsFromDatabaseModel(dbDetectors []database.Detector) []*Detector {
|
|
|
|
detectors := make([]*Detector, 0, len(dbDetectors))
|
|
|
|
for _, d := range dbDetectors {
|
|
|
|
detectors = append(detectors, DetectorFromDatabaseModel(d))
|
|
|
|
}
|
|
|
|
|
|
|
|
return detectors
|
|
|
|
}
|