Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1715,7 +1715,7 @@ Set to `"0"` to always revalidate with upstream (ETag-based conditional requests

When upstream is unreachable and the cached entry is past its TTL (with a `metadata_ttl` of `"0"`, on every such request), the proxy serves the stale cached copy with a `Warning: 110 - "Response is Stale"` header so clients can tell the data may be outdated. The `ETag` and `Last-Modified` sent with a document are always those of the bytes served, so a document fetched from upstream but not stored, for example because storage failed, carries the upstream's validators rather than the older cached copy's.

APT and RPM clients fetch a release file first (`InRelease` or `Release`, `repomd.xml`) and reject an index file whose checksum doesn't match it. So an index file the cached release file lists (`Packages`, `Sources`, a plainly named `primary.xml.gz`, …) does not follow its own `metadata_ttl`: its cached copy is served while it matches the cached release file's sha256, however old, and otherwise it is fetched from upstream. If upstream's copy doesn't match either, the repository has changed since the release file was cached; that copy is served and the release file is expired, so the next `apt update` or `dnf makecache` gets a matching pair. Where a suite has both `InRelease` and `Release` cached, the one fetched last decides. The release file itself follows `metadata_ttl`. Index files it doesn't list, such as `by-hash` paths, follow `metadata_ttl` too.
APT and RPM clients fetch a release file first (`InRelease` or `Release`, `repomd.xml`) and reject an index file whose checksum doesn't match it. So an index file the cached release file lists (`Packages`, `Sources`, a plainly named `primary.xml.gz`, …) does not follow its own `metadata_ttl`: its cached copy is served while it matches the cached release file's sha256, however old, and otherwise it is fetched from upstream. If upstream's copy doesn't match either, the repository has changed since the release file was cached; that copy is served and the release file is expired, so the next `apt update` or `dnf makecache` gets a matching pair. Where a suite has both `InRelease` and `Release` cached, the one fetched last decides. The release file itself follows `metadata_ttl`. Its detached signature (`Release.gpg`, `repomd.xml.asc`) follows it: a cached signature fetched before the release file was last fetched is fetched again, so it isn't served with a release file it doesn't sign. Index files it doesn't list, such as `by-hash` paths, follow `metadata_ttl` too.

### Change feeds

Expand Down
9 changes: 7 additions & 2 deletions internal/handler/debian.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ func (h *DebianHandler) handlePackageDownload(
// handleMetadata serves repository metadata files through the metadata cache,
// so they keep resolving while the upstream archive is unreachable.
// Index files the cached InRelease or Release lists follow that listing (see
// proxyListedIndex).
// proxyListedIndex), and Release.gpg follows Release (see
// proxyReleaseSignature).
func (h *DebianHandler) handleMetadata(
w http.ResponseWriter,
r *http.Request,
Expand All @@ -200,9 +201,13 @@ func (h *DebianHandler) handleMetadata(
cacheKey := h.metadataCacheKeyFor(repository, upstreamURL, path)
fileURL := fmt.Sprintf("%s/%s", upstreamURL, path)
switch pathpkg.Base(path) {
case "InRelease", "Release", "Release.gpg":
case "InRelease", "Release":
h.proxy.ProxyCached(w, r, fileURL, "debian", cacheKey, "*/*")
return
case "Release.gpg":
releaseKey := h.metadataCacheKeyFor(repository, upstreamURL, strings.TrimSuffix(path, ".gpg"))
h.proxy.proxyReleaseSignature(w, r, fileURL, "debian", cacheKey, releaseKey)
return
}
release, want := h.releaseListing(r, repository, upstreamURL, path)
h.proxy.proxyListedIndex(w, r, fileURL, "debian", cacheKey, release, want)
Expand Down
32 changes: 32 additions & 0 deletions internal/handler/release_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,35 @@ func repomdSHA256(repomd []byte, href string) string {
}
return ""
}

// proxyReleaseSignature serves the detached signature of the release file
// cached under releaseKey (Release.gpg, repomd.xml.asc). Each has its own
// cache entry, so a cached signature fetched before the release file was
// fetched again, after its TTL or because an index showed upstream had
// moved on, signs an older release file. It is fetched again rather than
// served with one it doesn't match.
func (p *Proxy) proxyReleaseSignature(w http.ResponseWriter, r *http.Request, upstreamURL, ecosystem, cacheKey, releaseKey string) {
if !p.signaturePredatesRelease(ecosystem, cacheKey, releaseKey) {
p.ProxyCached(w, r, upstreamURL, ecosystem, cacheKey, "*/*")
return
}
p.recordMetadataCacheMiss(ecosystem)
res := p.refreshMetadata(r.Context(), ecosystem, cacheKey, upstreamURL, "*/*", "identity", nil)
if res.err != nil {
p.writeMetadataFetchError(w, res.err)
return
}
writeMetadataResult(w, r, res)
}

// signaturePredatesRelease reports whether the signature cached under
// cacheKey was fetched before the release file cached under releaseKey.
func (p *Proxy) signaturePredatesRelease(ecosystem, cacheKey, releaseKey string) bool {
if !p.CacheMetadata || p.DB == nil {
return false
}
signature, _ := p.DB.GetMetadataCache(ecosystem, cacheKey)
release, _ := p.DB.GetMetadataCache(ecosystem, releaseKey)
return signature != nil && release != nil && signature.FetchedAt.Valid &&
release.FetchedAt.Time.After(signature.FetchedAt.Time)
}
120 changes: 120 additions & 0 deletions internal/handler/release_signature_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package handler

import (
"fmt"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
)

func signatureBody(gen int32) string { return fmt.Sprintf("signature of release generation %d\n", gen) }

// signatureCase is a repository whose release file has a detached
// signature: Debian Release and Release.gpg, RPM repomd.xml and
// repomd.xml.asc.
type signatureCase struct {
releaseIndexCase
signaturePath string
}

func signatureCases() []signatureCase {
cases := releaseIndexCases()
debian := cases[0]
debian.name = "debian Release.gpg"
debian.archive.releasePath = "dists/stable/Release"
return []signatureCase{
{releaseIndexCase: debian, signaturePath: "dists/stable/Release.gpg"},
{releaseIndexCase: cases[1], signaturePath: "f/39/os/repodata/repomd.xml.asc"},
}
}

// signedArchive serves tc's archive and a signature for each generation of
// its release file, counting the signature requests.
func signedArchive(t *testing.T, tc signatureCase) (http.Handler, func(path string), *atomic.Int32) {
t.Helper()
a := tc.archive
var signatures atomic.Int32
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/"+tc.signaturePath {
signatures.Add(1)
_, _ = w.Write([]byte(signatureBody(a.gen.Load())))
return
}
a.serve(w, r)
}))
t.Cleanup(upstream.Close)
proxy, db, _, _ := setupTestProxy(t)
proxy.CacheMetadata = true
proxy.MetadataTTL = time.Hour
proxy.HTTPClient = upstream.Client()
h, key := tc.handler(proxy, upstream.URL)
expire := func(path string) {
t.Helper()
if _, err := db.ExpireMetadataCache(tc.ecosystem, []string{key(path)}, time.Now().Add(time.Second)); err != nil {
t.Fatal(err)
}
}
return h, expire, &signatures
}

// TestSignatureFollowsRefetchedRelease checks that a cached detached
// signature isn't served with a release file fetched after it, which it
// can't sign.
func TestSignatureFollowsRefetchedRelease(t *testing.T) {
for _, tc := range signatureCases() {
t.Run(tc.name, func(t *testing.T) {
a := tc.archive
h, expire, signatures := signedArchive(t, tc)

getBody(t, h, a.releasePath)
getBody(t, h, tc.signaturePath)

// An index the cached release file doesn't list shows upstream
// has moved on, and the release file is expired.
a.gen.Store(1)
getBody(t, h, a.indexPath)
getBody(t, h, a.releasePath)
if got := getBody(t, h, tc.signaturePath); got != signatureBody(1) {
t.Errorf("signature after the release file was expired = %q, want %q", got, signatureBody(1))
}

// The release file's entry runs out before the signature's.
a.gen.Store(2)
expire(a.releasePath)
getBody(t, h, a.releasePath)
if got := getBody(t, h, tc.signaturePath); got != signatureBody(2) {
t.Errorf("signature after the release file's TTL = %q, want %q", got, signatureBody(2))
}

// A signature fetched after the release file is served from the
// cache.
before := signatures.Load()
if got := getBody(t, h, tc.signaturePath); got != signatureBody(2) {
t.Errorf("cached signature = %q", got)
}
if got := signatures.Load(); got != before {
t.Errorf("signature fetched %d more times, want it served from the cache", got-before)
}
})
}
}

// TestSignatureWithoutCachedRelease checks that a signature asked for
// whose release file isn't cached is served as other metadata.
func TestSignatureWithoutCachedRelease(t *testing.T) {
for _, tc := range signatureCases() {
t.Run(tc.name, func(t *testing.T) {
h, _, signatures := signedArchive(t, tc)
for range 2 {
if got := getBody(t, h, tc.signaturePath); got != signatureBody(0) {
t.Errorf("signature = %q, want %q", got, signatureBody(0))
}
}
if got := signatures.Load(); got != 1 {
t.Errorf("signature fetched %d times, want it cached", got)
}
})
}
}
10 changes: 8 additions & 2 deletions internal/handler/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,27 @@ func (h *RPMHandler) handlePackageDownload(w http.ResponseWriter, r *http.Reques
// primary.xml.gz, etc.). repomd.xml changes with every repository update and
// follows the metadata TTL. The files it points at are usually named after
// their own checksum and so never change; those are kept for good. Plainly
// named ones follow the cached repomd.xml (see proxyListedIndex).
// named ones follow the cached repomd.xml (see proxyListedIndex), and
// repomd.xml.asc follows repomd.xml (see proxyReleaseSignature).
func (h *RPMHandler) handleMetadata(w http.ResponseWriter, r *http.Request, path string) {
upstreamURL := fmt.Sprintf("%s/%s", h.upstreamURL, path)
if rpmContentAddressed(path) {
h.proxy.proxyImmutable(w, r, upstreamURL, "rpm", h.metadataCacheKey(path), "*/*")
return
}
i := strings.LastIndex(path, "/repodata/")
repomdPath := path[:i] + "/repodata/repomd.xml"
if path == repomdPath+".asc" {
h.proxy.proxyReleaseSignature(w, r, upstreamURL, "rpm", h.metadataCacheKey(path), h.metadataCacheKey(repomdPath))
return
}
if strings.HasPrefix(path[i:], "/repodata/repomd.xml") {
h.proxy.ProxyCached(w, r, upstreamURL, "rpm", h.metadataCacheKey(path), "*/*")
return
}
// Plainly named files (createrepo --simple-md-filenames) follow the
// cached repomd.xml that lists them.
repomd := h.proxy.readCachedRelease(r.Context(), "rpm", h.metadataCacheKey(path[:i]+"/repodata/repomd.xml"))
repomd := h.proxy.readCachedRelease(r.Context(), "rpm", h.metadataCacheKey(repomdPath))
var want string
if repomd != nil {
want = repomdSHA256(repomd.body, path[i+1:])
Expand Down
Loading