diff --git a/go.mod b/go.mod index e312d92190..d73d81f8cf 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,7 @@ require ( github.com/mattn/go-colorable v0.1.4 // indirect github.com/olekukonko/tablewriter v0.0.2-0.20190607075207-195002e6e56a github.com/spf13/afero v1.2.2 - github.com/stretchr/testify v1.4.0 + github.com/stretchr/testify v1.6.1 github.com/testcontainers/testcontainers-go v0.3.1 github.com/twitchtv/twirp v5.10.1+incompatible github.com/urfave/cli/v2 v2.2.0 diff --git a/go.sum b/go.sum index a422f2a1d6..d2483ac532 100644 --- a/go.sum +++ b/go.sum @@ -427,6 +427,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/testcontainers/testcontainers-go v0.3.1 h1:KZkEKNfnlsipJblzGCz6fmzd+0DzJ3djulYrislG3Zw= github.com/testcontainers/testcontainers-go v0.3.1/go.mod h1:br7bkzIukhPSIjy07Ma3OuXjjFvl2jm7CDU0LQNsqLw= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= @@ -644,6 +646,8 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gotest.tools v0.0.0-20181223230014-1083505acf35/go.mod h1:R//lfYlUuTOTfblYI3lGoAAAebUdzjvbmQsuB7Ykd90= gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= diff --git a/pkg/rpc/server/server.go b/pkg/rpc/server/server.go index 4ed0773734..0239a180e1 100644 --- a/pkg/rpc/server/server.go +++ b/pkg/rpc/server/server.go @@ -8,7 +8,6 @@ import ( "golang.org/x/xerrors" "github.com/aquasecurity/fanal/cache" - ftypes "github.com/aquasecurity/fanal/types" "github.com/aquasecurity/trivy/pkg/rpc" "github.com/aquasecurity/trivy/pkg/scanner" "github.com/aquasecurity/trivy/pkg/scanner/local" @@ -78,17 +77,9 @@ func (s *CacheServer) PutBlob(_ context.Context, in *rpcCache.PutBlobRequest) (* } func (s *CacheServer) MissingBlobs(_ context.Context, in *rpcCache.MissingBlobsRequest) (*rpcCache.MissingBlobsResponse, error) { - var layerIDs []string - for _, blobID := range in.BlobIds { - l, err := s.cache.GetBlob(blobID) - if err != nil || l.SchemaVersion != ftypes.BlobJSONSchemaVersion { - layerIDs = append(layerIDs, blobID) - } + missingArtifact, blobIDs, err := s.cache.MissingBlobs(in.ArtifactId, in.BlobIds) + if err != nil { + return nil, xerrors.Errorf("failed to get missing blobs: %w", err) } - var missingImage bool - img, err := s.cache.GetArtifact(in.ArtifactId) - if err != nil || img.SchemaVersion != ftypes.ArtifactJSONSchemaVersion { - missingImage = true - } - return &rpcCache.MissingBlobsResponse{MissingArtifact: missingImage, MissingBlobIds: layerIDs}, nil + return &rpcCache.MissingBlobsResponse{MissingArtifact: missingArtifact, MissingBlobIds: blobIDs}, nil } diff --git a/pkg/rpc/server/server_test.go b/pkg/rpc/server/server_test.go index 7a0c7e398a..eea9fa741f 100644 --- a/pkg/rpc/server/server_test.go +++ b/pkg/rpc/server/server_test.go @@ -473,12 +473,11 @@ func TestCacheServer_MissingBlobs(t *testing.T) { in *rpcCache.MissingBlobsRequest } tests := []struct { - name string - args args - getLayerExpectations []cache.LocalArtifactCacheGetBlobExpectation - getImageExpectations []cache.LocalArtifactCacheGetArtifactExpectation - want *rpcCache.MissingBlobsResponse - wantErr string + name string + args args + getArtifactCacheMissingBlobsExpectations []cache.ArtifactCacheMissingBlobsExpectation + want *rpcCache.MissingBlobsResponse + wantErr string }{ { name: "happy path", @@ -491,100 +490,24 @@ func TestCacheServer_MissingBlobs(t *testing.T) { }, }, }, - getLayerExpectations: []cache.LocalArtifactCacheGetBlobExpectation{ + getArtifactCacheMissingBlobsExpectations: []cache.ArtifactCacheMissingBlobsExpectation{ { - Args: cache.LocalArtifactCacheGetBlobArgs{ - BlobID: "sha256:932da51564135c98a49a34a193d6cd363d8fa4184d957fde16c9d8527b3f3b02", - }, - Returns: cache.LocalArtifactCacheGetBlobReturns{ - BlobInfo: ftypes.BlobInfo{}, - }, - }, - { - Args: cache.LocalArtifactCacheGetBlobArgs{ - BlobID: "sha256:dffd9992ca398466a663c87c92cfea2a2db0ae0cf33fcb99da60eec52addbfc5", - }, - Returns: cache.LocalArtifactCacheGetBlobReturns{ - BlobInfo: ftypes.BlobInfo{ - SchemaVersion: 1, - }, - }, - }, - }, - getImageExpectations: []cache.LocalArtifactCacheGetArtifactExpectation{ - { - Args: cache.LocalArtifactCacheGetArtifactArgs{ - ArtifactID: "sha256:e7d92cdc71feacf90708cb59182d0df1b911f8ae022d29e8e95d75ca6a99776a", - }, - Returns: cache.LocalArtifactCacheGetArtifactReturns{ - ArtifactInfo: ftypes.ArtifactInfo{ - SchemaVersion: 1, - }, - }, + Args: cache.ArtifactCacheMissingBlobsArgs{ArtifactID: "sha256:e7d92cdc71feacf90708cb59182d0df1b911f8ae022d29e8e95d75ca6a99776a", + BlobIDs: []string{"sha256:932da51564135c98a49a34a193d6cd363d8fa4184d957fde16c9d8527b3f3b02", "sha256:dffd9992ca398466a663c87c92cfea2a2db0ae0cf33fcb99da60eec52addbfc5"}}, + Returns: cache.ArtifactCacheMissingBlobsReturns{ + MissingArtifact: false, MissingBlobIDs: []string{"sha256:dffd9992ca398466a663c87c92cfea2a2db0ae0cf33fcb99da60eec52addbfc5"}, Err: nil}, }, }, want: &rpcCache.MissingBlobsResponse{ MissingArtifact: false, - MissingBlobIds: []string{"sha256:932da51564135c98a49a34a193d6cd363d8fa4184d957fde16c9d8527b3f3b02"}, - }, - }, - { - name: "schema version doesn't match", - args: args{ - in: &rpcCache.MissingBlobsRequest{ - ArtifactId: "sha256:e7d92cdc71feacf90708cb59182d0df1b911f8ae022d29e8e95d75ca6a99776a", - BlobIds: []string{ - "sha256:932da51564135c98a49a34a193d6cd363d8fa4184d957fde16c9d8527b3f3b02", - "sha256:dffd9992ca398466a663c87c92cfea2a2db0ae0cf33fcb99da60eec52addbfc5", - }, - }, - }, - getLayerExpectations: []cache.LocalArtifactCacheGetBlobExpectation{ - { - Args: cache.LocalArtifactCacheGetBlobArgs{ - BlobID: "sha256:932da51564135c98a49a34a193d6cd363d8fa4184d957fde16c9d8527b3f3b02", - }, - Returns: cache.LocalArtifactCacheGetBlobReturns{ - BlobInfo: ftypes.BlobInfo{ - SchemaVersion: 0, - }, - }, - }, - { - Args: cache.LocalArtifactCacheGetBlobArgs{ - BlobID: "sha256:dffd9992ca398466a663c87c92cfea2a2db0ae0cf33fcb99da60eec52addbfc5", - }, - Returns: cache.LocalArtifactCacheGetBlobReturns{ - BlobInfo: ftypes.BlobInfo{ - SchemaVersion: -1, - }, - }, - }, - }, - getImageExpectations: []cache.LocalArtifactCacheGetArtifactExpectation{ - { - Args: cache.LocalArtifactCacheGetArtifactArgs{ - ArtifactID: "sha256:e7d92cdc71feacf90708cb59182d0df1b911f8ae022d29e8e95d75ca6a99776a", - }, - Returns: cache.LocalArtifactCacheGetArtifactReturns{ - ArtifactInfo: ftypes.ArtifactInfo{}, - }, - }, - }, - want: &rpcCache.MissingBlobsResponse{ - MissingArtifact: true, - MissingBlobIds: []string{ - "sha256:932da51564135c98a49a34a193d6cd363d8fa4184d957fde16c9d8527b3f3b02", - "sha256:dffd9992ca398466a663c87c92cfea2a2db0ae0cf33fcb99da60eec52addbfc5", - }, + MissingBlobIds: []string{"sha256:dffd9992ca398466a663c87c92cfea2a2db0ae0cf33fcb99da60eec52addbfc5"}, }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { mockCache := new(mockCache) - mockCache.ApplyGetBlobExpectations(tt.getLayerExpectations) - mockCache.ApplyGetArtifactExpectations(tt.getImageExpectations) + mockCache.ApplyMissingBlobsExpectations(tt.getArtifactCacheMissingBlobsExpectations) s := NewCacheServer(mockCache) got, err := s.MissingBlobs(tt.args.ctx, tt.args.in) @@ -597,7 +520,7 @@ func TestCacheServer_MissingBlobs(t *testing.T) { } assert.Equal(t, tt.want, got) - mockCache.MockLocalArtifactCache.AssertExpectations(t) + mockCache.MockArtifactCache.AssertExpectations(t) }) } }