-
Notifications
You must be signed in to change notification settings - Fork 138
/
build_linux.go
363 lines (311 loc) · 10.7 KB
/
build_linux.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
// Copyright (c) 2021 Apptainer a Series of LF Projects LLC
// For website terms of use, trademark policy, privacy policy and other
// project policies see https://lfprojects.org/policies
// Copyright (c) 2020, Control Command Inc. All rights reserved.
// Copyright (c) 2018-2021, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package cli
import (
"context"
"fmt"
"os"
osExec "os/exec"
"runtime"
"strconv"
"strings"
"syscall"
"github.com/apptainer/apptainer/internal/pkg/build"
"github.com/apptainer/apptainer/internal/pkg/buildcfg"
"github.com/apptainer/apptainer/internal/pkg/cache"
"github.com/apptainer/apptainer/internal/pkg/remote/endpoint"
fakerootConfig "github.com/apptainer/apptainer/internal/pkg/runtime/engine/fakeroot/config"
"github.com/apptainer/apptainer/internal/pkg/util/fs"
"github.com/apptainer/apptainer/internal/pkg/util/interactive"
"github.com/apptainer/apptainer/internal/pkg/util/starter"
"github.com/apptainer/apptainer/internal/pkg/util/user"
"github.com/apptainer/apptainer/pkg/build/types"
"github.com/apptainer/apptainer/pkg/image"
"github.com/apptainer/apptainer/pkg/runtime/engine/config"
"github.com/apptainer/apptainer/pkg/sylog"
"github.com/apptainer/apptainer/pkg/util/cryptkey"
"github.com/spf13/cobra"
)
func fakerootExec(cmdArgs []string) {
if buildArgs.nvccli && !buildArgs.noTest {
sylog.Warningf("Due to writable-tmpfs limitations, %%test sections will fail with --nvccli & --fakeroot")
sylog.Infof("Use -T / --notest to disable running tests during the build")
}
useSuid := buildcfg.APPTAINER_SUID_INSTALL == 1
short := "-" + buildFakerootFlag.ShortHand
long := "--" + buildFakerootFlag.Name
envKey := fmt.Sprintf("APPTAINER_%s", buildFakerootFlag.EnvKeys[0])
fakerootEnv := os.Getenv(envKey) != ""
argsLen := len(os.Args) - 1
if fakerootEnv {
argsLen = len(os.Args)
os.Unsetenv(envKey)
}
args := make([]string, argsLen)
idx := 0
for i, arg := range os.Args {
if i == 0 {
path, _ := osExec.LookPath(arg)
arg = path
}
if arg != short && arg != long {
args[idx] = arg
idx++
}
}
user, err := user.GetPwUID(uint32(os.Getuid()))
if err != nil {
sylog.Fatalf("failed to retrieve user information: %s", err)
}
// Append the user's real UID to the environment as _CONTAINERS_ROOTLESS_UID.
// This is required in fakeroot builds that may use containers/image 5.7 and above.
// https://github.com/containers/image/issues/1066
// https://github.com/containers/image/blob/master/internal/rootless/rootless.go
os.Setenv("_CONTAINERS_ROOTLESS_UID", strconv.Itoa(os.Getuid()))
engineConfig := &fakerootConfig.EngineConfig{
Args: args,
Envs: os.Environ(),
Home: user.Dir,
BuildEnv: true,
}
cfg := &config.Common{
EngineName: fakerootConfig.Name,
ContainerID: "fakeroot",
EngineConfig: engineConfig,
}
err = starter.Exec(
"Apptainer fakeroot",
cfg,
starter.UseSuid(useSuid),
)
sylog.Fatalf("%s", err)
}
func runBuild(cmd *cobra.Command, args []string) {
if buildArgs.nvidia {
os.Setenv("APPTAINER_NV", "1")
}
if buildArgs.nvccli {
os.Setenv("APPTAINER_NVCCLI", "1")
}
if buildArgs.rocm {
os.Setenv("APPTAINER_ROCM", "1")
}
if len(buildArgs.bindPaths) > 0 {
os.Setenv("APPTAINER_BINDPATH", strings.Join(buildArgs.bindPaths, ","))
}
if len(buildArgs.mounts) > 0 {
os.Setenv("APPTAINER_MOUNT", strings.Join(buildArgs.mounts, "\n"))
}
if buildArgs.writableTmpfs {
if buildArgs.fakeroot {
sylog.Fatalf("--writable-tmpfs option is not supported for fakeroot build")
}
os.Setenv("APPTAINER_WRITABLE_TMPFS", "1")
}
if buildArgs.arch != runtime.GOARCH {
sylog.Fatalf("Requested architecture (%s) does not match host (%s). Cannot build locally.", buildArgs.arch, runtime.GOARCH)
}
dest := args[0]
spec := args[1]
// check if target collides with existing file
if err := checkBuildTarget(dest); err != nil {
sylog.Fatalf("While checking build target: %s", err)
}
runBuildLocal(cmd.Context(), cmd, dest, spec)
sylog.Infof("Build complete: %s", dest)
}
func runBuildLocal(ctx context.Context, cmd *cobra.Command, dst, spec string) {
var keyInfo *cryptkey.KeyInfo
if buildArgs.encrypt || promptForPassphrase || cmd.Flags().Lookup("pem-path").Changed {
if os.Getuid() != 0 {
sylog.Fatalf("You must be root to build an encrypted container")
}
k, err := getEncryptionMaterial(cmd)
if err != nil {
sylog.Fatalf("While handling encryption material: %v", err)
}
keyInfo = &k
} else {
_, passphraseEnvOK := os.LookupEnv("APPTAINER_ENCRYPTION_PASSPHRASE")
_, pemPathEnvOK := os.LookupEnv("APPTAINER_ENCRYPTION_PEM_PATH")
if passphraseEnvOK || pemPathEnvOK {
sylog.Warningf("Encryption related env vars found, but --encrypt was not specified. NOT encrypting container.")
}
}
imgCache := getCacheHandle(cache.Config{Disable: disableCache})
if imgCache == nil {
sylog.Fatalf("Failed to create an image cache handle")
}
if syscall.Getuid() != 0 && !buildArgs.fakeroot && fs.IsFile(spec) && !isImage(spec) {
sylog.Fatalf("You must be the root user, however you can --fakeroot to build from an Apptainer recipe file")
}
err := checkSections()
if err != nil {
sylog.Fatalf("Could not check build sections: %v", err)
}
authConf, err := makeDockerCredentials(cmd)
if err != nil {
sylog.Fatalf("While creating Docker credentials: %v", err)
}
// parse definition to determine build source
defs, err := build.MakeAllDefs(spec)
if err != nil {
sylog.Fatalf("Unable to build from %s: %v", spec, err)
}
hasLibrary := false
// only resolve remote endpoints if library is a build source
for _, d := range defs {
if d.Header["bootstrap"] == "library" {
hasLibrary = true
break
}
}
authToken := ""
if hasLibrary {
lc, err := getLibraryClientConfig(buildArgs.libraryURL)
if err != nil {
sylog.Fatalf("Unable to get library client configuration: %v", err)
}
buildArgs.libraryURL = lc.BaseURL
authToken = lc.AuthToken
}
co, err := getKeyserverClientOpts(buildArgs.keyServerURL, endpoint.KeyserverVerifyOp)
if err != nil {
sylog.Fatalf("Unable to get key server client configuration: %v", err)
}
buildFormat := "sif"
sandboxTarget := false
if buildArgs.sandbox {
buildFormat = "sandbox"
sandboxTarget = true
}
b, err := build.New(
defs,
build.Config{
Dest: dst,
Format: buildFormat,
NoCleanUp: buildArgs.noCleanUp,
Opts: types.Options{
ImgCache: imgCache,
TmpDir: tmpDir,
NoCache: disableCache,
Update: buildArgs.update,
Force: forceOverwrite,
Sections: buildArgs.sections,
NoTest: buildArgs.noTest,
NoHTTPS: noHTTPS,
LibraryURL: buildArgs.libraryURL,
LibraryAuthToken: authToken,
KeyServerOpts: co,
DockerAuthConfig: authConf,
EncryptionKeyInfo: keyInfo,
FixPerms: buildArgs.fixPerms,
SandboxTarget: sandboxTarget,
},
})
if err != nil {
sylog.Fatalf("Unable to create build: %v", err)
}
if err = b.Full(ctx); err != nil {
sylog.Fatalf("While performing build: %v", err)
}
}
func checkSections() error {
var all, none bool
for _, section := range buildArgs.sections {
if section == "none" {
none = true
}
if section == "all" {
all = true
}
}
if all && len(buildArgs.sections) > 1 {
return fmt.Errorf("section specification error: cannot have all and any other option")
}
if none && len(buildArgs.sections) > 1 {
return fmt.Errorf("section specification error: cannot have none and any other option")
}
return nil
}
func isImage(spec string) bool {
i, err := image.Init(spec, false)
if i != nil {
_ = i.File.Close()
}
return err == nil
}
// getEncryptionMaterial handles the setting of encryption environment and flag parameters to eventually be
// passed to the crypt package for handling.
// This handles the APPTAINER_ENCRYPTION_PASSPHRASE/PEM_PATH envvars outside of cobra in order to
// enforce the unique flag/env precedence for the encryption flow
func getEncryptionMaterial(cmd *cobra.Command) (cryptkey.KeyInfo, error) {
passphraseFlag := cmd.Flags().Lookup("passphrase")
PEMFlag := cmd.Flags().Lookup("pem-path")
passphraseEnv, passphraseEnvOK := os.LookupEnv("APPTAINER_ENCRYPTION_PASSPHRASE")
pemPathEnv, pemPathEnvOK := os.LookupEnv("APPTAINER_ENCRYPTION_PEM_PATH")
// checks for no flags/envvars being set
if !(PEMFlag.Changed || pemPathEnvOK || passphraseFlag.Changed || passphraseEnvOK) {
sylog.Fatalf("Unable to use container encryption. Must supply encryption material through environment variables or flags.")
}
// order of precedence:
// 1. PEM flag
// 2. Passphrase flag
// 3. PEM envvar
// 4. Passphrase envvar
if PEMFlag.Changed {
exists, err := fs.PathExists(encryptionPEMPath)
if err != nil {
sylog.Fatalf("Unable to verify existence of %s: %v", encryptionPEMPath, err)
}
if !exists {
sylog.Fatalf("Specified PEM file %s: does not exist.", encryptionPEMPath)
}
sylog.Verbosef("Using pem path flag for encrypted container")
// Check it's a valid PEM public key we can load, before starting the build (#4173)
if cmd.Name() == "build" {
if _, err := cryptkey.LoadPEMPublicKey(encryptionPEMPath); err != nil {
sylog.Fatalf("Invalid encryption public key: %v", err)
}
// or a valid private key before launching the engine for actions on a container (#5221)
} else {
if _, err := cryptkey.LoadPEMPrivateKey(encryptionPEMPath); err != nil {
sylog.Fatalf("Invalid encryption private key: %v", err)
}
}
return cryptkey.KeyInfo{Format: cryptkey.PEM, Path: encryptionPEMPath}, nil
}
if passphraseFlag.Changed {
sylog.Verbosef("Using interactive passphrase entry for encrypted container")
passphrase, err := interactive.AskQuestionNoEcho("Enter encryption passphrase: ")
if err != nil {
return cryptkey.KeyInfo{}, err
}
if passphrase == "" {
sylog.Fatalf("Cannot encrypt container with empty passphrase")
}
return cryptkey.KeyInfo{Format: cryptkey.Passphrase, Material: passphrase}, nil
}
if pemPathEnvOK {
exists, err := fs.PathExists(pemPathEnv)
if err != nil {
sylog.Fatalf("Unable to verify existence of %s: %v", pemPathEnv, err)
}
if !exists {
sylog.Fatalf("Specified PEM file %s: does not exist.", pemPathEnv)
}
sylog.Verbosef("Using pem path environment variable for encrypted container")
return cryptkey.KeyInfo{Format: cryptkey.PEM, Path: pemPathEnv}, nil
}
if passphraseEnvOK {
sylog.Verbosef("Using passphrase environment variable for encrypted container")
return cryptkey.KeyInfo{Format: cryptkey.Passphrase, Material: passphraseEnv}, nil
}
return cryptkey.KeyInfo{}, nil
}