diff --git a/modules/distribution/internal/holder/holder_test.go b/modules/distribution/internal/holder/holder_test.go index 9f2f0f77e..04fe48869 100644 --- a/modules/distribution/internal/holder/holder_test.go +++ b/modules/distribution/internal/holder/holder_test.go @@ -62,8 +62,9 @@ func (b fakeBuilder) Recorded(string, string, string) (bool, string, error) { } type fakeStore struct { - has map[string]bool - put []map[string][]byte + has map[string]bool + held map[string]string // name@version to the digest the store holds there + put []map[string][]byte } func (s *fakeStore) Has(_, d string) (bool, error) { return s.has[d], nil } @@ -71,8 +72,17 @@ func (s *fakeStore) Put(t PublishRequest, parts map[string][]byte) (string, erro s.put = append(s.put, parts) return t.Name + "@" + t.Digest, nil } -func (s *fakeStore) Held(string, string, string) (string, bool, error) { return "", false, nil } -func (s *fakeStore) Holds(r PublishRequest) (bool, error) { return s.has[r.Digest], nil } +func (s *fakeStore) Held(name, version, digest string) (string, string, error) { + switch d := s.held[name+"@"+version]; d { + case "": + return "", "", nil + case digest: + return name + "@" + d, "", nil + default: + return "", d, nil + } +} +func (s *fakeStore) Holds(r PublishRequest) (bool, error) { return s.has[r.Digest], nil } type said struct { seat, event string @@ -317,6 +327,16 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) { _, err := b.h.Publish(ArtifactSeat, builder, req("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"} + 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 + if !b.findings["publish-refused m/program@0.1.4"] || len(b.store.put) != 0 { + return errors.New("not refused loudly, or stored") + } + return err + }, "the store holds m/program@0.1.4 at digest sha256:bb, not sha256:aa"}, {"an image on the artifact seat", func(b *bed) error { r := req("t") r.Kind = KindImage diff --git a/modules/distribution/internal/holder/oci.go b/modules/distribution/internal/holder/oci.go index a721376ce..111ce3549 100644 --- a/modules/distribution/internal/holder/oci.go +++ b/modules/distribution/internal/holder/oci.go @@ -208,15 +208,15 @@ func (o *OCI) layerSize(t PublishRequest, parts map[string][]byte) (int64, error } // Held is the reference of name at version when the store holds digest under it. -func (o *OCI) Held(name, version, digest string) (string, bool, error) { +func (o *OCI) Held(name, version, digest string) (string, string, error) { d, err := o.Digest(name, version) if err != nil || d == "" { - return "", false, err + return "", "", err } if d != digest { - return "", false, nil + return "", d, nil } - return name + "@" + d, true, nil + return 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 7b3bcba67..dfb3e6814 100644 --- a/modules/distribution/internal/holder/transfer.go +++ b/modules/distribution/internal/holder/transfer.go @@ -141,8 +141,9 @@ type Store interface { // 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. - Held(name, version, digest string) (reference string, ok bool, err error) + // 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) // 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) @@ -266,15 +267,19 @@ func (h *Holder) Publish(seat, caller string, r PublishRequest) (PublishAnswer, } // A rebuild of a held fingerprint answers the held version and stores nothing. if r.Version != "" { - var ref string - var ok bool + var ref, other string err := h.timed("the store's read of what it holds (the program's registry)", func() (err error) { - ref, ok, err = h.Store.Held(r.Name, r.Version, r.Digest) + ref, other, err = h.Store.Held(r.Name, r.Version, r.Digest) return err }) if err != nil { return PublishAnswer{}, fmt.Errorf("publish: the store cannot say whether it holds %s@%s: %w", r.Name, r.Version, err) - } else if ok { + } else if other != "" { + // A second publish of a held version with another digest is refused before the reservation is asked, as the forge + // holds the first (A-490j, H-A-393): while the reserved digest is still empty it would pass, and the store's Put + // would move the version's tag. + return h.refusePublish(seat, r, fmt.Sprintf("the store holds %s@%s at digest %s, not %s", r.Name, r.Version, other, r.Digest)) + } else if ref != "" { return PublishAnswer{Transfer: r.Transfer, Ended: true}, h.timed("the published event on the seat", func() error { return h.Say(seat, EventPublished, h.eventOf(seat, r, SeatReference(seat, ref), "")) }) @@ -298,6 +303,16 @@ func (h *Holder) Publish(seat, caller string, r PublishRequest) (PublishAnswer, return answerOf(got), nil } +// refusePublish ends a publish refused before anything is stored: its finding raised, refused said on the seat, the reason +// answered. +func (h *Holder) refusePublish(seat string, r PublishRequest, reason string) (PublishAnswer, error) { + h.Finding("publish-refused", r.Name+"@"+r.Version, reason, true) + if err := h.Say(seat, EventRefused, h.eventOf(seat, r, "", reason)); err != nil { + return PublishAnswer{}, fmt.Errorf("publish: %s, and the refused is not said: %w", reason, err) + } + return PublishAnswer{Transfer: r.Transfer, Ended: true}, errors.New("publish: " + reason) +} + // tagHeld is a publish with a version, a digest and no parts (design 47 G A-417, A-73, A-50): what the store holds of that // digest under the name (the build's untagged publish, the artifact store's blob) is checked against the builder's reservation // and tagged with the version, and published is said; when the store holds none of it, the publish is refused as not stored, @@ -311,13 +326,7 @@ func (h *Holder) tagHeld(seat string, r PublishRequest) (PublishAnswer, error) { if err != nil { return PublishAnswer{}, fmt.Errorf("publish: the store cannot say whether it holds %s of %s: %w", r.Digest, r.Name, err) } - refuse := func(reason string) (PublishAnswer, error) { - h.Finding("publish-refused", r.Name+"@"+r.Version, reason, true) - if err := h.Say(seat, EventRefused, h.eventOf(seat, r, "", reason)); err != nil { - return PublishAnswer{}, fmt.Errorf("publish: %s, and the refused is not said: %w", reason, err) - } - return PublishAnswer{Transfer: r.Transfer, Ended: true}, errors.New("publish: " + reason) - } + refuse := func(reason string) (PublishAnswer, error) { return h.refusePublish(seat, r, reason) } if !held { return refuse(fmt.Sprintf("not stored: the store holds no %s of %s, so version %s names nothing", r.Digest, r.Name, r.Version)) }