forked from snyk-tech-services/jira-tickets-for-new-vulns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
809 lines (696 loc) · 25.8 KB
/
utils.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"reflect"
"strings"
"time"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)
/*
**
Function setDebug and getDebug
**
*/
func (m *debug) setDebug(b bool) {
m.PrintDebug = b
}
func (m *debug) getDebug() bool {
return m.PrintDebug
}
/*
**
Function Debug
check if flag is set
print debug
**
*/
func (m *debug) Debug(args ...interface{}) {
if m.PrintDebug {
m.Print(args...)
}
}
func (m *debug) Print(args ...interface{}) {
log.Print(args...)
}
/*
**
Function Debugf
check if flag is set
print debug with formatting directive
**
*/
func (m *debug) Debugf(format string, args ...interface{}) {
if m.PrintDebug {
m.Printf(format, args...)
}
}
func (m *debug) Printf(format string, args ...interface{}) {
log.Printf(format, args...)
}
/*
**
Function setOption
set the mandatory flags structure
**
*/
func (Mf *MandatoryFlags) setMandatoryFlags(apiTokenPtr *string, v viper.Viper) {
Mf.orgID = v.GetString("snyk.orgID")
Mf.endpointAPI = v.GetString("snyk.api")
Mf.apiToken = *apiTokenPtr
Mf.jiraProjectID = v.GetString("jira.jiraProjectID")
Mf.jiraProjectKey = v.GetString("jira.jiraProjectKey")
// Checking flag exist
// pflag required function does not work with viper
Mf.checkMandatoryAreSet()
}
/*
**
Function setOption
set the optional flags structure
**
*/
func (Of *optionalFlags) setOptionalFlags(debugPtr bool, dryRunPtr bool, v viper.Viper) {
Of.projectID = v.GetString("snyk.projectID")
Of.projectCriticality = v.GetString("snyk.projectCriticality")
Of.projectEnvironment = v.GetString("snyk.projectEnvironment")
Of.projectLifecycle = v.GetString("snyk.projectLifecycle")
Of.jiraTicketType = v.GetString("jira.jiraTicketType")
Of.severity = v.GetString("snyk.severity")
Of.issueType = v.GetString("snyk.type")
Of.maturityFilterString = v.GetString("snyk.maturityFilter")
Of.assigneeID = v.GetString("jira.assigneeID")
Of.labels = v.GetString("jira.labels")
Of.dueDate = v.GetString("jira.dueDate")
Of.priorityIsSeverity = v.GetBool("jira.priorityIsSeverity")
Of.priorityScoreThreshold = v.GetInt("snyk.priorityScoreThreshold")
Of.debug = debugPtr
Of.dryRun = dryRunPtr
Of.cveInTitle = v.GetBool("jira.cveInTitle")
Of.ifUpgradeAvailableOnly = v.GetBool("snyk.ifUpgradeAvailableOnly")
Of.ifAutoFixableOnly = v.GetBool("snyk.ifAutoFixableOnly")
}
/*
**
Function resetFlag
reset commands line flags
**
*/
func resetFlag() {
pflag.VisitAll(func(f *pflag.Flag) {
pflag.Lookup(f.Name).Value.Set(f.DefValue)
})
}
/*
**
Function setOption
get the arguments
set the flags structures
**
*/
func (opt *flags) setOption(args []string) {
var apiTokenPtr *string
var debugPtr *bool
var dryRunPtr *bool
var configFilePtr *string
// Using viper to bind config file and flags
v := viper.New()
// flags are all setup at the same time so if one is all of them should be enough
fs := pflag.NewFlagSet("", pflag.ContinueOnError)
fs.String("orgID", "", "Your Snyk Organization ID (check under Settings)")
fs.String("projectID", "", "Optional. Your Project ID. Will sync all projects Of your organization if not provided")
fs.String("api", "https://api.snyk.io", "Optional. Your API endpoint for onprem deployments (https://yourdeploymenthostname/api)")
apiTokenPtr = fs.String("token", "", "Your API token")
fs.String("jiraProjectID", "", "Your JIRA projectID (jiraProjectID or jiraProjectKey is required)")
fs.String("jiraProjectKey", "", "Your JIRA projectKey (jiraProjectID or jiraProjectKey is required)")
fs.String("jiraTicketType", "Bug", "Optional. Chosen JIRA ticket type")
fs.String("projectCriticality", "", "Optional. Include only projects whose criticality attribute contains one or more of the specified values.")
fs.String("projectEnvironment", "", "Optional. Include only projects whose environment attribute contains one or more of the specified values.")
fs.String("projectLifecycle", "", "Optional. Include only projects whose lifecycle attribute contains one or more of the specified values.")
fs.String("severity", "low", "Optional. Your severity threshold")
fs.String("maturityFilter", "", "Optional. include only maturity level(s) separated by commas [mature,proof-of-concept,no-known-exploit,no-data]")
fs.String("type", "all", "Optional. Your issue type (all|vuln|license)")
fs.String("assigneeId", "", "Optional. The Jira user accountId to assign issues to")
fs.String("labels", "", "Optional. Jira ticket labels")
fs.String("dueDate", "", "Optional. The built-in Due Date field")
fs.Bool("priorityIsSeverity", false, "Boolean. Use issue severity as priority")
fs.Int("priorityScoreThreshold", 0, "Optional. Your min priority score threshold [INT between 0 and 1000]")
debugPtr = fs.Bool("debug", false, "Optional. Boolean. enable debug mode")
dryRunPtr = fs.Bool("dryRun", false, "Optional. Boolean. Creates a file with all the tickets without open them on jira")
fs.Bool("cveInTitle", false, "Optional. Boolean. Adds the CVEs to the jira ticket title")
fs.Bool("ifUpgradeAvailableOnly", false, "Optional. Boolean. Opens tickets only for upgradable issues")
fs.Bool("ifAutoFixableOnly", false, "Optional. Boolean. Opens tickets for issues that are fixable (no effect when using ifUpgradeAvailableOnly)")
configFilePtr = fs.String("configFile", "", "Optional. Config file path. Use config file to set parameters")
errParse := fs.Parse(args)
if errParse != nil {
log.Println("*** ERROR *** Error parsing command line arguments: ", errParse.Error())
os.Exit(1)
}
// Have to set one by one because the name in the config file doesn't correspond to the flag name
// This can be done at any time
v.BindPFlag("snyk.orgID", fs.Lookup("orgID"))
v.BindPFlag("snyk.api", fs.Lookup("api"))
v.BindPFlag("jira.jiraProjectID", fs.Lookup("jiraProjectID"))
v.BindPFlag("jira.jiraProjectKey", fs.Lookup("jiraProjectKey"))
v.BindPFlag("snyk.projectID", fs.Lookup("projectID"))
v.BindPFlag("snyk.projectCriticality", fs.Lookup("projectCriticality"))
v.BindPFlag("snyk.projectEnvironment", fs.Lookup("projectEnvironment"))
v.BindPFlag("snyk.projectLifecycle", fs.Lookup("projectLifecycle"))
v.BindPFlag("jira.jiraTicketType", fs.Lookup("jiraTicketType"))
v.BindPFlag("snyk.severity", fs.Lookup("severity"))
v.BindPFlag("snyk.type", fs.Lookup("type"))
v.BindPFlag("snyk.maturityFilter", fs.Lookup("maturityFilter"))
v.BindPFlag("jira.assigneeID", fs.Lookup("assigneeId"))
v.BindPFlag("jira.labels", fs.Lookup("labels"))
v.BindPFlag("jira.cveInTitle", fs.Lookup("cveInTitle"))
v.BindPFlag("jira.dueDate", fs.Lookup("dueDate"))
v.BindPFlag("jira.priorityIsSeverity", fs.Lookup("priorityIsSeverity"))
v.BindPFlag("snyk.priorityScoreThreshold", fs.Lookup("priorityScoreThreshold"))
v.BindPFlag("snyk.ifUpgradeAvailableOnly", fs.Lookup("ifUpgradeAvailableOnly"))
v.BindPFlag("snyk.ifAutoFixableOnly", fs.Lookup("ifAutoFixableOnly"))
// Set and parse config file
v.SetConfigName("jira") // config file name without extension
v.SetConfigType("yaml")
if configFilePtr != nil || len(*configFilePtr) > 0 {
v.AddConfigPath(*configFilePtr)
} else {
v.AddConfigPath(".")
}
configFile, configFileLocation := ReadFile(*configFilePtr, true)
if err := v.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
fmt.Println("*** WARN *** Config file is not found or maybe empty at location:", configFileLocation)
} else {
fmt.Println("*** ERROR *** ", err)
}
}
customMandatoryJiraFields := CheckConfigFileFormat(configFile)
opt.customMandatoryJiraFields = customMandatoryJiraFields
// Setting the flags structure
opt.mandatoryFlags.setMandatoryFlags(apiTokenPtr, *v)
opt.optionalFlags.setOptionalFlags(*debugPtr, *dryRunPtr, *v)
// check the flags rules
opt.checkFlags()
}
/*
**
Function checkMandatoryAreSet
exit if the mandatory flags are missing
**
*/
func (flags *MandatoryFlags) checkMandatoryAreSet() {
if len(flags.orgID) == 0 || len(flags.apiToken) == 0 || (len(flags.jiraProjectID) == 0 && len(flags.jiraProjectKey) == 0) {
log.Println("*** ERROR *** Missing required flag(s). Please ensure orgID, token, jiraProjectID or jiraProjectKey are set.")
os.Exit(1)
}
}
/*
**
Function checkFlags
check flags rules
To work properly with jira these needs to be respected:
- set only jiraProjectID or jiraProjectKey, not both
- priorityScoreThreshold must be between 0 and 1000
**
*/
func (flags *flags) checkFlags() {
if flags.mandatoryFlags.jiraProjectID != "" && flags.mandatoryFlags.jiraProjectKey != "" {
log.Fatalf("*** ERROR *** You passed both jiraProjectID and jiraProjectKey in parameters\n Please, Use jiraProjectID OR jiraProjectKey, not both")
}
if flags.optionalFlags.priorityScoreThreshold < 0 || flags.optionalFlags.priorityScoreThreshold > 1000 {
log.Fatalf("*** ERROR *** %d is not a valid score. Must be between 0-1000.", flags.optionalFlags.priorityScoreThreshold)
}
}
/*
**
function CreateLogFile
return filename: string
argument: debug
Check if the file exist if not create it
**
*/
func CreateLogFile(customDebug debug, fileType string) string {
// Get date
date := getDate()
// Set filename
filename := fileType + date + ".json"
// If the file doesn't exist, create it, or append to the file
_, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
// Do not fail the tool if file cannot be created print a warning instead
customDebug.Debug("*** ERROR *** Could not create log file")
customDebug.Debug(err.Error())
}
return filename
}
/*
**
function getDate
return date: string
argument: none
return a string containing date and time
**
*/
func getDate() string {
now := time.Now().Round(0)
y := fmt.Sprint(now.Year()) + "_"
m := fmt.Sprint(int(now.Month())) + "_"
d := fmt.Sprint(now.Day()) + "_"
h := fmt.Sprint(now.Hour()) + "_"
min := fmt.Sprint(now.Minute()) + "_"
s := fmt.Sprint(now.Second())
return y + m + d + h + min + s
}
/*
**
function getDate
return date: string
argument: none
return a string containing date and time
**
*/
func getDateDayOnly() string {
now := time.Now().Round(0)
y := fmt.Sprint(now.Year()) + "_"
m := fmt.Sprint(int(now.Month())) + "_"
d := fmt.Sprint(now.Day()) + "_"
return y + m + d
}
/*
**
function writeLogFile
return date: string
input: map[string]interface{} logFile: details of the ticket to be written in the file
input: string filename: name of the file created in the main function
input: customDebug debug
Write the logFile in the file. Details are append to the file per project ID
**
*/
func writeLogFile(logFile map[string]map[string]interface{}, filename string, customDebug debug) {
// If the file doesn't exist => exit, append to the file otherwise
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
customDebug.Debug("*** ERROR *** Could not open file ", filename)
return
}
file, _ := json.MarshalIndent(logFile, "", "")
if _, err := f.Write(file); err != nil {
customDebug.Debug("*** ERROR *** Could write in file")
return
}
if err := f.Close(); err != nil {
customDebug.Debug("*** ERROR *** Could not close file")
return
}
return
}
func Sprintf2(format string, a ...interface{}) string {
a = append(a, "\r")
return fmt.Sprintf(format, a...)
}
func writeErrorFile(function string, errorText string, customDebug debug) {
errorsInterface := make(map[string]interface{})
// Get filePath
filename, err := FindFile("ErrorsFile")
// Read the file, unMarshallto get a map[]interface{} and append the new error and Marshall to create a json
// ReadFile
jsonErrofile, _ := ReadFile(filename, false)
// unMarshall
err = json.Unmarshal(jsonErrofile, &errorsInterface)
if err != nil {
log.Println("*** ERROR *** Please check the format config file", err)
}
// Add the new error
if errorsInterface[function] != nil {
errorsInterface[function] = Sprintf2(errorText, errorsInterface[function])
} else {
errorsInterface[function] = errorText
}
NewErrorsList, err := json.Marshal(errorsInterface)
if err != nil {
log.Println("*** ERROR *** Please check the format config file, could not extract 'jira' config", err)
}
err = ioutil.WriteFile(filename, NewErrorsList, 0644)
if err != nil {
log.Fatal(err)
}
return
}
/*
**
function IsTestRun
return: none
input: boolean
check is the EXECUTION_ENVIRONMENT env is set
**
*/
func IsTestRun() bool {
return os.Getenv("EXECUTION_ENVIRONMENT") == "test"
}
/*
**
function findCustomJiraMandatoryFlags
return: map[string]interface{} : list of mandatory fields and value associated
input: none
Read the config file and extract the jira fields than the mandatory field inside it
**
*/
func findCustomJiraMandatoryFlags(yamlFile []byte) map[string]interface{} {
config := make(map[interface{}]interface{})
yamlCustomJiraMandatoryField := make(map[interface{}]interface{})
jsonCustomJiraMandatoryField := make(map[string]interface{})
unMarshalledJiraValues := make(map[interface{}]interface{})
err := yaml.Unmarshal(yamlFile, &config)
if err != nil {
log.Println("*** ERROR *** Please check the format config file", err)
}
// extract jira fields
jiraValues := config["jira"]
marshalledJiraValues, err := yaml.Marshal(jiraValues)
if err != nil {
log.Println("*** ERROR *** Please check the format config file, could not extract 'jira' config", err)
}
err = yaml.Unmarshal(marshalledJiraValues, &unMarshalledJiraValues)
if err != nil {
log.Println("*** ERROR *** Please check the format config file, could not extract 'jira' config", err)
}
// extract mandatory fields
customJiraMandatoryField_ := unMarshalledJiraValues["customMandatoryFields"]
marshalCustomJiraMandatoryField, err := yaml.Marshal(customJiraMandatoryField_)
if err != nil {
log.Println("*** ERROR *** Please check the format config file, could not extract 'customMandatoryFields' config", err)
}
err = yaml.Unmarshal(marshalCustomJiraMandatoryField, &yamlCustomJiraMandatoryField)
if err != nil {
log.Println("*** ERROR *** Please check the format config file, could not extract 'customMandatoryFields' config", err)
}
// converting the type, the yaml type is not compatible with the json one
// json doesn't understand map[interface{}]interface{} => it will fail
// when marshalling the ticket in a json format
jsonCustomJiraMandatoryField = convertYamltoJson(yamlCustomJiraMandatoryField)
return jsonCustomJiraMandatoryField
}
/*
**
function convertYamltoJson
input map[interface{}]interface{}, type from unmarshalling yaml
return map[string]interface{} ticket type from unmarshalling json
convert the type we get from yaml to a json one
**
*/
func convertYamltoJson(m map[interface{}]interface{}) map[string]interface{} {
res := map[string]interface{}{}
for k, v := range m {
switch v2 := v.(type) {
case map[interface{}]interface{}:
res[fmt.Sprint(k)] = convertYamltoJson(v2)
default:
res[fmt.Sprint(k)] = v
}
}
return res
}
/*
**
function CheckConfigFileFormat
input path string, path to the config file
return []byte config file
Try to read the yaml file. If this fails the config file is not valid yaml
**
*/
func CheckConfigFileFormat(yamlFile []byte) map[string]interface{} {
// Check that each field in the file are supported by the tool
config := make(map[interface{}]interface{})
err := yaml.Unmarshal(yamlFile, &config)
if err != nil {
log.Println("*** ERROR *** Please check the format config file", err)
}
// extract and check snyk fields
snykValues := config["snyk"]
if !checkSnykValue(snykValues) {
log.Fatal()
}
// extract and check jira fields
jiraValues := config["jira"]
success, customFields := checkJiraValue(jiraValues)
if !success {
log.Fatal()
}
return customFields
}
func checkSnykValue(snykValues interface{}) bool {
isSnykConfigOk := true
unMarshalledSnykValues := make(map[interface{}]interface{})
marshalledSnykValues, err := yaml.Marshal(snykValues)
if err != nil {
log.Println("*** ERROR *** Please check the format config file, could not extract 'snyk' config", err)
}
err = yaml.Unmarshal(marshalledSnykValues, &unMarshalledSnykValues)
if err != nil {
log.Println("*** ERROR *** Please check the format config file, could not extract 'snyk' config", err)
}
unMarshalledSnykValuesJson := convertYamltoJson(unMarshalledSnykValues)
for key, value := range unMarshalledSnykValuesJson {
// first check the key is supported
switch key {
// check the type of the value is valid
case "projectID":
valueType := reflect.TypeOf(value).String()
if valueType != "string" {
log.Printf("*** ERROR *** Please check the format config file, %s is of type %s when it should be a string", key, reflect.TypeOf(value).String())
return false
}
case "api":
valueType := reflect.TypeOf(value).String()
if valueType != "string" {
log.Printf("*** ERROR *** Please check the format config file, %s is of type %s when it should be a string", key, reflect.TypeOf(value).String())
return false
}
case "orgID":
valueType := reflect.TypeOf(value).String()
if valueType != "string" {
log.Printf("*** ERROR *** Please check the format config file, %s is of type %s when it should be a string", key, reflect.TypeOf(value).String())
return false
}
case "projectCriticality":
valueType := reflect.TypeOf(value).String()
if valueType != "string" {
log.Printf("*** ERROR *** Please check the format config file, %s is of type %s when it should be a string", key, reflect.TypeOf(value).String())
return false
}
case "projectLifecycle":
valueType := reflect.TypeOf(value).String()
if valueType != "string" {
log.Printf("*** ERROR *** Please check the format config file, %s is of type %s when it should be a string", key, reflect.TypeOf(value).String())
return false
}
case "projectEnvironment":
valueType := reflect.TypeOf(value).String()
if valueType != "string" {
log.Printf("*** ERROR *** Please check the format config file, %s is of type %s when it should be a string", key, reflect.TypeOf(value).String())
return false
}
case "severity":
valueType := reflect.TypeOf(value).String()
if valueType != "string" {
log.Printf("*** ERROR *** Please check the format config file, %s is of type %s when it should be a string", key, reflect.TypeOf(value).String())
return false
}
case "type":
valueType := reflect.TypeOf(value).String()
if valueType != "string" {
log.Printf("*** ERROR *** Please check the format config file, %s is of type %s when it should be a string", key, reflect.TypeOf(value).String())
return false
}
case "maturityFilter":
valueType := reflect.TypeOf(value).String()
if valueType != "string" {
log.Printf("*** ERROR *** Please check the format config file, %s is of type %s when it should be a string", key, reflect.TypeOf(value).String())
return false
}
case "priorityScoreThreshold":
valueType := reflect.TypeOf(value).String()
if valueType != "int" {
log.Printf("*** ERROR *** Please check the format config file, %s is of type %s when it should be an integer", key, reflect.TypeOf(value).String())
return false
}
case "ifUpgradeAvailableOnly":
valueType := reflect.TypeOf(value).String()
if valueType != "bool" {
log.Printf("*** ERROR *** Please check the format config file, %s is of type %s when it should be a boolean", key, reflect.TypeOf(value).String())
return false
}
case "ifAutoFixableOnly":
valueType := reflect.TypeOf(value).String()
if valueType != "bool" {
log.Printf("*** ERROR *** Please check the format config file, %s is of type %s when it should be a boolean", key, reflect.TypeOf(value).String())
return false
}
default:
log.Printf("*** ERROR *** Please check the format config file, the snyk key %s is not supported by this tool", key)
return false
}
}
return isSnykConfigOk
}
func checkJiraValue(JiraValues interface{}) (bool, map[string]interface{}) {
isJiraConfigOk := true
yamlCustomJiraMandatoryField := make(map[interface{}]interface{})
unMarshalledJiraValues := make(map[interface{}]interface{})
customMandatoryJiraFields := make(map[string]interface{})
marshalledJiraValues, err := yaml.Marshal(JiraValues)
if err != nil {
log.Println("*** ERROR *** Please check the format config file, could not extract 'snyk' config", err)
}
err = yaml.Unmarshal(marshalledJiraValues, &unMarshalledJiraValues)
if err != nil {
log.Println("*** ERROR *** Please check the format config file, could not extract 'snyk' config", err)
}
unMarshalledJiraValuesJson := convertYamltoJson(unMarshalledJiraValues)
for key, value := range unMarshalledJiraValuesJson {
// first check the key is supported
switch key {
// check the type of the value is valid
case "jiraProjectID":
valueType := reflect.TypeOf(value).String()
if valueType != "int" {
log.Printf("*** ERROR *** Please check the format config file, %s is of type %s when it should be an integer", key, reflect.TypeOf(value).String())
return false, nil
}
case "jiraProjectKey":
valueType := reflect.TypeOf(value).String()
if valueType != "string" {
log.Printf("*** ERROR *** Please check the format config file, %s is of type %s when it should be a string", key, reflect.TypeOf(value).String())
return false, nil
}
case "jiraTicketType":
valueType := reflect.TypeOf(value).String()
if valueType != "string" {
log.Printf("*** ERROR *** Please check the format config file, %s is of type %s when it should be a string", key, reflect.TypeOf(value).String())
return false, nil
}
case "assigneeId":
valueType := reflect.TypeOf(value).String()
if valueType != "int" {
log.Printf("*** ERROR *** Please check the format config file, %s is of type %s when it should be an integer", key, reflect.TypeOf(value).String())
return false, nil
}
case "labels":
valueType := reflect.TypeOf(value).String()
if valueType != "string" {
log.Printf("*** ERROR *** Please check the format config file, %s is of type %s when it should be a string", key, reflect.TypeOf(value).String())
return false, nil
}
case "dueDate":
valueType := reflect.TypeOf(value).String()
if valueType != "string" {
log.Printf("*** ERROR *** Please check the format config file, %s is of type %s when it should be a string", key, reflect.TypeOf(value).String())
return false, nil
}
case "priorityIsSeverity":
valueType := reflect.TypeOf(value).String()
if valueType != "bool" {
log.Printf("*** ERROR *** Please check the format config file, %s is of type %s when it should be a boolean", key, reflect.TypeOf(value).String())
return false, nil
}
case "cveInTitle":
valueType := reflect.TypeOf(value).String()
if valueType != "bool" {
log.Printf("*** ERROR *** Please check the format config file, %s is of type %s when it should be a boolean", key, reflect.TypeOf(value).String())
return false, nil
}
case "customMandatoryFields":
customJiraMandatoryField_ := unMarshalledJiraValues["customMandatoryFields"]
isJiraConfigOk_, customMandatoryJiraFields_ := checkMandatoryField(customJiraMandatoryField_, yamlCustomJiraMandatoryField)
isJiraConfigOk = isJiraConfigOk_
customMandatoryJiraFields = customMandatoryJiraFields_
default:
log.Printf("*** ERROR *** Please check the format config file, the jira key %s is not supported by this tool", key)
return false, nil
}
}
return isJiraConfigOk, customMandatoryJiraFields
}
func checkMandatoryField(customJiraMandatoryField_ interface{}, yamlCustomJiraMandatoryField map[interface{}]interface{}) (bool, map[string]interface{}) {
jsonCustomJiraMandatoryField := make(map[string]interface{})
fields := make(map[string]interface{})
marshalCustomJiraMandatoryField, err := yaml.Marshal(customJiraMandatoryField_)
if err != nil {
log.Println("*** ERROR *** Please check the format config file, could not extract 'customMandatoryFields' config", err)
}
err = yaml.Unmarshal(marshalCustomJiraMandatoryField, &yamlCustomJiraMandatoryField)
if err != nil {
log.Println("*** ERROR *** Please check the format config file, could not extract 'customMandatoryFields' config", err)
}
// converting the type, the yaml type is not compatible with the json one
// json doesn't understand map[interface{}]interface{} => it will fail
// when marshalling the ticket in a json format
jsonCustomJiraMandatoryField = convertYamltoJson(yamlCustomJiraMandatoryField)
for i, s := range jsonCustomJiraMandatoryField {
value, ok := s.(map[string]interface{})
if ok {
v, ok := value["value"].(string)
if ok {
if strings.HasPrefix(v, JiraPrefix) {
s, err = supportJiraFormats(v, debug{PrintDebug: false})
if err != nil {
log.Printf("*** ERROR *** Error while extracting the mandatory Jira fields configuration\n %s", err)
return false, nil
}
}
}
} else {
log.Println(fmt.Sprintf("*** ERROR *** Expected mandatory Jira fields configuration to be in format map[string]interface{}, received type: %T for field %s ", s, i))
return false, nil
}
fields[i] = s
}
return true, fields
}
/*
**
function ReadFile
input path string, path to the config file
return []byte config file
Try to read the yaml file. If this fails the config file is not valid yaml
**
*/
func ReadFile(path string, config bool) ([]byte, string) {
if len(path) == 0 {
path = "."
}
var err error
filePath := path + "/jira.yaml"
if !config {
filePath, err = FindFile("ErrorsFile")
}
file, err := ioutil.ReadFile(filePath)
if err != nil {
fmt.Printf("*** ERROR *** Could not read file at location: %s. Please ensure the file exists and is formatted correctly.\nERROR: %s\n", filePath, err.Error())
}
return file, filePath
}
func FindFile(fileName string) (string, error) {
// list all file in the directory
fileInfo, err := ioutil.ReadDir(".")
if err != nil {
log.Fatal()
}
// Look for the one starting with listOfTicketCreated or ErrorsFile
for _, file := range fileInfo {
if !file.IsDir() {
if strings.HasPrefix(file.Name(), fileName) {
filePath := "./" + file.Name()
return filePath, nil
}
}
}
errorMessage := fmt.Sprintf("Failure, Could not find File %s", fileName)
return "", errors.New(errorMessage)
}