Skip to content

Commit eafd810

Browse files
authored
fix(fs): fix cache key generation to use UUID (#8275)
Signed-off-by: knqyf263 <[email protected]>
1 parent f12054e commit eafd810

File tree

6 files changed

+120
-96
lines changed

6 files changed

+120
-96
lines changed

integration/testdata/conda-cyclonedx.json.golden

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"$schema": "http://cyclonedx.org/schema/bom-1.6.schema.json",
33
"bomFormat": "CycloneDX",
44
"specVersion": "1.6",
5-
"serialNumber": "urn:uuid:3ff14136-e09f-4df9-80ea-000000000004",
5+
"serialNumber": "urn:uuid:3ff14136-e09f-4df9-80ea-000000000005",
66
"version": 1,
77
"metadata": {
88
"timestamp": "2021-08-25T12:20:30+00:00",
@@ -17,7 +17,7 @@
1717
]
1818
},
1919
"component": {
20-
"bom-ref": "3ff14136-e09f-4df9-80ea-000000000001",
20+
"bom-ref": "3ff14136-e09f-4df9-80ea-000000000002",
2121
"type": "application",
2222
"name": "testdata/fixtures/repo/conda",
2323
"properties": [
@@ -92,7 +92,7 @@
9292
],
9393
"dependencies": [
9494
{
95-
"ref": "3ff14136-e09f-4df9-80ea-000000000001",
95+
"ref": "3ff14136-e09f-4df9-80ea-000000000002",
9696
"dependsOn": [
9797
"pkg:conda/[email protected]",
9898
"pkg:conda/[email protected]"

integration/testdata/conda-environment-cyclonedx.json.golden

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"$schema": "http://cyclonedx.org/schema/bom-1.6.schema.json",
33
"bomFormat": "CycloneDX",
44
"specVersion": "1.6",
5-
"serialNumber": "urn:uuid:3ff14136-e09f-4df9-80ea-000000000004",
5+
"serialNumber": "urn:uuid:3ff14136-e09f-4df9-80ea-000000000005",
66
"version": 1,
77
"metadata": {
88
"timestamp": "2021-08-25T12:20:30+00:00",
@@ -17,7 +17,7 @@
1717
]
1818
},
1919
"component": {
20-
"bom-ref": "3ff14136-e09f-4df9-80ea-000000000001",
20+
"bom-ref": "3ff14136-e09f-4df9-80ea-000000000002",
2121
"type": "application",
2222
"name": "testdata/fixtures/repo/conda-environment",
2323
"properties": [
@@ -30,7 +30,7 @@
3030
},
3131
"components": [
3232
{
33-
"bom-ref": "3ff14136-e09f-4df9-80ea-000000000002",
33+
"bom-ref": "3ff14136-e09f-4df9-80ea-000000000003",
3434
"type": "application",
3535
"name": "environment.yaml",
3636
"properties": [
@@ -60,13 +60,13 @@
6060
],
6161
"dependencies": [
6262
{
63-
"ref": "3ff14136-e09f-4df9-80ea-000000000001",
63+
"ref": "3ff14136-e09f-4df9-80ea-000000000002",
6464
"dependsOn": [
65-
"3ff14136-e09f-4df9-80ea-000000000002"
65+
"3ff14136-e09f-4df9-80ea-000000000003"
6666
]
6767
},
6868
{
69-
"ref": "3ff14136-e09f-4df9-80ea-000000000002",
69+
"ref": "3ff14136-e09f-4df9-80ea-000000000003",
7070
"dependsOn": [
7171
"pkg:conda/[email protected]"
7272
]

integration/testdata/pom-cyclonedx.json.golden

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"$schema": "http://cyclonedx.org/schema/bom-1.6.schema.json",
33
"bomFormat": "CycloneDX",
44
"specVersion": "1.6",
5-
"serialNumber": "urn:uuid:3ff14136-e09f-4df9-80ea-000000000005",
5+
"serialNumber": "urn:uuid:3ff14136-e09f-4df9-80ea-000000000006",
66
"version": 1,
77
"metadata": {
88
"timestamp": "2021-08-25T12:20:30+00:00",
@@ -17,7 +17,7 @@
1717
]
1818
},
1919
"component": {
20-
"bom-ref": "3ff14136-e09f-4df9-80ea-000000000001",
20+
"bom-ref": "3ff14136-e09f-4df9-80ea-000000000002",
2121
"type": "application",
2222
"name": "testdata/fixtures/repo/pom",
2323
"properties": [
@@ -30,7 +30,7 @@
3030
},
3131
"components": [
3232
{
33-
"bom-ref": "3ff14136-e09f-4df9-80ea-000000000002",
33+
"bom-ref": "3ff14136-e09f-4df9-80ea-000000000003",
3434
"type": "application",
3535
"name": "pom.xml",
3636
"properties": [
@@ -83,13 +83,13 @@
8383
],
8484
"dependencies": [
8585
{
86-
"ref": "3ff14136-e09f-4df9-80ea-000000000001",
86+
"ref": "3ff14136-e09f-4df9-80ea-000000000002",
8787
"dependsOn": [
88-
"3ff14136-e09f-4df9-80ea-000000000002"
88+
"3ff14136-e09f-4df9-80ea-000000000003"
8989
]
9090
},
9191
{
92-
"ref": "3ff14136-e09f-4df9-80ea-000000000002",
92+
"ref": "3ff14136-e09f-4df9-80ea-000000000003",
9393
"dependsOn": [
9494
"pkg:maven/com.example/[email protected]"
9595
]

pkg/fanal/artifact/local/fs.go

+11-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package local
33
import (
44
"context"
55
"crypto/sha256"
6-
"encoding/json"
76
"os"
87
"path"
98
"path/filepath"
@@ -21,6 +20,7 @@ import (
2120
"github.com/aquasecurity/trivy/pkg/fanal/types"
2221
"github.com/aquasecurity/trivy/pkg/fanal/walker"
2322
"github.com/aquasecurity/trivy/pkg/semaphore"
23+
"github.com/aquasecurity/trivy/pkg/uuid"
2424
)
2525

2626
var (
@@ -141,7 +141,7 @@ func (a Artifact) Inspect(ctx context.Context) (artifact.Reference, error) {
141141
return artifact.Reference{}, xerrors.Errorf("failed to call hooks: %w", err)
142142
}
143143

144-
cacheKey, err := a.calcCacheKey(blobInfo)
144+
cacheKey, err := a.calcCacheKey()
145145
if err != nil {
146146
return artifact.Reference{}, xerrors.Errorf("failed to calculate a cache key: %w", err)
147147
}
@@ -172,18 +172,17 @@ func (a Artifact) Clean(reference artifact.Reference) error {
172172
return a.cache.DeleteBlobs(reference.BlobIDs)
173173
}
174174

175-
func (a Artifact) calcCacheKey(blobInfo types.BlobInfo) (string, error) {
176-
// calculate hash of JSON and use it as pseudo artifactID and blobID
175+
func (a Artifact) calcCacheKey() (string, error) {
176+
// Generate a random UUID for the cache key
177+
id := uuid.New()
178+
179+
// Calculate sha256 hash from UUID
177180
h := sha256.New()
178-
if err := json.NewEncoder(h).Encode(blobInfo); err != nil {
179-
return "", xerrors.Errorf("json error: %w", err)
181+
if _, err := h.Write([]byte(id.String())); err != nil {
182+
return "", xerrors.Errorf("sha256 calculation error: %w", err)
180183
}
181184

185+
// Format as sha256 digest
182186
d := digest.NewDigest(digest.SHA256, h)
183-
cacheKey, err := cache.CalcKey(d.String(), a.analyzer.AnalyzerVersions(), a.handlerManager.Versions(), a.artifactOption)
184-
if err != nil {
185-
return "", xerrors.Errorf("cache key: %w", err)
186-
}
187-
188-
return cacheKey, nil
187+
return d.String(), nil
189188
}

0 commit comments

Comments
 (0)