Skip to content

Commit 66bcfe3

Browse files
committed
Use shared artifacts at cache boundaries
1 parent b67cfb1 commit 66bcfe3

13 files changed

Lines changed: 267 additions & 84 deletions

File tree

‎go.mod‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/aws/aws-sdk-go-v2/config v1.32.38
99
github.com/aws/aws-sdk-go-v2/service/ecr v1.61.0
1010
github.com/git-pkgs/archives v0.5.1
11+
github.com/git-pkgs/artifacts v0.2.0
1112
github.com/git-pkgs/cooldown v0.2.0
1213
github.com/git-pkgs/enrichment v0.7.0
1314
github.com/git-pkgs/gcs v0.1.0
@@ -21,6 +22,7 @@ require (
2122
github.com/go-chi/chi/v5 v5.3.2
2223
github.com/jmoiron/sqlx v1.4.0
2324
github.com/lib/pq v1.12.3
25+
github.com/opencontainers/go-digest v1.0.0
2426
github.com/prometheus/client_golang v1.24.1
2527
github.com/prometheus/client_model v0.6.2
2628
github.com/spdx/tools-golang v0.5.7
@@ -132,7 +134,6 @@ require (
132134
github.com/fsnotify/fsnotify v1.9.0 // indirect
133135
github.com/fzipp/gocyclo v0.6.0 // indirect
134136
github.com/ghostiam/protogetter v0.3.21 // indirect
135-
github.com/git-pkgs/artifacts v0.2.0 // indirect
136137
github.com/git-pkgs/packageurl-go v0.3.1 // indirect
137138
github.com/git-pkgs/pom v0.1.7 // indirect
138139
github.com/github/go-spdx/v2 v2.7.0 // indirect
@@ -228,7 +229,6 @@ require (
228229
github.com/nunnatsa/ginkgolinter v0.24.0 // indirect
229230
github.com/oapi-codegen/nullable v1.2.0 // indirect
230231
github.com/oapi-codegen/runtime v1.6.0 // indirect
231-
github.com/opencontainers/go-digest v1.0.0 // indirect
232232
github.com/package-url/packageurl-go v0.1.7 // indirect
233233
github.com/pandatix/go-cvss v0.6.2 // indirect
234234
github.com/pelletier/go-toml v1.9.5 // indirect

‎internal/database/database_test.go‎

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"database/sql"
55
"os"
66
"path/filepath"
7+
"strings"
78
"testing"
89
"time"
910
)
@@ -279,14 +280,20 @@ func TestGetCachedArtifact(t *testing.T) {
279280
if cached.StoragePath != "/cache/npm/"+filename {
280281
t.Errorf("expected cached storage path, got %q", cached.StoragePath)
281282
}
282-
if cached.ContentHash.String != testContentHash {
283-
t.Errorf("expected cached content hash, got %q", cached.ContentHash.String)
283+
if cached.Artifact.PURL != versionPURL {
284+
t.Errorf("expected cached PURL %q, got %q", versionPURL, cached.Artifact.PURL)
284285
}
285-
if cached.Size.Int64 != 12345 {
286-
t.Errorf("expected cached size 12345, got %d", cached.Size.Int64)
286+
if cached.Artifact.Digest.String() != "sha256:"+testContentHash {
287+
t.Errorf("expected cached digest, got %q", cached.Artifact.Digest)
287288
}
288-
if cached.ContentType.String != "application/gzip" {
289-
t.Errorf("expected cached content type, got %q", cached.ContentType.String)
289+
if cached.Artifact.Size != 12345 {
290+
t.Errorf("expected cached size 12345, got %d", cached.Artifact.Size)
291+
}
292+
if cached.Artifact.Filename != filename {
293+
t.Errorf("expected cached filename %q, got %q", filename, cached.Artifact.Filename)
294+
}
295+
if cached.Artifact.MediaType != "application/gzip" {
296+
t.Errorf("expected cached content type, got %q", cached.Artifact.MediaType)
290297
}
291298
if cached.Integrity.String != testIntegrity {
292299
t.Errorf("expected cached integrity, got %q", cached.Integrity.String)
@@ -302,6 +309,77 @@ func TestGetCachedArtifact(t *testing.T) {
302309
})
303310
}
304311

312+
func TestCachedArtifactRowArtifact(t *testing.T) {
313+
validRow := cachedArtifactRow{
314+
Ecosystem: "npm",
315+
StoragePath: "npm/example/1.0.0/example.tgz",
316+
ContentHash: sql.NullString{String: testContentHash, Valid: true},
317+
Size: sql.NullInt64{Int64: 0, Valid: true},
318+
}
319+
tests := []struct {
320+
name string
321+
row cachedArtifactRow
322+
versionPURL string
323+
wantErr string
324+
}{
325+
{name: "zero byte", row: validRow, versionPURL: "pkg:npm/example@1.0.0"},
326+
{
327+
name: "missing hash",
328+
row: cachedArtifactRow{Size: sql.NullInt64{Int64: 1, Valid: true}},
329+
versionPURL: "pkg:npm/example@1.0.0",
330+
wantErr: "content hash is missing",
331+
},
332+
{
333+
name: "malformed hash",
334+
row: cachedArtifactRow{
335+
ContentHash: sql.NullString{String: "not-a-hash", Valid: true},
336+
Size: sql.NullInt64{Int64: 1, Valid: true},
337+
},
338+
versionPURL: "pkg:npm/example@1.0.0",
339+
wantErr: "digest",
340+
},
341+
{
342+
name: "missing size",
343+
row: cachedArtifactRow{
344+
ContentHash: sql.NullString{String: testContentHash, Valid: true},
345+
},
346+
versionPURL: "pkg:npm/example@1.0.0",
347+
wantErr: "size is missing",
348+
},
349+
{
350+
name: "negative size",
351+
row: cachedArtifactRow{
352+
ContentHash: sql.NullString{String: testContentHash, Valid: true},
353+
Size: sql.NullInt64{Int64: -1, Valid: true},
354+
},
355+
versionPURL: "pkg:npm/example@1.0.0",
356+
wantErr: "size",
357+
},
358+
{name: "malformed PURL", row: validRow, versionPURL: "not-a-purl", wantErr: "PURL"},
359+
}
360+
361+
for _, test := range tests {
362+
t.Run(test.name, func(t *testing.T) {
363+
cached, err := test.row.artifact(test.versionPURL, "example.tgz")
364+
if test.wantErr != "" {
365+
if err == nil {
366+
t.Fatal("artifact() error = nil")
367+
}
368+
if !strings.Contains(err.Error(), test.wantErr) {
369+
t.Errorf("error = %q, want %q", err, test.wantErr)
370+
}
371+
return
372+
}
373+
if err != nil {
374+
t.Fatalf("artifact() error = %v", err)
375+
}
376+
if err := cached.Artifact.Validate(); err != nil {
377+
t.Errorf("Artifact.Validate() error = %v", err)
378+
}
379+
})
380+
}
381+
}
382+
305383
func seedCachedArtifactTestData(t *testing.T, db *DB, packagePURL, versionPURL, filename string) {
306384
t.Helper()
307385

‎internal/database/queries.go‎

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import (
44
"database/sql"
55
"fmt"
66
"time"
7+
8+
"github.com/git-pkgs/artifacts"
9+
"github.com/opencontainers/go-digest"
710
)
811

912
// Package queries
@@ -225,7 +228,7 @@ func (db *DB) GetArtifact(versionPURL, filename string) (*Artifact, error) {
225228

226229
// GetCachedArtifact returns the fields needed to serve a cached artifact.
227230
func (db *DB) GetCachedArtifact(packagePURL, versionPURL, filename string) (*CachedArtifact, error) {
228-
var artifact CachedArtifact
231+
var row cachedArtifactRow
229232
query := db.Rebind(`
230233
SELECT packages.ecosystem, artifacts.storage_path, artifacts.content_hash, artifacts.size,
231234
artifacts.content_type, versions.integrity
@@ -235,14 +238,54 @@ func (db *DB) GetCachedArtifact(packagePURL, versionPURL, filename string) (*Cac
235238
WHERE packages.purl = ? AND artifacts.version_purl = ? AND artifacts.filename = ?
236239
AND artifacts.storage_path IS NOT NULL AND artifacts.fetched_at IS NOT NULL
237240
`)
238-
err := db.Get(&artifact, query, packagePURL, versionPURL, filename)
241+
err := db.Get(&row, query, packagePURL, versionPURL, filename)
239242
if err == sql.ErrNoRows {
240243
return nil, nil
241244
}
242245
if err != nil {
243246
return nil, err
244247
}
245-
return &artifact, nil
248+
return row.artifact(versionPURL, filename)
249+
}
250+
251+
type cachedArtifactRow struct {
252+
Ecosystem string `db:"ecosystem"`
253+
StoragePath string `db:"storage_path"`
254+
ContentHash sql.NullString `db:"content_hash"`
255+
Size sql.NullInt64 `db:"size"`
256+
ContentType sql.NullString `db:"content_type"`
257+
Integrity sql.NullString `db:"integrity"`
258+
}
259+
260+
func (row cachedArtifactRow) artifact(versionPURL, filename string) (*CachedArtifact, error) {
261+
if !row.ContentHash.Valid || row.ContentHash.String == "" {
262+
return nil, fmt.Errorf("cached artifact content hash is missing")
263+
}
264+
if !row.Size.Valid {
265+
return nil, fmt.Errorf("cached artifact size is missing")
266+
}
267+
268+
mediaType := ""
269+
if row.ContentType.Valid {
270+
mediaType = row.ContentType.String
271+
}
272+
sharedArtifact, err := artifacts.New(
273+
versionPURL,
274+
digest.Digest("sha256:"+row.ContentHash.String),
275+
row.Size.Int64,
276+
filename,
277+
mediaType,
278+
)
279+
if err != nil {
280+
return nil, fmt.Errorf("invalid cached artifact: %w", err)
281+
}
282+
283+
return &CachedArtifact{
284+
Ecosystem: row.Ecosystem,
285+
StoragePath: row.StoragePath,
286+
Artifact: sharedArtifact,
287+
Integrity: row.Integrity,
288+
}, nil
246289
}
247290

248291
func (db *DB) GetArtifactByPath(storagePath string) (*Artifact, error) {

‎internal/database/types.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"net/url"
66
"strings"
77
"time"
8+
9+
"github.com/git-pkgs/artifacts"
810
)
911

1012
// Package represents a package in the database.
@@ -148,12 +150,10 @@ func (a *Artifact) IsCached() bool {
148150

149151
// CachedArtifact contains the fields needed to serve a cached artifact.
150152
type CachedArtifact struct {
151-
Ecosystem string `db:"ecosystem"`
152-
StoragePath string `db:"storage_path"`
153-
ContentHash sql.NullString `db:"content_hash"`
154-
Size sql.NullInt64 `db:"size"`
155-
ContentType sql.NullString `db:"content_type"`
156-
Integrity sql.NullString `db:"integrity"`
153+
Ecosystem string
154+
StoragePath string
155+
Artifact artifacts.Artifact
156+
Integrity sql.NullString
157157
}
158158

159159
// MetadataCacheEntry represents a cached metadata blob for offline serving.

‎internal/handler/apk.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ func (h *APKHandler) handlePackageDownload(w http.ResponseWriter, r *http.Reques
129129
return
130130
}
131131

132-
if result.ContentType == "" {
133-
result.ContentType = "application/octet-stream"
132+
if result.Artifact.MediaType == "" {
133+
result.Artifact.MediaType = "application/octet-stream"
134134
}
135135
serveArtifact(w, r.Method, result)
136136
}

‎internal/handler/container.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ func (h *ContainerHandler) handleBlobDownload(w http.ResponseWriter, r *http.Req
161161
}
162162
if cached != nil {
163163
w.Header().Set("Docker-Content-Digest", digest)
164-
if cached.ContentType == "" {
165-
cached.ContentType = "application/octet-stream"
164+
if cached.Artifact.MediaType == "" {
165+
cached.Artifact.MediaType = "application/octet-stream"
166166
}
167167
serveArtifact(w, r.Method, cached)
168168
return
@@ -205,8 +205,8 @@ func (h *ContainerHandler) handleBlobDownload(w http.ResponseWriter, r *http.Req
205205
}
206206

207207
w.Header().Set("Docker-Content-Digest", digest)
208-
if result.ContentType == "" {
209-
result.ContentType = "application/octet-stream"
208+
if result.Artifact.MediaType == "" {
209+
result.Artifact.MediaType = "application/octet-stream"
210210
}
211211
ServeArtifact(w, result)
212212
}

‎internal/handler/download_test.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ func seedPackageWithPURL(t *testing.T, db *database.DB, store *mockStorage, ecos
4444

4545
storagePath := storage.ArtifactPath(ecosystem, "", name, version, filename)
4646
store.files[storagePath] = []byte(content)
47+
sharedArtifact := testArtifact(content, versionPURL, filename, "application/octet-stream")
4748

4849
art := &database.Artifact{
4950
VersionPURL: versionPURL,
5051
Filename: filename,
5152
UpstreamURL: "https://example.com/" + filename,
5253
StoragePath: sql.NullString{String: storagePath, Valid: true},
53-
ContentHash: sql.NullString{String: sha256Hex(content), Valid: true},
54+
ContentHash: sql.NullString{String: sharedArtifact.Digest.Encoded(), Valid: true},
5455
Size: sql.NullInt64{Int64: int64(len(content)), Valid: true},
5556
ContentType: sql.NullString{String: "application/octet-stream", Valid: true},
5657
FetchedAt: sql.NullTime{Time: time.Now(), Valid: true},

‎internal/handler/generic.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ func (h *GenericHandler) handleReleaseAsset(w http.ResponseWriter, r *http.Reque
130130
return
131131
}
132132

133-
if result.ContentType == "" {
134-
result.ContentType = "application/octet-stream"
133+
if result.Artifact.MediaType == "" {
134+
result.Artifact.MediaType = "application/octet-stream"
135135
}
136136
serveArtifact(w, r.Method, result)
137137
}

0 commit comments

Comments
 (0)