-
Notifications
You must be signed in to change notification settings - Fork 85
/
fs.go
407 lines (351 loc) · 11.4 KB
/
fs.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
package repo
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/mitchellh/go-homedir"
"github.com/rs/zerolog/log"
"github.com/bacalhau-project/bacalhau/pkg/config"
"github.com/bacalhau-project/bacalhau/pkg/storage/util"
"github.com/bacalhau-project/bacalhau/pkg/telemetry"
)
type RepoVersion struct {
Version int
}
const (
// repo versioning
RepoVersion1 = 1
RepoVersionFile = "repo.version"
// user key files
Libp2pPrivateKeyFileName = "libp2p_private_key"
UserPrivateKeyFileName = "user_id.pem"
// compute paths
ComputeStoragesPath = "executor_storages"
PluginsPath = "plugins"
// requester paths
AutoCertCachePath = "autocert-cache"
// update check paths
UpdateCheckStatePath = "update.json"
)
type FsRepo struct {
path string
}
func NewFS(path string) (*FsRepo, error) {
expandedPath, err := homedir.Expand(path)
if err != nil {
return nil, err
}
return &FsRepo{
path: expandedPath,
}, nil
}
func (fsr *FsRepo) writeVersion() error {
repoVersion := RepoVersion{Version: RepoVersion1}
versionJSON, err := json.Marshal(repoVersion)
if err != nil {
return err
}
return os.WriteFile(filepath.Join(fsr.path, RepoVersionFile), versionJSON, util.OS_USER_RW)
}
func (fsr *FsRepo) readVersion() (int, error) {
versionBytes, err := os.ReadFile(filepath.Join(fsr.path, RepoVersionFile))
if err != nil {
return -1, err
}
var version RepoVersion
if err := json.Unmarshal(versionBytes, &version); err != nil {
return -1, err
}
return version.Version, nil
}
func (fsr *FsRepo) Path() (string, error) {
if exists, err := fsr.Exists(); err != nil {
return "", err
} else if !exists {
return "", fmt.Errorf("repo is uninitialized")
}
return fsr.path, nil
}
func (fsr *FsRepo) Exists() (bool, error) {
// check if the path is present
if _, err := os.Stat(fsr.path); os.IsNotExist(err) {
return false, nil
} else if err != nil {
return false, err
}
// check if the repo version file is present
versionPath := filepath.Join(fsr.path, RepoVersionFile)
if _, err := os.Stat(versionPath); os.IsNotExist(err) {
return false, nil
} else if err != nil {
return false, err
}
version, err := fsr.readVersion()
if err != nil {
return false, err
}
if version != RepoVersion1 {
return false, fmt.Errorf("unknown repo version %d", version)
}
return true, nil
}
func (fsr *FsRepo) Open() error {
if exists, err := fsr.Exists(); err != nil {
return err
} else if !exists {
return fmt.Errorf("repo does not exist")
}
cfg, err := config.Load(fsr.path, configName, configType)
if err != nil {
return err
}
// TODO this should be a part of the config.
telemetry.SetupFromEnvs()
if cfg.User.KeyPath == "" {
// if the user has not specified the location of their user key via a config file, use the default value
cfg.User.KeyPath = filepath.Join(fsr.path, UserPrivateKeyFileName)
config.SetUserKey(cfg.User.KeyPath)
}
if cfg.User.Libp2pKeyPath == "" {
// if the user has not specified the location of their libp2p key via a config file, use the default value
cfg.User.Libp2pKeyPath = filepath.Join(fsr.path, Libp2pPrivateKeyFileName)
config.SetLibp2pKey(cfg.User.Libp2pKeyPath)
}
if cfg.Node.ExecutorPluginPath == "" {
cfg.Node.ExecutorPluginPath = filepath.Join(fsr.path, PluginsPath)
config.SetExecutorPluginPath(cfg.Node.ExecutorPluginPath)
}
if cfg.Node.ServerAPI.TLS.AutoCertCachePath == "" {
cfg.Node.ServerAPI.TLS.AutoCertCachePath = filepath.Join(fsr.path, AutoCertCachePath)
config.SetAutoCertCachePath(cfg.Node.ServerAPI.TLS.AutoCertCachePath)
}
if cfg.Node.ComputeStoragePath == "" {
cfg.Node.ComputeStoragePath = filepath.Join(fsr.path, ComputeStoragesPath)
config.SetComputeStoragesPath(cfg.Node.ComputeStoragePath)
}
if cfg.Update.CheckStatePath == "" {
cfg.Update.CheckStatePath = filepath.Join(fsr.path, UpdateCheckStatePath)
config.SetUpdateCheckStatePath(cfg.Update.CheckStatePath)
}
// Using a slice of paths to minimize repetitive checks
pathsToCheck := []string{
cfg.User.KeyPath,
cfg.User.Libp2pKeyPath,
cfg.Node.ExecutorPluginPath,
cfg.Node.ComputeStoragePath,
// Note that cfg.Update.StateCheckPath is deliberately omitted here:
// this file will get created on first run of the update check
}
for _, path := range pathsToCheck {
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("filepath '%s' does not exists", path)
}
return fmt.Errorf("failed to stat '%s': %w", path, err)
}
}
return nil
}
const (
configType = "yaml"
configName = "config"
repoPermission = 0755
)
func (fsr *FsRepo) Init() error {
defaultConfig := config.ForEnvironment()
if exists, err := fsr.Exists(); err != nil {
return err
} else if exists {
return fmt.Errorf("cannot init repo: repo already exists")
}
log.Info().Msgf("Initializing repo at '%s' for environment '%s'", fsr.path, config.GetConfigEnvironment())
// 0755: Owner can read, write, execute. Others can read and execute.
if err := os.MkdirAll(fsr.path, repoPermission); err != nil && !os.IsExist(err) {
return err
}
var err error
defaultConfig.User.KeyPath, err = fsr.ensureUserIDKey(UserPrivateKeyFileName)
if err != nil {
return fmt.Errorf("failed to create user key: %w", err)
}
defaultConfig.User.Libp2pKeyPath, err = fsr.ensureLibp2pKey(Libp2pPrivateKeyFileName)
if err != nil {
return fmt.Errorf("failed to create libp2p key: %w", err)
}
defaultConfig.Node.ExecutorPluginPath, err = fsr.ensureDir(PluginsPath)
if err != nil {
return fmt.Errorf("failed to create plugin dir: %w", err)
}
defaultConfig.Node.ComputeStoragePath, err = fsr.ensureDir(ComputeStoragesPath)
if err != nil {
return fmt.Errorf("failed to create executor storage dir: %w", err)
}
_, err = config.Init(defaultConfig, fsr.path, configName, configType)
if err != nil {
return err
}
// TODO this should be a part of the config.
telemetry.SetupFromEnvs()
return fsr.writeVersion()
}
func (fsr *FsRepo) ensureDir(name string) (string, error) {
path := filepath.Join(fsr.path, name)
if err := os.MkdirAll(path, util.OS_USER_RWX); err != nil {
return "", fmt.Errorf("failed to create directory at at '%s': %w", path, err)
}
return path, nil
}
const (
bitsPerKey = 2048 // number of bits in generated RSA keypairs
)
// ensureUserIDKey ensures that a default user ID key exists in the config dir.
func (fsr *FsRepo) ensureUserIDKey(name string) (string, error) {
keyFile := filepath.Join(fsr.path, name)
if _, err := os.Stat(keyFile); err != nil {
if os.IsNotExist(err) {
log.Debug().Msgf(
"user ID key file '%s' does not exist, creating one", keyFile)
var key *rsa.PrivateKey
key, err = rsa.GenerateKey(rand.Reader, bitsPerKey)
if err != nil {
return "", fmt.Errorf("failed to generate private key: %w", err)
}
keyBytes := x509.MarshalPKCS1PrivateKey(key)
keyBlock := pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: keyBytes,
}
var file *os.File
file, err = os.Create(keyFile)
if err != nil {
return "", fmt.Errorf("failed to create key file: %w", err)
}
if err = pem.Encode(file, &keyBlock); err != nil {
return "", fmt.Errorf("failed to encode key file: %w", err)
}
if err = file.Close(); err != nil {
return "", fmt.Errorf("failed to close key file: %w", err)
}
if err = os.Chmod(keyFile, util.OS_USER_RW); err != nil {
return "", fmt.Errorf("failed to set permission on key file: %w", err)
}
} else {
return "", fmt.Errorf("failed to stat user ID key '%s': %w",
keyFile, err)
}
}
return keyFile, nil
}
func (fsr *FsRepo) ensureLibp2pKey(name string) (string, error) {
// We include the port in the filename so that in devstack multiple nodes
// running on the same host get different identities
privKeyPath := filepath.Join(fsr.path, name)
if _, err := os.Stat(privKeyPath); errors.Is(err, os.ErrNotExist) {
// Private key does not exist - create and write it
// Creates a new RSA key pair for this host.
prvKey, _, err := crypto.GenerateKeyPairWithReader(crypto.RSA, bitsPerKey, rand.Reader)
if err != nil {
log.Error().Err(err)
return "", err
}
keyOut, err := os.OpenFile(privKeyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, util.OS_USER_RW)
if err != nil {
return "", fmt.Errorf("failed to open key.pem for writing: %v", err)
}
privBytes, err := crypto.MarshalPrivateKey(prvKey)
if err != nil {
return "", fmt.Errorf("unable to marshal private key: %v", err)
}
// base64 encode privBytes
b64 := base64.StdEncoding.EncodeToString(privBytes)
_, err = keyOut.WriteString(b64 + "\n")
if err != nil {
return "", fmt.Errorf("failed to write to key file: %v", err)
}
if err := keyOut.Close(); err != nil {
return "", fmt.Errorf("error closing key file: %v", err)
}
log.Debug().Msgf("wrote %s", privKeyPath)
} else {
return privKeyPath, err
}
// Now that we've ensured the private key is written to disk, read it! This
// ensures that loading it works even in the case where we've just created
// it.
// read the private key
keyBytes, err := os.ReadFile(privKeyPath)
if err != nil {
return "", fmt.Errorf("failed to read private key: %v", err)
}
// base64 decode keyBytes
b64, err := base64.StdEncoding.DecodeString(string(keyBytes))
if err != nil {
return "", fmt.Errorf("failed to decode private key: %v", err)
}
// parse the private key
_, err = crypto.UnmarshalPrivateKey(b64)
if err != nil {
return "", fmt.Errorf("failed to parse private key: %v", err)
}
return privKeyPath, nil
}
const defaultRunInfoFilename = "bacalhau.run"
const runInfoFilePermissions = 0755
func (fsr *FsRepo) WriteRunInfo(ctx context.Context, summaryShellVariablesString string) (string, error) {
runInfoPath := filepath.Join(fsr.path, defaultRunInfoFilename)
// TODO kill this
devStackRunInfoPath := os.Getenv("DEVSTACK_ENV_FILE")
if devStackRunInfoPath != "" {
runInfoPath = devStackRunInfoPath
}
// Use os.Create to truncate the file if it already exists
f, err := os.Create(runInfoPath)
if err != nil {
return "", err
}
defer func() {
err = f.Close()
if err != nil {
log.Ctx(ctx).Err(err).Msgf("Failed to close run info file %s", runInfoPath)
}
}()
// Set permissions to constant for read read/write only by user
err = f.Chmod(runInfoFilePermissions)
if err != nil {
return "", err
}
_, err = f.Write([]byte(summaryShellVariablesString))
if err != nil {
return "", err
}
return runInfoPath, nil
// TODO previous behavior put it in these places, we may consider creating a symlink later
/*
if writeable, _ := filefs.IsWritable("/run"); writeable {
writePath = "/run" // Linux
} else if writeable, _ := filefs.IsWritable("/var/run"); writeable {
writePath = "/var/run" // Older Linux
} else if writeable, _ := filefs.IsWritable("/private/var/run"); writeable {
writePath = "/private/var/run" // MacOS
} else {
// otherwise write to the user's dir, which should be available on all systems
userDir, err := os.UserHomeDir()
if err != nil {
log.Ctx(ctx).Err(err).Msg("Could not write to /run, /var/run, or /private/var/run, and could not get user's home dir")
return nil
}
log.Warn().Msgf("Could not write to /run, /var/run, or /private/var/run, writing to %s dir instead. "+
"This file contains sensitive information, so please ensure it is limited in visibility.", userDir)
writePath = userDir
}
*/
}