@@ -16,6 +16,7 @@ import (
1616 "sync"
1717 "time"
1818
19+ "github.com/git-pkgs/artifacts"
1920 "github.com/git-pkgs/cooldown"
2021 "github.com/git-pkgs/proxy/internal/database"
2122 "github.com/git-pkgs/proxy/internal/metrics"
@@ -24,6 +25,7 @@ import (
2425 "github.com/git-pkgs/proxy/internal/storage"
2526 "github.com/git-pkgs/purl"
2627 "github.com/git-pkgs/registries/fetch"
28+ "github.com/opencontainers/go-digest"
2729)
2830
2931// containsPathTraversal returns true if the path contains ".." segments
@@ -205,9 +207,7 @@ func NewProxy(db *database.DB, store storage.Storage, fetcher fetch.FetcherInter
205207type CacheResult struct {
206208 Reader io.ReadCloser
207209 RedirectURL string
208- Size int64
209- ContentType string
210- Hash string
210+ Artifact artifacts.Artifact
211211 Cached bool
212212 storagePath string
213213}
@@ -270,16 +270,14 @@ func (p *Proxy) checkCache(ctx context.Context, pkgPURL, versionPURL, filename s
270270 if artifact == nil {
271271 return nil , nil
272272 }
273- checks , err := newIntegrityChecks (artifact .ContentHash . String , artifact .Integrity .String )
273+ checks , err := newIntegrityChecks (artifact .Artifact . Digest . Encoded () , artifact .Integrity .String )
274274 if err != nil {
275275 p .rejectUnusableCacheRecord (artifact , versionPURL , filename , err )
276276 return nil , nil
277277 }
278278
279279 result := & CacheResult {
280- Size : artifact .Size .Int64 ,
281- ContentType : artifact .ContentType .String ,
282- Hash : artifact .ContentHash .String ,
280+ Artifact : artifact .Artifact ,
283281 Cached : true ,
284282 storagePath : artifact .StoragePath ,
285283 }
@@ -439,8 +437,16 @@ func (p *Proxy) storeArtifact(ctx context.Context, ecosystem, name, version, fil
439437 }
440438 }
441439
440+ sharedArtifact := artifacts.Artifact {
441+ PURL : versionPURL ,
442+ Digest : digest .Digest ("sha256:" + hash ),
443+ Size : size ,
444+ Filename : filename ,
445+ MediaType : artifact .ContentType ,
446+ }
447+
442448 // Update database
443- if err := p .updateCacheDB (ecosystem , name , filename , pkgPURL , versionPURL , upstreamURL , storagePath , hash , size , artifact . ContentType ); err != nil {
449+ if err := p .updateCacheDB (ecosystem , name , pkgPURL , upstreamURL , storagePath , sharedArtifact ); err != nil {
444450 p .Logger .Warn ("failed to update cache database" , "error" , err )
445451 // Continue anyway - we have the file
446452 }
@@ -456,11 +462,9 @@ func (p *Proxy) storeArtifact(ctx context.Context, ecosystem, name, version, fil
456462 }
457463
458464 return & CacheResult {
459- Reader : reader ,
460- Size : size ,
461- ContentType : artifact .ContentType ,
462- Hash : hash ,
463- Cached : false ,
465+ Reader : reader ,
466+ Artifact : sharedArtifact ,
467+ Cached : false ,
464468 }, nil
465469}
466470
@@ -501,7 +505,7 @@ func (p *Proxy) runScan(ctx context.Context, ecosystem, name, version, filename,
501505 return nil
502506}
503507
504- func (p * Proxy ) updateCacheDB (ecosystem , name , filename , pkgPURL , versionPURL , upstreamURL , storagePath , hash string , size int64 , contentType string ) error {
508+ func (p * Proxy ) updateCacheDB (ecosystem , name , pkgPURL , upstreamURL , storagePath string , artifact artifacts. Artifact ) error {
505509 now := time .Now ()
506510
507511 // Upsert package
@@ -518,7 +522,7 @@ func (p *Proxy) updateCacheDB(ecosystem, name, filename, pkgPURL, versionPURL, u
518522
519523 // Upsert version
520524 ver := & database.Version {
521- PURL : versionPURL ,
525+ PURL : artifact . PURL ,
522526 PackagePURL : pkgPURL ,
523527 EnrichedAt : sql.NullTime {Time : now , Valid : true },
524528 }
@@ -528,13 +532,13 @@ func (p *Proxy) updateCacheDB(ecosystem, name, filename, pkgPURL, versionPURL, u
528532
529533 // Upsert artifact
530534 art := & database.Artifact {
531- VersionPURL : versionPURL ,
532- Filename : filename ,
535+ VersionPURL : artifact . PURL ,
536+ Filename : artifact . Filename ,
533537 UpstreamURL : upstreamURL ,
534538 StoragePath : sql.NullString {String : storagePath , Valid : true },
535- ContentHash : sql.NullString {String : hash , Valid : true },
536- Size : sql.NullInt64 {Int64 : size , Valid : true },
537- ContentType : sql.NullString {String : contentType , Valid : true },
539+ ContentHash : sql.NullString {String : artifact . Digest . Encoded () , Valid : true },
540+ Size : sql.NullInt64 {Int64 : artifact . Size , Valid : true },
541+ ContentType : sql.NullString {String : artifact . MediaType , Valid : true },
538542 FetchedAt : sql.NullTime {Time : now , Valid : true },
539543 }
540544 if err := p .DB .UpsertArtifact (art ); err != nil {
@@ -550,9 +554,13 @@ func ServeArtifact(w http.ResponseWriter, result *CacheResult) {
550554}
551555
552556func serveArtifact (w http.ResponseWriter , method string , result * CacheResult ) {
557+ contentHash := ""
558+ if result .Artifact .Digest != "" {
559+ contentHash = result .Artifact .Digest .Encoded ()
560+ }
553561 if result .RedirectURL != "" {
554- if result . Hash != "" {
555- w .Header ().Set (headerETag , `"` + result . Hash + `"` )
562+ if contentHash != "" {
563+ w .Header ().Set (headerETag , `"` + contentHash + `"` )
556564 }
557565 w .Header ().Set ("Location" , result .RedirectURL )
558566 w .WriteHeader (http .StatusFound )
@@ -563,14 +571,14 @@ func serveArtifact(w http.ResponseWriter, method string, result *CacheResult) {
563571 defer func () { _ = result .Reader .Close () }()
564572 }
565573
566- if result .ContentType != "" {
567- w .Header ().Set (headerContentType , result .ContentType )
574+ if result .Artifact . MediaType != "" {
575+ w .Header ().Set (headerContentType , result .Artifact . MediaType )
568576 }
569- if result .Size > 0 || (method == http .MethodHead && result .Size == 0 ) {
570- w .Header ().Set (headerContentLength , strconv .FormatInt (result .Size , 10 ))
577+ if result .Artifact . Size > 0 || (method == http .MethodHead && result . Artifact .Size == 0 ) {
578+ w .Header ().Set (headerContentLength , strconv .FormatInt (result .Artifact . Size , 10 ))
571579 }
572- if result . Hash != "" {
573- w .Header ().Set (headerETag , `"` + result . Hash + `"` )
580+ if contentHash != "" {
581+ w .Header ().Set (headerETag , `"` + contentHash + `"` )
574582 }
575583
576584 w .WriteHeader (http .StatusOK )
@@ -1107,15 +1115,15 @@ func (p *Proxy) getCachedArtifactWithUpstreamHash(ctx context.Context, pkgPURL,
11071115 if err != nil || cached == nil {
11081116 return cached , err
11091117 }
1110- if artifactHashMatches (cached .Hash , upstreamHash ) {
1118+ if artifactHashMatches (cached .Artifact . Digest . Encoded () , upstreamHash ) {
11111119 return cached , nil
11121120 }
11131121
11141122 if cached .Reader != nil {
11151123 _ = cached .Reader .Close ()
11161124 }
11171125 p .Logger .Warn ("cached artifact hash disagrees with upstream metadata, discarding" ,
1118- "purl" , versionPURL , "filename" , filename , "cached" , cached .Hash , "upstream" , upstreamHash )
1126+ "purl" , versionPURL , "filename" , filename , "cached" , cached .Artifact . Digest . Encoded () , "upstream" , upstreamHash )
11191127 p .discardCachedArtifact (ctx , versionPURL , filename , cached .storagePath )
11201128 return nil , nil
11211129}
0 commit comments