This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
history.go
390 lines (342 loc) · 16.5 KB
/
history.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
Copyright © 2022 Johnson Shi <Johnson.Shi@microsoft.com>
*/
package history
import (
"fmt"
"strconv"
"strings"
"github.com/asottile/dockerfile"
)
// GetImageManifestLayerDockerfileCommandsHistory returns the exact Dockerfile commands
// that contributed to the creation of each image manifest layer.
// Layers returned are sorted from bottom layers (base image layers) to top layers.
// attributionAnnotations is only added to the layer history
// if the layer is not a primary base image layer
// (i.e. only added for layers not 'FROM <primary-base-image>')
func (h *ImageHistory) GetImageManifestLayerDockerfileCommandsHistory(attributionAnnotations map[string]string) ([]ImageManifestLayerDockerfileCommandsHistory, error) {
reversed, err := h.getReversedImageManifestLayerDockerfileCommandsHistory(attributionAnnotations)
if err != nil {
return nil, err
}
// Reverse the reversed slice to get the original order.
var ret []ImageManifestLayerDockerfileCommandsHistory
for i := len(reversed) - 1; i >= 0; i-- {
ret = append(ret, reversed[i])
}
return ret, nil
}
// getReversedImageManifestLayerDockerfileCommandsHistory returns the exact Dockerfile commands
// that contributed to the creation of each image manifest layer (layers returned are
// reversed, i.e. sorted from top layers to bottom layers/base image layers).
// attributionAnnotations is only added to the layer history
// if the layer is not a primary base image layer
// (i.e. only added for layers not 'FROM <primary-base-image>')
func (h *ImageHistory) getReversedImageManifestLayerDockerfileCommandsHistory(attributionAnnotations map[string]string) ([]ImageManifestLayerDockerfileCommandsHistory, error) {
imageLayerHistoryIndex := 0
imageManifestLayerIndex := len(h.ImageManifest.Layers) - 1
dockerfileCommandIndex := len(h.DockerfileCommands) - 1
manifestLayerHistory := []ImageManifestLayerDockerfileCommandsHistory{}
for imageLayerHistoryIndex < len(h.ImageLayerHistory) && imageManifestLayerIndex >= 0 && dockerfileCommandIndex >= 0 {
layerCmd := h.DockerfileCommands[dockerfileCommandIndex]
layerCmdInstruction := strings.ToUpper(layerCmd.Cmd)
// CASE 1: Handle the `FROM <image>` case.
//
// When traversing from the bottom of the Dockerfile upwards (as we are doing now),
// the first `FROM <image>` command encountered is the primary base image FROM import.
//
// We do not need to iterate over the remaining Dockerfile commands above the first `FROM <image>` command encountered,
// because the remaining Dockerfile commands correspond to multistage build stages.
// Those multistage build stage Dockerfile commands only produce a layer in the resulting image
// through `COPY --from=<multistage-build-stage>` commands, which we would have already handled below.
if layerCmdInstruction == "FROM" {
if len(layerCmd.Value) == 0 {
return nil, fmt.Errorf(
"invalid Dockerfile `FROM <image>` cmd (cmd: '%s') due to missing base image reference",
layerCmd.Original,
)
}
baseImageRef := layerCmd.Value[0]
for ; imageManifestLayerIndex >= 0; imageManifestLayerIndex-- {
layerHistory := ImageManifestLayerDockerfileCommandsHistory{
LayerDescriptor: h.ImageManifest.Layers[imageManifestLayerIndex],
LayerCreationParameters: LayerCreationParameters{
DockerfileLayerCreationType: FROMPrimaryBaseImageLayer,
DockerfileCommands: h.DockerfileCommands[dockerfileCommandIndex : dockerfileCommandIndex+1],
BaseImageRef: baseImageRef,
},
AttributedEntity: attributionAnnotations,
}
manifestLayerHistory = append(manifestLayerHistory, layerHistory)
}
return manifestLayerHistory, nil
}
// CASE 2: Handle empty layers in image layer history,
// such as empty layers generated by `ENV` or `EXPOSE` commands.
//
// Empty layers DO correspond to 1 Dockerfile command instruction only.
// Empty layers DO NOT correspond to any OCI image manifest layer.
//
// As such, we need to skip over this empty layer by
// (1) incrementing the image layer history index,
// (2) decrementing the dockerfile command index,
// (3) leaving the image manifest layer index UNCHANGED.
//
// Note: Empty layers are created by instructions that don't create a layer,
// Instructions that are *not* {FROM, ADD, COPY, RUN} will create empty layers.
if h.ImageLayerHistory[imageLayerHistoryIndex].Size == 0 {
imageLayerHistoryIndex++
dockerfileCommandIndex--
continue
}
// Non-Empty Layer Cases
//
// Handle non-empty layers in image layer history,
// except for non-empty layers created by `FROM <image>` command (which we would have handled above).
//
// Non-empty layers DO correspond to EITHER
// 1 Dockerfile command instruction
// OR multiple Dockerfile command instructions.
// Non-empty layers DO correspond to 1 OCI image manifest layer.
//
// Note: Non-empty layers are created by instructions that create a layer,
// such as `FROM`, `COPY`, `ADD`, or `RUN`.
// Note: We should NOT enocunter any `FROM` instructions here as we would have handled it above.
// CASE 3: Handle `COPY --from=<multistage-build-stage>` instruction.
//
// A `COPY --from=<multistage-build-stage>` instruction creates a layer
// by copying contents from a previous multistage build stage.
layerCmdFlags := layerCmd.Flags
if copyFromFlag := getSubstringContainsCaseInsensitive(layerCmdFlags, "--from="); layerCmdInstruction == "COPY" && copyFromFlag != "" {
// Get the stage name where the layer contents were copied from.
// E.g. get the <builder> stage name in "--from=<builder>".
stageName := parseStageNameFromCopyFromFlag(copyFromFlag)
if stageName == "" {
return nil, fmt.Errorf("could not find stage name in `%s`", copyFromFlag)
}
// Get the start and end indexes of the Dockerfile commands that correspond to the multistage build stage.
multistageBuildStageStart, multistageBuildStageEnd, err := FindMultistageBuildStage(h.DockerfileCommands, stageName)
if err != nil {
return nil, err
}
// Capture *ALL* the Dockerfile commands that contributed to this `COPY --from=<multistage-build-stage>` instruction.
//
// First, capture the Dockerfile commands that correspond to the *ENTIRE* multistage build stage.
// E.g. Capture ALL of the following instructions:
// FROM ubuntu:22.04 as builder
// RUN echo "hello" > /hello.txt
// RUN echo "world" > /world.txt
// ADD file.txt /file.txt
// Note: The *ENTIRE* multistage build stage MUST be captured to produce an accurate detailed layer history.
//
// Then, capture the Dockerfile command for the `COPY --from=<multistage-build-stage>` instruction.
// E.g. Capture the following instruction:
// COPY --from=builder /hello.txt /hello.txt
layerDockerfileCommands := make([]dockerfile.Command, multistageBuildStageEnd-multistageBuildStageStart+1)
// We create a new slice and copy elements over to prevent overwriting the original slice.
nCopied := copy(layerDockerfileCommands, h.DockerfileCommands[multistageBuildStageStart:multistageBuildStageEnd+1])
if nCopied != multistageBuildStageEnd-multistageBuildStageStart+1 {
return nil, fmt.Errorf("could not copy all Dockerfile commands for multistage build stage")
}
// Don't forget to append the `COPY --from=<multistage-build-stage>` instruction itself.
layerDockerfileCommands = append(layerDockerfileCommands, layerCmd)
// Obtain the base image ref of the `COPY --from=<multistage-build-stage>`` instruction
// by looking at the first Dockerfile command (which should be the FROM command) in the multistage build stage.
if len(layerDockerfileCommands) == 0 || len(layerDockerfileCommands[0].Value) == 0 {
return nil, fmt.Errorf(
"invalid Dockerfile `COPY --from=<multistage-build-stage>` cmd (cmd: '%s') due to missing base image reference in the `FROM` statement of the <multistage-build-stage>",
layerCmd.Original,
)
}
baseImageRef := layerDockerfileCommands[0].Value[0]
layerHistory := ImageManifestLayerDockerfileCommandsHistory{
LayerDescriptor: h.ImageManifest.Layers[imageManifestLayerIndex],
LayerCreationParameters: LayerCreationParameters{
DockerfileLayerCreationType: COPYFromMultistageBuildStageLayer,
DockerfileCommands: layerDockerfileCommands,
BaseImageRef: baseImageRef,
},
AttributedEntity: attributionAnnotations,
}
manifestLayerHistory = append(manifestLayerHistory, layerHistory)
imageLayerHistoryIndex++
dockerfileCommandIndex--
imageManifestLayerIndex--
continue
}
// CASE 4: Remaining cases are non-empty layers created by:
// COPY (except for COPY --from=<multistage-build-stage>),
// ADD
// RUN
var layerCreationType LayerCreationType
switch layerCmdInstruction {
case "COPY":
layerCreationType = COPYCommandLayer
case "ADD":
layerCreationType = ADDCommandLayer
case "RUN":
layerCreationType = RUNCommandLayer
default:
layerCreationType = UNKNOWNDockerfileCommandLayer
}
layerHistory := ImageManifestLayerDockerfileCommandsHistory{
LayerDescriptor: h.ImageManifest.Layers[imageManifestLayerIndex],
LayerCreationParameters: LayerCreationParameters{
DockerfileLayerCreationType: layerCreationType,
DockerfileCommands: h.DockerfileCommands[dockerfileCommandIndex : dockerfileCommandIndex+1],
},
AttributedEntity: attributionAnnotations,
}
manifestLayerHistory = append(manifestLayerHistory, layerHistory)
imageLayerHistoryIndex++
dockerfileCommandIndex--
imageManifestLayerIndex--
}
return manifestLayerHistory, nil
}
// FindMultistageBuildStage finds the start and end indexes of the
// Dockerfile commands that correspond to the multistage build stage.
// The stage is the `<multistage-build-stage>` value within
// the `COPY --from=<multistage-build-stage>` flag.
// The stage can be a stage name string or a stage number.
//
// Note: The specified build stage name can be a stage number as well, such as `--from=0`.
// See example at https://docs.docker.com/develop/develop-images/multistage-build/#:~:text=How%20does%20it,the%20final%20image.
func FindMultistageBuildStage(dockerfileCommands []dockerfile.Command, stage string) (int, int, error) {
// Attempt to parse the stage as a stage number.
stageNumber, err := strconv.Atoi(stage)
if err != nil {
// If the stage is not a stage number, then it must be a stage name string.
return findMultistageBuildStageFromStageNameString(dockerfileCommands, stage)
}
return findMultistageBuildStageFromStageNumber(dockerfileCommands, stageNumber)
}
// findMultistageBuildStageFromStageNameString finds the start and end indexes
// of the Dockerfile commands that correspond to the multistage build stage.
// The stage is the `<multistage-build-stage>` value within
// the `COPY --from=<multistage-build-stage>` flag.
// The stage is a stage name string (e.g. `builder` in `COPY --from=builder`).
func findMultistageBuildStageFromStageNameString(dockerfileCommands []dockerfile.Command, stageName string) (int, int, error) {
for i, dockerfileCommand := range dockerfileCommands {
if isMatchingNamedMultistageBuildStageFromCommand(dockerfileCommand, stageName) {
buildStageStartIndex := i
buildStageEndIndex, err := findEndOfCurrentBuildStage(dockerfileCommands, buildStageStartIndex)
if err != nil {
return 0, 0, err
}
return buildStageStartIndex, buildStageEndIndex, nil
}
}
return 0, 0, fmt.Errorf("could not find multistage build stage name: %s", stageName)
}
// isMatchingNamedMultistageBuildStageFromCommand checks if:
// 1. The Dockerfile command is a `FROM` command.
// 2. The `FROM` command is a *named* multistage build stage.
// 3. The named multistage build stage matches the specified stage name.
//
// E.g. (`FROM ubuntu AS fooBuilder`, "fooBuilder")
// returns TRUE as it is a MATCHING NAMED multistage build stage.
//
// E.g. (`FROM ubuntu AS fooBuilder`, "barBuilder")
// returns FALSE as it is NOT A MATCHING NAMED multistage build stage.
//
// E.g. (`FROM ubuntu`, "builder")
// returns FALSE as it is NOT A NAMED (UNNAMED) multistage build stage.
func isMatchingNamedMultistageBuildStageFromCommand(dockerfileCommand dockerfile.Command, stageName string) bool {
cmd := dockerfileCommand.Cmd // e.g. "FROM"
cmdValueContent := dockerfileCommand.Value // e.g. []string{"ubuntu", "AS", "builderStageName"}
cmdValueContentLen := len(cmdValueContent)
return strings.ToUpper(cmd) == "FROM" &&
cmdValueContentLen >= 3 &&
strings.EqualFold(cmdValueContent[cmdValueContentLen-1], stageName) &&
strings.ToUpper(cmdValueContent[cmdValueContentLen-2]) == "AS"
}
// findMultistageBuildStageFromStageNumber finds the start and end indexes
// of the Dockerfile commands that correspond to the multistage build stage.
// The stage is the `<multistage-build-stage>` value within
// the `COPY --from=<multistage-build-stage>` flag.
// The stage is a stage number (e.g. `0` in `COPY --from=0`).
// Note: Stage numbers start at 0.
func findMultistageBuildStageFromStageNumber(dockerfileCommands []dockerfile.Command, stageNumber int) (int, int, error) {
buildStagesSeen := 0
for i, dockerfileCommand := range dockerfileCommands {
if strings.ToUpper(dockerfileCommand.Cmd) == "FROM" {
// We will have found the multistage build stage when we:
// (1) see a `FROM` instruction and
// (2) have previously seen the correct number of build stages.
// Note: Stage numbers start at 0.
if buildStagesSeen == stageNumber {
buildStageStartIndex := i
buildStageEndIndex, err := findEndOfCurrentBuildStage(dockerfileCommands, buildStageStartIndex)
if err != nil {
return 0, 0, err
}
return buildStageStartIndex, buildStageEndIndex, nil
}
buildStagesSeen++
}
}
return 0, 0, fmt.Errorf("could not find multistage build stage number: %d", stageNumber)
}
// findEndOfCurrentBuildStage finds the end of the current build stage.
// The end of the current build stage is the line right before the
// next `FROM` instruction encountered.
func findEndOfCurrentBuildStage(dockerfileCommands []dockerfile.Command, buildStageStartIndex int) (int, error) {
nextFromCommandIndex, err := findNextFromCommandIndex(dockerfileCommands, buildStageStartIndex+1)
if err != nil {
return buildStageStartIndex, err
}
buildStageEndIndex := nextFromCommandIndex - 1
return buildStageEndIndex, nil
}
// findNextFromCommandIndex finds the index of the next encountered `FROM` command,
// starting from the specified start index.
func findNextFromCommandIndex(dockerfileCommands []dockerfile.Command, startIndex int) (int, error) {
for i := startIndex; i < len(dockerfileCommands); i++ {
if strings.ToUpper(dockerfileCommands[i].Cmd) == "FROM" {
return i, nil
}
}
return 0, fmt.Errorf("could not find next FROM command")
}
// Returns the stage name in a from flag (returns "builder" in "--from=builder"),
// otherwise returns an empty string.
func parseStageNameFromCopyFromFlag(copyFromFlag string) string {
_, after, found := strings.Cut(copyFromFlag, "--from=")
if !found {
return ""
}
return after
}
// getSubstringContainsCaseInsensitive returns the first string in
// the given string slice that contains the given substring, otherwise an empty string.
func getSubstringContainsCaseInsensitive(haystack []string, substr string) string {
for _, s := range haystack {
if strings.Contains(strings.ToLower(s), strings.ToLower(substr)) {
return s
}
}
return ""
}
// GetSimplifiedImageManifestLayerDockerfileCommandsHistory returns the exact Dockerfile commands
// that contributed to the creation of each image manifest layer (simplified format).
// attributionAnnotations is only added to the layer history
// if the layer is not a primary base image layer
// (i.e. only added for layers not 'FROM <primary-base-image>')
func GetSimplifiedImageManifestLayerDockerfileCommandsHistory(manifestLayerHistory []ImageManifestLayerDockerfileCommandsHistory) []SimplifiedImageManifestLayerDockerfileCommandsHistory {
var simplified []SimplifiedImageManifestLayerDockerfileCommandsHistory
for _, h := range manifestLayerHistory {
var concatenatedCmds []string
for _, c := range h.LayerCreationParameters.DockerfileCommands {
concatenatedCmds = append(concatenatedCmds, c.Original)
}
simplified = append(simplified, SimplifiedImageManifestLayerDockerfileCommandsHistory{
LayerDigest: h.LayerDescriptor.Digest,
DockerfileLayerCreationType: h.LayerCreationParameters.DockerfileLayerCreationType,
DockerfileCommands: concatenatedCmds,
BaseImageRef: h.LayerCreationParameters.BaseImageRef,
AttributedEntity: h.AttributedEntity,
})
}
return simplified
}