-
Notifications
You must be signed in to change notification settings - Fork 5
/
ci.go
853 lines (763 loc) · 26.1 KB
/
ci.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
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
package ci
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
trivymodels "github.com/fairwindsops/insights-plugins/plugins/trivy/pkg/models"
"github.com/hashicorp/go-multierror"
"github.com/samber/lo"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"github.com/fairwindsops/insights-plugins/plugins/ci/pkg/commands"
"github.com/fairwindsops/insights-plugins/plugins/ci/pkg/models"
"github.com/fairwindsops/insights-plugins/plugins/ci/pkg/util"
)
const configFileName = "fairwinds-insights.yaml"
const maxLinesForPrint = 8
const filesModifiedFileName = "files_modified"
var podSpecFields = []string{"jobTemplate", "spec", "template"}
var containerSpecFields = []string{"containers", "initContainers"}
var ErrExitCode = errors.New("ExitCode is set")
type formFile struct {
field string
filename string
location string
}
type CIScan struct {
autoScan bool
token string
baseFolder string // . or /app/repository
repoBaseFolder string // . or /app/repository/{repoName}
configFolder string
config *models.Configuration
}
type insightsReportConfig struct {
EnabledOnAutoDiscovery *bool
}
type insightsReportsConfig struct {
AutoScan map[string]insightsReportConfig
}
// Create a new CI instance based on flag cloneRepo
func NewCIScan(cloneRepo bool, token string) (*CIScan, error) {
baseFolder, repoBaseFolder, config, err := setupConfiguration(cloneRepo)
if err != nil {
return nil, fmt.Errorf("could not get configuration: %v", err)
}
configFolder := config.Options.TempFolder + "/configuration/"
err = os.MkdirAll(configFolder, 0755)
if err != nil {
return nil, fmt.Errorf("Could not make directory %s: %v", configFolder, err)
}
ci := CIScan{
autoScan: cloneRepo,
token: token,
repoBaseFolder: repoBaseFolder,
baseFolder: baseFolder,
configFolder: configFolder,
config: config,
}
logrus.Infof("Reports config is opa: %v, polaris: %v, pluto: %v, trivy: %v, tfsec: %v", ci.OPAEnabled(), ci.PolarisEnabled(), ci.PlutoEnabled(), ci.TrivyEnabled(), ci.TerraformEnabled())
return &ci, nil
}
// Close deletes all temporary folders created.
func (ci *CIScan) Close() {
os.RemoveAll(ci.config.Options.TempFolder)
os.RemoveAll(ci.config.Images.FolderName)
}
// getAllResources scans a folder of yaml files and returns all of the images and resources used.
func (ci *CIScan) getAllResources() ([]trivymodels.Image, []models.Resource, error) {
var images []trivymodels.Image
var resources []models.Resource
var errors *multierror.Error
err := filepath.Walk(ci.configFolder, func(path string, info os.FileInfo, err error) error {
if !strings.HasSuffix(info.Name(), ".yaml") && !strings.HasSuffix(info.Name(), ".yml") {
return nil
}
displayFilename, helmName, err := ci.getDisplayFilenameAndHelmName(path)
if err != nil {
errors = multierror.Append(errors, fmt.Errorf("error getting displayFilename and helmName for file %s: %v", path, err))
return nil
}
fileHandler, err := os.Open(path)
if err != nil {
errors = multierror.Append(errors, fmt.Errorf("error opening file %s: %v", path, err))
return nil
}
decoder := yaml.NewDecoder(fileHandler)
for {
// yaml.Node has access to the comments
// This allows us to get at the Filename comments that Helm leaves
yamlNodeOriginal := yaml.Node{}
err = decoder.Decode(&yamlNodeOriginal)
if err != nil {
if err != io.EOF {
errors = multierror.Append(errors, fmt.Errorf("error decoding file %s: %v", path, err))
return nil
}
break // EOF
}
yamlNode := map[string]interface{}{}
err = yamlNodeOriginal.Decode(&yamlNode)
if err != nil {
errors = multierror.Append(errors, fmt.Errorf("error decoding document %s: %v", path, err))
return nil
}
kind, ok := yamlNode["kind"].(string)
if !ok {
continue
}
if kind == "list" {
nodes := yamlNode["items"].([]interface{})
for _, node := range nodes {
obj, ok := node.(map[string]interface{})
if !ok {
logrus.Warningf("Found a malformed YAML list item at %s", path+info.Name())
}
_, kind, name, namespace := util.ExtractMetadata(obj)
if kind == "" {
logrus.Warningf("Found a YAML list item without kind at %s", path+info.Name())
continue
}
if name == "" {
logrus.Warningf("Found a YAML list item without metadata.name at %s", path+info.Name())
continue
}
newImages, containers := processYamlNode(node.(map[string]interface{}))
images = append(images, newImages...)
resources = append(resources, models.Resource{
Kind: kind,
Name: name,
Namespace: namespace,
Filename: displayFilename,
HelmName: helmName,
Containers: lo.Map(containers, func(c models.Container, _ int) string {
return c.Name
}),
})
}
} else {
_, kind, name, namespace := util.ExtractMetadata(yamlNode)
if kind == "" {
logrus.Warningf("Found a YAML file without kind at %s", path+info.Name())
continue
}
if name == "" {
logrus.Warningf("Found a YAML file without metadata.name at %s", path+info.Name())
continue
}
newImages, containers := processYamlNode(yamlNode)
images = append(images, newImages...)
resources = append(resources, models.Resource{
Kind: kind,
Name: name,
Namespace: namespace,
Filename: displayFilename,
HelmName: helmName,
Containers: lo.Map(containers, func(c models.Container, _ int) string {
return c.Name
}),
})
}
}
return nil
})
if err != nil {
errors = multierror.Append(errors, fmt.Errorf("error walking directory %s: %v", ci.configFolder, err))
return nil, nil, errors
}
// multiple images with the same name may belong to different owners, so we need to deduplicate them
dedupedImages := dedupImages(images)
return dedupedImages, resources, errors.ErrorOrNil()
}
func dedupImages(images []trivymodels.Image) []trivymodels.Image {
imageOwnersMap := map[string][]trivymodels.Resource{}
for _, img := range images {
if _, ok := imageOwnersMap[img.Name]; !ok {
imageOwnersMap[img.Name] = []trivymodels.Resource{} // initialize
}
imageOwnersMap[img.Name] = append(imageOwnersMap[img.Name], img.Owners...)
}
imagesMap := lo.KeyBy(images, func(img trivymodels.Image) string { return img.Name })
dedupedImages := []trivymodels.Image{}
for k, i := range imagesMap {
dedupedImages = append(dedupedImages, trivymodels.Image{
ID: i.ID,
PullRef: i.PullRef,
Name: i.Name,
Owners: imageOwnersMap[k],
RecommendationOnly: i.RecommendationOnly,
})
}
return dedupedImages
}
func (ci *CIScan) getDisplayFilenameAndHelmName(path string) (string, string, error) {
var displayFilename, helmName string
displayFilename, err := filepath.Rel(ci.configFolder, path)
if err != nil {
return "", "", fmt.Errorf("cannot be made relative to basepath: %v", err)
}
for _, helm := range ci.config.Manifests.Helm {
if strings.HasPrefix(displayFilename, helm.Name+"/") {
parts := strings.Split(displayFilename, "/")
parts = parts[2:]
displayFilename = strings.Join(parts, "/")
if helm.IsLocal() {
displayFilename = filepath.Join(helm.Path, displayFilename)
} else if helm.IsRemote() {
if helm.IsFluxFile() {
displayFilename = filepath.Join(helm.FluxFile, displayFilename)
} else {
displayFilename = filepath.Join(helm.Chart, displayFilename)
}
}
helmName = helm.Name
}
}
return displayFilename, helmName, err
}
func processYamlNode(yamlNode map[string]interface{}) ([]trivymodels.Image, []models.Container) {
_, kind, name, namespace := util.ExtractMetadata(yamlNode)
if kind == "" || name == "" {
return nil, nil
}
podSpec := getPodSpec(yamlNode)
images := getImages(podSpec.(map[string]interface{}))
return lo.Map(images, func(c models.Container, _ int) trivymodels.Image {
return trivymodels.Image{
Name: c.Image,
Owners: []trivymodels.Resource{
{
Kind: kind,
Container: c.Name,
Name: name,
Namespace: namespace,
},
},
}
}), images
}
// getPodSpec looks inside arbitrary YAML for a PodSpec
func getPodSpec(yaml map[string]interface{}) interface{} {
for _, child := range podSpecFields {
if childYaml, ok := yaml[child]; ok {
return getPodSpec(childYaml.(map[string]interface{}))
}
}
return yaml
}
func getImages(podSpec map[string]interface{}) []models.Container {
images := make([]models.Container, 0)
for _, field := range containerSpecFields {
containerField, ok := podSpec[field]
if !ok {
continue
}
containers, ok := containerField.([]interface{})
if !ok {
logrus.Warningf("Found a podSpec with no containers: %v", podSpec)
continue
}
for _, container := range containers {
containerMap := container.(map[string]interface{})
image, _ := containerMap["image"].(string)
name, _ := containerMap["name"].(string)
newContainer := models.Container{
Image: image,
Name: name,
}
images = append(images, newContainer)
}
}
return images
}
// sendResults sends the results to Insights
func (ci *CIScan) sendResults(reports []*models.ReportInfo) (*models.ScanResults, error) {
var b bytes.Buffer
formFiles := []formFile{{
field: "fairwinds-insights",
filename: configFileName,
location: filepath.Join(ci.repoBaseFolder, configFileName),
}}
for _, report := range reports {
formFiles = append(formFiles, formFile{
field: report.Report,
filename: report.Filename,
location: filepath.Join(ci.config.Options.TempFolder, report.Filename),
})
}
w := multipart.NewWriter(&b)
for _, file := range formFiles {
fw, err := w.CreateFormFile(file.field, file.filename)
if err != nil {
logrus.Fatalf("Unable to create form for %s: %v", file.field, err)
}
r, err := os.Open(file.location)
if err != nil {
logrus.Fatalf("Unable to open file for %s: %v", file.field, err)
}
defer r.Close()
_, err = io.Copy(fw, r)
if err != nil {
logrus.Fatalf("Unable to write contents for %s: %v", file.field, err)
}
}
repoDetails, err := getGitInfo(commands.ExecInDir, ci.config.Options.CIRunner, ci.repoBaseFolder, ci.config.Options.RepositoryName, ci.config.Options.BaseBranch)
if err != nil {
logrus.Fatalf("Unable to get git details: %v", err)
}
if len(repoDetails.filesModified) > 0 {
fw, err := w.CreateFormFile(filesModifiedFileName, filesModifiedFileName)
if err != nil {
logrus.Fatalf("Unable to create form for %s: %v", "files_modified", err)
}
_, err = fw.Write([]byte(strings.Join(repoDetails.filesModified, "\n")))
if err != nil {
logrus.Fatalf("Unable to write file for %s: %v", "files_modified", err)
}
}
w.Close()
url := fmt.Sprintf("%s/v0/organizations/%s/ci/scan-results", ci.config.Options.Hostname, ci.config.Options.Organization)
req, err := http.NewRequest("POST", url, &b)
if err != nil {
logrus.Warn("Unable to create Request")
return nil, err
}
req.Header.Set("Content-Type", w.FormDataContentType())
req.Header.Set("Authorization", "Bearer "+ci.token)
req.Header.Set("X-Commit-Hash", repoDetails.currentHash)
req.Header.Set("X-Commit-Message", repoDetails.commitMessage)
req.Header.Set("X-Branch-Name", repoDetails.branch)
req.Header.Set("X-Master-Hash", repoDetails.masterHash)
req.Header.Set("X-Base-Branch", ci.config.Options.BaseBranch)
req.Header.Set("X-Origin", repoDetails.origin)
req.Header.Set("X-Repository-Name", repoDetails.repoName)
req.Header.Set("X-New-AI-Threshold", strconv.Itoa(ci.config.Options.NewActionItemThreshold))
req.Header.Set("X-Severity-Threshold", ci.config.Options.SeverityThreshold)
req.Header.Set("X-Script-Version", os.Getenv("SCRIPT_VERSION"))
req.Header.Set("X-Image-Version", os.Getenv("IMAGE_VERSION"))
req.Header.Set("X-CI-Runner", string(ci.config.Options.CIRunner))
for _, report := range reports {
req.Header.Set("X-Fairwinds-Report-Version-"+report.Report, strings.TrimSuffix(report.Version, "\n"))
}
client := http.DefaultClient
if os.Getenv("SKIP_SSL_VALIDATION") == "true" {
transport := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
client = &http.Client{Transport: transport}
}
resp, err := client.Do(req)
if err != nil {
logrus.Warn("Unable to Post results to Insights")
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
logrus.Warn("Unable to read results")
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Invalid status code: %d - %s", resp.StatusCode, string(body))
}
var results models.ScanResults
err = json.Unmarshal(body, &results)
if err != nil {
logrus.Warn("Unable to unmarshal results")
return nil, err
}
return &results, nil
}
// all modifications to config struct must be done in this context
func setupConfiguration(cloneRepo bool) (string, string, *models.Configuration, error) {
if cloneRepo {
return getConfigurationForCloneRepo()
}
return getDefaultConfiguration()
}
func getDefaultConfiguration() (string, string, *models.Configuration, error) {
// i.e.: ./fairwinds-insights.yaml
config, err := readConfigurationFromFile("./" + configFileName)
if err != nil {
if !os.IsNotExist(errors.Unwrap(err)) {
return "", "", nil, err
}
logrus.Infof("Could not detect fairwinds-insights.yaml file... auto-detecting...")
config, err = ConfigFileAutoDetection("")
if err != nil {
return "", "", nil, err
}
err := createFileFromConfig("", configFileName, *config)
if err != nil {
return "", "", nil, err
}
}
err = config.SetDefaults()
if err != nil {
return "", "", nil, err
}
config.SetPathDefaults()
logrus.Infof("Running with configuration %#v", config)
err = config.CheckForErrors()
if err != nil {
return "", "", nil, fmt.Errorf("Error parsing fairwinds-insights.yaml: %v", err)
}
return filepath.Base(""), filepath.Base(""), config, nil
}
func getConfigurationForCloneRepo() (string, string, *models.Configuration, error) {
repoFullName := strings.TrimSpace(os.Getenv("REPOSITORY_NAME"))
if repoFullName == "" {
return "", "", nil, errors.New("REPOSITORY_NAME environment variable not set")
}
branch := strings.TrimSpace(os.Getenv("BRANCH_NAME"))
if branch == "" {
return "", "", nil, errors.New("BRANCH environment variable not set")
}
if strings.TrimSpace(os.Getenv("IMAGE_VERSION")) == "" {
return "", "", nil, errors.New("IMAGE_VERSION environment variable not set")
}
var basePath string = os.Getenv("CI_BASE_PATH")
if basePath != "" {
logrus.Infof("using basePath of %q from environment CI_BASE_PATH", basePath)
} else {
basePath = filepath.Join("/app", "repository")
}
_, repoName := util.GetRepoDetails(repoFullName)
baseRepoPath := filepath.Join(basePath, repoName)
err := os.RemoveAll(baseRepoPath)
if err != nil {
return "", "", nil, fmt.Errorf("unable to delete existing directory: %v", err)
}
url := fmt.Sprintf("https://@github.com/%s", repoFullName)
accessToken := strings.TrimSpace(os.Getenv("GITHUB_ACCESS_TOKEN"))
if accessToken != "" {
// access token is required for private repos
url = fmt.Sprintf("https://x-access-token:%s@github.com/%s", accessToken, repoFullName)
}
_, err = commands.ExecInDir(basePath, exec.Command("git", "clone", "--branch", branch, url), "cloning github repository")
if err != nil {
return "", "", nil, fmt.Errorf("unable to clone repository: %v", err)
}
// i.e.: /app/repository/blog/fairwinds-insights.yaml
configFilePath := filepath.Join(basePath, repoName, configFileName)
config, err := readConfigurationFromFile(configFilePath)
if err != nil {
if !os.IsNotExist(errors.Unwrap(err)) {
return "", "", nil, err
}
logrus.Infof("Could not detect fairwinds-insights.yaml file... auto-detecting...")
config, err = ConfigFileAutoDetection(baseRepoPath)
if err != nil {
return "", "", nil, err
}
// this is how we support enabling/disabling reports on auto-discovery (when no fairwinds-insights.yaml file is found)
if strings.TrimSpace(os.Getenv("REPORTS_CONFIG")) != "" {
err := unmarshalAndOverrideConfig(config)
if err != nil {
return "", "", nil, err
}
}
err := createFileFromConfig(baseRepoPath, configFileName, *config)
if err != nil {
return "", "", nil, err
}
}
err = config.SetDefaults()
if err != nil {
return "", "", nil, err
}
err = config.SetMountedPathDefaults(basePath, baseRepoPath)
if err != nil {
return "", "", nil, fmt.Errorf("Could not set set path defaults correctly: %v", err)
}
err = config.CheckForErrors()
if err != nil {
return "", "", nil, fmt.Errorf("Error parsing fairwinds-insights.yaml: %v", err)
}
config.Options.SetExitCode = false // always false when running on auto-scan mode
_, err = commands.ExecInDir(baseRepoPath, exec.Command("git", "update-ref", "refs/heads/"+config.Options.BaseBranch, "refs/remotes/origin/"+config.Options.BaseBranch), "updating branch ref")
if err != nil {
return "", "", nil, fmt.Errorf("unable to update ref for branch %s: %v", config.Options.BaseBranch, err)
}
return filepath.Join(baseRepoPath, "../"), baseRepoPath, config, nil
}
func unmarshalAndOverrideConfig(config *models.Configuration) error {
var insightsReportConfig insightsReportsConfig
err := json.Unmarshal([]byte(os.Getenv("REPORTS_CONFIG")), &insightsReportConfig)
if err != nil {
return fmt.Errorf("unable to parse auto-scan reports config: %v", err)
}
overrideReportsEnabled(config, insightsReportConfig)
return nil
}
func overrideReportsEnabled(cfg *models.Configuration, reportConfig insightsReportsConfig) {
if rCfg, ok := reportConfig.AutoScan["opa"]; ok {
cfg.Reports.OPA.Enabled = rCfg.EnabledOnAutoDiscovery
}
if rCfg, ok := reportConfig.AutoScan["polaris"]; ok {
cfg.Reports.Polaris.Enabled = rCfg.EnabledOnAutoDiscovery
}
if rCfg, ok := reportConfig.AutoScan["pluto"]; ok {
cfg.Reports.Pluto.Enabled = rCfg.EnabledOnAutoDiscovery
}
if rCfg, ok := reportConfig.AutoScan["trivy"]; ok {
cfg.Reports.Trivy.Enabled = rCfg.EnabledOnAutoDiscovery
}
if rCfg, ok := reportConfig.AutoScan["tfsec"]; ok {
cfg.Reports.TFSec.Enabled = rCfg.EnabledOnAutoDiscovery
}
}
func readConfigurationFromFile(configFilePath string) (*models.Configuration, error) {
configHandler, err := os.Open(configFilePath)
if err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("Please add fairwinds-insights.yaml to the base of your repository: %w", err)
} else {
return nil, fmt.Errorf("Could not open fairwinds-insights.yaml: %v", err)
}
}
return readConfigurationFromReader(configHandler)
}
func readConfigurationFromReader(configHandler io.Reader) (*models.Configuration, error) {
configContents, err := io.ReadAll(configHandler)
if err != nil {
return nil, fmt.Errorf("Could not read fairwinds-insights.yaml: %v", err)
}
config := models.Configuration{}
err = yaml.Unmarshal(configContents, &config)
if err != nil {
return nil, fmt.Errorf("Could not parse fairwinds-insights.yaml: %v", err)
}
return &config, nil
}
func (ci *CIScan) ProcessRepository() ([]*models.ReportInfo, error) {
var scanErrorsReportProperties models.ScanErrorsReportProperties // errors encountered during scan
err := ci.ProcessHelmTemplates()
if err != nil {
scanErrorsReportProperties.AddScanErrorsReportResultFromError(err)
}
err = ci.CopyYaml()
if err != nil {
scanErrorsReportProperties.AddScanErrorsReportResultFromError(models.ScanErrorsReportResult{
ErrorMessage: err.Error(),
ErrorContext: "copying yaml files to configuration directory",
Kind: "InternalOperation",
ResourceName: "CopyYaml",
Remediation: "Examine the CI logs to determine why yaml files failed to copy to the configuration directory. Perhaps memory or disk space is low.",
})
}
// Scan YAML, find all images/kind/etc
manifestImages, resources, err := ci.getAllResources()
if err != nil {
scanErrorsReportProperties.AddScanErrorsReportResultFromError(models.ScanErrorsReportResult{
ErrorMessage: err.Error(),
ErrorContext: "getting all resources from manifest files",
Kind: "InternalOperation",
ResourceName: "GetAllResources",
})
}
var reports []*models.ReportInfo
// Scan manifests with Polaris
if ci.PolarisEnabled() {
polarisReport, err := ci.GetPolarisReport()
if err != nil {
scanErrorsReportProperties.AddScanErrorsReportResultFromError(models.ScanErrorsReportResult{
ErrorMessage: err.Error(),
ErrorContext: "running polaris",
Kind: "InternalOperation",
ResourceName: "GetPolarisReport",
})
}
if polarisReport != nil {
reports = append(reports, polarisReport)
}
}
workloadReport, err := ci.GetWorkloadReport(resources)
if err != nil {
return nil, fmt.Errorf("unable to get workloads report, which is depended on by other reports: %v", err)
}
if workloadReport != nil {
reports = append(reports, workloadReport)
}
if ci.TrivyEnabled() {
manifestImagesToScan := manifestImages
if ci.SkipTrivyManifests() {
manifestImagesToScan = []trivymodels.Image{}
}
dockerImages := getDockerImages(ci.config.Images.Docker, ci.autoScan)
trivyReport, err := ci.GetTrivyReport(dockerImages, manifestImagesToScan)
if err != nil {
scanErrorsReportProperties.AddScanErrorsReportResultFromError(err, models.ScanErrorsReportResult{
ErrorContext: "downloading images and running trivy",
Kind: "InternalOperation",
ResourceName: "GetTrivyReport",
})
}
if trivyReport != nil {
reports = append(reports, trivyReport)
}
}
if ci.OPAEnabled() {
opaReport, err := ci.ProcessOPA(context.Background())
if err != nil {
scanErrorsReportProperties.AddScanErrorsReportResultFromError(err, models.ScanErrorsReportResult{
ErrorContext: "processing OPA policies",
Kind: "InternalOperation",
ResourceName: "ProcessOPA",
})
}
if opaReport != nil {
reports = append(reports, opaReport)
}
}
if ci.PlutoEnabled() {
plutoReport, err := ci.GetPlutoReport()
if err != nil {
scanErrorsReportProperties.AddScanErrorsReportResultFromError(err, models.ScanErrorsReportResult{
ErrorContext: "running pluto",
Kind: "InternalOperation",
ResourceName: "GetPlutoReport",
})
}
if plutoReport != nil {
reports = append(reports, plutoReport)
}
}
if ci.TerraformEnabled() {
terraformReports, err := ci.ProcessTerraformPaths()
if err != nil {
scanErrorsReportProperties.AddScanErrorsReportResultFromError(err, models.ScanErrorsReportResult{
ErrorContext: "processing terraform",
Kind: "InternalOperation",
ResourceName: "ProcessTerraformPaths",
})
}
if terraformReports != nil {
logrus.Debugln("the Terraform report contains results")
reports = append(reports, terraformReports)
}
}
if len(scanErrorsReportProperties.Items) > 0 {
printScanErrors(scanErrorsReportProperties)
scanErrorsReport, err := ci.processScanErrorsReportProperties(scanErrorsReportProperties)
if err != nil {
return nil, fmt.Errorf("unable to process scan errors report items: %w", err)
}
reports = append(reports, scanErrorsReport)
}
return reports, nil
}
func hasEnvVar(s string) bool {
return strings.Contains(s, "$")
}
func getDockerImages(dockerImagesStr []string, autoScan bool) []trivymodels.DockerImage {
dockerImages := []trivymodels.DockerImage{}
for _, n := range dockerImagesStr {
if hasEnvVar(n) {
if autoScan {
// only log if on auto-scan, because when running on CI runners - the CI script already have downloaded these images
logrus.Warnf("image %s skipped, env. variable substitution is not supported on in-container image download", n)
}
continue
}
dockerImages = append(dockerImages, trivymodels.DockerImage{Name: n})
}
return dockerImages
}
func (ci *CIScan) SendAndPrintResults(reports []*models.ReportInfo) error {
ci.printScannedFilesInfo()
results, err := ci.sendResults(reports)
if err != nil {
return fmt.Errorf("Error while sending results back to %s: %v", ci.config.Options.Hostname, err)
}
fmt.Printf("%d new Action Items:\n", len(results.NewActionItems))
printActionItems(results.NewActionItems)
fmt.Printf("%d fixed Action Items:\n", len(results.FixedActionItems))
printActionItems(results.FixedActionItems)
if ci.JUnitEnabled() {
err = ci.SaveJUnitFile(*results)
if err != nil {
return fmt.Errorf("Could not save jUnit results: %v", err)
}
}
if !results.Pass {
fmt.Printf("\n\nFairwinds Insights checks failed:\n%v\n\nVisit %s/orgs/%s/repositories for more information\n\n", err, ci.config.Options.Hostname, ci.config.Options.Organization)
if ci.config.Options.SetExitCode {
return ErrExitCode
}
} else {
fmt.Println("\n\nFairwinds Insights checks passed.")
}
return nil
}
func (ci *CIScan) printScannedFilesInfo() {
s := len(ci.config.Manifests.YamlPaths)
if s > 0 {
fmt.Println("Kubernetes files scanned:")
for i, p := range ci.config.Manifests.YamlPaths {
fmt.Printf("\t[%d/%d] - %s\n", i+1, s, p)
}
}
s = len(ci.config.Manifests.Helm)
if s > 0 {
fmt.Println("Helm charts scanned:")
for i, h := range ci.config.Manifests.Helm {
fmt.Printf("\t[%d/%d] - %s/%s\n", i+1, s, h.Path, h.Name)
}
}
s = len(ci.config.Terraform.Paths)
if s > 0 {
fmt.Println("Terraform files scanned:")
for i, p := range ci.config.Terraform.Paths {
fmt.Printf("\t[%d/%d] - %s\n", i+1, s, p)
}
}
}
func printActionItems(ais []models.ActionItem) {
for _, ai := range ais {
fmt.Println(ai.GetReadableTitle())
printMultilineString("Description", ai.Description)
printMultilineString("Remediation", ai.Remediation)
fmt.Println()
}
}
func printMultilineString(title, str string) {
fmt.Println(" " + title + ":")
if str == "" {
str = "Unspecified"
}
lines := strings.Split(str, "\n")
for idx, line := range lines {
fmt.Println(" " + line)
if idx == maxLinesForPrint {
fmt.Println(" [truncated]")
break
}
}
}
func createFileFromConfig(path, filename string, cfg models.Configuration) error {
bytes, err := yaml.Marshal(cfg)
if err != nil {
return err
}
err = os.WriteFile(filepath.Join(path, filename), bytes, 0644)
if err != nil {
return err
}
return nil
}
func printScanErrors(scanErrorReport models.ScanErrorsReportProperties) {
fmt.Println("Scan Errors(these are soft errors that do not prevent the scan from completing):")
for _, r := range scanErrorReport.Items {
fmt.Println("\t- " + createErrorItemMessage(r))
}
}