forked from sonatype-nexus-community/nancy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
395 lines (348 loc) · 11.4 KB
/
main.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
//
// Copyright 2018-present Sonatype Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package main
import (
"errors"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
"github.com/sirupsen/logrus"
. "github.com/sonatype-nexus-community/nancy/logger"
"github.com/sonatype-nexus-community/nancy/types"
"github.com/common-nighthawk/go-figure"
"github.com/golang/dep"
"github.com/sonatype-nexus-community/nancy/audit"
"github.com/sonatype-nexus-community/nancy/buildversion"
"github.com/sonatype-nexus-community/nancy/configuration"
"github.com/sonatype-nexus-community/nancy/customerrors"
"github.com/sonatype-nexus-community/nancy/iq"
"github.com/sonatype-nexus-community/nancy/ossindex"
"github.com/sonatype-nexus-community/nancy/packages"
"github.com/sonatype-nexus-community/nancy/parse"
)
func main() {
LogLady.Info("Starting Nancy")
var err error
if len(os.Args) > 1 && os.Args[1] == "iq" {
err = doIq(os.Args[2:])
} else if len(os.Args) > 1 && os.Args[1] == "config" {
err = doConfig(os.Stdin)
} else {
err = doOssi(os.Args[1:])
}
if err != nil {
if exiterr, ok := err.(customerrors.ErrorExit); ok {
os.Exit(exiterr.ExitCode)
} else {
// really don't expect this
LogLady.WithError(err).Error("unexpected error in main")
os.Exit(4)
}
}
}
func doOssi(ossiArgs []string) (err error) {
LogLady.Info("Nancy parsing config for OSS Index")
ossIndexConfig, err := configuration.Parse(ossiArgs)
if err != nil {
flag.Usage()
err = customerrors.ErrorExit{Err: err, ExitCode: 1}
return
}
if err = processConfig(ossIndexConfig); err != nil {
LogLady.Info("Nancy finished parsing config for OSS Index, vulnerability found")
return
}
LogLady.Info("Nancy finished parsing config for OSS Index")
return
}
func doConfig(stdin io.Reader) (err error) {
LogLady.Info("Nancy setting config via the command line")
if err = configuration.GetConfigFromCommandLine(stdin); err != nil {
err = customerrors.NewErrorExitPrintHelp(err, "Unable to set config for Nancy")
}
return
}
func doIq(iqArgs []string) (err error) {
LogLady.Info("Nancy parsing config for IQ")
config, err := configuration.ParseIQ(iqArgs)
if err != nil {
flag.Usage()
err = customerrors.ErrorExit{Err: err, ExitCode: 2} // flag.Usage used to exit with code 2
return
}
LogLady.WithField("config", config).Info("Obtained IQ config")
if err = processIQConfig(config); err != nil {
LogLady.Info("Nancy finished parsing config for IQ, vulnerability found")
return
}
LogLady.Info("Nancy finished parsing config for IQ")
return
}
func printHeader(print bool) {
if print {
LogLady.Info("Attempting to print header")
figure.NewFigure("Nancy", "larry3d", true).Print()
figure.NewFigure("By Sonatype & Friends", "pepper", true).Print()
LogLady.WithFields(logrus.Fields{
"build_time": buildversion.BuildTime,
"build_commit": buildversion.BuildCommit,
"version": buildversion.BuildVersion,
"operating_system": runtime.GOOS,
"architecture": runtime.GOARCH,
}).Info("Printing Nancy version")
fmt.Println("Nancy version: " + buildversion.BuildVersion)
LogLady.Info("Finished printing header")
}
}
func processConfig(config configuration.Configuration) (err error) {
if config.Help {
LogLady.Info("Printing usage and exiting clean")
flag.Usage()
err = customerrors.ErrorExit{ExitCode: 0}
return
}
if config.Version {
LogLady.WithFields(logrus.Fields{
"build_time": buildversion.BuildTime,
"build_commit": buildversion.BuildCommit,
"version": buildversion.BuildVersion,
"operating_system": runtime.GOOS,
"architecture": runtime.GOARCH,
}).Info("Printing version information and exiting clean")
fmt.Println(buildversion.BuildVersion)
_, _ = fmt.Printf("build time: %s\n", buildversion.BuildTime)
_, _ = fmt.Printf("build commit: %s\n", buildversion.BuildCommit)
err = customerrors.ErrorExit{ExitCode: 0}
return
}
if config.Info {
LogLady.Level = logrus.InfoLevel
}
if config.Debug {
LogLady.Level = logrus.DebugLevel
}
if config.Trace {
LogLady.Level = logrus.TraceLevel
}
if config.CleanCache {
LogLady.Info("Attempting to clean cache")
if err = ossindex.RemoveCacheDirectory(); err != nil {
LogLady.WithField("error", err).Error("Error cleaning cache")
fmt.Printf("ERROR: cleaning cache: %v\n", err)
err = customerrors.ErrorExit{Err: err, ExitCode: 1}
return
}
LogLady.Info("Cache cleaned")
return
}
printHeader(!config.Quiet && reflect.TypeOf(config.Formatter).String() == "*audit.AuditLogTextFormatter")
if config.UseStdIn {
LogLady.Info("Parsing config for StdIn")
if err = doStdInAndParse(config); err != nil {
return
}
}
if !config.UseStdIn {
LogLady.Info("Parsing config for file based scan")
if err = doCheckExistenceAndParse(config); err != nil {
return
}
}
return
}
func processIQConfig(config configuration.IqConfiguration) (err error) {
// TODO: a lot of this code is a duplication of the OSS Index config, probably should extract some of it
if config.Help {
LogLady.Info("Printing usage and exiting clean")
flag.Usage()
err = customerrors.ErrorExit{ExitCode: 0}
return
}
if config.Version {
LogLady.WithFields(logrus.Fields{
"build_time": buildversion.BuildTime,
"build_commit": buildversion.BuildCommit,
"version": buildversion.BuildVersion,
}).Info("Printing version information and exiting clean")
fmt.Println(buildversion.BuildVersion)
_, _ = fmt.Printf("build time: %s\n", buildversion.BuildTime)
_, _ = fmt.Printf("build commit: %s\n", buildversion.BuildCommit)
err = customerrors.ErrorExit{ExitCode: 0}
return
}
if config.Info {
LogLady.Level = logrus.InfoLevel
}
if config.Debug {
LogLady.Level = logrus.DebugLevel
}
if config.Trace {
LogLady.Level = logrus.TraceLevel
}
if config.Application == "" {
LogLady.Info("No application specified, printing usage and exiting with error")
flag.Usage()
err = customerrors.NewErrorExitPrintHelp(fmt.Errorf("no IQ application id specified"), "Missing IQ application ID")
return
}
printHeader(true)
LogLady.Info("Parsing IQ config for StdIn")
if err = doStdInAndParseForIQ(config); err != nil {
return
}
return
}
func doStdInAndParse(config configuration.Configuration) (err error) {
LogLady.Info("Beginning StdIn parse for OSS Index")
if err = checkStdIn(); err != nil {
return
}
LogLady.Info("Instantiating go.mod package")
mod := packages.Mod{}
LogLady.Info("Beginning to parse StdIn")
mod.ProjectList, err = parse.GoListAgnostic(os.Stdin)
if err != nil {
LogLady.Error(err)
return
}
LogLady.WithFields(logrus.Fields{
"projectList": mod.ProjectList,
}).Debug("Obtained project list")
var purls = mod.ExtractPurlsFromManifest()
LogLady.WithFields(logrus.Fields{
"purls": purls,
}).Debug("Extracted purls")
LogLady.Info("Auditing purls with OSS Index")
err = checkOSSIndex(purls, nil, config)
return
}
func checkStdIn() (err error) {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
LogLady.Info("StdIn is valid")
} else {
LogLady.Error("StdIn is invalid, either empty or another reason")
flag.Usage()
err = customerrors.ErrorExit{ExitCode: 1}
return
}
return
}
func doStdInAndParseForIQ(config configuration.IqConfiguration) (err error) {
LogLady.Debug("Beginning StdIn parse for IQ")
if err = checkStdIn(); err != nil {
return
}
LogLady.Info("Instantiating go.mod package")
mod := packages.Mod{}
LogLady.Info("Beginning to parse StdIn")
mod.ProjectList, err = parse.GoListAgnostic(os.Stdin)
if err != nil {
LogLady.Error(err)
return
}
LogLady.WithFields(logrus.Fields{
"projectList": mod.ProjectList,
}).Debug("Obtained project list")
var purls = mod.ExtractPurlsFromManifest()
LogLady.WithFields(logrus.Fields{
"purls": purls,
}).Debug("Extracted purls")
LogLady.Info("Auditing purls with IQ Server")
err = auditWithIQServer(purls, config.Application, config)
return
}
func doCheckExistenceAndParse(config configuration.Configuration) error {
switch {
case strings.Contains(config.Path, "Gopkg.lock"):
workingDir := filepath.Dir(config.Path)
if workingDir == "." {
workingDir, _ = os.Getwd()
}
getenv := os.Getenv("GOPATH")
ctx := dep.Ctx{
WorkingDir: workingDir,
GOPATHs: []string{getenv},
}
project, err := ctx.LoadProject()
if err != nil {
return customerrors.NewErrorExitPrintHelp(err, fmt.Sprintf("could not read lock at path %s", config.Path))
}
if project.Lock == nil {
return customerrors.NewErrorExitPrintHelp(errors.New("dep failed to parse lock file and returned nil"), "nancy could not continue due to dep failure")
}
purls, invalidPurls := packages.ExtractPurlsUsingDep(project)
return checkOSSIndex(purls, invalidPurls, config)
case strings.Contains(config.Path, "go.sum"):
mod := packages.Mod{}
mod.GoSumPath = config.Path
manifestExists, err := mod.CheckExistenceOfManifest()
if err != nil {
return err
}
if manifestExists {
mod.ProjectList, _ = parse.GoSum(config.Path)
var purls = mod.ExtractPurlsFromManifest()
return checkOSSIndex(purls, nil, config)
}
default:
return customerrors.ErrorExit{ExitCode: 3}
}
return nil
}
func checkOSSIndex(purls []string, invalidpurls []string, config configuration.Configuration) error {
var packageCount = len(purls)
coordinates, err := ossindex.AuditPackagesWithOSSIndex(purls, &config)
if err != nil {
return customerrors.NewErrorExitPrintHelp(err, "Error auditing packages")
}
var invalidCoordinates []types.Coordinate
for _, invalidpurl := range invalidpurls {
invalidCoordinates = append(invalidCoordinates, types.Coordinate{Coordinates: invalidpurl, InvalidSemVer: true})
}
if count := audit.LogResults(config.Formatter, packageCount, coordinates, invalidCoordinates, config.CveList.Cves); count > 0 {
return customerrors.ErrorExit{ExitCode: count}
}
return nil
}
func auditWithIQServer(purls []string, applicationID string, config configuration.IqConfiguration) error {
LogLady.Debug("Sending purls to be Audited by IQ Server")
res, err := iq.AuditPackages(purls, applicationID, config)
if err != nil {
return customerrors.NewErrorExitPrintHelp(err, "Uh oh! There was an error with your request to Nexus IQ Server")
}
fmt.Println()
if res.IsError {
LogLady.WithField("res", res).Error("An error occurred with the request to IQ Server")
return customerrors.NewErrorExitPrintHelp(errors.New(res.ErrorMessage), "Uh oh! There was an error with your request to Nexus IQ Server")
}
if res.PolicyAction != "Failure" {
LogLady.WithField("res", res).Debug("Successful in communicating with IQ Server")
fmt.Println("Wonderbar! No policy violations reported for this audit!")
fmt.Println("Report URL: ", res.ReportHTMLURL)
return nil
} else {
LogLady.WithField("res", res).Debug("Successful in communicating with IQ Server")
fmt.Println("Hi, Nancy here, you have some policy violations to clean up!")
fmt.Println("Report URL: ", res.ReportHTMLURL)
return customerrors.ErrorExit{ExitCode: 1}
}
}