forked from mcoops/deplist
-
Notifications
You must be signed in to change notification settings - Fork 5
/
deplist.go
333 lines (293 loc) · 8.33 KB
/
deplist.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
package deplist
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/RedHatProductSecurity/deplist/internal/scan"
"github.com/RedHatProductSecurity/deplist/internal/utils"
log "github.com/sirupsen/logrus"
)
// enums start at 1 to allow us to specify found languages 0 = nil
const (
LangGolang = 1 << iota
LangJava
LangNodeJS
LangPython
LangRuby
)
func init() {
// check for the library required binaries
languages := map[string]string{
"yarn": "yarn",
"npm": "npm",
"go": "go",
"mvn": "maven",
"bundle": "bundler gem",
}
for lang_bin, lang_name := range languages {
if _, err := exec.LookPath(lang_bin); err != nil {
log.Fatal(lang_name, " is required in PATH")
}
}
}
type Discovered struct {
deps []Dependency
foundTypes Bitmask
}
func addPackagesToDeps(discovered Discovered, pkgs map[string]string, lang Bitmask) Discovered {
if len(pkgs) > 0 {
discovered.foundTypes.DepFoundAddFlag(lang)
}
for name, version := range pkgs {
discovered.deps = append(discovered.deps,
Dependency{
DepType: lang,
Path: strings.TrimSuffix(name, "\n"),
Version: strings.Replace(version, "v", "", 1),
Files: []string{},
})
}
return discovered
}
func getDeps(fullPath string) ([]Dependency, Bitmask, error) {
var discovered Discovered
// special var so we don't double handle both repos with both
// a Gemfile and Gemfile.lock
var seenGemfile string
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
return nil, 0, os.ErrNotExist
}
pomPath := filepath.Join(fullPath, "pom.xml")
// goPath := filepath.Join(fullPath, "go.mod")
goPkgPath := filepath.Join(fullPath, "Gopkg.lock")
glidePath := filepath.Join(fullPath, "glide.lock")
rubyPath := filepath.Join(fullPath, "Gemfile") // Later we translate Gemfile.lock -> Gemfile to handle both cases
pythonPath := filepath.Join(fullPath, "requirements.txt")
// point at the parent repo, but can't assume where the indicators will be
err := filepath.Walk(fullPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
// prevent panic by handling failure https://golang.org/pkg/path/filepath/#Walk
return err
}
if info.IsDir() {
// prevent walking down the vendors, docs, etc
if utils.BelongsToIgnoreList(info.Name()) {
return filepath.SkipDir
}
} else {
// Two checks, one for filenames and the second switch for full
// paths. Useful if we're looking for top of repo
// comparisons here are made against the filename only, not full path
// so matches will be found at any level of the file tree, not just top-level
filename := info.Name()
switch filename {
case "go.mod":
pkgs, err := scan.GetGolangDeps(path)
if err != nil {
return err
}
if len(pkgs) > 0 {
discovered.foundTypes.DepFoundAddFlag(LangGolang)
}
for path, goPkg := range pkgs {
d := Dependency{
DepType: LangGolang,
Path: path,
Files: goPkg.Gofiles,
Version: goPkg.Version,
}
discovered.deps = append(discovered.deps, d)
}
case "package-lock.json":
// if theres not a yarn.lock fall thru
if _, err := os.Stat(
filepath.Join(
filepath.Dir(path),
"yarn.lock")); err == nil {
return nil
}
fallthrough
case "yarn.lock":
pkgs, err := scan.GetNodeJSDeps(path)
if err != nil {
// ignore error
log.Debugf("failed to scan for nodejs: %s", path)
return nil
}
if len(pkgs) > 0 {
discovered.foundTypes.DepFoundAddFlag(LangNodeJS)
}
for _, p := range pkgs {
discovered.deps = append(discovered.deps,
Dependency{
DepType: LangNodeJS,
Path: p.Name,
Version: p.Version,
Files: []string{},
})
}
default:
ext := filepath.Ext(filename)
// java
switch ext {
case ".zip":
// be more aggressive with zip files, must contain something java ish
if ok, _ := utils.ZipContainsJava(path); !ok {
return nil
}
fallthrough
case ".jar":
fallthrough
case ".war":
fallthrough
case ".ear":
fallthrough
case ".adm":
fallthrough
case ".hpi":
file := strings.Replace(filepath.Base(path), ext, "", 1) // get filename, check if we can ignore
if strings.HasSuffix(file, "-sources") || strings.HasSuffix(file, "-javadoc") {
return nil
}
pkgs, err := scan.GetJarDeps(path)
if err == nil {
if len(pkgs) > 0 {
discovered.foundTypes.DepFoundAddFlag(LangJava)
}
for name, version := range pkgs {
// just in case we report the full path to the dep
name = strings.Replace(name, fullPath, "", 1)
// if the dep ends with -javadoc or -sources, not really interested
if !strings.HasSuffix(version, "-javadoc") && !strings.HasSuffix(version, "-sources") {
discovered.deps = append(discovered.deps,
Dependency{
DepType: LangJava,
Path: name,
Version: version,
Files: []string{},
})
}
}
}
}
}
// translate Gemfile.lock -> Gemfile, to handle either case
// but also avoid double-handling, i.e. scanning once for each file
path = strings.Replace(path, "Gemfile.lock", "Gemfile", 1)
// comparisons here are against the full filepath, so will not match if
// these filesames are found in subdirectories, only the top level
switch path {
case goPkgPath:
pkgs, err := scan.GetGoPkgDeps(path)
if err != nil {
return err
}
if len(pkgs) > 0 {
discovered.foundTypes.DepFoundAddFlag(LangGolang)
}
for _, goPkg := range pkgs {
d := Dependency{
DepType: LangGolang,
Path: goPkg.Name,
Version: goPkg.Version,
}
discovered.deps = append(discovered.deps, d)
}
case glidePath:
pkgs, err := scan.GetGlideDeps(path)
if err != nil {
return err
}
if len(pkgs) > 0 {
discovered.foundTypes.DepFoundAddFlag(LangGolang)
}
for _, goPkg := range pkgs {
d := Dependency{
DepType: LangGolang,
Path: goPkg.Name,
Version: goPkg.Version,
}
discovered.deps = append(discovered.deps, d)
}
case pomPath:
pkgs, err := scan.GetMvnDeps(path)
if err != nil {
return err
}
discovered = addPackagesToDeps(discovered, pkgs, LangJava)
case rubyPath:
// To prevent double handling of both Gemfile and Gemfile.lock
// Earier we translate Gemfile.lock -> Gemfile
if path == seenGemfile {
break
}
pkgs, err := scan.GetRubyDeps(path)
if err != nil {
return err
}
discovered = addPackagesToDeps(discovered, pkgs, LangRuby)
seenGemfile = path
case pythonPath:
pkgs, err := scan.GetPythonDeps(path)
if err != nil {
return err
}
discovered = addPackagesToDeps(discovered, pkgs, LangPython)
}
}
return nil
})
if err != nil {
return nil, 0, err // should't matter
}
return discovered.deps, discovered.foundTypes, nil
}
// findBaseDir walks a directory tree through empty subdirs til it finds a directory with content
func findBaseDir(fullPath string) (string, error) {
log.Debugf("Checking %s", fullPath)
files, err := os.ReadDir(fullPath)
if err != nil {
return "", fmt.Errorf("Could not read: %s", err)
}
if len(files) == 1 && files[0].IsDir() {
return findBaseDir(filepath.Join(fullPath, files[0].Name()))
}
return fullPath, nil
}
// GetDeps scans a given repository and returns all dependencies found in a DependencyList struct.
func GetDeps(fullPath string) ([]Dependency, Bitmask, error) {
fullPath, err := findBaseDir(fullPath)
if err != nil {
return nil, 0, err
}
deps, foundTypes, err := getDeps(fullPath)
if err != nil {
return deps, foundTypes, err
}
// if no deps found, check one level lower in 'src' directory
// but ignore any new errors
if len(deps) == 0 {
fullPath = filepath.Join(fullPath, "src")
if _, err := os.Stat(fullPath); err != nil {
log.Debugf("No deps found, trying %s", fullPath)
deps, foundTypes, _ = getDeps(fullPath)
}
}
// de-duplicate
unique := removeDuplicates(deps)
return unique, foundTypes, err
}
func removeDuplicates(deps []Dependency) []Dependency {
seen := map[string]bool{}
filtered := []Dependency{}
for _, dep := range deps {
key := dep.ToString()
if _, ok := seen[key]; !ok {
seen[key] = true
filtered = append(filtered, dep)
}
}
return filtered
}