|
| 1 | +//go:build !remote |
| 2 | + |
| 3 | +package compat |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + |
| 9 | + "github.com/containers/image/v5/docker" |
| 10 | + "github.com/containers/image/v5/docker/reference" |
| 11 | + "github.com/containers/image/v5/manifest" |
| 12 | + "github.com/containers/podman/v5/pkg/api/handlers/utils" |
| 13 | + registrytypes "github.com/docker/docker/api/types/registry" |
| 14 | + ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 15 | +) |
| 16 | + |
| 17 | +func InspectDistribution(w http.ResponseWriter, r *http.Request) { |
| 18 | + w.Header().Set("Content-Type", "application/json") |
| 19 | + |
| 20 | + name := utils.GetName(r) |
| 21 | + |
| 22 | + ref, err := reference.ParseAnyReference(name) |
| 23 | + if err != nil { |
| 24 | + utils.Error(w, http.StatusBadRequest, fmt.Errorf("error parsing image reference %q: %w", name, err)) |
| 25 | + return |
| 26 | + } |
| 27 | + |
| 28 | + namedRef, ok := ref.(reference.Named) |
| 29 | + if !ok { |
| 30 | + if _, ok := ref.(reference.Digested); ok { |
| 31 | + // full image ID |
| 32 | + utils.Error(w, http.StatusBadRequest, fmt.Errorf("no manifest found for full image ID")) |
| 33 | + return |
| 34 | + } |
| 35 | + utils.Error(w, http.StatusBadRequest, fmt.Errorf("unknown image reference format: %s", name)) |
| 36 | + return |
| 37 | + } |
| 38 | + |
| 39 | + namedRef = reference.TagNameOnly(namedRef) |
| 40 | + |
| 41 | + imgRef, err := docker.NewReference(namedRef) |
| 42 | + if err != nil { |
| 43 | + utils.Error(w, http.StatusInternalServerError, fmt.Errorf("error creating image reference: %w", err)) |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + imgSrc, err := imgRef.NewImageSource(r.Context(), nil) |
| 48 | + if err != nil { |
| 49 | + utils.Error(w, http.StatusInternalServerError, fmt.Errorf("error getting image source: %w", err)) |
| 50 | + return |
| 51 | + } |
| 52 | + defer imgSrc.Close() |
| 53 | + |
| 54 | + manBlob, manType, err := imgSrc.GetManifest(r.Context(), nil) |
| 55 | + if err != nil { |
| 56 | + utils.Error(w, http.StatusInternalServerError, fmt.Errorf("error getting manifest: %w", err)) |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + digest, err := manifest.Digest(manBlob) |
| 61 | + if err != nil { |
| 62 | + utils.Error(w, http.StatusInternalServerError, fmt.Errorf("error getting manifest digest: %w", err)) |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + // todo: handle other manifest types |
| 67 | + ociIndex, err := manifest.OCI1IndexFromManifest(manBlob) |
| 68 | + if err != nil { |
| 69 | + utils.Error(w, http.StatusInternalServerError, fmt.Errorf("error getting OCI index from manifest: %w", err)) |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + platforms := make([]ocispec.Platform, len(ociIndex.Manifests)) |
| 74 | + for i, m := range ociIndex.Manifests { |
| 75 | + platforms[i] = *m.Platform |
| 76 | + } |
| 77 | + |
| 78 | + distributionInspect := registrytypes.DistributionInspect{ |
| 79 | + Descriptor: ocispec.Descriptor{ |
| 80 | + Digest: digest, |
| 81 | + Size: int64(len(manBlob)), |
| 82 | + MediaType: manType, |
| 83 | + }, |
| 84 | + Platforms: platforms, |
| 85 | + } |
| 86 | + |
| 87 | + utils.WriteResponse(w, http.StatusOK, distributionInspect) |
| 88 | +} |
0 commit comments