-
Notifications
You must be signed in to change notification settings - Fork 1
/
package.go
192 lines (156 loc) · 5.95 KB
/
package.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
package app
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"strings"
kimage "sigs.k8s.io/kustomize/api/image"
"github.com/docker/labs-brown-tape/attest"
"github.com/docker/labs-brown-tape/attest/manifest"
"github.com/docker/labs-brown-tape/manifest/imagecopier"
"github.com/docker/labs-brown-tape/manifest/imageresolver"
"github.com/docker/labs-brown-tape/manifest/imagescanner"
"github.com/docker/labs-brown-tape/manifest/loader"
"github.com/docker/labs-brown-tape/manifest/packager"
"github.com/docker/labs-brown-tape/manifest/updater"
"github.com/docker/labs-brown-tape/oci"
)
type TapePackageCommand struct {
tape *TapeCommand
InputManifestDirOptions
// WithImages map[string]string `short:"I" long:"with-images" required:"false" description:"Names of new images to use instead of what specified in the manifests"`
OutputImage string `short:"O" long:"output-image" required:"true" description:"Name of the image to push"`
// TODO: implement
// Push bool `short:"P" long:"push" description:"Push the resulting image to the registry"`
}
func (c *TapePackageCommand) ValidateFlags() error {
name, tag, digest := kimage.Split(c.OutputImage)
invalidOutputImageErr := func(reason string, values ...interface{}) error {
return fmt.Errorf("invalid output image name %q: "+reason, values...)
}
if tag != "" {
return invalidOutputImageErr("tag shouldn't be specified", c.OutputImage)
}
if digest != "" {
return invalidOutputImageErr("digest shouldn't be specified", c.OutputImage)
}
if name == "" {
return invalidOutputImageErr("name must not be empty", name)
}
if strings.ToLower(name) != name {
return invalidOutputImageErr("must not contain upper case characters", name)
}
return nil
}
func (c *TapePackageCommand) Execute(args []string) error {
ctx := context.WithValue(c.tape.ctx, "command", "package")
if len(args) != 0 {
return fmt.Errorf("unexpected arguments: %v", args)
}
if err := c.tape.Init(); err != nil {
return err
}
if err := c.ValidateFlags(); err != nil {
return err
}
loader := loader.NewRecursiveManifestDirectoryLoader(c.ManifestDir)
if err := loader.Load(); err != nil {
return fmt.Errorf("failed to load manifests: %w", err)
}
c.tape.log.Debugf("loaded manifests: %v", loader.Paths())
repoDetected, attreg, err := attest.DetectVCS(c.ManifestDir)
if err != nil {
return err
}
/// baseDir := c.ManifestDir
if vcsSummary := attreg.BaseDirSummary(); repoDetected && vcsSummary != nil {
// baseDir = vcsSummary.Common().Path
summaryJSON, err := json.Marshal(vcsSummary.Full())
if err != nil {
return err
}
c.tape.log.Infof("VCS info for %q: %s", c.ManifestDir, summaryJSON)
} else {
c.tape.log.Warnf("path %q is not in VCS", c.ManifestDir)
}
scanner := imagescanner.NewDefaultImageScanner()
scanner.WithProvinanceAttestor(attreg)
if err := scanner.Scan(loader.RelPaths()); err != nil {
return fmt.Errorf("failed to scan images: %w", err)
}
images := scanner.GetImages()
c.tape.log.Debugf("found images: %#v", images.Items())
if err := attreg.AssociateCoreStatements(); err != nil {
return err
}
if err := attreg.AssociateStatements(manifest.MakeOriginalImageRefStatements(images)...); err != nil {
return err
}
client := oci.NewClient(nil)
// TODO: use client.LoginWithCredentials() and/or other options
// TODO: integrate with docker-credential-helpers
resolver := imageresolver.NewRegistryResolver(client)
copier := imagecopier.NewRegistryCopier(client, c.OutputImage)
c.tape.log.Info("resolving image digests")
if err := resolver.ResolveDigests(ctx, images); err != nil {
return fmt.Errorf("failed to resolve digests: %w", err)
}
if err := images.Dedup(); err != nil {
return fmt.Errorf("failed to dedup images: %w", err)
}
if err := attreg.AssociateStatements(manifest.MakeResovedImageRefStatements(images)...); err != nil {
return err
}
c.tape.log.Info("resolving related images")
related, err := resolver.FindRelatedTags(ctx, images)
if err != nil {
return fmt.Errorf("failed to find related tags: %w", err)
}
_, relatedToManifests, err := resolver.FindRelatedFromIndecies(ctx, images, nil)
if err != nil {
return fmt.Errorf("failed to find images related to manifests: %w", err)
}
c.tape.log.Info("copying images")
imageRefs, err := copier.CopyImages(ctx, images, related, relatedToManifests)
if err != nil {
return fmt.Errorf("failed to copy images: %w", err)
}
c.tape.log.Infof("copied images: %s", strings.Join(imageRefs, ", "))
c.tape.log.Info("updating manifest files")
updater := updater.NewFileUpdater()
if err := updater.Update(images); err != nil {
return fmt.Errorf("failed to update manifest files: %w", err)
}
attreg.RegisterMutated(updater.Mutations())
scanner.Reset()
if err := scanner.Scan(loader.RelPaths()); err != nil {
return fmt.Errorf("failed to scan updated manifest files: %w", err)
}
replacedImages := scanner.GetImages()
replacedImages.Dedup()
if err := attreg.AssociateStatements(manifest.MakeReplacedImageRefStatements(replacedImages)...); err != nil {
return err
}
c.tape.log.DebugFn(func() []interface{} {
buf := bytes.NewBuffer(make([]byte, 0, 1024))
base64 := base64.NewEncoder(base64.StdEncoding, buf)
if err := attreg.EncodeAllAttestations(base64); err != nil {
return []interface{}{"failed to encode attestations", err}
}
if err := base64.Close(); err != nil {
return []interface{}{"failed to close base64 encoder while encoding attestations", err}
}
return []interface{}{"attestations: ", buf.String()}
})
path, sourceEpochTimestamp := loader.MostRecentlyModified()
c.tape.log.Debugf("using source epoch timestamp %s from most recently modified manifest file %q", sourceEpochTimestamp, path)
packager := packager.NewDefaultPackager(client, c.OutputImage, &sourceEpochTimestamp, attreg.GetStatements()...)
packageRef, err := packager.Push(ctx, images.Dir())
if err != nil {
return fmt.Errorf("failed to create package: %w", err)
}
c.tape.log.Infof("created package %q", packageRef)
return nil
}