// Package registry reads the mesh's artifact store over its own door: whether it holds what a reference names, and // whether a manifest is an index. It writes nothing: what the artifact sweep holds and removes it asks the registry // seats the store's holder serves (CAT-AS1, novox/hq ADR 0617 ยง2). package registry import ( "context" "encoding/json" "errors" "fmt" "io" "net/http" "strings" "git.novox.be/novox/mesh-sdk/go/catalogue" ) // Store is the artifact store at an address, as this machine reaches it. type Store struct { // Address is `host:port` โ€” the store as the caller reaches it now, composed and never // recorded (novox/hq 04-ISSUES/102). Address string // HTTP is the client used; nil is a client with a modest timeout. HTTP *http.Client } // Gone is the answer when the store does not hold it: the outcome wanted, already true. var Gone = errors.New("the store does not hold it") // ErrNotOurs is a reference this sweep will not address: not the mesh's own, or naming nothing // the store holds by digest. // // **A fact about the record, not about the store** (novox/hq issue 226). The two deserve opposite // responses โ€” skip one and go on, abandon the sweep for the other โ€” and collapsing them into "an // error" is how a cautious loop became one that did nothing while reporting the right number. var ErrNotOurs = errors.New("not a reference into the mesh's artifact store") // anyManifest is every manifest the store may hold, indexes included, so it answers with the // document as it is. var anyManifest = []string{ "application/vnd.oci.image.index.v1+json", "application/vnd.docker.distribution.manifest.list.v2+json", "application/vnd.oci.image.manifest.v1+json", "application/vnd.docker.distribution.manifest.v2+json", } // Platforms is the manifests an index names, by digest; none for a manifest that is not an index. // Gone when the store does not hold it. func (s Store) Platforms(ctx context.Context, repository, digest string) ([]string, error) { request, err := http.NewRequestWithContext(ctx, http.MethodGet, s.url(repository, "manifests", digest), nil) if err != nil { return nil, err } request.Header.Set("Accept", strings.Join(anyManifest, ", ")) response, err := s.client().Do(request) if err != nil { return nil, err } defer response.Body.Close() switch response.StatusCode { case http.StatusOK: case http.StatusNotFound: return nil, Gone default: return nil, fmt.Errorf("the artifact store answered %s when asked for %s@%s", response.Status, repository, digest) } var document struct { Manifests []struct { Digest string `json:"digest"` } `json:"manifests"` } if err := json.NewDecoder(io.LimitReader(response.Body, 4<<20)).Decode(&document); err != nil { return nil, fmt.Errorf("%s@%s is not a manifest: %w", repository, digest, err) } var out []string for _, m := range document.Manifests { if strings.HasPrefix(m.Digest, "sha256:") { out = append(out, m.Digest) } } return out, nil } // split reads a recorded path into the repository, which endpoint names the thing, and the digest. // // Two shapes, which are the two the mesh records: `@sha256:` is a manifest, and // `/blobs/sha256:` is a blob. func split(path string) (repository, kind, digest string, err error) { if before, after, ok := strings.Cut(path, "@sha256:"); ok { return before, "manifests", "sha256:" + after, nil } if before, after, ok := strings.Cut(path, "/blobs/sha256:"); ok { return before, "blobs", "sha256:" + after, nil } return "", "", "", fmt.Errorf("%w: %q names nothing the store holds by digest", ErrNotOurs, path) } // IsIndex is whether a recorded reference is an index the store holds: one that names manifests. A // blob, or a manifest that names none, is not. Gone when the store does not hold it. func (s Store) IsIndex(ctx context.Context, reference string) (bool, error) { path, ours := catalogue.InArtifactStore(reference) if !ours { return false, fmt.Errorf("%w: %s", ErrNotOurs, reference) } repository, kind, digest, err := split(path) if err != nil || kind != "manifests" { return false, err } children, err := s.Platforms(ctx, repository, digest) return len(children) > 0, err } // Holds is whether the store holds what a recorded reference names: the blob of an archive, the manifest of an image. // Gone when it does not; asks and changes nothing (ADR 0526 clause 3: a registration by hand is refused at bytes that // are not there). func (s Store) Holds(ctx context.Context, reference string) error { path, ours := catalogue.InArtifactStore(reference) if !ours { return fmt.Errorf("%w: %s", ErrNotOurs, reference) } repository, kind, digest, err := split(path) if err != nil { return err } if s.Address == "" { return fmt.Errorf("this mesh has no artifact store on its network to ask about %s", reference) } if kind == "blobs" { _, err := s.blobSize(ctx, repository, digest) return err } held, err := s.has(ctx, s.url(repository, kind, digest), anyManifest...) if err != nil { return err } if !held { return Gone } return nil }