Skip to content

Commit 1262471

Browse files
authored
fix: prevent digest-mismatch on archive updates (canonical#296)
Chisel intermittently fails with `expected digest X, got Y` when Ubuntu's archive is mid-publication and InRelease is temporarily inconsistent with `Packages.gz` at the named path (a window we observed lasting at least 39 minutes against the same publication in real CI runs for [Ella Core](https://github.com/ellanetworks/core)). Fetch the package index by content hash using apt's `Acquire-By-Hash` , which Ubuntu archives have advertised since 16.04 (2016), so the URL itself encodes the expected bytes; falls back to the named path when an archive doesn't advertise the feature or a specific hash has been garbage-collected. Fixes canonical#295 Reference: https://wiki.debian.org/DebianRepository/Format#Acquire-By-Hash
1 parent ea61b84 commit 1262471

3 files changed

Lines changed: 195 additions & 30 deletions

File tree

‎internal/archive/archive.go‎

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,13 @@ func Open(options *Options) (Archive, error) {
6464
type fetchFlags uint
6565

6666
const (
67-
fetchBulk fetchFlags = 1 << iota
67+
fetchBulk fetchFlags = 1 << iota
68+
fetchGzip
6869
fetchDefault fetchFlags = 0
6970
)
7071

72+
var errNotFound = fmt.Errorf("cannot find archive data")
73+
7174
var httpClient = &http.Client{
7275
Timeout: 30 * time.Second,
7376
}
@@ -326,17 +329,40 @@ func (index *ubuntuIndex) fetchRelease() error {
326329
}
327330

328331
func (index *ubuntuIndex) fetchIndex() error {
329-
digests := index.release.Get("SHA256")
332+
releaseDigests := index.release.Get("SHA256")
330333
packagesPath := fmt.Sprintf("%s/binary-%s/Packages", index.component, index.arch)
331-
digest, _, _ := control.ParsePathInfo(digests, packagesPath)
332-
if digest == "" {
334+
packagesDigest, _, _ := control.ParsePathInfo(releaseDigests, packagesPath)
335+
if packagesDigest == "" {
333336
return fmt.Errorf("%s is missing from %s %s component digests", packagesPath, index.suite, index.component)
334337
}
335338

336339
logf("Fetching index for %s %s %s %s component...", index.displayName(), index.version, index.suite, index.component)
337-
reader, err := index.fetch(index.distPath(packagesPath+".gz"), digest, fetchBulk)
338-
if err != nil {
339-
return err
340+
341+
// Prefer acquire-by-hash when the archive advertises it. By-hash URLs
342+
// are content-addressed and so are immune to the inconsistent view of
343+
// InRelease vs Packages.gz that mirrors and CDNs can serve while a
344+
// publication is propagating. See https://wiki.ubuntu.com/AptByHash.
345+
packagesGzPath := packagesPath + ".gz"
346+
var reader io.ReadSeekCloser
347+
if index.release.Get("Acquire-By-Hash") == "yes" {
348+
packagesGzDigest, _, _ := control.ParsePathInfo(releaseDigests, packagesGzPath)
349+
if packagesGzDigest != "" {
350+
packagesByHashPath := fmt.Sprintf("%s/binary-%s/by-hash/SHA256/%s", index.component, index.arch, packagesGzDigest)
351+
r, err := index.fetch(index.distPath(packagesByHashPath), packagesDigest, fetchBulk|fetchGzip)
352+
if err != nil && err != errNotFound {
353+
return err
354+
}
355+
// On 404 fall through to the named-path fetch below: the hash
356+
// may have been garbage-collected on the mirror.
357+
reader = r
358+
}
359+
}
360+
if reader == nil {
361+
r, err := index.fetch(index.distPath(packagesGzPath), packagesDigest, fetchBulk|fetchGzip)
362+
if err != nil {
363+
return err
364+
}
365+
reader = r
340366
}
341367
ctrl, err := control.ParseReader("Package", reader)
342368
if err != nil {
@@ -411,13 +437,13 @@ func (index *ubuntuIndex) fetch(path, digest string, flags fetchFlags) (io.ReadS
411437
case 401:
412438
return nil, fmt.Errorf("cannot fetch from %q: unauthorized", index.label)
413439
case 404:
414-
return nil, fmt.Errorf("cannot find archive data")
440+
return nil, errNotFound
415441
default:
416442
return nil, fmt.Errorf("error from archive: %v", resp.Status)
417443
}
418444

419445
body := resp.Body
420-
if strings.HasSuffix(path, ".gz") {
446+
if flags&fetchGzip != 0 {
421447
reader, err := gzip.NewReader(body)
422448
if err != nil {
423449
return nil, fmt.Errorf("cannot decompress data: %v", err)

‎internal/archive/archive_test.go‎

Lines changed: 140 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,25 @@ import (
2222
"github.com/canonical/chisel/internal/testutil"
2323
)
2424

25+
type requestResult struct {
26+
path string
27+
status int
28+
}
29+
2530
type httpSuite struct {
26-
logf func(string, ...any)
27-
base string
28-
request *http.Request
29-
requests []*http.Request
30-
response string
31-
responses map[string][]byte
32-
err error
33-
header http.Header
34-
status int
35-
restore func()
36-
privKey *packet.PrivateKey
37-
pubKey *packet.PublicKey
31+
logf func(string, ...any)
32+
base string
33+
request *http.Request
34+
requests []*http.Request
35+
requestResults []requestResult
36+
response string
37+
responses map[string][]byte
38+
err error
39+
header http.Header
40+
status int
41+
restore func()
42+
privKey *packet.PrivateKey
43+
pubKey *packet.PublicKey
3844
}
3945

4046
var _ = Suite(&httpSuite{})
@@ -54,6 +60,7 @@ func (s *httpSuite) SetUpTest(c *C) {
5460
s.base = "http://archive.ubuntu.com/ubuntu/"
5561
s.request = nil
5662
s.requests = nil
63+
s.requestResults = nil
5764
s.response = ""
5865
s.responses = make(map[string][]byte)
5966
s.header = nil
@@ -83,14 +90,19 @@ func (s *httpSuite) Do(req *http.Request) (*http.Response, error) {
8390
s.request = req
8491
s.requests = append(s.requests, req)
8592
body := s.response
93+
status := s.status
8694
s.logf("Request: %s", req.URL.String())
8795
if response, ok := s.responses[path.Clean(req.URL.Path)]; ok {
8896
body = string(response)
97+
} else if len(s.responses) > 0 && s.status == 200 {
98+
// Unknown path with responses populated: behave like a real archive.
99+
status = 404
89100
}
101+
s.requestResults = append(s.requestResults, requestResult{path: req.URL.Path, status: status})
90102
rsp := &http.Response{
91103
Body: io.NopCloser(strings.NewReader(body)),
92104
Header: s.header,
93-
StatusCode: s.status,
105+
StatusCode: status,
94106
}
95107
return rsp, s.err
96108
}
@@ -625,6 +637,121 @@ func read(r io.Reader) string {
625637
return string(data)
626638
}
627639

640+
// fetchRequestStatus checks whether a request was made whose URL path
641+
// contains the given substring. If so, it returns true and the HTTP status
642+
// code from the most recent matching request. If not, it returns false, 0.
643+
func (s *httpSuite) fetchRequestStatus(pathSubstring string) (bool, int) {
644+
for i := len(s.requestResults) - 1; i >= 0; i-- {
645+
if strings.Contains(s.requestResults[i].path, pathSubstring) {
646+
return true, s.requestResults[i].status
647+
}
648+
}
649+
return false, 0
650+
}
651+
652+
func (s *httpSuite) TestFetchByHashSucceedsWhenNamedPathIsStale(c *C) {
653+
s.prepareArchiveAdjustRelease("jammy", "22.04", "amd64", []string{"main"}, func(r *testarchive.Release) {
654+
r.ByHash = true
655+
})
656+
657+
// Simulate a mirror serving stale content at the named Packages.gz path
658+
// while the by-hash path still has the correct data.
659+
for p := range s.responses {
660+
if strings.Contains(p, "Packages.gz") && !strings.Contains(p, "/by-hash/") {
661+
s.responses[p] = testarchive.MakeGzip([]byte("stale Packages from previous publication"))
662+
}
663+
}
664+
665+
options := archive.Options{
666+
Label: "ubuntu",
667+
Version: "22.04",
668+
Arch: "amd64",
669+
Suites: []string{"jammy"},
670+
Components: []string{"main"},
671+
CacheDir: c.MkDir(),
672+
PubKeys: []*packet.PublicKey{s.pubKey},
673+
}
674+
675+
testArchive, err := archive.Open(&options)
676+
c.Assert(err, IsNil)
677+
678+
pkg, _, err := testArchive.Fetch("mypkg1")
679+
c.Assert(err, IsNil)
680+
c.Assert(read(pkg), Equals, "mypkg1 1.1 data")
681+
682+
// The by-hash request must have been attempted and succeeded with 200,
683+
// since the named path has stale content and only by-hash has the
684+
// correct data.
685+
attempted, status := s.fetchRequestStatus("/by-hash/SHA256/")
686+
c.Assert(attempted, Equals, true)
687+
c.Assert(status, Equals, 200)
688+
}
689+
690+
func (s *httpSuite) TestFetchByHashFallsBackOnNotFound(c *C) {
691+
s.prepareArchiveAdjustRelease("jammy", "22.04", "amd64", []string{"main"}, func(r *testarchive.Release) {
692+
r.ByHash = true
693+
})
694+
695+
// Simulate a mirror that garbage-collected the by-hash entries.
696+
for p := range s.responses {
697+
if strings.Contains(p, "/by-hash/") {
698+
delete(s.responses, p)
699+
}
700+
}
701+
702+
options := archive.Options{
703+
Label: "ubuntu",
704+
Version: "22.04",
705+
Arch: "amd64",
706+
Suites: []string{"jammy"},
707+
Components: []string{"main"},
708+
CacheDir: c.MkDir(),
709+
PubKeys: []*packet.PublicKey{s.pubKey},
710+
}
711+
712+
testArchive, err := archive.Open(&options)
713+
c.Assert(err, IsNil)
714+
715+
pkg, _, err := testArchive.Fetch("mypkg1")
716+
c.Assert(err, IsNil)
717+
c.Assert(read(pkg), Equals, "mypkg1 1.1 data")
718+
719+
// The by-hash request must have been attempted but got 404 (the hash
720+
// was garbage-collected), so we fell back to the named path which
721+
// returned 200 with the correct data.
722+
attempted, status := s.fetchRequestStatus("/by-hash/SHA256/")
723+
c.Assert(attempted, Equals, true)
724+
c.Assert(status, Equals, 404)
725+
attempted, status = s.fetchRequestStatus("Packages.gz")
726+
c.Assert(attempted, Equals, true)
727+
c.Assert(status, Equals, 200)
728+
}
729+
730+
func (s *httpSuite) TestFetchSkipsByHashWhenNotAdvertised(c *C) {
731+
s.prepareArchive("jammy", "22.04", "amd64", []string{"main"})
732+
733+
options := archive.Options{
734+
Label: "ubuntu",
735+
Version: "22.04",
736+
Arch: "amd64",
737+
Suites: []string{"jammy"},
738+
Components: []string{"main"},
739+
CacheDir: c.MkDir(),
740+
PubKeys: []*packet.PublicKey{s.pubKey},
741+
}
742+
743+
_, err := archive.Open(&options)
744+
c.Assert(err, IsNil)
745+
746+
// When by-hash is not advertised, no by-hash request should be
747+
// attempted; only the named Packages.gz path is fetched.
748+
attempted, _ := s.fetchRequestStatus("/by-hash/SHA256/")
749+
c.Assert(attempted, Equals, false)
750+
attempted, status := s.fetchRequestStatus("Packages.gz")
751+
c.Assert(attempted, Equals, true)
752+
c.Assert(status, Equals, 200)
753+
}
754+
628755
// ----------------------------------------------------------------------------------------
629756
// Real archive tests, only enabled via:
630757
// 1. --real-archive for non-Pro archives (e.g. standard jammy archive),

‎internal/archive/testarchive/testarchive.go‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (gz *Gzip) Section() []byte {
5454
}
5555

5656
func (gz *Gzip) Content() []byte {
57-
return makeGzip(gz.Item.Content())
57+
return MakeGzip(gz.Item.Content())
5858
}
5959

6060
type Package struct {
@@ -107,6 +107,9 @@ type Release struct {
107107
Label string
108108
Items []Item
109109
PrivKey *packet.PrivateKey
110+
// ByHash enables the Acquire-By-Hash flag in the Release file
111+
// and renders by-hash URLs alongside named paths.
112+
ByHash bool
110113
}
111114

112115
func (r *Release) Walk(f func(Item) error) error {
@@ -127,6 +130,10 @@ func (r *Release) Content() []byte {
127130
content := item.Content()
128131
fmt.Fprintf(&digests, " %s %d %s\n", makeSha256(content), len(content), item.Path())
129132
}
133+
acquireByHash := ""
134+
if r.ByHash {
135+
acquireByHash = "Acquire-By-Hash: yes\n"
136+
}
130137
content := fmt.Sprintf(string(testutil.Reindent(`
131138
Origin: Ubuntu
132139
Label: %s
@@ -137,9 +144,9 @@ func (r *Release) Content() []byte {
137144
Architectures: amd64 arm64 armhf i386 ppc64el riscv64 s390x
138145
Components: main restricted universe multiverse
139146
Description: Ubuntu %s
140-
SHA256:
147+
%sSHA256:
141148
%s
142-
`)), r.Label, r.Suite, r.Version, r.Version, digests.String())
149+
`)), r.Label, r.Suite, r.Version, r.Version, acquireByHash, digests.String())
143150

144151
var buf bytes.Buffer
145152
writer, err := clearsign.Encode(&buf, r.PrivKey, nil)
@@ -160,12 +167,17 @@ func (r *Release) Content() []byte {
160167
func (r *Release) Render(prefix string, content map[string][]byte) error {
161168
return r.Walk(func(item Item) error {
162169
itemPath := item.Path()
170+
itemContent := item.Content()
163171
if strings.HasPrefix(itemPath, "pool/") {
164-
itemPath = path.Join(prefix, itemPath)
165-
} else {
166-
itemPath = path.Join(prefix, "dists", r.Suite, itemPath)
172+
content[path.Join(prefix, itemPath)] = itemContent
173+
return nil
174+
}
175+
distItemPath := path.Join(prefix, "dists", r.Suite, itemPath)
176+
content[distItemPath] = itemContent
177+
if r.ByHash && itemPath != r.Path() {
178+
byHashPath := path.Join(prefix, "dists", r.Suite, path.Dir(itemPath), "by-hash", "SHA256", makeSha256(itemContent))
179+
content[byHashPath] = itemContent
167180
}
168-
content[itemPath] = item.Content()
169181
return nil
170182
})
171183
}
@@ -204,7 +216,7 @@ func makeSha256(b []byte) string {
204216
return fmt.Sprintf("%x", sha256.Sum256(b))
205217
}
206218

207-
func makeGzip(b []byte) []byte {
219+
func MakeGzip(b []byte) []byte {
208220
var buf bytes.Buffer
209221
gz := gzip.NewWriter(&buf)
210222
_, err := gz.Write(b)

0 commit comments

Comments
 (0)