-
Notifications
You must be signed in to change notification settings - Fork 9
/
session.go
151 lines (124 loc) · 4.12 KB
/
session.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package buildkit
import (
"context"
"fmt"
"os"
"strings"
"github.com/docker/cli/cli/config"
"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/session/auth/authprovider"
"github.com/moby/buildkit/session/secrets/secretsprovider"
"github.com/moby/buildkit/session/sshforward/sshprovider"
"namespacelabs.dev/foundation/framework/secrets"
"namespacelabs.dev/foundation/internal/artifacts/oci"
"namespacelabs.dev/foundation/internal/build/buildkit/bkkeychain"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/executor"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/parsing/devhost"
"namespacelabs.dev/foundation/internal/workspace/dirs"
"namespacelabs.dev/foundation/schema"
)
type FrontendRequest struct {
Def *llb.Definition
OriginalState *llb.State
Frontend string
FrontendAttrs map[string]string
FrontendInputs map[string]llb.State
Secrets []*schema.PackageRef
}
func MakeLocalExcludes(src LocalContents) []string {
excludePatterns := []string{}
excludePatterns = append(excludePatterns, dirs.BasePatternsToExclude...)
excludePatterns = append(excludePatterns, devhost.HostOnlyFiles()...)
excludePatterns = append(excludePatterns, src.ExcludePatterns...)
return excludePatterns
}
func MakeLocalState(src LocalContents) llb.State {
return llb.Local(src.Abs(),
llb.WithCustomName(fmt.Sprintf("Workspace %s (from %s)", src.Path, src.Module.ModuleName())),
llb.SharedKeyHint(src.Abs()),
llb.LocalUniqueID(src.Abs()),
llb.ExcludePatterns(MakeLocalExcludes(src)))
}
func prepareSession(ctx context.Context, keychain oci.Keychain, src secrets.GroundedSecrets, secrets []*schema.PackageRef) ([]session.Attachable, error) {
var fs []secretsprovider.Source
for _, def := range strings.Split(BuildkitSecrets, ";") {
if def == "" {
continue
}
parts := strings.Split(def, ":")
if len(parts) != 3 {
return nil, fnerrors.BadInputError("bad secret definition, expected {name}:env|file:{value}")
}
src := secretsprovider.Source{
ID: parts[0],
}
switch parts[1] {
case "env":
src.Env = parts[2]
case "file":
src.FilePath = parts[2]
default:
return nil, fnerrors.BadInputError("expected env or file, got %q", parts[1])
}
fs = append(fs, src)
}
store, err := secretsprovider.NewStore(fs)
if err != nil {
return nil, err
}
secretValues := map[string][]byte{}
if len(secrets) > 0 {
if src == nil {
return nil, fnerrors.InternalError("secrets specified, but secret source missing")
}
eg := executor.New(ctx, "buildkit.load-secrets")
results := make([][]byte, len(secrets))
for k, sec := range secrets {
k := k
sec := sec
eg.Go(func(ctx context.Context) error {
result, err := src.Get(ctx, sec)
if err != nil {
return err
}
if result.Value == nil {
return fnerrors.New("can't use secret %q, no value available (it's generated)", sec.Canonical())
}
results[k] = result.Value
return nil
})
}
if err := eg.Wait(); err != nil {
return nil, err
}
for k, sec := range secrets {
secretValues[sec.Canonical()] = results[k]
}
}
attachables := []session.Attachable{
secretsprovider.NewSecretProvider(secretSource{store, secretValues}),
}
if ForwardKeychain {
if keychain != nil {
attachables = append(attachables, bkkeychain.Wrapper{Context: ctx, ErrorLogger: console.Output(ctx, "buildkit-auth"), Keychain: keychain})
}
} else {
dockerConfig := config.LoadDefaultConfigFile(console.Stderr(ctx))
attachables = append(attachables, authprovider.NewDockerAuthProvider(dockerConfig))
}
// XXX make this configurable; eg at the devhost side.
if os.Getenv("SSH_AUTH_SOCK") != "" {
ssh, err := sshprovider.NewSSHAgentProvider([]sshprovider.AgentConfig{{ID: SSHAgentProviderID}})
if err != nil {
return nil, err
}
attachables = append(attachables, ssh)
}
return attachables, nil
}