feat(server): log errors on server side (#3397)

This commit is contained in:
Jack Lin
2023-01-10 16:21:31 +08:00
committed by GitHub
parent a5919ca363
commit a8b671bc29

View File

@@ -8,6 +8,7 @@ import (
"golang.org/x/xerrors" "golang.org/x/xerrors"
"github.com/aquasecurity/trivy/pkg/fanal/cache" "github.com/aquasecurity/trivy/pkg/fanal/cache"
"github.com/aquasecurity/trivy/pkg/log"
"github.com/aquasecurity/trivy/pkg/rpc" "github.com/aquasecurity/trivy/pkg/rpc"
"github.com/aquasecurity/trivy/pkg/scanner" "github.com/aquasecurity/trivy/pkg/scanner"
"github.com/aquasecurity/trivy/pkg/scanner/local" "github.com/aquasecurity/trivy/pkg/scanner/local"
@@ -33,6 +34,12 @@ func NewScanServer(s scanner.Driver) *ScanServer {
return &ScanServer{localScanner: s} return &ScanServer{localScanner: s}
} }
// Log and return an error
func teeError(err error) error {
log.Logger.Errorf("%+v", err)
return err
}
// Scan scans and return response // Scan scans and return response
func (s *ScanServer) Scan(ctx context.Context, in *rpcScanner.ScanRequest) (*rpcScanner.ScanResponse, error) { func (s *ScanServer) Scan(ctx context.Context, in *rpcScanner.ScanRequest) (*rpcScanner.ScanResponse, error) {
options := types.ScanOptions{ options := types.ScanOptions{
@@ -42,7 +49,7 @@ func (s *ScanServer) Scan(ctx context.Context, in *rpcScanner.ScanRequest) (*rpc
} }
results, os, err := s.localScanner.Scan(ctx, in.Target, in.ArtifactId, in.BlobIds, options) results, os, err := s.localScanner.Scan(ctx, in.Target, in.ArtifactId, in.BlobIds, options)
if err != nil { if err != nil {
return nil, xerrors.Errorf("failed scan, %s: %w", in.Target, err) return nil, teeError(xerrors.Errorf("failed scan, %s: %w", in.Target, err))
} }
return rpc.ConvertToRPCScanResponse(results, os), nil return rpc.ConvertToRPCScanResponse(results, os), nil
@@ -61,11 +68,11 @@ func NewCacheServer(c cache.Cache) *CacheServer {
// PutArtifact puts the artifacts in cache // PutArtifact puts the artifacts in cache
func (s *CacheServer) PutArtifact(_ context.Context, in *rpcCache.PutArtifactRequest) (*google_protobuf.Empty, error) { func (s *CacheServer) PutArtifact(_ context.Context, in *rpcCache.PutArtifactRequest) (*google_protobuf.Empty, error) {
if in.ArtifactInfo == nil { if in.ArtifactInfo == nil {
return nil, xerrors.Errorf("empty image info") return nil, teeError(xerrors.Errorf("empty image info"))
} }
imageInfo := rpc.ConvertFromRPCPutArtifactRequest(in) imageInfo := rpc.ConvertFromRPCPutArtifactRequest(in)
if err := s.cache.PutArtifact(in.ArtifactId, imageInfo); err != nil { if err := s.cache.PutArtifact(in.ArtifactId, imageInfo); err != nil {
return nil, xerrors.Errorf("unable to store image info in cache: %w", err) return nil, teeError(xerrors.Errorf("unable to store image info in cache: %w", err))
} }
return &google_protobuf.Empty{}, nil return &google_protobuf.Empty{}, nil
} }
@@ -73,11 +80,11 @@ func (s *CacheServer) PutArtifact(_ context.Context, in *rpcCache.PutArtifactReq
// PutBlob puts the blobs in cache // PutBlob puts the blobs in cache
func (s *CacheServer) PutBlob(_ context.Context, in *rpcCache.PutBlobRequest) (*google_protobuf.Empty, error) { func (s *CacheServer) PutBlob(_ context.Context, in *rpcCache.PutBlobRequest) (*google_protobuf.Empty, error) {
if in.BlobInfo == nil { if in.BlobInfo == nil {
return nil, xerrors.Errorf("empty layer info") return nil, teeError(xerrors.Errorf("empty layer info"))
} }
layerInfo := rpc.ConvertFromRPCPutBlobRequest(in) layerInfo := rpc.ConvertFromRPCPutBlobRequest(in)
if err := s.cache.PutBlob(in.DiffId, layerInfo); err != nil { if err := s.cache.PutBlob(in.DiffId, layerInfo); err != nil {
return nil, xerrors.Errorf("unable to store layer info in cache: %w", err) return nil, teeError(xerrors.Errorf("unable to store layer info in cache: %w", err))
} }
return &google_protobuf.Empty{}, nil return &google_protobuf.Empty{}, nil
} }
@@ -86,7 +93,7 @@ func (s *CacheServer) PutBlob(_ context.Context, in *rpcCache.PutBlobRequest) (*
func (s *CacheServer) MissingBlobs(_ context.Context, in *rpcCache.MissingBlobsRequest) (*rpcCache.MissingBlobsResponse, error) { func (s *CacheServer) MissingBlobs(_ context.Context, in *rpcCache.MissingBlobsRequest) (*rpcCache.MissingBlobsResponse, error) {
missingArtifact, blobIDs, err := s.cache.MissingBlobs(in.ArtifactId, in.BlobIds) missingArtifact, blobIDs, err := s.cache.MissingBlobs(in.ArtifactId, in.BlobIds)
if err != nil { if err != nil {
return nil, xerrors.Errorf("failed to get missing blobs: %w", err) return nil, teeError(xerrors.Errorf("failed to get missing blobs: %w", err))
} }
return &rpcCache.MissingBlobsResponse{MissingArtifact: missingArtifact, MissingBlobIds: blobIDs}, nil return &rpcCache.MissingBlobsResponse{MissingArtifact: missingArtifact, MissingBlobIds: blobIDs}, nil
} }
@@ -95,7 +102,7 @@ func (s *CacheServer) MissingBlobs(_ context.Context, in *rpcCache.MissingBlobsR
func (s *CacheServer) DeleteBlobs(_ context.Context, in *rpcCache.DeleteBlobsRequest) (*google_protobuf.Empty, error) { func (s *CacheServer) DeleteBlobs(_ context.Context, in *rpcCache.DeleteBlobsRequest) (*google_protobuf.Empty, error) {
blobIDs := rpc.ConvertFromDeleteBlobsRequest(in) blobIDs := rpc.ConvertFromDeleteBlobsRequest(in)
if err := s.cache.DeleteBlobs(blobIDs); err != nil { if err := s.cache.DeleteBlobs(blobIDs); err != nil {
return nil, xerrors.Errorf("failed to remove a blobs: %w", err) return nil, teeError(xerrors.Errorf("failed to remove a blobs: %w", err))
} }
return &google_protobuf.Empty{}, nil return &google_protobuf.Empty{}, nil
} }