Debian Codegraph @

util/containerdutil/manifest.go

raw
1 /*
2 Copyright The containerd Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package containerdutil
18
19 import (
20 "context"
21 "encoding/json"
22 "fmt"
23 "io"
24 "sort"
25 "time"
26
27 "github.com/containerd/containerd/content"
28 "github.com/containerd/containerd/errdefs"
29 "github.com/containerd/containerd/images"
30 "github.com/containerd/containerd/platforms"
31 "github.com/containerd/containerd/remotes"
32 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
33 )
34
35 func ManifestDesc(ctx context.Context, provider content.Provider, image ocispec.Descriptor, platform platforms.MatchComparer) (ocispec.Descriptor, error) {
36 var (
37 limit = 1
38 m []ocispec.Descriptor
39 wasIndex bool
40 )
41 if err := images.Walk(ctx, images.HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
42 switch desc.MediaType {
43 case images.MediaTypeDockerSchema2Manifest, ocispec.MediaTypeImageManifest:
44 p, err := content.ReadBlob(ctx, provider, desc)
45 if err != nil {
46 return nil, err
47 }
48 if err := ValidateMediaType(p, desc.MediaType); err != nil {
49 return nil, err
50 }
51 var manifest ocispec.Manifest
52 if err := json.Unmarshal(p, &manifest); err != nil {
53 return nil, err
54 }
55 if desc.Digest != image.Digest {
56 if desc.Platform != nil && !platform.Match(*desc.Platform) {
57 return nil, nil
58 }
59 if desc.Platform == nil {
60 p, err := content.ReadBlob(ctx, provider, manifest.Config)
61 if err != nil {
62 return nil, err
63 }
64 var image ocispec.Image
65 if err := json.Unmarshal(p, &image); err != nil {
66 return nil, err
67 }
68 if !platform.Match(platforms.Normalize(ocispec.Platform{OS: image.OS, Architecture: image.Architecture})) {
69 return nil, nil
70 }
71 }
72 }
73 m = append(m, desc)
74 return nil, nil
75 case images.MediaTypeDockerSchema2ManifestList, ocispec.MediaTypeImageIndex:
76 p, err := content.ReadBlob(ctx, provider, desc)
77 if err != nil {
78 return nil, err
79 }
80 if err := ValidateMediaType(p, desc.MediaType); err != nil {
81 return nil, err
82 }
83 var idx ocispec.Index
84 if err := json.Unmarshal(p, &idx); err != nil {
85 return nil, err
86 }
87 var descs []ocispec.Descriptor
88 for _, d := range idx.Manifests {
89 if d.Platform == nil || platform.Match(*d.Platform) {
90 descs = append(descs, d)
91 }
92 }
93 sort.SliceStable(descs, func(i, j int) bool {
94 if descs[i].Platform == nil {
95 return false
96 }
97 if descs[j].Platform == nil {
98 return true
99 }
100 return platform.Less(*descs[i].Platform, *descs[j].Platform)
101 })
102 wasIndex = true
103 if len(descs) > limit {
104 return descs[:limit], nil
105 }
106 return descs, nil
107 }
108 return nil, fmt.Errorf("unexpected media type %v for %v: %w", desc.MediaType, desc.Digest, errdefs.ErrNotFound)
109 }), image); err != nil {
110 return ocispec.Descriptor{}, err
111 }
112 if len(m) == 0 {
113 err := fmt.Errorf("manifest %v: %w", image.Digest, errdefs.ErrNotFound)
114 if wasIndex {
115 err = fmt.Errorf("no match for platform in manifest %v: %w", image.Digest, errdefs.ErrNotFound)
116 }
117 return ocispec.Descriptor{}, err
118 }
119 return m[0], nil
120 }
121
122 // Forked from github.com/containerd/containerd/images/image.go
123 // commit: a776a27af54a803657d002e7574a4425b3949f56
124
125 // unknownDocument represents a manifest, manifest list, or index that has not
126 // yet been validated.
127 type unknownDocument struct {
128 MediaType string `json:"mediaType,omitempty"`
129 Config json.RawMessage `json:"config,omitempty"`
130 Layers json.RawMessage `json:"layers,omitempty"`
131 Manifests json.RawMessage `json:"manifests,omitempty"`
132 FSLayers json.RawMessage `json:"fsLayers,omitempty"` // schema 1
133 }
134
135 // ValidateMediaType returns an error if the byte slice is invalid JSON or if
136 // the media type identifies the blob as one format but it contains elements of
137 // another format.
138 func ValidateMediaType(b []byte, mt string) error {
139 var doc unknownDocument
140 if err := json.Unmarshal(b, &doc); err != nil {
141 return err
142 }
143 if len(doc.FSLayers) != 0 {
144 return fmt.Errorf("media-type: schema 1 not supported")
145 }
146 switch mt {
147 case images.MediaTypeDockerSchema2Manifest, ocispec.MediaTypeImageManifest:
148 if len(doc.Manifests) != 0 ||
149 doc.MediaType == images.MediaTypeDockerSchema2ManifestList ||
150 doc.MediaType == ocispec.MediaTypeImageIndex {
151 return fmt.Errorf("media-type: expected manifest but found index (%s)", mt)
152 }
153 case images.MediaTypeDockerSchema2ManifestList, ocispec.MediaTypeImageIndex:
154 if len(doc.Config) != 0 || len(doc.Layers) != 0 ||
155 doc.MediaType == images.MediaTypeDockerSchema2Manifest ||
156 doc.MediaType == ocispec.MediaTypeImageManifest {
157 return fmt.Errorf("media-type: expected index but found manifest (%s)", mt)
158 }
159 }
160 return nil
161 }
162
163 // Fetch manifest of the specified platform
164 func FetchManifestPlatform(ctx context.Context, fetcher remotes.Fetcher, desc ocispec.Descriptor, platform ocispec.Platform) (ocispec.Manifest, error) {
165 ctx, cancel := context.WithTimeout(ctx, time.Minute)
166 defer cancel()
167
168 r, err := fetcher.Fetch(ctx, desc)
169 if err != nil {
170 return ocispec.Manifest{}, err
171 }
172 defer r.Close()
173
174 var manifest ocispec.Manifest
175 switch desc.MediaType {
176 case images.MediaTypeDockerSchema2Manifest, ocispec.MediaTypeImageManifest:
177 p, err := io.ReadAll(r)
178 if err != nil {
179 return ocispec.Manifest{}, err
180 }
181 if err := ValidateMediaType(p, desc.MediaType); err != nil {
182 return ocispec.Manifest{}, err
183 }
184 if err := json.Unmarshal(p, &manifest); err != nil {
185 return ocispec.Manifest{}, err
186 }
187 return manifest, nil
188 case images.MediaTypeDockerSchema2ManifestList, ocispec.MediaTypeImageIndex:
189 var index ocispec.Index
190 p, err := io.ReadAll(r)
191 if err != nil {
192 return ocispec.Manifest{}, err
193 }
194 if err := ValidateMediaType(p, desc.MediaType); err != nil {
195 return ocispec.Manifest{}, err
196 }
197 if err = json.Unmarshal(p, &index); err != nil {
198 return ocispec.Manifest{}, err
199 }
200 var target ocispec.Descriptor
201 found := false
202 for _, m := range index.Manifests {
203 p := platforms.DefaultSpec()
204 if m.Platform != nil {
205 p = *m.Platform
206 }
207 if !platforms.NewMatcher(platform).Match(p) {
208 continue
209 }
210 target = m
211 found = true
212 break
213 }
214 if !found {
215 return ocispec.Manifest{}, fmt.Errorf("no manifest found for platform")
216 }
217 return FetchManifestPlatform(ctx, fetcher, target, platform)
218 }
219 return ocispec.Manifest{}, fmt.Errorf("unknown mediatype %q", desc.MediaType)
220 }