-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathinitializer.go
519 lines (456 loc) · 16.5 KB
/
initializer.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.
package initializer
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/opencontainers/go-digest"
"github.com/opentracing/opentracing-go"
"golang.org/x/xerrors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/gitpod-io/gitpod/common-go/log"
"github.com/gitpod-io/gitpod/common-go/tracing"
csapi "github.com/gitpod-io/gitpod/content-service/api"
"github.com/gitpod-io/gitpod/content-service/pkg/archive"
"github.com/gitpod-io/gitpod/content-service/pkg/git"
"github.com/gitpod-io/gitpod/content-service/pkg/storage"
)
const (
// WorkspaceReadyFile is the name of the ready file we're placing in a workspace
WorkspaceReadyFile = ".gitpod/ready"
// GitpodUID is the user ID of the gitpod user
GitpodUID = 33333
// GitpodGID is the group ID of the gitpod user group
GitpodGID = 33333
// otsDownloadAttempts is the number of times we'll attempt to download the one-time secret
otsDownloadAttempts = 20
)
// Initializer can initialize a workspace with content
type Initializer interface {
Run(ctx context.Context, mappings []archive.IDMapping) (csapi.WorkspaceInitSource, error)
}
// EmptyInitializer does nothing
type EmptyInitializer struct{}
// Run does nothing
func (e *EmptyInitializer) Run(ctx context.Context, mappings []archive.IDMapping) (csapi.WorkspaceInitSource, error) {
return csapi.WorkspaceInitFromOther, nil
}
// CompositeInitializer does nothing
type CompositeInitializer []Initializer
// Run calls run on all child initializers
func (e CompositeInitializer) Run(ctx context.Context, mappings []archive.IDMapping) (csapi.WorkspaceInitSource, error) {
_, ctx = opentracing.StartSpanFromContext(ctx, "CompositeInitializer.Run")
for _, init := range e {
_, err := init.Run(ctx, mappings)
if err != nil {
return csapi.WorkspaceInitFromOther, err
}
}
return csapi.WorkspaceInitFromOther, nil
}
// NewFromRequestOpts configures the initializer produced from a content init request
type NewFromRequestOpts struct {
// ForceGitpodUserForGit forces gitpod:gitpod ownership on all files produced by the Git initializer.
// For FWB workspaces the content init is run from supervisor which runs as UID 0. Using this flag, the
// Git content is forced to the Gitpod user. All other content (backup, prebuild, snapshot) will already
// have the correct user.
ForceGitpodUserForGit bool
}
// NewFromRequest picks the initializer from the request but does not execute it.
// Returns gRPC errors.
func NewFromRequest(ctx context.Context, loc string, rs storage.DirectDownloader, req *csapi.WorkspaceInitializer, opts NewFromRequestOpts) (i Initializer, err error) {
//nolint:ineffassign,staticcheck
span, ctx := opentracing.StartSpanFromContext(ctx, "NewFromRequest")
defer tracing.FinishSpan(span, &err)
span.LogKV("opts", opts)
spec := req.Spec
var initializer Initializer
if _, ok := spec.(*csapi.WorkspaceInitializer_Empty); ok {
initializer = &EmptyInitializer{}
} else if ir, ok := spec.(*csapi.WorkspaceInitializer_Composite); ok {
initializers := make([]Initializer, len(ir.Composite.Initializer))
for i, init := range ir.Composite.Initializer {
initializers[i], err = NewFromRequest(ctx, loc, rs, init, opts)
if err != nil {
return nil, err
}
}
initializer = CompositeInitializer(initializers)
} else if ir, ok := spec.(*csapi.WorkspaceInitializer_Git); ok {
if ir.Git == nil {
return nil, status.Error(codes.InvalidArgument, "missing Git initializer spec")
}
initializer, err = newGitInitializer(ctx, loc, ir.Git, opts.ForceGitpodUserForGit)
} else if ir, ok := spec.(*csapi.WorkspaceInitializer_Prebuild); ok {
if ir.Prebuild == nil {
return nil, status.Error(codes.InvalidArgument, "missing prebuild initializer spec")
}
if ir.Prebuild.Git == nil {
return nil, status.Error(codes.InvalidArgument, "missing prebuild Git initializer spec")
}
gitinit, err := newGitInitializer(ctx, loc, ir.Prebuild.Git, opts.ForceGitpodUserForGit)
if err != nil {
return nil, err
}
var snapshot *SnapshotInitializer
if ir.Prebuild.Prebuild != nil {
snapshot, err = newSnapshotInitializer(loc, rs, ir.Prebuild.Prebuild)
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("cannot setup prebuild init: %v", err))
}
}
initializer = &PrebuildInitializer{
Git: gitinit,
Prebuild: snapshot,
}
} else if ir, ok := spec.(*csapi.WorkspaceInitializer_Snapshot); ok {
initializer, err = newSnapshotInitializer(loc, rs, ir.Snapshot)
} else if ir, ok := spec.(*csapi.WorkspaceInitializer_Download); ok {
initializer, err = newFileDownloadInitializer(loc, ir.Download)
} else if ir, ok := spec.(*csapi.WorkspaceInitializer_Backup); ok {
initializer, err = newFromBackupInitializer(loc, rs, ir.Backup)
} else {
initializer = &EmptyInitializer{}
}
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
return initializer, nil
}
// newFileDownloadInitializer creates a download initializer for a request
func newFileDownloadInitializer(loc string, req *csapi.FileDownloadInitializer) (*fileDownloadInitializer, error) {
fileInfos := make([]fileInfo, len(req.Files))
for i, f := range req.Files {
dgst, err := digest.Parse(f.Digest)
if err != nil {
return nil, xerrors.Errorf("invalid digest %s: %w", f.Digest, err)
}
fileInfos[i] = fileInfo{
URL: f.Url,
Path: f.FilePath,
Digest: dgst,
}
}
initializer := &fileDownloadInitializer{
FilesInfos: fileInfos,
TargetLocation: filepath.Join(loc, req.TargetLocation),
HTTPClient: http.DefaultClient,
RetryTimeout: 1 * time.Second,
}
return initializer, nil
}
// newFromBackupInitializer creates a backup restoration initializer for a request
func newFromBackupInitializer(loc string, rs storage.DirectDownloader, req *csapi.FromBackupInitializer) (*fromBackupInitializer, error) {
return &fromBackupInitializer{
Location: loc,
RemoteStorage: rs,
}, nil
}
type fromBackupInitializer struct {
Location string
RemoteStorage storage.DirectDownloader
}
func (bi *fromBackupInitializer) Run(ctx context.Context, mappings []archive.IDMapping) (src csapi.WorkspaceInitSource, err error) {
hasBackup, err := bi.RemoteStorage.Download(ctx, bi.Location, storage.DefaultBackup, mappings)
if !hasBackup {
return src, xerrors.Errorf("no backup found")
}
if err != nil {
return src, xerrors.Errorf("cannot restore backup: %w", err)
}
return csapi.WorkspaceInitFromBackup, nil
}
// newGitInitializer creates a Git initializer based on the request.
// Returns gRPC errors.
func newGitInitializer(ctx context.Context, loc string, req *csapi.GitInitializer, forceGitpodUser bool) (*GitInitializer, error) {
if req.Config == nil {
return nil, status.Error(codes.InvalidArgument, "Git initializer misses config")
}
var targetMode CloneTargetMode
switch req.TargetMode {
case csapi.CloneTargetMode_LOCAL_BRANCH:
targetMode = LocalBranch
case csapi.CloneTargetMode_REMOTE_BRANCH:
targetMode = RemoteBranch
case csapi.CloneTargetMode_REMOTE_COMMIT:
targetMode = RemoteCommit
case csapi.CloneTargetMode_REMOTE_HEAD:
targetMode = RemoteHead
default:
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("invalid target mode: %v", req.TargetMode))
}
var authMethod = git.BasicAuth
if req.Config.Authentication == csapi.GitAuthMethod_NO_AUTH {
authMethod = git.NoAuth
}
// the auth provider must cache the OTS because it may be used several times,
// but can download the one-time-secret only once.
authProvider := git.CachingAuthProvider(func() (user string, pwd string, err error) {
switch req.Config.Authentication {
case csapi.GitAuthMethod_BASIC_AUTH:
user = req.Config.AuthUser
pwd = req.Config.AuthPassword
case csapi.GitAuthMethod_BASIC_AUTH_OTS:
user, pwd, err = downloadOTS(ctx, req.Config.AuthOts)
if err != nil {
log.WithField("location", loc).WithError(err).Error("cannot download Git auth OTS")
return "", "", status.Error(codes.InvalidArgument, "cannot get OTS")
}
case csapi.GitAuthMethod_NO_AUTH:
default:
return "", "", status.Error(codes.InvalidArgument, fmt.Sprintf("invalid Git authentication method: %v", req.Config.Authentication))
}
return
})
log.WithField("location", loc).Debug("using Git initializer")
return &GitInitializer{
Client: git.Client{
Location: filepath.Join(loc, req.CheckoutLocation),
RemoteURI: req.RemoteUri,
UpstreamRemoteURI: req.Upstream_RemoteUri,
Config: req.Config.CustomConfig,
AuthMethod: authMethod,
AuthProvider: authProvider,
},
TargetMode: targetMode,
CloneTarget: req.CloneTaget,
Chown: forceGitpodUser,
}, nil
}
func newSnapshotInitializer(loc string, rs storage.DirectDownloader, req *csapi.SnapshotInitializer) (*SnapshotInitializer, error) {
return &SnapshotInitializer{
Location: loc,
Snapshot: req.Snapshot,
Storage: rs,
}, nil
}
func downloadOTS(ctx context.Context, url string) (user, pwd string, err error) {
//nolint:ineffassign
span, ctx := opentracing.StartSpanFromContext(ctx, "downloadOTS")
defer tracing.FinishSpan(span, &err)
span.LogKV("url", url)
dl := func() (user, pwd string, err error) {
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return "", "", err
}
_ = opentracing.GlobalTracer().Inject(span.Context(), opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", "", xerrors.Errorf("non-OK OTS response: %s", resp.Status)
}
secret, err := io.ReadAll(resp.Body)
if err != nil {
return "", "", err
}
pwd = string(secret)
if segs := strings.Split(pwd, ":"); len(segs) >= 2 {
user = segs[0]
pwd = strings.Join(segs[1:], ":")
}
return
}
for i := 0; i < otsDownloadAttempts; i++ {
span.LogKV("attempt", i)
if i > 0 {
time.Sleep(time.Second)
}
user, pwd, err = dl()
if err == context.Canceled || err == context.DeadlineExceeded {
return
}
if err == nil {
break
}
log.WithError(err).WithField("attempt", i).Warn("cannot download OTS")
}
if err != nil {
log.WithError(err).Warn("failed to download OTS")
return "", "", err
}
return user, pwd, nil
}
// InitializeOpt configures the initialisation procedure
type InitializeOpt func(*initializeOpts)
type initializeOpts struct {
Initializer Initializer
CleanSlate bool
UID int
GID int
mappings []archive.IDMapping
}
// WithMappings configures the UID mappings that're used during content initialization
func WithMappings(mappings []archive.IDMapping) InitializeOpt {
return func(o *initializeOpts) {
o.mappings = mappings
}
}
// WithInitializer configures the initializer that's used during content initialization
func WithInitializer(initializer Initializer) InitializeOpt {
return func(o *initializeOpts) {
o.Initializer = initializer
}
}
// WithCleanSlate ensures there's no prior content in the workspace location
func WithCleanSlate(o *initializeOpts) {
o.CleanSlate = true
}
// WithChown sets a custom UID/GID the content will have after initialisation
func WithChown(uid, gid int) InitializeOpt {
return func(o *initializeOpts) {
o.UID = uid
o.GID = gid
}
}
// InitializeWorkspace initializes a workspace from backup or an initializer
func InitializeWorkspace(ctx context.Context, location string, remoteStorage storage.DirectDownloader, opts ...InitializeOpt) (src csapi.WorkspaceInitSource, err error) {
//nolint:ineffassign
span, ctx := opentracing.StartSpanFromContext(ctx, "InitializeWorkspace")
span.SetTag("location", location)
defer tracing.FinishSpan(span, &err)
cfg := initializeOpts{
Initializer: &EmptyInitializer{},
CleanSlate: false,
GID: GitpodGID,
UID: GitpodUID,
}
for _, o := range opts {
o(&cfg)
}
src = csapi.WorkspaceInitFromOther
// Note: it's important that CleanSlate does not remove the location itself, but merely its content.
// If the location were removed that might break the filesystem quota we have put in place prior.
if cfg.CleanSlate {
// 1. Clean out the workspace directory
if _, err := os.Stat(location); os.IsNotExist(err) {
// in the very unlikely event that the workspace Pod did not mount (and thus create) the workspace directory, create it
err = os.Mkdir(location, 0755)
if os.IsExist(err) {
log.WithError(err).WithField("location", location).Debug("ran into non-atomic workspace location existence check")
span.SetTag("exists", true)
} else if err != nil {
return src, xerrors.Errorf("cannot create workspace: %w", err)
}
}
fs, err := os.ReadDir(location)
if err != nil {
return src, xerrors.Errorf("cannot clean workspace folder: %w", err)
}
for _, f := range fs {
path := filepath.Join(location, f.Name())
err := os.RemoveAll(path)
if err != nil {
return src, xerrors.Errorf("cannot clean workspace folder: %w", err)
}
}
// Chown the workspace directory
err = os.Chown(location, cfg.UID, cfg.GID)
if err != nil {
return src, xerrors.Errorf("cannot create workspace: %w", err)
}
}
// Run the initializer
hasBackup, err := remoteStorage.Download(ctx, location, storage.DefaultBackup, cfg.mappings)
if err != nil {
return src, xerrors.Errorf("cannot restore backup: %w", err)
}
span.SetTag("hasBackup", hasBackup)
if hasBackup {
src = csapi.WorkspaceInitFromBackup
} else {
src, err = cfg.Initializer.Run(ctx, cfg.mappings)
if err != nil {
return src, xerrors.Errorf("cannot initialize workspace: %w", err)
}
}
return
}
// Some workspace content may have a `/dst/.gitpod` file or directory. That would break
// the workspace ready file placement (see https://github.com/gitpod-io/gitpod/issues/7694).
// This function ensures that workspaces do not have a `.gitpod` file or directory present.
func EnsureCleanDotGitpodDirectory(ctx context.Context, wspath string) error {
var mv func(src, dst string) error
if git.IsWorkingCopy(wspath) {
c := &git.Client{
Location: wspath,
}
mv = func(src, dst string) error {
return c.Git(ctx, "mv", src, dst)
}
} else {
mv = os.Rename
}
dotGitpod := filepath.Join(wspath, ".gitpod")
stat, err := os.Stat(dotGitpod)
if os.IsNotExist(err) {
return nil
}
if stat.IsDir() {
// we need this to be a directory, we're probably ok
return nil
}
candidateFN := filepath.Join(wspath, ".gitpod.yaml")
if _, err := os.Stat(candidateFN); err == nil {
// Our candidate file already exists, hence we cannot just move things.
// As fallback we'll delete the .gitpod entry.
return os.RemoveAll(dotGitpod)
}
err = mv(dotGitpod, candidateFN)
if err != nil {
return err
}
return nil
}
// PlaceWorkspaceReadyFile writes a file in the workspace which indicates that the workspace has been initialized
func PlaceWorkspaceReadyFile(ctx context.Context, wspath string, initsrc csapi.WorkspaceInitSource, uid, gid int) (err error) {
//nolint:ineffassign,staticcheck
span, ctx := opentracing.StartSpanFromContext(ctx, "placeWorkspaceReadyFile")
span.SetTag("source", initsrc)
defer tracing.FinishSpan(span, &err)
content := csapi.WorkspaceReadyMessage{
Source: initsrc,
}
fc, err := json.Marshal(content)
if err != nil {
return xerrors.Errorf("cannot marshal workspace ready message: %w", err)
}
gitpodDir := filepath.Join(wspath, filepath.Dir(WorkspaceReadyFile))
err = os.MkdirAll(gitpodDir, 0777)
if err != nil {
return xerrors.Errorf("cannot create directory for workspace ready file: %w", err)
}
err = os.Chown(gitpodDir, uid, gid)
if err != nil {
return xerrors.Errorf("cannot chown directory for workspace ready file: %w", err)
}
tempWorkspaceReadyFile := WorkspaceReadyFile + ".tmp"
fn := filepath.Join(wspath, tempWorkspaceReadyFile)
err = os.WriteFile(fn, []byte(fc), 0644)
if err != nil {
return xerrors.Errorf("cannot write workspace ready file content: %w", err)
}
err = os.Chown(fn, uid, gid)
if err != nil {
return xerrors.Errorf("cannot chown workspace ready file: %w", err)
}
// Theia will listen for a rename event as trigger to start the tasks. This is a rename event
// because we're writing to the file and this is the most convenient way we can tell Theia that we're done writing.
err = os.Rename(fn, filepath.Join(wspath, WorkspaceReadyFile))
if err != nil {
return xerrors.Errorf("cannot rename workspace ready file: %w", err)
}
return nil
}