diff --git a/modules/distribution/internal/holder/holder_test.go b/modules/distribution/internal/holder/holder_test.go index 62aa77ca3..127318716 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 @@ -336,6 +346,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/reserved.go b/modules/distribution/internal/holder/reserved.go index b649431d5..13e873378 100644 --- a/modules/distribution/internal/holder/reserved.go +++ b/modules/distribution/internal/holder/reserved.go @@ -3,13 +3,14 @@ package holder import ( "encoding/json" "fmt" + "strings" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds" ) // Reserved is the holder's check of a publish against the builder's reservation (novox/hq design 47 §381, §478, §504): it asks the // seat mesh-builds' verb reserved with the publish's name and version, a package's name in its ecosystem, and accepts the publish -// only when that version is the one reserved and an artifact of it has the publish's digest. It reads no catalog and a publish gains +// only when that version is the one reserved and an artifact of it has the publish's digest or none recorded yet (A-490j). It reads no catalog and a publish gains // no field. It is the Builder the holder's Check asks. type Reserved struct { // Ask is the runtime's request and reply over the bus (mesh-sdk stdio.Ask). @@ -31,12 +32,20 @@ func (r Reserved) Recorded(name, version, digest string) (bool, string, error) { return false, "", fmt.Errorf("%s answered no reservation for %s@%s: %s", Address, name, version, raw) } if a.Version != version { - return false, fmt.Sprintf("%s reserved %s for %s, not %s", a.Module, a.Version, name, version), nil + return false, fmt.Sprintf("%s has no reservation of version %s of %s (it reserved %s)", a.Module, version, name, a.Version), nil } + var reserved []string for _, art := range a.Artifacts { - if art.Digest == digest { + // An artifact whose build has not recorded its digest yet takes the publish: the builder records a build's digests only when + // the build ends, while the build module publishes during it with the reserved version (design 47 G §369, A-415, A-490j, + // H-A-387). A digest the build then records that differs from what the holder holds fails the build. + if art.Digest == digest || art.Digest == "" { return true, "", nil } + reserved = append(reserved, art.Digest) } - return false, fmt.Sprintf("%s reserved %s@%s for another digest than %s", a.Module, name, version, digest), nil + if len(reserved) == 0 { + return false, fmt.Sprintf("%s reserved %s@%s with no artifact, so no digest takes %s", a.Module, name, version, digest), nil + } + return false, fmt.Sprintf("%s reserved %s@%s for digest %s, not %s", a.Module, name, version, strings.Join(reserved, ", "), digest), nil } diff --git a/modules/distribution/internal/holder/reserved_test.go b/modules/distribution/internal/holder/reserved_test.go index faa32b074..18852f080 100644 --- a/modules/distribution/internal/holder/reserved_test.go +++ b/modules/distribution/internal/holder/reserved_test.go @@ -19,8 +19,10 @@ func reservedChecksAPublishAgainstTheReservation(t *testing.T) { }{ {name: "the reserved version and digest", answer: `{"module":"mesh-image","version":"0.1.7","artifacts":[{"artifact":"mesh-image:image","digest":"sha256:aa"}]}`, ok: true}, {name: "another artifact of it carries the digest", answer: `{"module":"mesh-image","version":"0.1.7","artifacts":[{"artifact":"mesh-image:aux"},{"artifact":"mesh-image:image","digest":"sha256:aa"}]}`, ok: true}, - {name: "another digest", answer: `{"module":"mesh-image","version":"0.1.7","artifacts":[{"artifact":"mesh-image:image","digest":"sha256:bb"}]}`, why: "another digest than sha256:aa"}, - {name: "another version reserved", answer: `{"module":"mesh-image","version":"0.1.8","artifacts":[]}`, why: "reserved 0.1.8 for registry/mesh-image, not 0.1.7"}, + {name: "no digest recorded yet", answer: `{"module":"mesh-image","version":"0.1.7","artifacts":[{"artifact":"mesh-image:image"}]}`, ok: true}, + {name: "another digest", answer: `{"module":"mesh-image","version":"0.1.7","artifacts":[{"artifact":"mesh-image:image","digest":"sha256:bb"}]}`, why: "reserved registry/mesh-image@0.1.7 for digest sha256:bb, not sha256:aa"}, + {name: "no artifact reserved", answer: `{"module":"mesh-image","version":"0.1.7","artifacts":[]}`, why: "with no artifact, so no digest takes sha256:aa"}, + {name: "another version reserved", answer: `{"module":"mesh-image","version":"0.1.8","artifacts":[]}`, why: "no reservation of version 0.1.7 of registry/mesh-image (it reserved 0.1.8)"}, {name: "the builder refused the name", err: errors.New("no entry carries registry/mesh-image"), why: "no entry carries registry/mesh-image"}, {name: "an answer that is none", answer: `{"error":"x"}`, why: "answered no reservation"}, } { diff --git a/modules/distribution/internal/holder/transfer.go b/modules/distribution/internal/holder/transfer.go index 11574ddf2..ec0b888c2 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)) }