-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathlocalstorage.go
503 lines (425 loc) · 13.7 KB
/
localstorage.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
package cnappgoat
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"github.com/ermetic-research/CNAPPgoat/infra"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"io"
"net/url"
"os"
"path/filepath"
"strings"
"time"
)
const stateFile = "state.yaml"
const gitMetadataFile = ".gitMetadataFile"
type gitMetadata struct {
CommitHash string `yaml:"commitHash"`
Date string `yaml:"date"`
}
type LocalStorage struct {
WorkingDir string
}
func NewLocalStorage() (*LocalStorage, error) {
workDir, err := getLocalWorkDirPath()
if err != nil {
return nil, fmt.Errorf("unable to get local working directory path: %w", err)
}
return &LocalStorage{
WorkingDir: workDir,
}, nil
}
func (l *LocalStorage) DeleteWorkingDir() error {
return os.RemoveAll(l.WorkingDir)
}
func (l *LocalStorage) GetProjectPath(scenario *Scenario) string {
return filepath.Join(l.GetScenarioWorkingDir(scenario), "Pulumi.yaml")
}
func (l *LocalStorage) GetPulumiBackendURL() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("unable to get user home directory: %w", err)
}
pulumiBackendPath := filepath.ToSlash(filepath.Join(homeDir, ".cnappgoat"))
u := &url.URL{
Scheme: "file",
Path: pulumiBackendPath,
}
return u.String(), nil
}
func (l *LocalStorage) GetPulumiHomeDir() (string, error) {
localWorkDir, err := getLocalWorkDirPath()
if err != nil {
return "", fmt.Errorf("unable to get local working directory: %w", err)
}
return filepath.Join(localWorkDir, ".pulumi"), nil
}
func (l *LocalStorage) GetScenarioWorkingDir(scenario *Scenario) string {
return filepath.Join(
l.WorkingDir,
"scenarios",
strings.ToLower(string(scenario.ScenarioParams.Module)),
strings.ToLower(string(scenario.ScenarioParams.Platform)),
strings.ToLower(strings.Join(strings.Split(scenario.ScenarioParams.ID, "-")[2:], "-")),
)
}
func (l *LocalStorage) UpdateScenariosFromGit() (map[string]*Scenario, error) {
metadata, err := l.getCurrentGitMetadata()
today := time.Now().Format("2006-01-02")
if metadata != nil && metadata.Date == today {
logrus.Debug("scenarios downloaded today, skipping git update")
scenarios, err := l.loadScenariosFromWorkingDir()
if err != nil {
return nil, fmt.Errorf("unable to load scenarios from working directory: %w", err)
}
return scenarios, nil
}
tempDir, remoteHash, err := infra.GitDownloadScenariosToTempDir()
defer func() {
if err := os.RemoveAll(tempDir); err != nil {
logrus.WithError(err).Error("unable to remove temporary directory")
}
}()
if err != nil {
return nil, fmt.Errorf("unable to clone git repository: %w", err)
}
if metadata != nil && remoteHash == metadata.CommitHash {
logrus.Debug("remote hash matches local hash, skipping git update, and updating local saved date")
// write the current date to the git metadata file
if err := l.writeGitMetadata(remoteHash, today); err != nil {
return nil, fmt.Errorf("unable to save git metadata: %w", err)
}
scenarios, err := l.loadScenariosFromWorkingDir()
if err != nil {
return nil, fmt.Errorf("unable to load scenarios from working directory: %w", err)
}
return scenarios, nil
}
scenarios, err := l.updateScenariosFromFolder(tempDir)
if err != nil {
return nil, fmt.Errorf("unable to update scenario folder: %w", err)
}
if err := l.writeGitMetadata(remoteHash, today); err != nil {
return nil, fmt.Errorf("unable to save git metadata: %w", err)
}
return scenarios, nil
}
func (l *LocalStorage) LoadScenariosFromWorkingDir() (map[string]*Scenario, error) {
return l.loadScenarios(l.WorkingDir)
}
func (l *LocalStorage) ReadCnappGoatConfig(scenario *Scenario) (map[string]string, error) {
scenarioWorkDir := l.GetScenarioWorkingDir(scenario)
path := filepath.Join(scenarioWorkDir, "Pulumi.yaml")
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read Pulumi.yaml: %w", err)
}
var cnappGoatConfig Scenario
if err = yaml.Unmarshal(data, &cnappGoatConfig); err != nil {
return nil, fmt.Errorf("failed to parse Pulumi.yaml: %w", err)
}
if err := cnappGoatConfig.ScenarioParams.IsValid(); err != nil {
return nil, fmt.Errorf("scenarioParams field is empty for scenario %v", scenario.Name)
}
return cnappGoatConfig.ScenarioParams.Config, nil
}
func (l *LocalStorage) WriteStateToFile(scenario *Scenario, state State) error {
// Create the file path.
filePath := filepath.Join(l.GetScenarioWorkingDir(scenario), stateFile)
// Marshal the struct to YAML.
data, err := yaml.Marshal(&state)
if err != nil {
return fmt.Errorf("failed to marshal state to YAML: %w", err)
}
// Create the file if it doesn't exist.
if !l.fileExists(filePath) {
// Create a new file.
_, err := os.Create(filePath)
if err != nil {
return fmt.Errorf("failed to create state file: %w", err)
}
}
// Write the YAML data to the file.
err = os.WriteFile(filePath, data, 0644)
if err != nil {
return fmt.Errorf("failed to write state to file: %w", err)
}
return nil
}
func (l *LocalStorage) updateScenariosFromFolder(scenariosFullPath string) (map[string]*Scenario, error) {
if !l.fileExists(scenariosFullPath) {
return nil, fmt.Errorf("scenario folder does not exist, cannot perform UpdateScenarioFolder: %s", scenariosFullPath)
}
scenariosFromScenarioDir, err := l.loadScenarios(scenariosFullPath)
if err != nil {
return nil, fmt.Errorf("unable to load scenarios from scenario directory: %w", err)
}
var scenariosFromWorkDir map[string]*Scenario
if l.WorkingDirectoryExists() {
scenariosFromWorkDir, err = l.loadScenarios(l.WorkingDir)
if err != nil {
return nil, fmt.Errorf("unable to load scenarios from working directory: %w", err)
}
} else {
scenariosFromWorkDir = make(map[string]*Scenario)
err = os.MkdirAll(l.WorkingDir, 0755)
}
for _, scenarioFromScenariosDir := range scenariosFromScenarioDir {
exists := false
for _, scenarioFromWorkDir := range scenariosFromWorkDir {
if scenarioFromWorkDir.ScenarioParams.ID == scenarioFromScenariosDir.ScenarioParams.ID {
exists = true
if scenarioFromWorkDir.Hash != scenarioFromScenariosDir.Hash {
err = l.copyScenario(scenarioFromScenariosDir)
if err != nil {
return nil, fmt.Errorf("unable to copy scenario to working directory: %w", err)
}
scenariosFromWorkDir[scenarioFromScenariosDir.ScenarioParams.ID] = scenarioFromScenariosDir
logrus.Infof("scenario %v exists in the working directory but has changed. Updating.", scenarioFromWorkDir.ScenarioParams.ID)
} else {
logrus.Debugf("scenario %v exists in the working directory and has not changed. Skipping.", scenarioFromWorkDir.ScenarioParams.ID)
}
}
}
if !exists {
// if the scenario does not exist in the working directory, copy it over
logrus.Infof("scenario %v does not exist in the working directory. Copying over.", scenarioFromScenariosDir.ScenarioParams.ID)
err = l.copyScenario(scenarioFromScenariosDir)
if err != nil {
return nil, fmt.Errorf("unable to copy scenario to working directory: %w", err)
}
scenariosFromWorkDir[scenarioFromScenariosDir.ScenarioParams.ID] = scenarioFromScenariosDir
}
}
return scenariosFromWorkDir, err
}
func (l *LocalStorage) WorkingDirectoryExists() bool {
// stat the working directory
stat, err := os.Stat(l.WorkingDir)
if err != nil {
return false
}
return stat.IsDir()
}
func (l *LocalStorage) loadScenariosFromWorkingDir() (map[string]*Scenario, error) {
return l.loadScenarios(l.WorkingDir)
}
func (l *LocalStorage) getCurrentGitMetadata() (*gitMetadata, error) {
// check if the git metadata file exists
if !l.fileExists(filepath.Join(l.WorkingDir, gitMetadataFile)) {
return nil, fmt.Errorf("git metadata file does not exist in working directory")
}
data, err := os.ReadFile(filepath.Join(l.WorkingDir, gitMetadataFile))
if err != nil {
return nil, err
}
metadata := gitMetadata{}
if err := yaml.Unmarshal(data, &metadata); err != nil {
return nil, err
}
return &metadata, nil
}
func (l *LocalStorage) writeGitMetadata(hash, date string) error {
metadata := gitMetadata{
CommitHash: hash,
Date: date,
}
data, err := yaml.Marshal(metadata)
if err != nil {
return err
}
return os.WriteFile(filepath.Join(l.WorkingDir, gitMetadataFile), data, 0644)
}
func (l *LocalStorage) copyScenario(scenario *Scenario) error {
if err := copyAllScenariosFromDir(scenario.SrcDir, l.GetScenarioWorkingDir(scenario)); err != nil {
return fmt.Errorf("unable to copy scenario to working directory: %w", err)
}
stateFilePath := filepath.Join(l.GetScenarioWorkingDir(scenario), stateFile)
if !l.fileExists(stateFilePath) {
if err := l.WriteStateToFile(scenario, State{State: NotDeployed}); err != nil {
return fmt.Errorf("unable to write state file to working directory: %w", err)
}
}
return nil
}
func (l *LocalStorage) createScenario(path string) (*Scenario, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read Pulumi.yaml: %w", err)
}
scenario := Scenario{}
if err = yaml.Unmarshal(data, &scenario); err != nil {
return nil, fmt.Errorf("failed to parse Pulumi.yaml: %w", err)
}
if err = scenario.ScenarioParams.IsValid(); err != nil {
return nil, fmt.Errorf("scenarioParams field is invalid for scenario %v: %w", scenario.Name, err)
}
scenario.SrcDir = filepath.Dir(path)
scenarioWorkDir := l.GetScenarioWorkingDir(&scenario)
statePath := filepath.Join(scenarioWorkDir, stateFile)
if !l.fileExists(statePath) {
scenario.State.State = NotDeployed
} else {
data, err = os.ReadFile(statePath)
if err != nil {
return nil, fmt.Errorf("failed to read state.yaml: %w", err)
}
if err = yaml.Unmarshal(data, &scenario.State); err != nil {
return nil, fmt.Errorf("failed to parse state.yaml: %w", err)
}
}
hash, err := hashDirectory(filepath.Dir(path))
if err != nil {
return nil, fmt.Errorf("could not hash directory: %w", err)
}
scenario.Hash = hash
return &scenario, nil
}
func (l *LocalStorage) fileExists(path string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
} else if err != nil {
logrus.WithError(err).Error("unable to check if file exists")
return false
}
return true
}
func (l *LocalStorage) loadScenarios(path string) (map[string]*Scenario, error) {
scenarios := make(map[string]*Scenario)
if err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("error loading scenarios, unable to walk path %s: %w", path, err)
}
if !info.IsDir() && info.Name() == "Pulumi.yaml" {
scenario, err := l.createScenario(path)
if err != nil {
logrus.Errorf("error loading scenarios, unable to create scenario: %v", err)
return nil // because we'd still like to carry on and load the other scenarios
}
scenarios[scenario.ScenarioParams.ID] = scenario
}
return nil
}); err != nil {
return nil, err
}
return scenarios, nil
}
func copyAllScenariosFromDir(srcDir, dstDir string) error {
if err := os.MkdirAll(dstDir, 0755); err != nil {
return err
}
src, err := os.Open(srcDir)
if err != nil {
return err
}
defer func() {
if err := src.Close(); err != nil {
logrus.WithError(err).Error()
}
}()
files, err := src.Readdir(-1)
if err != nil {
return err
}
for _, file := range files {
srcPath := filepath.Join(srcDir, file.Name())
dstPath := filepath.Join(dstDir, file.Name())
if file.IsDir() {
if err := copyAllScenariosFromDir(srcPath, dstPath); err != nil {
return err
}
} else {
if err := copyFile(srcPath, dstPath); err != nil {
return err
}
}
}
return nil
}
func getLocalWorkDirPath() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("unable to get user home directory: %w", err)
}
return filepath.Join(homeDir, ".cnappgoat"), nil
}
func hashDirectory(dirPath string) (string, error) {
var builder strings.Builder
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() || isExcluded(filepath.Base(path)) {
return nil
}
hash, err := hashFile(path)
if err != nil {
return err
}
builder.WriteString(hash)
return nil
})
if err != nil {
return "", fmt.Errorf("error walking the path %v: %w", dirPath, err)
}
hasher := sha256.New()
if _, err := hasher.Write([]byte(builder.String())); err != nil {
return "", fmt.Errorf("error writing to hasher: %w", err)
}
return hex.EncodeToString(hasher.Sum(nil)), nil
}
func hashFile(filepath string) (string, error) {
// check if file exists
if _, err := os.Stat(filepath); os.IsNotExist(err) {
return "", fmt.Errorf("error when hashing the file %s, file doesn't exist: %v", filepath, err)
}
file, err := os.Open(filepath)
if err != nil {
return "", fmt.Errorf("error when hashing the file %s, cannot open file: %v", filepath, err)
}
defer func() {
if err := file.Close(); err != nil {
logrus.WithError(err).Error()
}
}()
hasher := sha256.New()
if _, err := io.Copy(hasher, file); err != nil {
return "", fmt.Errorf("error when hashing the file %s, cannot copy file: %w", filepath, err)
}
return hex.EncodeToString(hasher.Sum(nil)), nil
}
func isExcluded(filename string) bool {
if filename == "state.yaml" {
return true
}
if strings.HasPrefix(filename, "Pulumi.") && strings.HasSuffix(filename, ".yaml") && filename != "Pulumi.yaml" {
return true
}
return false
}
func copyFile(srcPath, dstPath string) error {
srcFile, err := os.Open(srcPath)
if err != nil {
return err
}
defer func() {
if err := srcFile.Close(); err != nil {
logrus.WithError(err).Error()
}
}()
dstFile, err := os.Create(dstPath)
if err != nil {
return err
}
defer func() {
if err := dstFile.Close(); err != nil {
logrus.WithError(err).Error()
}
}()
if _, err := io.Copy(dstFile, srcFile); err != nil {
return err
}
return nil
}