api: Add detectors for RPC

Change the V3 implementation to accommondate the detectors.
This commit is contained in:
Sida Chen 2018-09-20 15:39:10 -04:00
parent 9c49d9dc55
commit 48427e9b88
3 changed files with 56 additions and 32 deletions

View File

@ -22,6 +22,13 @@ import (
"github.com/coreos/clair/ext/versionfmt" "github.com/coreos/clair/ext/versionfmt"
) )
// DatabaseDetectorTypeMapping maps the database detector type to the integer
// enum proto.
var DatabaseDetectorTypeMapping = map[database.DetectorType]Detector_Type{
database.NamespaceDetectorType: Detector_Type(1),
database.FeatureDetectorType: Detector_Type(2),
}
// PagedVulnerableAncestriesFromDatabaseModel converts database // PagedVulnerableAncestriesFromDatabaseModel converts database
// PagedVulnerableAncestries to api PagedVulnerableAncestries and assigns // PagedVulnerableAncestries to api PagedVulnerableAncestries and assigns
// indexes to ancestries. // indexes to ancestries.
@ -122,23 +129,38 @@ func VulnerabilityWithFixedInFromDatabaseModel(dbVuln database.VulnerabilityWith
return vuln, nil return vuln, nil
} }
// LayerFromDatabaseModel converts database layer to api layer.
func LayerFromDatabaseModel(dbLayer database.LayerMetadata) *Layer {
layer := Layer{Hash: dbLayer.Hash}
return &layer
}
// NamespacedFeatureFromDatabaseModel converts database namespacedFeature to api Feature. // NamespacedFeatureFromDatabaseModel converts database namespacedFeature to api Feature.
func NamespacedFeatureFromDatabaseModel(feature database.NamespacedFeature) *Feature { func NamespacedFeatureFromDatabaseModel(feature database.AncestryFeature) *Feature {
version := feature.Feature.Version version := feature.Feature.Version
if version == versionfmt.MaxVersion { if version == versionfmt.MaxVersion {
version = "None" version = "None"
} }
return &Feature{ return &Feature{
Name: feature.Feature.Name, Name: feature.Feature.Name,
NamespaceName: feature.Namespace.Name, Namespace: &Namespace{
Name: feature.Namespace.Name,
Detector: DetectorFromDatabaseModel(feature.NamespaceBy),
},
VersionFormat: feature.Namespace.VersionFormat, VersionFormat: feature.Namespace.VersionFormat,
Version: version, Version: version,
Detector: DetectorFromDatabaseModel(feature.FeatureBy),
} }
} }
func DetectorFromDatabaseModel(detector database.Detector) *Detector {
return &Detector{
Name: detector.Name,
Version: detector.Version,
Type: DatabaseDetectorTypeMapping[detector.DType],
}
}
func DetectorsFromDatabaseModel(dbDetectors []database.Detector) []*Detector {
detectors := make([]*Detector, 0, len(dbDetectors))
for _, d := range dbDetectors {
detectors = append(detectors, DetectorFromDatabaseModel(d))
}
return detectors
}

View File

@ -129,9 +129,8 @@ func (s *AncestryServer) GetAncestry(ctx context.Context, req *pb.GetAncestryReq
} }
pbAncestry := &pb.GetAncestryResponse_Ancestry{ pbAncestry := &pb.GetAncestryResponse_Ancestry{
Name: ancestry.Name, Name: ancestry.Name,
ScannedDetectors: ancestry.ProcessedBy.Detectors, Detectors: pb.DetectorsFromDatabaseModel(ancestry.By),
ScannedListers: ancestry.ProcessedBy.Listers,
} }
for _, layer := range ancestry.Layers { for _, layer := range ancestry.Layers {

View File

@ -13,8 +13,7 @@ import (
// protobuf struct. // protobuf struct.
func GetClairStatus(store database.Datastore) (*pb.ClairStatus, error) { func GetClairStatus(store database.Datastore) (*pb.ClairStatus, error) {
status := &pb.ClairStatus{ status := &pb.ClairStatus{
Listers: clair.Processors.Listers, Detectors: pb.DetectorsFromDatabaseModel(clair.EnabledDetectors),
Detectors: clair.Processors.Detectors,
} }
t, firstUpdate, err := clair.GetLastUpdateTime(store) t, firstUpdate, err := clair.GetLastUpdateTime(store)
@ -34,19 +33,16 @@ func GetClairStatus(store database.Datastore) (*pb.ClairStatus, error) {
// GetPbAncestryLayer retrieves an ancestry layer with vulnerabilities and // GetPbAncestryLayer retrieves an ancestry layer with vulnerabilities and
// features in an ancestry based on the provided database layer. // features in an ancestry based on the provided database layer.
func GetPbAncestryLayer(session database.Session, layer database.AncestryLayer) (*pb.GetAncestryResponse_AncestryLayer, error) { func GetPbAncestryLayer(tx database.Session, layer database.AncestryLayer) (*pb.GetAncestryResponse_AncestryLayer, error) {
pbLayer := &pb.GetAncestryResponse_AncestryLayer{ pbLayer := &pb.GetAncestryResponse_AncestryLayer{
Layer: &pb.Layer{ Layer: &pb.Layer{
Hash: layer.Hash, Hash: layer.Hash,
}, },
} }
var ( features := layer.GetFeatures()
features []database.NullableAffectedNamespacedFeature affectedFeatures, err := tx.FindAffectedNamespacedFeatures(features)
err error if err != nil {
)
if features, err = session.FindAffectedNamespacedFeatures(layer.DetectedFeatures); err != nil {
return nil, status.Error(codes.Internal, err.Error()) return nil, status.Error(codes.Internal, err.Error())
} }
@ -59,20 +55,27 @@ func GetPbAncestryLayer(session database.Session, layer database.AncestryLayer)
return nil, status.Error(codes.Internal, "ancestry feature is not found") return nil, status.Error(codes.Internal, "ancestry feature is not found")
} }
var ( for _, detectedFeature := range layer.Features {
pbFeature = pb.NamespacedFeatureFromDatabaseModel(feature.NamespacedFeature) if detectedFeature.NamespacedFeature != feature.NamespacedFeature {
pbVuln *pb.Vulnerability continue
err error
)
for _, vuln := range feature.AffectedBy {
if pbVuln, err = pb.VulnerabilityWithFixedInFromDatabaseModel(vuln); err != nil {
return nil, status.Error(codes.Internal, err.Error())
} }
pbFeature.Vulnerabilities = append(pbFeature.Vulnerabilities, pbVuln) var (
} pbFeature = pb.NamespacedFeatureFromDatabaseModel(detectedFeature)
pbVuln *pb.Vulnerability
err error
)
pbLayer.DetectedFeatures = append(pbLayer.DetectedFeatures, pbFeature) for _, vuln := range feature.AffectedBy {
if pbVuln, err = pb.VulnerabilityWithFixedInFromDatabaseModel(vuln); err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
pbFeature.Vulnerabilities = append(pbFeature.Vulnerabilities, pbVuln)
}
pbLayer.DetectedFeatures = append(pbLayer.DetectedFeatures, pbFeature)
}
} }
return pbLayer, nil return pbLayer, nil