|
| 1 | +//go:build !remote |
| 2 | + |
| 3 | +package compat |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "errors" |
| 8 | + "net/http" |
| 9 | + |
| 10 | + "github.com/containers/podman/v6/libpod" |
| 11 | + "github.com/containers/podman/v6/pkg/api/handlers/utils" |
| 12 | + api "github.com/containers/podman/v6/pkg/api/types" |
| 13 | + "github.com/containers/podman/v6/pkg/auth" |
| 14 | + "github.com/docker/distribution/registry/api/errcode" |
| 15 | + "github.com/hashicorp/go-multierror" |
| 16 | + dockerRegistry "github.com/moby/moby/api/types/registry" |
| 17 | + ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 18 | + "go.podman.io/image/v5/docker" |
| 19 | + "go.podman.io/image/v5/docker/reference" |
| 20 | + "go.podman.io/image/v5/image" |
| 21 | + "go.podman.io/image/v5/manifest" |
| 22 | + "go.podman.io/image/v5/pkg/shortnames" |
| 23 | + "go.podman.io/image/v5/types" |
| 24 | +) |
| 25 | + |
| 26 | +func DistributionInspect(w http.ResponseWriter, r *http.Request) { |
| 27 | + runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime) |
| 28 | + imageName := utils.GetName(r) |
| 29 | + |
| 30 | + named, err := reference.ParseNormalizedNamed(imageName) |
| 31 | + if err != nil { |
| 32 | + utils.Error(w, http.StatusBadRequest, err) |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + authConf, authfile, err := auth.GetCredentials(r) |
| 37 | + if err != nil { |
| 38 | + utils.Error(w, http.StatusBadRequest, err) |
| 39 | + return |
| 40 | + } |
| 41 | + defer auth.RemoveAuthfile(authfile) |
| 42 | + |
| 43 | + sys := runtime.SystemContext() |
| 44 | + sys.AuthFilePath = authfile |
| 45 | + sys.DockerAuthConfig = authConf |
| 46 | + |
| 47 | + resolved, err := shortnames.Resolve(sys, named.String()) |
| 48 | + if err != nil { |
| 49 | + utils.InternalServerError(w, err) |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + var merr *multierror.Error |
| 54 | + |
| 55 | + for _, candidate := range resolved.PullCandidates { |
| 56 | + inspect, err := inspectCandidate(r.Context(), sys, candidate.Value) |
| 57 | + if err != nil { |
| 58 | + merr = multierror.Append(merr, err) |
| 59 | + continue |
| 60 | + } |
| 61 | + |
| 62 | + utils.WriteResponse(w, http.StatusOK, inspect) |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + if merr.ErrorOrNil() == nil { |
| 67 | + utils.InternalServerError(w, errors.New("no candidate succeeded but got no error")) |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + lastErr := merr.Errors[len(merr.Errors)-1] |
| 72 | + var registryErrCode errcode.ErrorCoder |
| 73 | + if errors.As(lastErr, ®istryErrCode) { |
| 74 | + switch registryErrCode.ErrorCode().Descriptor().HTTPStatusCode { |
| 75 | + case http.StatusUnauthorized, http.StatusNotFound: |
| 76 | + utils.Error(w, http.StatusUnauthorized, lastErr) |
| 77 | + default: |
| 78 | + utils.InternalServerError(w, lastErr) |
| 79 | + } |
| 80 | + return |
| 81 | + } |
| 82 | + utils.InternalServerError(w, lastErr) |
| 83 | +} |
| 84 | + |
| 85 | +func inspectCandidate(ctx context.Context, sys *types.SystemContext, named reference.Named) (dockerRegistry.DistributionInspect, error) { |
| 86 | + ref, err := docker.NewReference(named) |
| 87 | + if err != nil { |
| 88 | + return dockerRegistry.DistributionInspect{}, err |
| 89 | + } |
| 90 | + |
| 91 | + src, err := ref.NewImageSource(ctx, sys) |
| 92 | + if err != nil { |
| 93 | + return dockerRegistry.DistributionInspect{}, err |
| 94 | + } |
| 95 | + defer src.Close() |
| 96 | + |
| 97 | + unparsed := image.UnparsedInstance(src, nil) |
| 98 | + manifestBytes, manifestType, err := unparsed.Manifest(ctx) |
| 99 | + if err != nil { |
| 100 | + return dockerRegistry.DistributionInspect{}, err |
| 101 | + } |
| 102 | + |
| 103 | + inspect := dockerRegistry.DistributionInspect{ |
| 104 | + Descriptor: ocispec.Descriptor{ |
| 105 | + MediaType: manifestType, |
| 106 | + Size: int64(len(manifestBytes)), |
| 107 | + }, |
| 108 | + } |
| 109 | + |
| 110 | + if canonical, ok := named.(reference.Canonical); ok { |
| 111 | + inspect.Descriptor.Digest = canonical.Digest() |
| 112 | + } else { |
| 113 | + digest, err := manifest.Digest(manifestBytes) |
| 114 | + if err != nil { |
| 115 | + return dockerRegistry.DistributionInspect{}, err |
| 116 | + } |
| 117 | + inspect.Descriptor.Digest = digest |
| 118 | + } |
| 119 | + |
| 120 | + if manifest.MIMETypeIsMultiImage(manifestType) { |
| 121 | + list, err := manifest.ListFromBlob(manifestBytes, manifestType) |
| 122 | + if err != nil { |
| 123 | + return dockerRegistry.DistributionInspect{}, err |
| 124 | + } |
| 125 | + for _, d := range list.Instances() { |
| 126 | + instance, err := list.Instance(d) |
| 127 | + if err != nil { |
| 128 | + return dockerRegistry.DistributionInspect{}, err |
| 129 | + } |
| 130 | + if instance.ReadOnly.Platform != nil { |
| 131 | + inspect.Platforms = append(inspect.Platforms, *instance.ReadOnly.Platform) |
| 132 | + } |
| 133 | + } |
| 134 | + } else { |
| 135 | + img, err := image.FromUnparsedImage(ctx, sys, unparsed) |
| 136 | + if err != nil { |
| 137 | + return dockerRegistry.DistributionInspect{}, err |
| 138 | + } |
| 139 | + ociConfig, err := img.OCIConfig(ctx) |
| 140 | + if err != nil { |
| 141 | + return dockerRegistry.DistributionInspect{}, err |
| 142 | + } |
| 143 | + if ociConfig.OS != "" || ociConfig.Architecture != "" { |
| 144 | + inspect.Platforms = append(inspect.Platforms, ociConfig.Platform) |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + return inspect, nil |
| 149 | +} |
0 commit comments