From 5fea4eaef823b7ec9c99a165b5f532540e25c998 Mon Sep 17 00:00:00 2001 From: Kota Kanbe Date: Fri, 27 Nov 2020 09:55:09 +0900 Subject: [PATCH] feat(nocgo): enable to build with CGO_ENABLED=0 (#1080) --- GNUmakefile | 16 ++++++++++------ main.go => cmd/scanner/main.go | 5 +---- exploit/exploit.go | 2 ++ gost/base.go | 2 ++ gost/debian.go | 2 ++ gost/gost.go | 2 ++ gost/microsoft.go | 2 ++ gost/pseudo.go | 8 ++------ gost/redhat.go | 2 ++ gost/util.go | 5 +++++ models/utils.go | 2 ++ msf/empty.go | 1 + msf/msf.go | 2 ++ oval/alpine.go | 2 ++ oval/debian.go | 2 ++ oval/debian_test.go | 2 ++ oval/empty.go | 1 + oval/oval.go | 2 ++ oval/redhat.go | 2 ++ oval/redhat_test.go | 2 ++ oval/suse.go | 2 ++ oval/util.go | 2 ++ oval/util_test.go | 2 ++ report/cve_client.go | 2 ++ report/db_client.go | 2 ++ report/report.go | 7 ++----- report/util.go | 8 ++++++-- server/empty.go | 1 + server/server.go | 2 ++ {commands => subcmds}/configtest.go | 2 +- {commands => subcmds}/discover.go | 2 +- {commands => subcmds}/history.go | 2 +- {commands => subcmds}/report.go | 4 +++- {commands => subcmds}/scan.go | 2 +- {commands => subcmds}/server.go | 4 +++- {commands => subcmds}/tui.go | 4 +++- {commands => subcmds}/util.go | 2 +- 37 files changed, 83 insertions(+), 31 deletions(-) rename main.go => cmd/scanner/main.go (79%) create mode 100644 msf/empty.go create mode 100644 oval/empty.go create mode 100644 server/empty.go rename {commands => subcmds}/configtest.go (99%) rename {commands => subcmds}/discover.go (99%) rename {commands => subcmds}/history.go (99%) rename {commands => subcmds}/report.go (99%) rename {commands => subcmds}/scan.go (99%) rename {commands => subcmds}/server.go (99%) rename {commands => subcmds}/tui.go (99%) rename {commands => subcmds}/util.go (97%) diff --git a/GNUmakefile b/GNUmakefile index 12da3108..3e34f03d 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -20,19 +20,23 @@ BUILDTIME := $(shell date "+%Y%m%d_%H%M%S") LDFLAGS := -X 'github.com/future-architect/vuls/config.Version=$(VERSION)' \ -X 'github.com/future-architect/vuls/config.Revision=build-$(BUILDTIME)_$(REVISION)' GO := GO111MODULE=on go +CGO_UNABLED := CGO_ENABLED=0 go GO_OFF := GO111MODULE=off go all: build -build: main.go pretest fmt - $(GO) build -a -ldflags "$(LDFLAGS)" -o vuls $< +build: ./cmd/vuls/main.go pretest fmt + $(GO) build -a -ldflags "$(LDFLAGS)" -o vuls ./cmd/vuls -b: main.go pretest fmt - $(GO) build -ldflags "$(LDFLAGS)" -o vuls $< +install: ./cmd/vuls/main.go pretest fmt + $(GO) install -ldflags "$(LDFLAGS)" ./cmd/vuls -install: main.go pretest - $(GO) install -ldflags "$(LDFLAGS)" +build-scanner: ./cmd/scanner/main.go pretest fmt + $(CGO_UNABLED) build -tags=scanner -a -ldflags "$(LDFLAGS)" -o vuls ./cmd/scanner + +install-scanner: ./cmd/scanner/main.go pretest fmt + $(CGO_UNABLED) install -tags=scanner -ldflags "$(LDFLAGS)" ./cmd/scanner lint: $(GO_OFF) get -u golang.org/x/lint/golint diff --git a/main.go b/cmd/scanner/main.go similarity index 79% rename from main.go rename to cmd/scanner/main.go index a6ddc35b..99d4b41b 100644 --- a/main.go +++ b/cmd/scanner/main.go @@ -7,8 +7,8 @@ import ( "context" - "github.com/future-architect/vuls/commands" "github.com/future-architect/vuls/config" + commands "github.com/future-architect/vuls/subcmds" "github.com/google/subcommands" ) @@ -17,12 +17,9 @@ func main() { subcommands.Register(subcommands.FlagsCommand(), "") subcommands.Register(subcommands.CommandsCommand(), "") subcommands.Register(&commands.DiscoverCmd{}, "discover") - subcommands.Register(&commands.TuiCmd{}, "tui") subcommands.Register(&commands.ScanCmd{}, "scan") subcommands.Register(&commands.HistoryCmd{}, "history") - subcommands.Register(&commands.ReportCmd{}, "report") subcommands.Register(&commands.ConfigtestCmd{}, "configtest") - subcommands.Register(&commands.ServerCmd{}, "server") var v = flag.Bool("v", false, "Show version") diff --git a/exploit/exploit.go b/exploit/exploit.go index c2d2c617..017e7d35 100644 --- a/exploit/exploit.go +++ b/exploit/exploit.go @@ -1,3 +1,5 @@ +// +build !scanner + package exploit import ( diff --git a/gost/base.go b/gost/base.go index 59833d8a..d9306088 100644 --- a/gost/base.go +++ b/gost/base.go @@ -1,3 +1,5 @@ +// +build !scanner + package gost import ( diff --git a/gost/debian.go b/gost/debian.go index 5c50537b..b991e958 100644 --- a/gost/debian.go +++ b/gost/debian.go @@ -1,3 +1,5 @@ +// +build !scanner + package gost import ( diff --git a/gost/gost.go b/gost/gost.go index 9b559cfd..a5387ada 100644 --- a/gost/gost.go +++ b/gost/gost.go @@ -1,3 +1,5 @@ +// +build !scanner + package gost import ( diff --git a/gost/microsoft.go b/gost/microsoft.go index 7cfd5d7e..ea920284 100644 --- a/gost/microsoft.go +++ b/gost/microsoft.go @@ -1,3 +1,5 @@ +// +build !scanner + package gost import ( diff --git a/gost/pseudo.go b/gost/pseudo.go index 74a3875c..dc9358fb 100644 --- a/gost/pseudo.go +++ b/gost/pseudo.go @@ -1,8 +1,8 @@ +// +build !scanner + package gost import ( - "strings" - "github.com/future-architect/vuls/models" "github.com/knqyf263/gost/db" ) @@ -16,7 +16,3 @@ type Pseudo struct { func (pse Pseudo) DetectUnfixed(driver db.DB, r *models.ScanResult, _ bool) (int, error) { return 0, nil } - -func major(osVer string) (majorVersion string) { - return strings.Split(osVer, ".")[0] -} diff --git a/gost/redhat.go b/gost/redhat.go index 2a3fc729..726a7dde 100644 --- a/gost/redhat.go +++ b/gost/redhat.go @@ -1,3 +1,5 @@ +// +build !scanner + package gost import ( diff --git a/gost/util.go b/gost/util.go index 46a188b0..0db2ec06 100644 --- a/gost/util.go +++ b/gost/util.go @@ -2,6 +2,7 @@ package gost import ( "net/http" + "strings" "time" "github.com/cenkalti/backoff" @@ -181,3 +182,7 @@ func httpGet(url string, req request, resChan chan<- response, errChan chan<- er json: body, } } + +func major(osVer string) (majorVersion string) { + return strings.Split(osVer, ".")[0] +} diff --git a/models/utils.go b/models/utils.go index de7937b6..880f704b 100644 --- a/models/utils.go +++ b/models/utils.go @@ -1,3 +1,5 @@ +// +build !scanner + package models import ( diff --git a/msf/empty.go b/msf/empty.go new file mode 100644 index 00000000..565ac70f --- /dev/null +++ b/msf/empty.go @@ -0,0 +1 @@ +package msf diff --git a/msf/msf.go b/msf/msf.go index 194586ac..a704765f 100644 --- a/msf/msf.go +++ b/msf/msf.go @@ -1,3 +1,5 @@ +// +build !scanner + package msf import ( diff --git a/oval/alpine.go b/oval/alpine.go index 648b1a11..3e7cd21d 100644 --- a/oval/alpine.go +++ b/oval/alpine.go @@ -1,3 +1,5 @@ +// +build !scanner + package oval import ( diff --git a/oval/debian.go b/oval/debian.go index b1573612..a86aa7ff 100644 --- a/oval/debian.go +++ b/oval/debian.go @@ -1,3 +1,5 @@ +// +build !scanner + package oval import ( diff --git a/oval/debian_test.go b/oval/debian_test.go index df9d4959..51fa5a67 100644 --- a/oval/debian_test.go +++ b/oval/debian_test.go @@ -1,3 +1,5 @@ +// +build !scanner + package oval import ( diff --git a/oval/empty.go b/oval/empty.go new file mode 100644 index 00000000..5df0f81b --- /dev/null +++ b/oval/empty.go @@ -0,0 +1 @@ +package oval diff --git a/oval/oval.go b/oval/oval.go index 72aa5efb..b9263c8c 100644 --- a/oval/oval.go +++ b/oval/oval.go @@ -1,3 +1,5 @@ +// +build !scanner + package oval import ( diff --git a/oval/redhat.go b/oval/redhat.go index ff519618..cd1cd5df 100644 --- a/oval/redhat.go +++ b/oval/redhat.go @@ -1,3 +1,5 @@ +// +build !scanner + package oval import ( diff --git a/oval/redhat_test.go b/oval/redhat_test.go index 9e50a796..fdf8fe9f 100644 --- a/oval/redhat_test.go +++ b/oval/redhat_test.go @@ -1,3 +1,5 @@ +// +build !scanner + package oval import ( diff --git a/oval/suse.go b/oval/suse.go index 02925e9f..9de65619 100644 --- a/oval/suse.go +++ b/oval/suse.go @@ -1,3 +1,5 @@ +// +build !scanner + package oval import ( diff --git a/oval/util.go b/oval/util.go index 1f74a080..bdba1f61 100644 --- a/oval/util.go +++ b/oval/util.go @@ -1,3 +1,5 @@ +// +build !scanner + package oval import ( diff --git a/oval/util_test.go b/oval/util_test.go index bf1caa88..a919c6f3 100644 --- a/oval/util_test.go +++ b/oval/util_test.go @@ -1,3 +1,5 @@ +// +build !scanner + package oval import ( diff --git a/report/cve_client.go b/report/cve_client.go index 2d223e00..84b9bdca 100644 --- a/report/cve_client.go +++ b/report/cve_client.go @@ -1,3 +1,5 @@ +// +build !scanner + package report import ( diff --git a/report/db_client.go b/report/db_client.go index 4b5f4c60..97c6f9f5 100644 --- a/report/db_client.go +++ b/report/db_client.go @@ -1,3 +1,5 @@ +// +build !scanner + package report import ( diff --git a/report/report.go b/report/report.go index a7756818..72b695ba 100644 --- a/report/report.go +++ b/report/report.go @@ -1,3 +1,5 @@ +// +build !scanner + package report import ( @@ -36,11 +38,6 @@ import ( "golang.org/x/xerrors" ) -const ( - vulsOpenTag = "" - vulsCloseTag = "" -) - // FillCveInfos fills CVE Detailed Information func FillCveInfos(dbclient DBClient, rs []models.ScanResult, dir string) ([]models.ScanResult, error) { diff --git a/report/util.go b/report/util.go index 6ad66bbd..07bc8ffe 100644 --- a/report/util.go +++ b/report/util.go @@ -22,7 +22,11 @@ import ( "golang.org/x/xerrors" ) -const maxColWidth = 100 +const ( + vulsOpenTag = "" + vulsCloseTag = "" + maxColWidth = 100 +) func formatScanSummary(rs ...models.ScanResult) string { table := uitable.New() @@ -555,7 +559,7 @@ func getDiffCves(previous, current models.ScanResult) models.VulnInfos { // TODO commented out because a bug of diff logic when multiple oval defs found for a certain CVE-ID and same updated_at // if these OVAL defs have different affected packages, this logic detects as updated. - // This logic will be uncomented after integration with ghost https://github.com/knqyf263/gost + // This logic will be uncomented after integration with gost https://github.com/knqyf263/gost // } else if isCveFixed(v, previous) { // updated[v.CveID] = v // util.Log.Debugf("fixed: %s", v.CveID) diff --git a/server/empty.go b/server/empty.go new file mode 100644 index 00000000..abb4e431 --- /dev/null +++ b/server/empty.go @@ -0,0 +1 @@ +package server diff --git a/server/server.go b/server/server.go index 1d625ff9..a8b084ed 100644 --- a/server/server.go +++ b/server/server.go @@ -1,3 +1,5 @@ +// +build !scanner + package server import ( diff --git a/commands/configtest.go b/subcmds/configtest.go similarity index 99% rename from commands/configtest.go rename to subcmds/configtest.go index 8839f746..f51a8ceb 100644 --- a/commands/configtest.go +++ b/subcmds/configtest.go @@ -1,4 +1,4 @@ -package commands +package subcmds import ( "context" diff --git a/commands/discover.go b/subcmds/discover.go similarity index 99% rename from commands/discover.go rename to subcmds/discover.go index 0d6e000a..2c73b84a 100644 --- a/commands/discover.go +++ b/subcmds/discover.go @@ -1,4 +1,4 @@ -package commands +package subcmds import ( "context" diff --git a/commands/history.go b/subcmds/history.go similarity index 99% rename from commands/history.go rename to subcmds/history.go index 8aef60be..616aca04 100644 --- a/commands/history.go +++ b/subcmds/history.go @@ -1,4 +1,4 @@ -package commands +package subcmds import ( "context" diff --git a/commands/report.go b/subcmds/report.go similarity index 99% rename from commands/report.go rename to subcmds/report.go index ae1d6eac..2fec8a65 100644 --- a/commands/report.go +++ b/subcmds/report.go @@ -1,4 +1,6 @@ -package commands +// +build !scanner + +package subcmds import ( "context" diff --git a/commands/scan.go b/subcmds/scan.go similarity index 99% rename from commands/scan.go rename to subcmds/scan.go index 510a5d33..23f6d13a 100644 --- a/commands/scan.go +++ b/subcmds/scan.go @@ -1,4 +1,4 @@ -package commands +package subcmds import ( "context" diff --git a/commands/server.go b/subcmds/server.go similarity index 99% rename from commands/server.go rename to subcmds/server.go index 9ed06149..5bd6268d 100644 --- a/commands/server.go +++ b/subcmds/server.go @@ -1,4 +1,6 @@ -package commands +// +build !scanner + +package subcmds import ( "context" diff --git a/commands/tui.go b/subcmds/tui.go similarity index 99% rename from commands/tui.go rename to subcmds/tui.go index f69e63cf..0b8d7b65 100644 --- a/commands/tui.go +++ b/subcmds/tui.go @@ -1,4 +1,6 @@ -package commands +// +build !scanner + +package subcmds import ( "context" diff --git a/commands/util.go b/subcmds/util.go similarity index 97% rename from commands/util.go rename to subcmds/util.go index 14e6dfef..908808bc 100644 --- a/commands/util.go +++ b/subcmds/util.go @@ -1,4 +1,4 @@ -package commands +package subcmds import ( "fmt"