diff --git a/modules/distribution/cmd/distribution-tools/contract.go b/modules/distribution/cmd/distribution-tools/contract.go index 4fe26822a..b8937f4d0 100644 --- a/modules/distribution/cmd/distribution-tools/contract.go +++ b/modules/distribution/cmd/distribution-tools/contract.go @@ -40,11 +40,11 @@ func partAnswer(in reg.PublishPartInput, a holder.PublishAnswer) reg.PublishPart return reg.PublishPartAnswer{Transfer: in.Transfer, Part: in.Part, Received: in.Sequence + 1, Complete: a.Ended} } -// versionsAnswer is `versions`: each version with the digest the store holds under it. +// versionsAnswer is `versions`: each version with the digest its reference names (holder.Verbs.VersionDigest). func versionsAnswer(v *holder.Verbs, kind string, a holder.VersionsAnswer) reg.VersionsAnswer { out := reg.VersionsAnswer{Name: a.Name, Versions: []reg.Artifact{}} for _, ver := range a.Versions { - d, _ := v.Catalog.Digest(a.Name, ver) + d, _ := v.VersionDigest(a.Seat, a.Name, ver) out.Versions = append(out.Versions, reg.Artifact{Name: a.Name, Version: ver, Digest: d, Kind: kind}) } return out diff --git a/modules/distribution/internal/holder/holder_test.go b/modules/distribution/internal/holder/holder_test.go index bec32edc6..b4eef37bc 100644 --- a/modules/distribution/internal/holder/holder_test.go +++ b/modules/distribution/internal/holder/holder_test.go @@ -109,6 +109,9 @@ func (c *fakeCatalog) Versions(name string) ([]string, error) { func (c *fakeCatalog) Digest(name, version string) (string, error) { return c.tags[name+":"+version], nil } +func (c *fakeCatalog) Blob(name, version string) (string, error) { + return c.tags[name+":"+version], nil +} // Remove deletes by the digest, as the store does: every tag naming the digest goes with it. func (c *fakeCatalog) Remove(name, version string) error { @@ -709,6 +712,18 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) { if err := json.Unmarshal(store.manifests["m/tools:0.1.4"], &man); err != nil || len(man.Layers) != 1 || man.Layers[0].Size != int64(len(blob)) || man.Layers[0].Digest != digestOf(blob) { t.Errorf("the version's manifest %s, want one layer of %d bytes", store.manifests["m/tools:0.1.4"], len(blob)) } + // `versions` answers the digest the version's reference names and its published carries (design 47 G A-430a): on the + // artifact seat the blob, which the node-engine fetches as /blobs/, never the manifest's own digest. + v := &Verbs{Holder: b.h, Catalog: &OCI{Base: base}} + for _, c := range []struct{ seat, version, want string }{ + {ArtifactSeat, "0.1.4", digestOf(blob)}, + {ImageSeat, "0.1.4", digestOf(store.manifests["m/tools:0.1.4"])}, + {ArtifactSeat, "0.1.9", ""}, + } { + if got, err := v.VersionDigest(c.seat, "m/tools", c.version); err != nil || got != c.want { + t.Errorf("VersionDigest(%s, m/tools, %s) = %q, %v, want %q", c.seat, c.version, got, err, c.want) + } + } gone := "sha256:" + strings.Repeat("e", 64) _, err = b.h.Publish(ArtifactSeat, "x.mesh-delivery-builder", PublishRequest{Name: "m/tools", Version: "0.1.5", Kind: KindBundle, Digest: gone, Transfer: "republish-m-0-1-5-tools"}) if err == nil || !strings.Contains(err.Error(), "not stored: ") || !strings.Contains(err.Error(), gone) || store.manifests["m/tools:0.1.5"] != nil || diff --git a/modules/distribution/internal/holder/oci.go b/modules/distribution/internal/holder/oci.go index 65abad32f..7959bb7ab 100644 --- a/modules/distribution/internal/holder/oci.go +++ b/modules/distribution/internal/holder/oci.go @@ -273,6 +273,29 @@ func (o *OCI) Digest(name, version string) (string, error) { return resp.Header.Get("Docker-Content-Digest"), nil } +// Blob is the one blob the manifest name holds at version keeps (Manifest.Holder's shape): a bundle's or an archive's, +// what its reference names. Empty when none is held at version, or the manifest keeps no single blob. +func (o *OCI) Blob(name, version string) (string, error) { + resp, b, err := o.do(http.MethodGet, "/v2/"+name+"/manifests/"+version, "", nil, mediaManifest) + if err != nil { + return "", err + } + if resp.StatusCode == http.StatusNotFound { + return "", nil + } + if resp.StatusCode != http.StatusOK { + return "", fmt.Errorf("the store answered %d for %s:%s", resp.StatusCode, name, version) + } + m, err := ParseManifest(b) + if err != nil { + return "", fmt.Errorf("the manifest of %s:%s cannot be read: %w", name, version, err) + } + if !m.Holder() { + return "", nil + } + return m.Layers[0], nil +} + // ErrNoTagAlone is a store that cannot delete a tag alone. registry 2.8.3, the store's pinned image, is one: its // DeleteManifest (distribution v2.8.3 registry/handlers/manifests.go) ignores a tag reference and deletes the empty // digest, whose check fails, answered 400 DIGEST_INVALID; registry 3 untags alone. diff --git a/modules/distribution/internal/holder/verbs.go b/modules/distribution/internal/holder/verbs.go index 113697d9d..d8ccb239b 100644 --- a/modules/distribution/internal/holder/verbs.go +++ b/modules/distribution/internal/holder/verbs.go @@ -19,6 +19,9 @@ type Catalog interface { Versions(name string) ([]string, error) // Digest is the digest name holds at version, empty when none. Digest(name, version string) (string, error) + // Blob is the one blob the manifest name holds at version keeps, for a bundle or an archive: the digest its reference + // names and its published event carries. Empty when none is held, or the manifest is not of that shape. + Blob(name, version string) (string, error) // Remove deletes name at version by its manifest's digest, every tag naming that digest with it; the store's // collector frees its blobs. Remove(name, version string) error @@ -112,6 +115,16 @@ func (v *Verbs) Versions(seat, caller, name string) (VersionsAnswer, error) { return VersionsAnswer{Seat: seat, Name: name, Versions: got}, nil } +// VersionDigest is the digest `versions` answers for name at version on seat: the one an artifact's reference names and +// its event `published` carries (the contract's Artifact), so a node-engine fetches the bytes by it (design 47 G A-430a): +// on the artifact seat a bundle's or an archive's blob, on the image seat the image's manifest. Empty when none is held. +func (v *Verbs) VersionDigest(seat, name, version string) (string, error) { + if seat == ArtifactSeat { + return v.Catalog.Blob(name, version) + } + return v.Catalog.Digest(name, version) +} + // RemoveRequest is `remove`: a tagged version, or with the reason retention an untagged one by its digest. type RemoveRequest struct { Name string `json:"name"`