478 identify go binaries and extract mod information (#534)

* add query by MIME type to source.FileResolver

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* import stereoscope lib changes to find mime type

- add bin cataloger
- add bin parser
- add mime type go utils
- import new resolver

Signed-off-by: Christopher Angelo Phillips <christopher.phillips@anchore.com>

* add go std library code to unpack bin

- keep them in their own (original) files
- add note for "this code was copied from"
- comment the lines the required changing

Signed-off-by: Christopher Angelo Phillips <christopher.phillips@anchore.com>
Co-authored-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
Christopher Angelo Phillips
2021-10-07 12:16:38 -04:00
committed by GitHub
co-authored by Alex Goodman
parent 05ac3f1eff
commit 3462e18af3
20 changed files with 859 additions and 27 deletions
+63
View File
@@ -0,0 +1,63 @@
package golang
import (
"bufio"
"io"
"strings"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/source"
)
const (
packageIdentifier = "dep"
replaceIdentifier = "=>"
)
func parseGoBin(path string, reader io.ReadCloser) ([]pkg.Package, error) {
// Identify if bin was compiled by go
x, err := openExe(reader)
if err != nil {
return nil, err
}
_, mod := findVers(x)
pkgs := buildGoPkgInfo(path, mod)
return pkgs, nil
}
func buildGoPkgInfo(path, mod string) []pkg.Package {
pkgsSlice := make([]pkg.Package, 0)
scanner := bufio.NewScanner(strings.NewReader(mod))
// filter mod dependencies: [dep, name, version, sha]
for scanner.Scan() {
fields := strings.Fields(scanner.Text())
// must have dep, name, version
if len(fields) < 3 {
continue
}
switch fields[0] {
case packageIdentifier:
pkgsSlice = append(pkgsSlice, pkg.Package{
Name: fields[1],
Version: fields[2],
Language: pkg.Go,
Type: pkg.GoModulePkg,
Locations: []source.Location{
{
RealPath: path,
},
},
})
case replaceIdentifier:
pkg := &pkgsSlice[len(pkgsSlice)-1]
pkg.Name = fields[1]
pkg.Version = fields[2]
}
}
return pkgsSlice
}