-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathupdater.go
338 lines (292 loc) · 8.58 KB
/
updater.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
package infra
import (
"archive/tar"
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"
"github.com/dependabot/cli/internal/model"
"github.com/docker/cli/cli/streams"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network"
"github.com/goware/prefixer"
"github.com/moby/moby/client"
"github.com/moby/moby/pkg/stdcopy"
)
const jobID = "cli"
const (
root = "root"
dependabot = "dependabot"
)
const (
guestInputDir = "/home/dependabot/dependabot-updater/job.json"
guestOutput = "/home/dependabot/dependabot-updater/output.json"
guestRepoDir = "/home/dependabot/dependabot-updater/repo"
)
type Updater struct {
cli *client.Client
containerID string
}
const (
certsPath = "/etc/ssl/certs"
dbotCert = "/usr/local/share/ca-certificates/dbot-ca.crt"
)
// NewUpdater starts the update container interactively running /bin/sh, so it does not stop.
func NewUpdater(ctx context.Context, cli *client.Client, net *Networks, params *RunParams, prox *Proxy, collector *Collector) (*Updater, error) {
containerCfg := &container.Config{
User: dependabot,
Image: params.UpdaterImage,
Cmd: []string{"/bin/sh"},
Tty: true, // prevent container from stopping
}
if params.CollectorConfigPath != "" {
containerCfg.Env = append(
containerCfg.Env,
[]string{
"OTEL_ENABLED=true",
fmt.Sprintf("OTEL_EXPORTER_OTLP_ENDPOINT=%s", collector.url),
}...)
}
hostCfg := &container.HostConfig{}
var err error
for _, v := range params.Volumes {
var local, remote string
var readOnly bool
local, remote, readOnly, err = mountOptions(v)
if err != nil {
return nil, err
}
hostCfg.Mounts = append(hostCfg.Mounts, mount.Mount{
Type: mount.TypeBind,
Source: local,
Target: remote,
ReadOnly: readOnly,
})
}
netCfg := &network.NetworkingConfig{
EndpointsConfig: map[string]*network.EndpointSettings{
net.noInternetName: {
NetworkID: net.NoInternet.ID,
},
},
}
updaterContainer, err := cli.ContainerCreate(ctx, containerCfg, hostCfg, netCfg, nil, "")
if err != nil {
return nil, fmt.Errorf("failed to create updater container: %w", err)
}
updater := &Updater{
cli: cli,
containerID: updaterContainer.ID,
}
if err = putUpdaterInputs(ctx, cli, prox.ca.Cert, updaterContainer.ID, params.Job); err != nil {
updater.Close()
return nil, err
}
if err = cli.ContainerStart(ctx, updaterContainer.ID, types.ContainerStartOptions{}); err != nil {
updater.Close()
return nil, fmt.Errorf("failed to start updater container: %w", err)
}
return updater, nil
}
func putUpdaterInputs(ctx context.Context, cli *client.Client, cert, id string, job *model.Job) error {
opt := types.CopyToContainerOptions{}
if t, err := tarball(dbotCert, cert); err != nil {
return fmt.Errorf("failed to create cert tarball: %w", err)
} else if err = cli.CopyToContainer(ctx, id, "/", t, opt); err != nil {
return fmt.Errorf("failed to copy cert to container: %w", err)
}
data, err := JobFile{Job: job}.ToJSON()
if err != nil {
return fmt.Errorf("failed to marshal job file: %w", err)
}
if t, err := tarball(guestInputDir, data); err != nil {
return fmt.Errorf("failed create input tarball: %w", err)
} else if err = cli.CopyToContainer(ctx, id, "/", t, opt); err != nil {
return fmt.Errorf("failed to copy input to container: %w", err)
}
return nil
}
var ErrInvalidVolume = fmt.Errorf("invalid volume syntax")
func mountOptions(v string) (local, remote string, readOnly bool, err error) {
parts := strings.Split(v, ":")
if len(parts) < 2 || len(parts) > 3 {
return "", "", false, ErrInvalidVolume
}
local = parts[0]
remote = parts[1]
if len(parts) == 3 {
if parts[2] != "ro" {
return "", "", false, ErrInvalidVolume
}
readOnly = true
}
if !path.IsAbs(local) {
wd, _ := os.Getwd()
local = filepath.Clean(filepath.Join(wd, local))
}
return local, remote, readOnly, nil
}
func userEnv(proxyURL string, apiUrl string) []string {
return []string{
"GITHUB_ACTIONS=true", // sets exit code when fetch fails
fmt.Sprintf("http_proxy=%s", proxyURL),
fmt.Sprintf("HTTP_PROXY=%s", proxyURL),
fmt.Sprintf("https_proxy=%s", proxyURL),
fmt.Sprintf("HTTPS_PROXY=%s", proxyURL),
fmt.Sprintf("DEPENDABOT_JOB_ID=%v", firstNonEmpty(os.Getenv("DEPENDABOT_JOB_ID"), jobID)),
fmt.Sprintf("DEPENDABOT_JOB_TOKEN=%v", ""),
fmt.Sprintf("DEPENDABOT_JOB_PATH=%v", guestInputDir),
fmt.Sprintf("DEPENDABOT_OUTPUT_PATH=%v", guestOutput),
fmt.Sprintf("DEPENDABOT_REPO_CONTENTS_PATH=%v", guestRepoDir),
fmt.Sprintf("DEPENDABOT_API_URL=%s", apiUrl),
fmt.Sprintf("SSL_CERT_FILE=%v/ca-certificates.crt", certsPath),
"UPDATER_ONE_CONTAINER=true",
"UPDATER_DETERMINISTIC=true",
}
}
// RunShell executes an interactive shell, blocks until complete.
func (u *Updater) RunShell(ctx context.Context, proxyURL string, apiUrl string) error {
execCreate, err := u.cli.ContainerExecCreate(ctx, u.containerID, types.ExecConfig{
AttachStdin: true,
AttachStdout: true,
AttachStderr: true,
Tty: true,
User: dependabot,
Env: append(userEnv(proxyURL, apiUrl), "DEBUG=1"),
Cmd: []string{"/bin/bash", "-c", "update-ca-certificates && /bin/bash"},
})
if err != nil {
return fmt.Errorf("failed to create exec: %w", err)
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
execResp, err := u.cli.ContainerExecAttach(ctx, execCreate.ID, types.ExecStartCheck{})
if err != nil {
return fmt.Errorf("failed to start exec: %w", err)
}
ch := make(chan struct{})
out := streams.NewOut(os.Stdout)
_ = out.SetRawTerminal()
in := streams.NewIn(os.Stdin)
_ = in.SetRawTerminal()
defer func() {
out.RestoreTerminal()
in.RestoreTerminal()
in.Close()
}()
go func() {
_, _ = stdcopy.StdCopy(out, os.Stderr, execResp.Reader)
ch <- struct{}{}
}()
go func() {
_, _ = io.Copy(execResp.Conn, in)
ch <- struct{}{}
}()
_ = MonitorTtySize(ctx, out, u.cli, execCreate.ID, true)
select {
case <-ctx.Done():
return ctx.Err()
case <-ch:
cancel()
}
return nil
}
// RunCmd executes the update scripts as the dependabot user, blocks until complete.
func (u *Updater) RunCmd(ctx context.Context, cmd, user string, env ...string) error {
execCreate, err := u.cli.ContainerExecCreate(ctx, u.containerID, types.ExecConfig{
AttachStdout: true,
AttachStderr: true,
User: user,
Env: env,
Cmd: []string{"/bin/sh", "-c", cmd},
})
if err != nil {
return fmt.Errorf("failed to create exec: %w", err)
}
execResp, err := u.cli.ContainerExecAttach(ctx, execCreate.ID, types.ExecStartCheck{})
if err != nil {
return fmt.Errorf("failed to start exec: %w", err)
}
r, w := io.Pipe()
go func() {
_, _ = io.Copy(os.Stderr, prefixer.New(r, "updater | "))
}()
// blocks until update is complete or ctl-c
ch := make(chan struct{})
go func() {
_, _ = stdcopy.StdCopy(w, w, execResp.Reader)
ch <- struct{}{}
}()
select {
case <-ctx.Done():
return ctx.Err()
case <-ch:
}
return nil
}
// Wait blocks until the condition is true.
func (u *Updater) Wait(ctx context.Context, condition container.WaitCondition) error {
wait, errCh := u.cli.ContainerWait(ctx, u.containerID, condition)
select {
case v := <-wait:
if v.StatusCode != 0 {
return fmt.Errorf("updater exited with code: %v", v.StatusCode)
}
case err := <-errCh:
return fmt.Errorf("updater error while waiting: %w", err)
}
return nil
}
// Close kills and deletes the container and deletes updater mount paths related to the run.
func (u *Updater) Close() error {
return u.cli.ContainerRemove(context.Background(), u.containerID, types.ContainerRemoveOptions{
Force: true,
})
}
// JobFile is the payload passed to file updater containers.
type JobFile struct {
Job *model.Job `json:"job"`
}
func (j JobFile) ToJSON() (string, error) {
data, err := json.Marshal(j)
return string(data), err
}
func tarball(name, contents string) (*bytes.Buffer, error) {
var buf bytes.Buffer
t := tar.NewWriter(&buf)
if err := addFileToArchive(t, name, 0777, contents); err != nil {
return nil, fmt.Errorf("adding file to archive: %w", err)
}
return &buf, t.Flush()
}
func addFileToArchive(tw *tar.Writer, name string, mode int64, content string) error {
header := &tar.Header{
Name: name,
Size: int64(len(content)),
Mode: mode,
}
err := tw.WriteHeader(header)
if err != nil {
return err
}
_, err = tw.Write([]byte(content))
if err != nil {
return err
}
return nil
}
func firstNonEmpty(values ...string) string {
for _, v := range values {
if v != "" {
return v
}
}
return ""
}