Skip to content

Commit

Permalink
fix(fs): fix cache key generation to use UUID (aquasecurity#8275)
Browse files Browse the repository at this point in the history
Signed-off-by: knqyf263 <[email protected]>
  • Loading branch information
knqyf263 authored Jan 23, 2025
1 parent f12054e commit eafd810
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 96 deletions.
6 changes: 3 additions & 3 deletions integration/testdata/conda-cyclonedx.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "http://cyclonedx.org/schema/bom-1.6.schema.json",
"bomFormat": "CycloneDX",
"specVersion": "1.6",
"serialNumber": "urn:uuid:3ff14136-e09f-4df9-80ea-000000000004",
"serialNumber": "urn:uuid:3ff14136-e09f-4df9-80ea-000000000005",
"version": 1,
"metadata": {
"timestamp": "2021-08-25T12:20:30+00:00",
Expand All @@ -17,7 +17,7 @@
]
},
"component": {
"bom-ref": "3ff14136-e09f-4df9-80ea-000000000001",
"bom-ref": "3ff14136-e09f-4df9-80ea-000000000002",
"type": "application",
"name": "testdata/fixtures/repo/conda",
"properties": [
Expand Down Expand Up @@ -92,7 +92,7 @@
],
"dependencies": [
{
"ref": "3ff14136-e09f-4df9-80ea-000000000001",
"ref": "3ff14136-e09f-4df9-80ea-000000000002",
"dependsOn": [
"pkg:conda/[email protected]",
"pkg:conda/[email protected]"
Expand Down
12 changes: 6 additions & 6 deletions integration/testdata/conda-environment-cyclonedx.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "http://cyclonedx.org/schema/bom-1.6.schema.json",
"bomFormat": "CycloneDX",
"specVersion": "1.6",
"serialNumber": "urn:uuid:3ff14136-e09f-4df9-80ea-000000000004",
"serialNumber": "urn:uuid:3ff14136-e09f-4df9-80ea-000000000005",
"version": 1,
"metadata": {
"timestamp": "2021-08-25T12:20:30+00:00",
Expand All @@ -17,7 +17,7 @@
]
},
"component": {
"bom-ref": "3ff14136-e09f-4df9-80ea-000000000001",
"bom-ref": "3ff14136-e09f-4df9-80ea-000000000002",
"type": "application",
"name": "testdata/fixtures/repo/conda-environment",
"properties": [
Expand All @@ -30,7 +30,7 @@
},
"components": [
{
"bom-ref": "3ff14136-e09f-4df9-80ea-000000000002",
"bom-ref": "3ff14136-e09f-4df9-80ea-000000000003",
"type": "application",
"name": "environment.yaml",
"properties": [
Expand Down Expand Up @@ -60,13 +60,13 @@
],
"dependencies": [
{
"ref": "3ff14136-e09f-4df9-80ea-000000000001",
"ref": "3ff14136-e09f-4df9-80ea-000000000002",
"dependsOn": [
"3ff14136-e09f-4df9-80ea-000000000002"
"3ff14136-e09f-4df9-80ea-000000000003"
]
},
{
"ref": "3ff14136-e09f-4df9-80ea-000000000002",
"ref": "3ff14136-e09f-4df9-80ea-000000000003",
"dependsOn": [
"pkg:conda/[email protected]"
]
Expand Down
12 changes: 6 additions & 6 deletions integration/testdata/pom-cyclonedx.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "http://cyclonedx.org/schema/bom-1.6.schema.json",
"bomFormat": "CycloneDX",
"specVersion": "1.6",
"serialNumber": "urn:uuid:3ff14136-e09f-4df9-80ea-000000000005",
"serialNumber": "urn:uuid:3ff14136-e09f-4df9-80ea-000000000006",
"version": 1,
"metadata": {
"timestamp": "2021-08-25T12:20:30+00:00",
Expand All @@ -17,7 +17,7 @@
]
},
"component": {
"bom-ref": "3ff14136-e09f-4df9-80ea-000000000001",
"bom-ref": "3ff14136-e09f-4df9-80ea-000000000002",
"type": "application",
"name": "testdata/fixtures/repo/pom",
"properties": [
Expand All @@ -30,7 +30,7 @@
},
"components": [
{
"bom-ref": "3ff14136-e09f-4df9-80ea-000000000002",
"bom-ref": "3ff14136-e09f-4df9-80ea-000000000003",
"type": "application",
"name": "pom.xml",
"properties": [
Expand Down Expand Up @@ -83,13 +83,13 @@
],
"dependencies": [
{
"ref": "3ff14136-e09f-4df9-80ea-000000000001",
"ref": "3ff14136-e09f-4df9-80ea-000000000002",
"dependsOn": [
"3ff14136-e09f-4df9-80ea-000000000002"
"3ff14136-e09f-4df9-80ea-000000000003"
]
},
{
"ref": "3ff14136-e09f-4df9-80ea-000000000002",
"ref": "3ff14136-e09f-4df9-80ea-000000000003",
"dependsOn": [
"pkg:maven/com.example/[email protected]"
]
Expand Down
23 changes: 11 additions & 12 deletions pkg/fanal/artifact/local/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package local
import (
"context"
"crypto/sha256"
"encoding/json"
"os"
"path"
"path/filepath"
Expand All @@ -21,6 +20,7 @@ import (
"github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/fanal/walker"
"github.com/aquasecurity/trivy/pkg/semaphore"
"github.com/aquasecurity/trivy/pkg/uuid"
)

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

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

func (a Artifact) calcCacheKey(blobInfo types.BlobInfo) (string, error) {
// calculate hash of JSON and use it as pseudo artifactID and blobID
func (a Artifact) calcCacheKey() (string, error) {
// Generate a random UUID for the cache key
id := uuid.New()

// Calculate sha256 hash from UUID
h := sha256.New()
if err := json.NewEncoder(h).Encode(blobInfo); err != nil {
return "", xerrors.Errorf("json error: %w", err)
if _, err := h.Write([]byte(id.String())); err != nil {
return "", xerrors.Errorf("sha256 calculation error: %w", err)
}

// Format as sha256 digest
d := digest.NewDigest(digest.SHA256, h)
cacheKey, err := cache.CalcKey(d.String(), a.analyzer.AnalyzerVersions(), a.handlerManager.Versions(), a.artifactOption)
if err != nil {
return "", xerrors.Errorf("cache key: %w", err)
}

return cacheKey, nil
return d.String(), nil
}
Loading

0 comments on commit eafd810

Please sign in to comment.