-
Notifications
You must be signed in to change notification settings - Fork 17
/
image.go
282 lines (245 loc) · 8.07 KB
/
image.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
//
// Copyright 2024 Stacklok, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package image provides utilities to work with container images.
package image
import (
"context"
"errors"
"fmt"
"slices"
"strings"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"
dockerparser "github.com/moby/buildkit/frontend/dockerfile/parser"
"github.com/stacklok/frizbee/internal/cli"
"github.com/stacklok/frizbee/pkg/interfaces"
"github.com/stacklok/frizbee/pkg/utils/config"
"github.com/stacklok/frizbee/pkg/utils/store"
)
const (
// ContainerImageRegex is regular expression pattern to match container image usage in YAML
// nolint:lll
ContainerImageRegex = `image\s*:\s*["']?([^\s"']+/[^\s"']+|[^\s"']+)(:[^\s"']+)?(@[^\s"']+)?["']?|FROM\s+(--platform=[^\s]+[^\s]*\s+)?([^\s]+(/[^\s]+)?(:[^\s]+)?(@[^\s]+)?)`
prefixFROM = "FROM "
prefixImage = "image: "
// ReferenceType is the type of the reference
ReferenceType = "container"
)
// Parser is a struct to replace container image references with digests
type Parser struct {
regex string
cache store.RefCacher
}
type unresolvedImage struct {
imageRef string
flags []string
}
// New creates a new Parser
func New() *Parser {
return &Parser{
regex: ContainerImageRegex,
cache: store.NewRefCacher(),
}
}
// SetCache sets the cache to store the image references
func (p *Parser) SetCache(cache store.RefCacher) {
p.cache = cache
}
// SetRegex sets the regular expression pattern to match container image usage
func (p *Parser) SetRegex(regex string) {
p.regex = regex
}
// GetRegex returns the regular expression pattern to match container image usage
func (p *Parser) GetRegex() string {
return p.regex
}
// Replace replaces the container image reference with the digest
func (p *Parser) Replace(
ctx context.Context,
matchedLine string,
_ interfaces.REST,
cfg config.Config,
) (*interfaces.EntityRef, error) {
var imageRef string
var extraArgs string
// Trim the prefix
hasFROMPrefix := false
hasImagePrefix := false
// Check if the image reference has the FROM prefix, i.e. Dockerfile
if strings.HasPrefix(matchedLine, prefixFROM) {
parsedFrom, err := getRefFromDockerfileFROM(matchedLine)
if err != nil {
return nil, err
}
// Check if the image reference should be excluded, i.e. scratch
if shouldSkipImageRef(&cfg, parsedFrom.imageRef) {
return nil, fmt.Errorf("image reference %s should be excluded - %w", matchedLine, interfaces.ErrReferenceSkipped)
}
imageRef = parsedFrom.imageRef
extraArgs = strings.Join(parsedFrom.flags, " ")
if extraArgs != "" {
extraArgs += " "
}
hasFROMPrefix = true
} else if strings.HasPrefix(matchedLine, prefixImage) {
// Check if the image reference has the image prefix, i.e. Kubernetes or Docker Compose YAML
imageRef = strings.TrimPrefix(matchedLine, prefixImage)
// Check if the image reference should be excluded, i.e. scratch
if shouldSkipImageRef(&cfg, imageRef) {
return nil, fmt.Errorf("image reference %s should be excluded - %w", matchedLine, interfaces.ErrReferenceSkipped)
}
hasImagePrefix = true
} else {
imageRef = matchedLine
}
// Get the digest of the image reference
imageRefWithDigest, err := GetImageDigestFromRef(ctx, imageRef, cfg.Platform, p.cache)
if err != nil {
return nil, err
}
// Add the prefix back
if hasFROMPrefix {
imageRefWithDigest.Prefix = fmt.Sprintf("%s%s%s", prefixFROM, extraArgs, imageRefWithDigest.Prefix)
} else if hasImagePrefix {
imageRefWithDigest.Prefix = fmt.Sprintf("%s%s", prefixImage, imageRefWithDigest.Prefix)
}
// Return the reference
return imageRefWithDigest, nil
}
// ConvertToEntityRef converts a container image reference to an EntityRef
func (_ *Parser) ConvertToEntityRef(reference string) (*interfaces.EntityRef, error) {
reference = strings.TrimPrefix(reference, prefixImage)
reference = strings.TrimPrefix(reference, prefixFROM)
var sep string
var frags []string
if strings.Contains(reference, "@") {
sep = "@"
} else if strings.Contains(reference, ":") {
sep = ":"
}
if sep != "" {
frags = strings.Split(reference, sep)
if len(frags) != 2 {
return nil, fmt.Errorf("invalid container reference: %s", reference)
}
} else {
frags = []string{reference, "latest"}
}
return &interfaces.EntityRef{
Name: frags[0],
Ref: frags[1],
Type: ReferenceType,
}, nil
}
// GetImageDigestFromRef returns the digest of a container image reference
// from a name.Reference.
func GetImageDigestFromRef(ctx context.Context, imageRef, platform string, cache store.RefCacher) (*interfaces.EntityRef, error) {
// Parse the image reference
ref, err := name.ParseReference(imageRef)
if err != nil {
return nil, err
}
opts := []remote.Option{
remote.WithContext(ctx),
remote.WithUserAgent(cli.UserAgent),
remote.WithAuthFromKeychain(authn.DefaultKeychain),
}
// Set the platform if provided
if platform != "" {
platformSplit := strings.Split(platform, "/")
if len(platformSplit) != 2 {
return nil, errors.New("platform must be in the format os/arch")
}
opts = append(opts, remote.WithPlatform(v1.Platform{
OS: platformSplit[0],
Architecture: platformSplit[1],
}))
}
// Get the digest of the image reference
var digest string
if cache != nil {
if d, ok := cache.Load(imageRef); ok {
digest = d
} else {
desc, err := remote.Get(ref, opts...)
if err != nil {
return nil, err
}
digest = desc.Digest.String()
cache.Store(imageRef, digest)
}
} else {
desc, err := remote.Get(ref, opts...)
if err != nil {
return nil, err
}
digest = desc.Digest.String()
}
// Compare the digest with the reference and return the original reference if they already match
if digest == ref.Identifier() {
return nil, fmt.Errorf("image already referenced by digest: %s %w", imageRef, interfaces.ErrReferenceSkipped)
}
return &interfaces.EntityRef{
Name: ref.Context().Name(),
Ref: digest,
Type: ReferenceType,
Tag: ref.Identifier(),
}, nil
}
func shouldSkipImageRef(cfg *config.Config, ref string) bool {
// Parse the image reference
nameRef, err := name.ParseReference(ref)
if err != nil {
// we wouldn't know how to resolve this reference, so let's skip
return true
}
imageName := getImageNameFromRef(nameRef)
if slices.Contains(cfg.Images.ImageFilter.ExcludeImages, imageName) {
return true
}
tag := nameRef.Identifier()
return slices.Contains(cfg.Images.ImageFilter.ExcludeTags, tag)
}
// TODO(jakub): this is a bit of a hack, but I didn't find a better way to get just the name
func getImageNameFromRef(nameRef name.Reference) string {
fullRepositoryName := nameRef.Context().Name()
parts := strings.Split(fullRepositoryName, "/")
if len(parts) > 1 {
return parts[len(parts)-1]
}
return ""
}
func getRefFromDockerfileFROM(line string) (unresolvedImage, error) {
parseResult, err := dockerparser.Parse(strings.NewReader(line))
if err != nil {
return unresolvedImage{}, fmt.Errorf("failed to parse Dockerfile line: %w", err)
}
if len(parseResult.AST.Children) == 0 ||
parseResult.AST.Children[0] == nil ||
strings.ToUpper(parseResult.AST.Children[0].Value) != "FROM" {
return unresolvedImage{}, errors.New("invalid Dockerfile line: the first parsed node is not FROM")
}
fromNode := parseResult.AST.Children[0]
imgNode := parseResult.AST.Children[0].Next
if imgNode == nil {
return unresolvedImage{}, errors.New("invalid Dockerfile line: no image node found")
}
return unresolvedImage{
imageRef: imgNode.Value,
flags: fromNode.Flags,
}, nil
}