This repository has been archived by the owner on May 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
atomic_scanner.go
147 lines (124 loc) · 4.3 KB
/
atomic_scanner.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"encoding/json"
)
const (
APP_VERSION = "0.1"
scan_input_path = "scanin"
scan_output_path = "scanout"
)
type input struct {
apiUrl string
username string
password string
}
var in input
func init() {
flag.StringVar(&in.apiUrl, "a", "REQUIRED", "api url for example https://saleshub.blackducksoftware.com")
flag.StringVar(&in.username, "u", "REQUIRED", "the username that can be used to access rest endpoints")
flag.StringVar(&in.password, "p", "REQUIRED", "the password")
}
// The flag package provides a default help printer via -h switch
var versionFlag *bool = flag.Bool("v", false, "Print the version number.")
func main() {
flag.Parse() // Scan the arguments list
if *versionFlag {
fmt.Println("Version:", APP_VERSION)
}
config, ok := GetConfig()
if !ok {
// create config and exit
fmt.Printf("Config has to be created.")
CreateConfig()
os.Exit(1)
}
// check if the input path exists
if _, err := os.Stat(scan_input_path); os.IsNotExist(err) {
fmt.Errorf("ERROR input directory %s doesn't exist\n%s\n\n", scan_input_path, err)
fmt.Printf("ERROR input directory %s doesn't exist\n%s\n\n", scan_input_path, err)
os.Exit(1)
} else if _, err := os.Stat(scan_output_path); os.IsNotExist(err) {
fmt.Printf("ERROR ouput directory %s doesn't exist\n%s\n\n", scan_output_path, err)
os.Exit(1)
} else {
// check if the directory is empty
files, err := ioutil.ReadDir(scan_input_path)
if err != nil {
fmt.Printf("ERROR reading dir %s\n%s\n\n", scan_input_path, err)
os.Exit(1)
}
if len(files) == 0 {
fmt.Printf("WARNING dir %s is empty nothing to scan\n", scan_input_path)
os.Exit(1)
}
}
//ok all is fine start scan
ScanImage(scan_input_path, config)
// now the scan is finished find the BOM report
p, _ := filepath.Abs(scan_input_path)
p = strings.Replace(p, "\\", "/", -1)
h, _ := os.Hostname()
searchString := fmt.Sprintf("%s/%s", h, p)
searchString = strings.Replace(searchString, "//", "/", -1)
hub := HubServer{Config: config}
if ok := hub.login(); !ok {
fmt.Printf("ERROR login into the hub.\n")
os.Exit(1)
}
// check if the scan was completed
codelocations := hub.findCodeLocations(searchString)
if len(codelocations.Items) != 1 {
fmt.Printf("ERROR no code locations for search string : \n%s\n\n", searchString)
os.Exit(1)
}
for strings.Compare(codelocations.Items[0].Status, "COMPLETE") != 0 {
time.Sleep( 1 * time.Minute )
codelocations = hub.findCodeLocations(searchString)
fmt.Printf("Scan status : %s\n", codelocations.Items[0].Status)
}
// check if the BOM creation is completed
bomRows := hub.getBomRows(codelocations.Items[0].Version.Id, 1)
count := bomRows.TotalCount
for {
time.Sleep(1 * time.Minute)
bomRows = hub.getBomRows(codelocations.Items[0].Version.Id, 1)
if count == bomRows.TotalCount {
break
}
count = bomRows.TotalCount
fmt.Printf("Building BOM : %d rows\n", count)
}
// now get the components with vulnerabilities
vulnBom := hub.getVulnerabilityBom(codelocations.Items[0].Version.Id, 5000)
// get the vulnerabilities per component
var totalVulnerabilitiesList []vulnerability
for _, v := range vulnBom.Items {
vulnList := hub.getVulnerabilities(codelocations.Items[0].Version.Id, v.ChannelRelease.Id, v.ProducerReleases[0].Id, 5000)
totalVulnerabilitiesList = append(totalVulnerabilitiesList, vulnList.Items...)
}
fmt.Printf("total nr of vulnerabilities : %d\n", len(totalVulnerabilitiesList))
//timeStamp := time.Now().Format(time.RFC3339)
t := time.Now()
timeStamp := fmt.Sprintf("%d-%02d-%02dT%02d_%02d_%02d-00_00",
t.Year(), t.Month(), t.Day(),
t.Hour(), t.Minute(), t.Second())
reportUrl := fmt.Sprintf("%s/#versions/id:%s/vier:bom", config.Url, codelocations.Items[0].Version.Id)
report := &Report{UUID: "uuid", ScannerName: "blackduck", Time: timeStamp, Vulnerabilities: totalVulnerabilitiesList, ReportUrl: reportUrl}
jsonReport , err := json.Marshal(report)
if err != nil {
fmt.Printf("ERROR marshall of report went wrong : \n%s\n", err)
os.Exit(1)
}
fileName := fmt.Sprintf("%s.json", timeStamp)
if err := ioutil.WriteFile(filepath.Join(scan_output_path, fileName), jsonReport, 0755); err != nil {
fmt.Printf("ERROR writing file %s \n%s\n\n", fileName, err)
os.Exit(1)
}
}