|
| 1 | +//go:build !remote |
| 2 | + |
| 3 | +package compat |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | + |
| 11 | + "github.com/containers/image/v5/docker" |
| 12 | + "github.com/containers/image/v5/docker/reference" |
| 13 | + "github.com/containers/image/v5/image" |
| 14 | + "github.com/containers/image/v5/manifest" |
| 15 | + "github.com/containers/image/v5/types" |
| 16 | + "github.com/containers/podman/v5/libpod" |
| 17 | + "github.com/containers/podman/v5/pkg/api/handlers/utils" |
| 18 | + api "github.com/containers/podman/v5/pkg/api/types" |
| 19 | + registrytypes "github.com/docker/docker/api/types/registry" |
| 20 | + ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 21 | +) |
| 22 | + |
| 23 | +func InspectDistribution(w http.ResponseWriter, r *http.Request) { |
| 24 | + w.Header().Set("Content-Type", "application/json") |
| 25 | + |
| 26 | + runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime) |
| 27 | + |
| 28 | + name := utils.GetName(r) |
| 29 | + |
| 30 | + _, imgRef, err := parseImageReference(name) |
| 31 | + if err != nil { |
| 32 | + utils.Error(w, http.StatusUnauthorized, err) |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + imgSrc, err := imgRef.NewImageSource(r.Context(), nil) |
| 37 | + if err != nil { |
| 38 | + var unauthErr docker.ErrUnauthorizedForCredentials |
| 39 | + if errors.As(err, &unauthErr) { |
| 40 | + utils.Error(w, http.StatusUnauthorized, errors.New("401 Unauthorized")) |
| 41 | + } else { |
| 42 | + utils.Error(w, http.StatusUnauthorized, fmt.Errorf("image not found: %w", err)) |
| 43 | + } |
| 44 | + return |
| 45 | + } |
| 46 | + defer imgSrc.Close() |
| 47 | + |
| 48 | + unparsedImage := image.UnparsedInstance(imgSrc, nil) |
| 49 | + manBlob, manType, err := unparsedImage.Manifest(r.Context()) |
| 50 | + if err != nil { |
| 51 | + utils.Error(w, http.StatusInternalServerError, fmt.Errorf("error getting manifest: %w", err)) |
| 52 | + return |
| 53 | + } |
| 54 | + img, err := image.FromUnparsedImage(r.Context(), runtime.SystemContext(), unparsedImage) |
| 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 | + distributionInspect := registrytypes.DistributionInspect{ |
| 67 | + Descriptor: ocispec.Descriptor{ |
| 68 | + Digest: digest, |
| 69 | + Size: int64(len(manBlob)), |
| 70 | + MediaType: manType, |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + platforms, err := getPlatformsFromManifest(r.Context(), img, manBlob, manType) |
| 75 | + if err != nil { |
| 76 | + utils.Error(w, http.StatusInternalServerError, err) |
| 77 | + return |
| 78 | + } |
| 79 | + distributionInspect.Platforms = platforms |
| 80 | + |
| 81 | + utils.WriteResponse(w, http.StatusOK, distributionInspect) |
| 82 | +} |
| 83 | + |
| 84 | +func parseImageReference(name string) (reference.Named, types.ImageReference, error) { |
| 85 | + namedRef, err := reference.ParseNormalizedNamed(name) |
| 86 | + if err != nil { |
| 87 | + return nil, nil, fmt.Errorf("not a valid image reference: %q", name) |
| 88 | + } |
| 89 | + |
| 90 | + namedRef = reference.TagNameOnly(namedRef) |
| 91 | + |
| 92 | + imgRef, err := docker.NewReference(namedRef) |
| 93 | + if err != nil { |
| 94 | + return nil, nil, fmt.Errorf("error creating image reference: %w", err) |
| 95 | + } |
| 96 | + |
| 97 | + return namedRef, imgRef, nil |
| 98 | +} |
| 99 | + |
| 100 | +func getPlatformsFromManifest(ctx context.Context, img types.Image, manBlob []byte, manType string) ([]ocispec.Platform, error) { |
| 101 | + if manType == "" { |
| 102 | + manType = manifest.GuessMIMEType(manBlob) |
| 103 | + } |
| 104 | + |
| 105 | + if manifest.MIMETypeIsMultiImage(manType) { |
| 106 | + manifestList, err := manifest.ListFromBlob(manBlob, manType) |
| 107 | + if err != nil { |
| 108 | + return nil, fmt.Errorf("error parsing manifest list: %w", err) |
| 109 | + } |
| 110 | + |
| 111 | + instanceDigests := manifestList.Instances() |
| 112 | + platforms := make([]ocispec.Platform, 0, len(instanceDigests)) |
| 113 | + for _, digest := range instanceDigests { |
| 114 | + instance, err := manifestList.Instance(digest) |
| 115 | + if err != nil { |
| 116 | + return nil, fmt.Errorf("error getting manifest list instance: %w", err) |
| 117 | + } |
| 118 | + if instance.ReadOnly.Platform == nil { |
| 119 | + continue |
| 120 | + } |
| 121 | + platforms = append(platforms, *instance.ReadOnly.Platform) |
| 122 | + } |
| 123 | + return platforms, nil |
| 124 | + } |
| 125 | + |
| 126 | + switch manType { |
| 127 | + case ocispec.MediaTypeImageManifest, manifest.DockerV2Schema2MediaType: |
| 128 | + config, err := img.OCIConfig(ctx) |
| 129 | + if err != nil { |
| 130 | + return nil, fmt.Errorf("error getting OCI config: %w", err) |
| 131 | + } |
| 132 | + return []ocispec.Platform{config.Platform}, nil |
| 133 | + } |
| 134 | + return []ocispec.Platform{}, nil |
| 135 | +} |
0 commit comments