-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
containerd.go
286 lines (242 loc) · 8.1 KB
/
containerd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package daemon
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strings"
"time"
"github.com/containerd/containerd"
"github.com/containerd/containerd/content"
"github.com/containerd/containerd/images/archive"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/platforms"
refdocker "github.com/containerd/containerd/reference/docker"
api "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/go-connections/nat"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/samber/lo"
"golang.org/x/xerrors"
"github.com/aquasecurity/trivy/pkg/fanal/types"
)
const (
defaultContainerdSocket = "/run/containerd/containerd.sock"
defaultContainerdNamespace = "default"
)
type familiarNamed string
func (n familiarNamed) Name() string {
return strings.Split(string(n), ":")[0]
}
func (n familiarNamed) Tag() string {
s := strings.Split(string(n), ":")
if len(s) < 2 {
return ""
}
return s[1]
}
func (n familiarNamed) String() string {
return string(n)
}
func imageWriter(client *containerd.Client, img containerd.Image, platform types.Platform) imageSave {
return func(ctx context.Context, ref []string) (io.ReadCloser, error) {
if len(ref) < 1 {
return nil, xerrors.New("no image reference")
}
imgOpts := archive.WithImage(client.ImageService(), ref[0])
manifestOpts := archive.WithManifest(img.Target())
var platformMatchComparer platforms.MatchComparer
if platform.Platform == nil {
platformMatchComparer = platforms.DefaultStrict()
} else {
platformMatchComparer = img.Platform()
}
platOpts := archive.WithPlatform(platformMatchComparer)
pr, pw := io.Pipe()
go func() {
pw.CloseWithError(archive.Export(ctx, client.ContentStore(), pw, imgOpts, manifestOpts, platOpts))
}()
return pr, nil
}
}
// ContainerdImage implements v1.Image
func ContainerdImage(ctx context.Context, imageName string, opts types.ImageOptions) (Image, func(), error) {
cleanup := func() {}
addr := os.Getenv("CONTAINERD_ADDRESS")
if addr == "" {
// TODO: support rootless
addr = defaultContainerdSocket
}
if _, err := os.Stat(addr); errors.Is(err, os.ErrNotExist) {
return nil, cleanup, xerrors.Errorf("containerd socket not found: %s", addr)
}
ref, searchFilters, err := parseReference(imageName)
if err != nil {
return nil, cleanup, err
}
var options []containerd.ClientOpt
if opts.RegistryOptions.Platform.Platform != nil {
ociPlatform, err := platforms.Parse(opts.RegistryOptions.Platform.String())
if err != nil {
return nil, cleanup, err
}
options = append(options, containerd.WithDefaultPlatform(platforms.OnlyStrict(ociPlatform)))
}
client, err := containerd.New(addr, options...)
if err != nil {
return nil, cleanup, xerrors.Errorf("failed to initialize a containerd client: %w", err)
}
namespace := os.Getenv("CONTAINERD_NAMESPACE")
if namespace == "" {
namespace = defaultContainerdNamespace
}
ctx = namespaces.WithNamespace(ctx, namespace)
imgs, err := client.ListImages(ctx, searchFilters...)
if err != nil {
return nil, cleanup, xerrors.Errorf("failed to list images from containerd client: %w", err)
}
if len(imgs) < 1 {
return nil, cleanup, xerrors.Errorf("image not found in containerd store: %s", imageName)
}
img := imgs[0]
f, err := os.CreateTemp("", "fanal-containerd-*")
if err != nil {
return nil, cleanup, xerrors.Errorf("failed to create a temporary file: %w", err)
}
cleanup = func() {
_ = client.Close()
_ = f.Close()
_ = os.Remove(f.Name())
}
insp, history, ref, err := inspect(ctx, img, ref)
if err != nil {
return nil, cleanup, xerrors.Errorf("inspect error: %w", err)
}
return &image{
opener: imageOpener(ctx, ref.String(), f, imageWriter(client, img, opts.RegistryOptions.Platform)),
inspect: insp,
history: history,
}, cleanup, nil
}
func parseReference(imageName string) (refdocker.Reference, []string, error) {
ref, err := refdocker.ParseAnyReference(imageName)
if err != nil {
return nil, nil, xerrors.Errorf("parse error: %w", err)
}
d, isDigested := ref.(refdocker.Digested)
n, isNamed := ref.(refdocker.Named)
nt, isNamedAndTagged := ref.(refdocker.NamedTagged)
// a name plus a digest
// example: name@sha256:41adb3ef...
if isDigested && isNamed {
dgst := d.Digest()
// for the filters, each slice entry is logically or'd. each
// comma-separated filter is logically anded
return ref, []string{
fmt.Sprintf(`name~="^%s(:|@).*",target.digest==%q`, n.Name(), dgst),
fmt.Sprintf(`name~="^%s(:|@).*",target.digest==%q`, refdocker.FamiliarName(n), dgst),
}, nil
}
// digested, but not named. i.e. a plain digest
// example: sha256:41adb3ef...
if isDigested {
return ref, []string{fmt.Sprintf(`target.digest==%q`, d.Digest())}, nil
}
// a name plus a tag
// example: name:tag
if isNamedAndTagged {
tag := nt.Tag()
return familiarNamed(imageName), []string{
fmt.Sprintf(`name=="%s:%s"`, nt.Name(), tag),
fmt.Sprintf(`name=="%s:%s"`, refdocker.FamiliarName(nt), tag),
}, nil
}
return nil, nil, xerrors.Errorf("failed to parse image reference: %s", imageName)
}
// readImageConfig reads the config spec (`application/vnd.oci.image.config.v1+json`) for img.platform from content store.
// ported from https://github.com/containerd/nerdctl/blob/7dfbaa2122628921febeb097e7a8a86074dc931d/pkg/imgutil/imgutil.go#L377-L393
func readImageConfig(ctx context.Context, img containerd.Image) (ocispec.Image, ocispec.Descriptor, error) {
var config ocispec.Image
configDesc, err := img.Config(ctx) // aware of img.platform
if err != nil {
return config, configDesc, err
}
p, err := content.ReadBlob(ctx, img.ContentStore(), configDesc)
if err != nil {
return config, configDesc, err
}
if err = json.Unmarshal(p, &config); err != nil {
return config, configDesc, err
}
return config, configDesc, nil
}
// ported from https://github.com/containerd/nerdctl/blob/d110fea18018f13c3f798fa6565e482f3ff03591/pkg/inspecttypes/dockercompat/dockercompat.go#L279-L321
func inspect(ctx context.Context, img containerd.Image, ref refdocker.Reference) (api.ImageInspect, []v1.History, refdocker.Reference, error) {
if _, ok := ref.(refdocker.Digested); ok {
ref = familiarNamed(img.Name())
}
var tag string
if tagged, ok := ref.(refdocker.Tagged); ok {
tag = tagged.Tag()
}
var repository string
if n, isNamed := ref.(refdocker.Named); isNamed {
repository = refdocker.FamiliarName(n)
}
imgConfig, imgConfigDesc, err := readImageConfig(ctx, img)
if err != nil {
return api.ImageInspect{}, nil, nil, err
}
var lastHistory ocispec.History
if len(imgConfig.History) > 0 {
lastHistory = imgConfig.History[len(imgConfig.History)-1]
}
var history []v1.History
for _, h := range imgConfig.History {
history = append(history, v1.History{
Author: h.Author,
Created: v1.Time{Time: *h.Created},
CreatedBy: h.CreatedBy,
Comment: h.Comment,
EmptyLayer: h.EmptyLayer,
})
}
portSet := make(nat.PortSet)
for k := range imgConfig.Config.ExposedPorts {
portSet[nat.Port(k)] = struct{}{}
}
created := ""
if lastHistory.Created != nil {
created = lastHistory.Created.Format(time.RFC3339Nano)
}
return api.ImageInspect{
ID: imgConfigDesc.Digest.String(),
RepoTags: []string{fmt.Sprintf("%s:%s", repository, tag)},
RepoDigests: []string{fmt.Sprintf("%s@%s", repository, img.Target().Digest)},
Comment: lastHistory.Comment,
Created: created,
Author: lastHistory.Author,
Config: &container.Config{
User: imgConfig.Config.User,
ExposedPorts: portSet,
Env: imgConfig.Config.Env,
Cmd: imgConfig.Config.Cmd,
Volumes: imgConfig.Config.Volumes,
WorkingDir: imgConfig.Config.WorkingDir,
Entrypoint: imgConfig.Config.Entrypoint,
Labels: imgConfig.Config.Labels,
},
Architecture: imgConfig.Architecture,
Os: imgConfig.OS,
RootFS: api.RootFS{
Type: imgConfig.RootFS.Type,
Layers: lo.Map(imgConfig.RootFS.DiffIDs, func(d digest.Digest, _ int) string {
return d.String()
}),
},
}, history, ref, nil
}