2019-03-05 16:42:59 +00:00
|
|
|
// Copyright 2019 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.
|
|
|
|
|
2018-08-30 19:09:24 +00:00
|
|
|
package v3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/coreos/clair"
|
|
|
|
pb "github.com/coreos/clair/api/v3/clairpb"
|
|
|
|
"github.com/coreos/clair/database"
|
|
|
|
"github.com/golang/protobuf/ptypes"
|
2018-09-07 15:31:35 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2018-08-30 19:09:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetClairStatus retrieves the current status of Clair and wrap it inside
|
|
|
|
// protobuf struct.
|
|
|
|
func GetClairStatus(store database.Datastore) (*pb.ClairStatus, error) {
|
|
|
|
status := &pb.ClairStatus{
|
2019-02-20 22:14:51 +00:00
|
|
|
Detectors: pb.DetectorsFromDatabaseModel(clair.EnabledDetectors()),
|
2018-08-30 19:09:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t, firstUpdate, err := clair.GetLastUpdateTime(store)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if firstUpdate {
|
|
|
|
return status, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
status.LastUpdateTime, err = ptypes.TimestampProto(t)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return status, nil
|
|
|
|
}
|
2018-09-07 15:31:35 +00:00
|
|
|
|
|
|
|
// GetPbAncestryLayer retrieves an ancestry layer with vulnerabilities and
|
|
|
|
// features in an ancestry based on the provided database layer.
|
2019-03-04 23:55:14 +00:00
|
|
|
func (s *AncestryServer) GetPbAncestryLayer(layer database.AncestryLayer) (*pb.GetAncestryResponse_AncestryLayer, error) {
|
2018-09-07 15:31:35 +00:00
|
|
|
pbLayer := &pb.GetAncestryResponse_AncestryLayer{
|
|
|
|
Layer: &pb.Layer{
|
|
|
|
Hash: layer.Hash,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-20 19:39:10 +00:00
|
|
|
features := layer.GetFeatures()
|
2019-03-04 23:55:14 +00:00
|
|
|
affectedFeatures, err := database.FindAffectedNamespacedFeaturesAndRollback(s.Store, features)
|
2018-09-20 19:39:10 +00:00
|
|
|
if err != nil {
|
2019-03-04 23:55:14 +00:00
|
|
|
return nil, newRPCErrorWithClairError(codes.Internal, err)
|
2018-09-07 15:31:35 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 20:41:45 +00:00
|
|
|
for _, feature := range affectedFeatures {
|
2018-09-07 15:31:35 +00:00
|
|
|
if !feature.Valid {
|
2019-03-04 23:55:14 +00:00
|
|
|
panic("feature is missing in the database, it indicates the database is corrupted.")
|
2018-09-07 15:31:35 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 19:39:10 +00:00
|
|
|
for _, detectedFeature := range layer.Features {
|
|
|
|
if detectedFeature.NamespacedFeature != feature.NamespacedFeature {
|
|
|
|
continue
|
2018-09-07 15:31:35 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 19:39:10 +00:00
|
|
|
var (
|
|
|
|
pbFeature = pb.NamespacedFeatureFromDatabaseModel(detectedFeature)
|
|
|
|
pbVuln *pb.Vulnerability
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, vuln := range feature.AffectedBy {
|
|
|
|
if pbVuln, err = pb.VulnerabilityWithFixedInFromDatabaseModel(vuln); err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
2018-09-07 15:31:35 +00:00
|
|
|
|
2018-09-20 19:39:10 +00:00
|
|
|
pbFeature.Vulnerabilities = append(pbFeature.Vulnerabilities, pbVuln)
|
|
|
|
}
|
|
|
|
|
|
|
|
pbLayer.DetectedFeatures = append(pbLayer.DetectedFeatures, pbFeature)
|
|
|
|
}
|
2018-09-07 15:31:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pbLayer, nil
|
|
|
|
}
|