@@ -3,12 +3,14 @@ package app
3
3
import (
4
4
"fmt"
5
5
"os"
6
+ "path"
6
7
"regexp"
7
8
"strings"
8
9
"sync"
9
10
10
11
lla "github.com/emirpasic/gods/lists/arraylist"
11
12
lls "github.com/emirpasic/gods/stacks/linkedliststack"
13
+ "github.com/golang-module/carbon/v2"
12
14
"github.com/stackup-app/stackup/lib/cache"
13
15
"github.com/stackup-app/stackup/lib/checksums"
14
16
"github.com/stackup-app/stackup/lib/downloader"
@@ -37,18 +39,22 @@ type StackupWorkflow struct {
37
39
}
38
40
39
41
type WorkflowInclude struct {
40
- Url string `yaml:"url"`
41
- Headers []string `yaml:"headers"`
42
- File string `yaml:"file"`
43
- ChecksumUrl string `yaml:"checksum-url"`
44
- VerifyChecksum * bool `yaml:"verify,omitempty"`
45
- AccessKey string `yaml:"access-key"`
46
- SecretKey string `yaml:"secret-key"`
47
- Secure bool `yaml:"secure"`
48
- ChecksumIsValid * bool
49
- ValidationState string
50
- Contents string
51
- Hash string
42
+ Url string `yaml:"url"`
43
+ Headers []string `yaml:"headers"`
44
+ File string `yaml:"file"`
45
+ ChecksumUrl string `yaml:"checksum-url"`
46
+ VerifyChecksum * bool `yaml:"verify,omitempty"`
47
+ AccessKey string `yaml:"access-key"`
48
+ SecretKey string `yaml:"secret-key"`
49
+ Secure bool `yaml:"secure"`
50
+ ChecksumIsValid * bool
51
+ ValidationState string
52
+ Contents string
53
+ Hash string
54
+ FoundChecksum string
55
+ HashAlgorithm string
56
+ ChecksumValidated bool
57
+ FromCache bool
52
58
//Workflow *StackupWorkflow
53
59
}
54
60
@@ -137,63 +143,41 @@ func (wi *WorkflowInclude) getChecksumFromContents(contents string) string {
137
143
return strings .TrimSpace (contents )
138
144
}
139
145
140
- func (wi * WorkflowInclude ) ValidateChecksum (contents string ) (bool , error ) {
146
+ func (wi * WorkflowInclude ) ValidateChecksum (contents string ) (bool , string , error ) {
141
147
if wi .VerifyChecksum != nil && * wi .VerifyChecksum == false {
142
- return true , nil
148
+ return true , "" , nil
143
149
}
144
150
145
151
checksumUrls := []string {
152
+ wi .FullUrlPath () + "/checksums.txt" ,
146
153
wi .FullUrlPath () + "/checksums.sha256.txt" ,
147
154
wi .FullUrlPath () + "/checksums.sha512.txt" ,
155
+ wi .FullUrlPath () + "sha256sum" ,
156
+ wi .FullUrlPath () + "sha512sum" ,
148
157
wi .FullUrl () + ".sha256" ,
149
158
wi .FullUrl () + ".sha512" ,
150
159
}
151
160
152
- algorithm := ""
153
- storedChecksum := ""
154
- checksumContents := ""
155
-
156
161
for _ , url := range checksumUrls {
157
-
158
- if ! App .Workflow .Cache .IsExpired (url ) {
159
- wi .ChecksumUrl = url
160
- checksumContents = App .Workflow .Cache .Get (url )
161
- // fmt.Printf("using cached checksum file %s\n", url)
162
- break
163
- }
164
-
165
162
checksumContents , err := utils .GetUrlContents (url )
166
163
if err != nil {
167
164
continue
168
165
}
169
166
170
167
wi .ChecksumUrl = url
168
+ wi .FoundChecksum = wi .getChecksumFromContents (checksumContents )
169
+ wi .HashAlgorithm = wi .GetChecksumAlgorithm ()
171
170
172
- if checksumContents != "" {
173
- // fmt.Printf("using checksum file %s\n", wi.ChecksumUrl)
174
- App .Workflow .Cache .Set (url , checksumContents , 3 )
175
- // fmt.Printf("using non-cached checksum file %s\n", url)
176
- break
177
- }
178
- }
179
-
180
- if checksumContents != "" {
181
- storedChecksum = wi .getChecksumFromContents (checksumContents )
182
-
183
- // wi.ChecksumUrl = hashUrl
184
- // fmt.Println("checksum url: " + wi.ChecksumUrl)
185
- algorithm = wi .GetChecksumAlgorithm ()
171
+ break
186
172
}
187
173
188
- // algorithm = "sha256"
189
-
190
- if algorithm == "unknown" || algorithm == "" {
191
- return false , fmt .Errorf ("unable to find valid checksum file for %s" , wi .DisplayUrl ())
174
+ if wi .HashAlgorithm == "unknown" || wi .HashAlgorithm == "" {
175
+ return false , "" , fmt .Errorf ("unable to find valid checksum file for %s" , wi .DisplayUrl ())
192
176
}
193
177
194
178
var hash string
195
179
196
- switch algorithm {
180
+ switch wi . HashAlgorithm {
197
181
case "sha256" :
198
182
hash = checksums .CalculateSha256Hash (contents )
199
183
break
@@ -202,16 +186,16 @@ func (wi *WorkflowInclude) ValidateChecksum(contents string) (bool, error) {
202
186
break
203
187
default :
204
188
wi .SetChecksumIsValid (false )
205
- return false , fmt .Errorf ("unsupported algorithm: %s" , algorithm )
189
+ return false , "" , fmt .Errorf ("unsupported algorithm: %s" , wi . HashAlgorithm )
206
190
}
207
191
208
- if ! strings .EqualFold (hash , storedChecksum ) {
192
+ if ! strings .EqualFold (hash , wi . FoundChecksum ) {
209
193
wi .SetChecksumIsValid (false )
210
- return false , nil
194
+ return false , "" , nil
211
195
}
212
196
213
197
wi .SetChecksumIsValid (true )
214
- return true , nil
198
+ return true , hash , nil
215
199
}
216
200
217
201
func (wi * WorkflowInclude ) IsLocalFile () bool {
@@ -277,6 +261,37 @@ func (wi *WorkflowInclude) GetChecksumAlgorithm() string {
277
261
return "sha512"
278
262
}
279
263
264
+ if strings .EqualFold (path .Base (wi .ChecksumUrl ), "sha256sum" ) {
265
+ return "sha256"
266
+ }
267
+
268
+ if strings .EqualFold (path .Base (wi .ChecksumUrl ), "sha512sum" ) {
269
+ return "sha512"
270
+ }
271
+
272
+ // check for md4 hash length:
273
+ if len (wi .FoundChecksum ) == 16 {
274
+ return "md4"
275
+ }
276
+ if len (wi .FoundChecksum ) == 32 {
277
+ return "md5"
278
+ }
279
+ if len (wi .FoundChecksum ) == 40 {
280
+ return "sha1"
281
+ }
282
+ if len (wi .FoundChecksum ) == 48 {
283
+ return "sha224"
284
+ }
285
+ if len (wi .FoundChecksum ) == 64 {
286
+ return "sha256"
287
+ }
288
+ if len (wi .FoundChecksum ) == 96 {
289
+ return "sha384"
290
+ }
291
+ if len (wi .FoundChecksum ) == 128 {
292
+ return "sha512"
293
+ }
294
+
280
295
return "unknown"
281
296
}
282
297
@@ -413,12 +428,6 @@ func (workflow *StackupWorkflow) RemoveTasks(uuidsToRemove []string) {
413
428
workflow .Tasks = newTasks
414
429
}
415
430
416
- // func (workflow *StackupWorkflow) ProcessIncludes() {
417
- // for _, include := range workflow.Includes {
418
- // workflow.ProcessInclude(include)
419
- // }
420
- // }
421
-
422
431
func (workflow * StackupWorkflow ) ProcessIncludes () {
423
432
var wg sync.WaitGroup
424
433
for _ , include := range workflow .Includes {
@@ -438,14 +447,16 @@ func (workflow *StackupWorkflow) ProcessInclude(include *WorkflowInclude) bool {
438
447
439
448
var err error
440
449
441
- if workflow .Cache .Has (include .DisplayName ()) && ! workflow .Cache .IsExpired (include .DisplayName ()) {
442
- include .Contents = workflow .Cache .Get (include .DisplayName ())
443
- include .Hash = workflow .Cache .GetHash (include .DisplayName ())
444
- // fmt.Println("loaded from cache")
450
+ data , found := workflow .Cache .Get (include .DisplayName ())
451
+ include .FromCache = found
452
+
453
+ if found {
454
+ include .Hash = data .Hash
455
+ include .HashAlgorithm = data .Hash
456
+ include .Contents = data .Value
445
457
}
446
458
447
- if ! workflow .Cache .Has (include .DisplayName ()) || workflow .Cache .IsExpired (include .DisplayName ()) {
448
- // fmt.Println("not loaded from cache")
459
+ if ! found || data .IsExpired () {
449
460
if include .IsLocalFile () {
450
461
include .Contents , err = utils .GetFileContents (include .Filename ())
451
462
} else if include .IsRemoteUrl () {
@@ -455,58 +466,61 @@ func (workflow *StackupWorkflow) ProcessInclude(include *WorkflowInclude) bool {
455
466
include .SecretKey = os .ExpandEnv (include .SecretKey )
456
467
include .Contents = downloader .ReadS3FileContents (include .FullUrl (), include .AccessKey , include .SecretKey , include .Secure )
457
468
} else {
469
+ fmt .Printf ("unknown include type: %s\n " , include .DisplayName ())
458
470
return false
459
471
}
460
472
461
473
include .Hash = checksums .CalculateSha256Hash (include .Contents )
462
- workflow . Cache . Set ( include . DisplayName (), include . Contents , 3 )
463
- }
474
+ expires := carbon . Now (). AddMinutes ( App . Workflow . Settings . Cache . TtlMinutes )
475
+ now := carbon . Now ()
464
476
465
- // fmt.Printf("value: %v\n", workflow.Cache.Get(include.DisplayName()))
466
- // fmt.Printf("expires: %v\n", workflow.Cache.GetExpiresAt(include.DisplayName()))
467
- // fmt.Printf("hash: %v\n", workflow.Cache.GetHash(include.DisplayName()))
477
+ item := cache .CreateCacheEntry (
478
+ include .DisplayName (),
479
+ include .Contents ,
480
+ & expires ,
481
+ include .Hash ,
482
+ include .HashAlgorithm ,
483
+ & now ,
484
+ )
485
+
486
+ workflow .Cache .Set (include .DisplayName (), item , App .Workflow .Settings .Cache .TtlMinutes )
487
+ }
468
488
469
- // fmt.Printf("include: %s\n", include.DisplayName())
470
- // fmt.Printf("include: %s\n", include.FullUrl())
471
- // fmt.Printf("contents: %s\n", include.Contents)
489
+ include .ChecksumValidated , include .FoundChecksum , _ = include .ValidateChecksum (include .Contents )
472
490
473
491
if err != nil {
474
492
fmt .Println (err )
475
493
return false
476
494
}
477
495
478
- include .ValidationState = "checksum not validated "
496
+ include .ValidationState = "verification skipped "
479
497
480
498
if include .IsRemoteUrl () {
481
499
if * include .VerifyChecksum == true || include .VerifyChecksum == nil {
482
- // support.StatusMessage("Validating checksum for remote include: "+include.DisplayUrl(), false)
483
- validated , _ := include .ValidateChecksum (include .Contents )
484
-
485
- if include .ChecksumIsValid != nil && * include .ChecksumIsValid == true {
486
- include .ValidationState = "checksum validated"
500
+ if include .ChecksumValidated {
501
+ include .ValidationState = "verified"
487
502
}
488
503
489
- if include .ChecksumIsValid != nil && * include .ChecksumIsValid == false {
490
- include .ValidationState = "checksum mismatch "
504
+ if ! include .ChecksumValidated && include .FoundChecksum != "" {
505
+ include .ValidationState = "verification failed "
491
506
}
492
507
493
508
// if err != nil {
494
509
// fmt.Println(err)
495
510
// return false
496
511
// }
497
512
498
- if ! validated {
499
- if App .Workflow .Settings .ExitOnChecksumMismatch {
500
- support .FailureMessageWithXMark ("Exiting due to checksum mismatch." )
501
- App .exitApp ()
502
- return false
503
- }
513
+ if ! include .ChecksumValidated && App .Workflow .Settings .ExitOnChecksumMismatch {
514
+ support .FailureMessageWithXMark ("Exiting due to checksum mismatch." )
515
+ App .exitApp ()
516
+ return false
504
517
}
505
518
}
506
519
}
507
520
508
- template := & IncludedTemplate {}
509
- err = yaml .Unmarshal ([]byte (include .Contents ), template )
521
+ // if strings.HasPrefix(include.Contents, "template:") {
522
+ var template IncludedTemplate
523
+ err = yaml .Unmarshal ([]byte (include .Contents ), & template )
510
524
511
525
if err != nil {
512
526
fmt .Println (err )
@@ -536,7 +550,13 @@ func (workflow *StackupWorkflow) ProcessInclude(include *WorkflowInclude) bool {
536
550
App .Workflow .Tasks = append (App .Workflow .Tasks , t )
537
551
}
538
552
539
- support .SuccessMessageWithCheck ("Included file (" + include .ValidationState + "): " + include .DisplayName ())
553
+ cachedText := ""
554
+
555
+ if include .FromCache {
556
+ cachedText = ", cached"
557
+ }
558
+
559
+ support .SuccessMessageWithCheck ("Included file (" + include .ValidationState + cachedText + "): " + include .DisplayName ())
540
560
541
561
return true
542
562
}
0 commit comments