Skip to content

Commit 9154b81

Browse files
authored
feat(cache): remove temporary cache after filesystem scanning (#1868)
1 parent f36d9b6 commit 9154b81

File tree

10 files changed

+496
-97
lines changed

10 files changed

+496
-97
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/Masterminds/sprig/v3 v3.2.2
88
github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46
99
github.com/aquasecurity/bolt-fixtures v0.0.0-20200903104109-d34e7f983986
10-
github.com/aquasecurity/fanal v0.0.0-20220317181013-c4fac2e5fe9c
10+
github.com/aquasecurity/fanal v0.0.0-20220324154234-b2df5b98f8cd
1111
github.com/aquasecurity/go-dep-parser v0.0.0-20220302151315-ff6d77c26988
1212
github.com/aquasecurity/go-gem-version v0.0.0-20201115065557-8eed6fe000ce
1313
github.com/aquasecurity/go-npm-version v0.0.0-20201110091526-0b796d180798

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ github.com/aquasecurity/bolt-fixtures v0.0.0-20200903104109-d34e7f983986 h1:2a30
237237
github.com/aquasecurity/bolt-fixtures v0.0.0-20200903104109-d34e7f983986/go.mod h1:NT+jyeCzXk6vXR5MTkdn4z64TgGfE5HMLC8qfj5unl8=
238238
github.com/aquasecurity/defsec v0.17.1 h1:gen/DInkQZ+BnV2X/UCI4Kb7SgJzPKiSb91duNhOWcg=
239239
github.com/aquasecurity/defsec v0.17.1/go.mod h1:fmymhKkorY0+cTGAML6LQI+BpCEP1zURaI8smST5rV0=
240-
github.com/aquasecurity/fanal v0.0.0-20220317181013-c4fac2e5fe9c h1:Php9oTqRg5CyEfLaxaLROfKHu6Wldc20+PkJwJczaOI=
241-
github.com/aquasecurity/fanal v0.0.0-20220317181013-c4fac2e5fe9c/go.mod h1:PL2i7JtbuPnLlJVG5HVPAVLMmAUdpA9J/iV7b7E5Gbg=
240+
github.com/aquasecurity/fanal v0.0.0-20220324154234-b2df5b98f8cd h1:hMHRTOuuWsPOGhYBV2MWkTVF8E6oRH0CMH4tKuNzm2M=
241+
github.com/aquasecurity/fanal v0.0.0-20220324154234-b2df5b98f8cd/go.mod h1:F83w17YIlAOD45TNtwgp1sUE9XqVzLARpynv+emTvGw=
242242
github.com/aquasecurity/go-dep-parser v0.0.0-20220302151315-ff6d77c26988 h1:Hd6q0/VF/bC/MT1K/63W2u5ChRIy6cPSQk0YbJ3Vcb8=
243243
github.com/aquasecurity/go-dep-parser v0.0.0-20220302151315-ff6d77c26988/go.mod h1:XxIz2s4UymZBcg9WwAc2km77lFt9rVE/LmKJe2YVOtY=
244244
github.com/aquasecurity/go-gem-version v0.0.0-20201115065557-8eed6fe000ce h1:QgBRgJvtEOBtUXilDb1MLi1p1MWoyFDXAu5DEUl5nwM=

pkg/cache/remote.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,12 @@ func (c RemoteCache) MissingBlobs(imageID string, layerIDs []string) (bool, []st
6161
}
6262
return layers.MissingArtifact, layers.MissingBlobIds, nil
6363
}
64+
65+
// DeleteBlobs removes blobs by IDs from RemoteCache
66+
func (c RemoteCache) DeleteBlobs(blobIDs []string) error {
67+
_, err := c.client.DeleteBlobs(c.ctx, rpc.ConvertToDeleteBlobsRequest(blobIDs))
68+
if err != nil {
69+
return xerrors.Errorf("unable to delete blobs on the server: %w", err)
70+
}
71+
return nil
72+
}

pkg/cache/remote_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ func (s *mockCacheServer) MissingBlobs(_ context.Context, in *rpcCache.MissingBl
5151
return &rpcCache.MissingBlobsResponse{MissingArtifact: true, MissingBlobIds: layerIDs}, nil
5252
}
5353

54+
func (s *mockCacheServer) DeleteBlobs(_ context.Context, in *rpcCache.DeleteBlobsRequest) (*google_protobuf.Empty, error) {
55+
for _, blobId := range in.GetBlobIds() {
56+
if strings.Contains(blobId, "invalid") {
57+
return &google_protobuf.Empty{}, xerrors.New("invalid layer ID")
58+
}
59+
}
60+
return &google_protobuf.Empty{}, nil
61+
}
62+
5463
func withToken(base http.Handler, token, tokenHeader string) http.Handler {
5564
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
5665
if token != "" && token != r.Header.Get(tokenHeader) {

pkg/rpc/convert.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,3 +584,14 @@ func ConvertToRPCScanResponse(results types.Results, fos *ftypes.OS) *scanner.Sc
584584
Results: rpcResults,
585585
}
586586
}
587+
588+
func ConvertToDeleteBlobsRequest(blobIDs []string) *cache.DeleteBlobsRequest {
589+
return &cache.DeleteBlobsRequest{BlobIds: blobIDs}
590+
}
591+
592+
func ConvertFromDeleteBlobsRequest(deleteBlobsRequest *cache.DeleteBlobsRequest) []string {
593+
if deleteBlobsRequest == nil {
594+
return []string{}
595+
}
596+
return deleteBlobsRequest.GetBlobIds()
597+
}

pkg/rpc/server/server.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,12 @@ func (s *CacheServer) MissingBlobs(_ context.Context, in *rpcCache.MissingBlobsR
9696
}
9797
return &rpcCache.MissingBlobsResponse{MissingArtifact: missingArtifact, MissingBlobIds: blobIDs}, nil
9898
}
99+
100+
// DeleteBlobs removes blobs by IDs
101+
func (s *CacheServer) DeleteBlobs(_ context.Context, in *rpcCache.DeleteBlobsRequest) (*google_protobuf.Empty, error) {
102+
blobIDs := rpc.ConvertFromDeleteBlobsRequest(in)
103+
if err := s.cache.DeleteBlobs(blobIDs); err != nil {
104+
return nil, xerrors.Errorf("failed to remove a blobs: %w", err)
105+
}
106+
return &google_protobuf.Empty{}, nil
107+
}

pkg/scanner/scan.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ func (s Scanner) ScanArtifact(ctx context.Context, options types.ScanOptions) (t
110110
if err != nil {
111111
return types.Report{}, xerrors.Errorf("failed analysis: %w", err)
112112
}
113+
defer func() {
114+
if err := s.artifact.Clean(artifactInfo); err != nil {
115+
log.Logger.Warnf("Failed to clean the artifact %q: %v", artifactInfo.Name, err)
116+
}
117+
}()
113118

114119
results, osFound, err := s.driver.Scan(artifactInfo.Name, artifactInfo.ID, artifactInfo.BlobIDs, options)
115120
if err != nil {

rpc/cache/service.pb.go

Lines changed: 112 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/cache/service.proto

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ service Cache {
1111
rpc PutArtifact(PutArtifactRequest) returns (google.protobuf.Empty);
1212
rpc PutBlob(PutBlobRequest) returns (google.protobuf.Empty);
1313
rpc MissingBlobs(MissingBlobsRequest) returns (MissingBlobsResponse);
14+
rpc DeleteBlobs(DeleteBlobsRequest) returns (google.protobuf.Empty);
1415
}
1516

1617
message ArtifactInfo {
@@ -59,3 +60,7 @@ message MissingBlobsResponse {
5960
bool missing_artifact = 1;
6061
repeated string missing_blob_ids = 2;
6162
}
63+
64+
message DeleteBlobsRequest {
65+
repeated string blob_ids = 1;
66+
}

0 commit comments

Comments
 (0)