/
runner.go
174 lines (140 loc) · 3.96 KB
/
runner.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
package runner
import (
"fmt"
"github.com/Invoca/tenable-scan-launcher/pkg/aws"
"github.com/Invoca/tenable-scan-launcher/pkg/config"
"github.com/Invoca/tenable-scan-launcher/pkg/gcloud"
"github.com/Invoca/tenable-scan-launcher/pkg/tenable"
"github.com/Invoca/tenable-scan-launcher/pkg/wrapper"
log "github.com/sirupsen/logrus"
)
type Runner struct {
ec2Svc wrapper.CloudWrapper
gcloud wrapper.CloudWrapper
slackSvc wrapper.SlackSvc
tenable wrapper.Tenable
includeGCloud bool
includeAWS bool
generateReport bool
}
func (r *Runner) SetupRunner(config *config.BaseConfig) error {
var err error
r.includeAWS = config.IncludeAWS
r.includeGCloud = config.IncludeGCloud
if config.TenableConfig == nil {
return fmt.Errorf("SetupRunner: TenableConfig in config cannot be nil")
}
//if config.SlackConfig == nil {
// return fmt.Errorf("SetupRunner: SlackConfig in config cannot be nil ")
//}
//r.slackSvc, err = slack.New(*config)
if r.includeAWS {
ec2Svc := &aws.AWSEc2{}
err := ec2Svc.Setup(config)
if err != nil {
return fmt.Errorf("SetupRunner: Error setting up AWS")
}
r.ec2Svc = ec2Svc
}
if r.includeGCloud {
if config.GCloudConfig == nil {
return fmt.Errorf("SetupRunner: GCloudConfig in config cannot be nil")
}
r.gcloud = &gcloud.GCloud{}
err := r.gcloud.Setup(config)
if err != nil {
return fmt.Errorf("SetupRunner: Error setting up GCloud")
}
}
tenableClient, err := tenable.SetupTenable(config.TenableConfig)
if err != nil {
return fmt.Errorf("SetupRunner: Error creating tenable client")
}
r.tenable = tenableClient
r.generateReport = config.TenableConfig.GenerateReport
return nil
}
func (r *Runner) Run() error {
log.Debug("Run")
err := r.getIPs()
if err != nil {
return fmt.Errorf("Run: Error getting ips %s", err)
}
log.Debug("IPs have been gathered from sources. Launching scan.")
err = r.tenable.LaunchScan()
if err != nil {
return fmt.Errorf("Run: Error launching scan %s", err)
}
log.Debug("Scan launched. Waiting for scan to complete.")
/**
err = r.tenable.WaitForScanToComplete()
if err != nil {
return fmt.Errorf("Run: Error Waiting For Scan To Complete %s", err)
}
log.Debug("Scan complete.")
log.Debug("Fetching Critical alerts from Tenable Dashboard")
alerts, err := r.tenable.GetVulnerabilities()
if err != nil {
return fmt.Errorf("Run: Error Fetching alerts from Tenable Dashboard %s", err)
}
if alerts.TotalVulnerabilityCount > 0 {
log.Debug("Posting to Slack")
err = r.slackSvc.PrintAlerts(*alerts)
if err != nil {
return fmt.Errorf("Run: Error posting to slack %s", err)
}
}
if r.generateReport {
err = r.tenable.StartExport()
if err != nil {
return fmt.Errorf("Run: Error Starting Scan %s", err)
}
log.Debug("Export Started. Waiting for file to be ready.")
err = r.tenable.WaitForExport()
if err != nil {
return fmt.Errorf("Run: Error Waiting For Export %s", err)
}
log.Debug("Starting file download")
err = r.tenable.DownloadExport()
if err != nil {
return fmt.Errorf("Run: Error Downloading Export %s", err)
}
log.Debug("File successfully downloaded")
}
**/
log.Debug("Run Finished")
return nil
}
func (r *Runner) getIPs() error {
log.Debug("getIPs")
var ips []string
if r.includeGCloud {
log.Debug("Gathering Google Cloud IPs")
gcloudIPs, err := r.gcloud.GatherIPs()
log.Debug(err)
if err != nil {
return fmt.Errorf("getIPs: Error retrieving GCloud IPs %s", err)
}
if len(gcloudIPs) == 0 {
log.Debug("No GCloud IPs found")
}
ips = append(ips, gcloudIPs...)
}
if r.includeAWS {
log.Debug("Gathering AWS IPs")
awsIPs, err := r.ec2Svc.GatherIPs()
if err != nil {
return fmt.Errorf("getIPs: Error retrieving AWS IPs %s", err)
}
if len(awsIPs) == 0 {
log.Debug("No AWS IPs found")
}
ips = append(ips, awsIPs...)
}
err := r.tenable.SetTargets(ips)
if err != nil {
return fmt.Errorf("getIPs: Error setting Tenable targets")
}
log.Debug("\n\nALL:", ips)
return nil
}