diff --git a/modules/distribution/internal/holder/holder_test.go b/modules/distribution/internal/holder/holder_test.go index aa76a8bcb..6329d6f54 100644 --- a/modules/distribution/internal/holder/holder_test.go +++ b/modules/distribution/internal/holder/holder_test.go @@ -63,24 +63,32 @@ func (b fakeBuilder) Recorded(string, string, string) (bool, string, error) { type fakeStore struct { has map[string]bool - held map[string]string // name@version to the digest the store holds there + held map[string]heldAt // name@version to what the store holds there put []map[string][]byte } +// heldAt is what a version's tag names in the store: the manifest's digest and, for a bundle or an archive, the one layer +// that manifest keeps, two digests as the store holds them (H-A-474). +type heldAt struct{ manifest, layer string } + func (s *fakeStore) Has(_, d string) (bool, error) { return s.has[d], nil } func (s *fakeStore) Put(t PublishRequest, parts map[string][]byte) (string, error) { s.put = append(s.put, parts) return t.Name + "@" + t.Digest, nil } -func (s *fakeStore) Held(name, version, digest string) (string, string, error) { - switch d := s.held[name+"@"+version]; d { - case "": +func (s *fakeStore) Held(r PublishRequest) (string, string, error) { + at, ok := s.held[r.Name+"@"+r.Version] + if !ok { return "", "", nil - case digest: - return name + "@" + d, "", nil - default: + } + d := at.layer + if r.Kind == KindImage || r.Kind == KindUpstream { + d = at.manifest + } + if d != r.Digest { return "", d, nil } + return r.Name + "@" + d, "", nil } func (s *fakeStore) Holds(r PublishRequest) (bool, error) { return s.has[r.Digest], nil } @@ -346,7 +354,7 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) { return err }, "the builder did not reserve it"}, {"a version the store holds at another digest", func(b *bed) error { - b.store.held = map[string]string{"m/program@0.1.4": "sha256:bb"} + b.store.held = map[string]heldAt{"m/program@0.1.4": {manifest: "sha256:cc", layer: "sha256:bb"}} r := req("t") r.Digest = "sha256:aa" _, err := b.h.Publish(ArtifactSeat, builder, r) // the reservation names no digest yet: the bed's builder says yes @@ -388,6 +396,47 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) { } } }) + // H-A-474: the store judges a held version like with like, through its own OCI code over a store in memory. A bundle + // is stored as a manifest whose digest is not its layer's: the same bytes published again are already held (ended, no + // error, nothing stored again, also on a repeated ask); other bytes are refused naming the layer the store holds. An + // image is judged by its manifest's digest. + t.Run("a held version is judged by the artifact's own digest", func(t *testing.T) { + reg := newFakeRegistry() + sts := httptest.NewServer(reg) + defer sts.Close() + u, _ := url.Parse(sts.URL) + o := &OCI{Base: u} + if _, err := o.Put(req("first"), map[string][]byte{part.Digest: blob}); err != nil { + t.Fatal(err) + } + if m, _ := o.Digest("m/program", "0.1.4"); m == "" || m == part.Digest { + t.Fatalf("the stored manifest's digest %q; want one, not the layer's %s", m, part.Digest) + } + b := newBed(true) + b.h.Store = o + uploads := len(reg.blobs) + a, err := b.h.Publish(ArtifactSeat, builder, req("again")) + if err != nil || !a.Ended || len(a.Missing) != 0 || len(b.said) != 1 || b.said[0].event != EventPublished || b.findings["publish-refused m/program@0.1.4"] { + t.Errorf("the same bundle again = %+v, %v; said %+v; want already held, published said, nothing refused", a, err, b.said) + } + if a, err := b.h.Repeat(ArtifactSeat, builder, req("again")); err != nil || !a.Ended { + t.Errorf("a repeated ask of the held bundle = %+v, %v; want ended", a, err) + } + other := req("other") + other.Digest = digestOf([]byte("other bytes")) + if _, err := b.h.Publish(ArtifactSeat, builder, other); err == nil || !strings.Contains(err.Error(), "at digest "+part.Digest+", not "+other.Digest) { + t.Errorf("other bytes at the held version = %v; want refused naming the held layer %s", err, part.Digest) + } + if len(reg.blobs) != uploads { + t.Errorf("the store took %d blobs more; want none", len(reg.blobs)-uploads) + } + img := []byte(`{"schemaVersion":2,"mediaType":"application/vnd.oci.image.manifest.v1+json","layers":[]}`) + reg.manifests["m/image:0.2.0"], reg.types["m/image:0.2.0"] = img, mediaManifest + ir := PublishRequest{Name: "m/image", Version: "0.2.0", Kind: KindImage, Digest: digestOf(img)} + if ref, other, err := o.Held(ir); err != nil || ref != "m/image@"+digestOf(img) || other != "" { + t.Errorf("an image held at its manifest = %q, %q, %v; want held", ref, other, err) + } + }) t.Run("only the missing layers are sent, and an idle transfer is dropped", func(t *testing.T) { b := newBed(true) other := []byte("layer two") diff --git a/modules/distribution/internal/holder/oci.go b/modules/distribution/internal/holder/oci.go index 111ce3549..74a7d1a07 100644 --- a/modules/distribution/internal/holder/oci.go +++ b/modules/distribution/internal/holder/oci.go @@ -207,16 +207,25 @@ func (o *OCI) layerSize(t PublishRequest, parts map[string][]byte) (int64, error return resp.ContentLength, nil } -// Held is the reference of name at version when the store holds digest under it. -func (o *OCI) Held(name, version, digest string) (string, string, error) { - d, err := o.Digest(name, version) +// Held is the reference of r's name at r's version when the store holds r's digest under it, compared like with like +// (H-A-474): an image's by the manifest's digest the version's tag names, a bundle's or an archive's by the one layer +// that manifest keeps (the blob Put stored and the reference names). When the store holds the version at another digest, +// other names that digest of the same kind. +func (o *OCI) Held(r PublishRequest) (string, string, error) { + var d string + var err error + if r.Kind == KindImage || r.Kind == KindUpstream { + d, err = o.Digest(r.Name, r.Version) + } else { + d, err = o.Blob(r.Name, r.Version) + } if err != nil || d == "" { return "", "", err } - if d != digest { + if d != r.Digest { return "", d, nil } - return name + "@" + d, "", nil + return r.Name + "@" + d, "", nil } // Holds is whether the store holds what r's digest names under r's name: an image's manifest by its digest, or a bundle's diff --git a/modules/distribution/internal/holder/transfer.go b/modules/distribution/internal/holder/transfer.go index 706f8e989..4114ae5e3 100644 --- a/modules/distribution/internal/holder/transfer.go +++ b/modules/distribution/internal/holder/transfer.go @@ -140,10 +140,11 @@ type Store interface { Has(name, digest string) (bool, error) // Put stores a publish's parts under its name and version and answers the reference's path (SeatReference). Put(r PublishRequest, parts map[string][]byte) (reference string, err error) - // Held is the reference's path of name at version when the store already holds digest under it: a rebuild of a held - // fingerprint answers it and stores nothing. When the store holds the version at another digest, other names that digest - // and reference is empty; neither is set when the store holds no such version. - Held(name, version, digest string) (reference, other string, err error) + // Held is the reference's path of a publish's name at its version when the store already holds its digest under it: a + // rebuild of a held fingerprint answers it and stores nothing. The digest is compared like with like (H-A-474): an + // image's manifest digest, a bundle's or an archive's blob digest. When the store holds the version at another digest, + // other names that digest and reference is empty; neither is set when the store holds no such version. + Held(r PublishRequest) (reference, other string, err error) // Holds says whether the store holds what a publish's digest names under its name: an image's manifest, or a bundle's or // an archive's blob. The store is the artifact store (A-50): `artifact-store:///blobs/` is that blob. Holds(r PublishRequest) (bool, error) @@ -269,7 +270,7 @@ func (h *Holder) Publish(seat, caller string, r PublishRequest) (PublishAnswer, if r.Version != "" { var ref, other string err := h.timed("the store's read of what it holds (the program's registry)", func() (err error) { - ref, other, err = h.Store.Held(r.Name, r.Version, r.Digest) + ref, other, err = h.Store.Held(r) return err }) if err != nil { @@ -417,7 +418,7 @@ func (h *Holder) Repeat(seat, caller string, r PublishRequest) (PublishAnswer, e if r.Version != "" { var ref string err := h.timed("the store's read of what it holds (the program's registry)", func() (err error) { - ref, _, err = h.Store.Held(r.Name, r.Version, r.Digest) + ref, _, err = h.Store.Held(r) return err }) if err != nil {