Skip to content

Commit

Permalink
refactor: use pure go sqlite driver (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaineK00n committed May 10, 2023
1 parent b82ae91 commit dd6a015
Show file tree
Hide file tree
Showing 16 changed files with 151 additions and 140 deletions.
16 changes: 7 additions & 9 deletions .github/workflows/goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ jobs:
go-version-file: go.mod
-
name: Run GoReleaser
run: |
docker run --rm \
-e CGO_ENABLED=1 \
-e GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }} \
-v /var/run/docker.sock:/var/run/docker.sock \
-v `pwd`:/go/src/github.com/vulsio/go-exploitdb \
-w /go/src/github.com/vulsio/go-exploitdb \
ghcr.io/goreleaser/goreleaser-cross:v1.20 \
release --clean
uses: goreleaser/goreleaser-action@v4
with:
distribution: goreleaser
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
35 changes: 2 additions & 33 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,15 @@ release:
owner: vulsio
name: go-exploitdb
env:
- GO111MODULE=on
- CGO_ENABLED=1
- CGO_ENABLED=0
builds:
- id: linux-amd64
- id: go-exploitdb
goos:
- linux
goarch:
- amd64
env:
- CC=x86_64-linux-gnu-gcc
main: .
ldflags: -s -w -X github.com/vulsio/go-exploitdb/config.Version={{.Version}} -X github.com/vulsio/go-exploitdb/config.Revision={{.Commit}}
binary: go-exploitdb
- id: linux-arm64
goos:
- linux
goarch:
- arm64
env:
- CC=aarch64-linux-gnu-gcc
main: .
ldflags: -s -w -X github.com/vulsio/go-exploitdb/config.Version={{.Version}} -X github.com/vulsio/go-exploitdb/config.Revision={{.Commit}}
binary: go-exploitdb
- id: windows-amd64
goos:
- windows
goarch:
- amd64
env:
- CC=x86_64-w64-mingw32-gcc
main: .
ldflags: -s -w -X github.com/vulsio/go-exploitdb/config.Version={{.Version}} -X github.com/vulsio/go-exploitdb/config.Revision={{.Commit}}
binary: go-exploitdb
- id: windows-arm64
goos:
- windows
goarch:
- arm64
env:
- CC=/llvm-mingw/bin/aarch64-w64-mingw32-gcc
main: .
ldflags: -s -w -X github.com/vulsio/go-exploitdb/config.Version={{.Version}} -X github.com/vulsio/go-exploitdb/config.Revision={{.Commit}}
binary: go-exploitdb
Expand Down
14 changes: 5 additions & 9 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ VERSION := $(shell git describe --tags --abbrev=0)
REVISION := $(shell git rev-parse --short HEAD)
LDFLAGS := -X 'github.com/vulsio/go-exploitdb/config.Version=$(VERSION)' \
-X 'github.com/vulsio/go-exploitdb/config.Revision=$(REVISION)'
GO := GO111MODULE=on go
GO_OFF := GO111MODULE=off go
GO := CGO_ENABLED=0 go

all: build pretest

Expand All @@ -40,15 +39,15 @@ install: main.go
$(GO) install -ldflags "$(LDFLAGS)"

lint:
$(GO) install github.com/mgechev/revive@latest
go install github.com/mgechev/revive@latest
revive -config ./.revive.toml -formatter plain $(PKGS)

golangci:
$(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
golangci-lint run

vet:
$(foreach pkg,$(PKGS),go vet $(pkg);)
echo $(PKGS) | xargs env $(GO) vet || exit;

fmt:
gofmt -w $(SRCS)
Expand All @@ -59,10 +58,7 @@ fmtcheck:
pretest: lint vet fmtcheck

test: pretest
$(foreach pkg,$(PKGS),go test -v $(pkg) || exit;)

integration:
go test -tags docker_integration -run TestIntegration -v
$(GO) test -cover -v ./... || exit;

cov:
@ go get -v github.com/axw/gocov/gocov
Expand Down
11 changes: 3 additions & 8 deletions commands/fetch-awesomepoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,10 @@ func fetchAwesomePoc(_ *cobra.Command, _ []string) (err error) {
return xerrors.Errorf("Failed to SetLogger. err: %w", err)
}

driver, locked, err := db.NewDB(
viper.GetString("dbtype"),
viper.GetString("dbpath"),
viper.GetBool("debug-sql"),
db.Option{},
)
driver, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{})
if err != nil {
if locked {
return xerrors.Errorf("Failed to initialize DB. Close DB connection before fetching. err: %w", err)
if xerrors.Is(err, db.ErrDBLocked) {
return xerrors.Errorf("Failed to open DB. Close DB connection before fetching. err: %w", err)
}
return xerrors.Errorf("Failed to open DB. err: %w", err)
}
Expand Down
11 changes: 3 additions & 8 deletions commands/fetch-exploitdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,10 @@ func fetchExploitDB(_ *cobra.Command, _ []string) (err error) {
return xerrors.Errorf("Failed to SetLogger. err: %w", err)
}

driver, locked, err := db.NewDB(
viper.GetString("dbtype"),
viper.GetString("dbpath"),
viper.GetBool("debug-sql"),
db.Option{},
)
driver, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{})
if err != nil {
if locked {
return xerrors.Errorf("Failed to initialize DB. Close DB connection before fetching. err: %w", err)
if xerrors.Is(err, db.ErrDBLocked) {
return xerrors.Errorf("Failed to open DB. Close DB connection before fetching. err: %w", err)
}
return xerrors.Errorf("Failed to open DB. err: %w", err)
}
Expand Down
11 changes: 3 additions & 8 deletions commands/fetch-githubrepos.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,10 @@ func fetchGitHubRepos(_ *cobra.Command, _ []string) (err error) {
return xerrors.Errorf("Failed to SetLogger. err: %w", err)
}

driver, locked, err := db.NewDB(
viper.GetString("dbtype"),
viper.GetString("dbpath"),
viper.GetBool("debug-sql"),
db.Option{},
)
driver, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{})
if err != nil {
if locked {
return xerrors.Errorf("Failed to initialize DB. Close DB connection before fetching. err: %w", err)
if xerrors.Is(err, db.ErrDBLocked) {
return xerrors.Errorf("Failed to open DB. Close DB connection before fetching. err: %w", err)
}
return xerrors.Errorf("Failed to open DB. err: %w", err)
}
Expand Down
11 changes: 3 additions & 8 deletions commands/fetch-inthewild.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,10 @@ func fetchInTheWild(_ *cobra.Command, _ []string) (err error) {
return xerrors.Errorf("Failed to SetLogger. err: %w", err)
}

driver, locked, err := db.NewDB(
viper.GetString("dbtype"),
viper.GetString("dbpath"),
viper.GetBool("debug-sql"),
db.Option{},
)
driver, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{})
if err != nil {
if locked {
return xerrors.Errorf("Failed to initialize DB. Close DB connection before fetching. err: %w", err)
if xerrors.Is(err, db.ErrDBLocked) {
return xerrors.Errorf("Failed to open DB. Close DB connection before fetching. err: %w", err)
}
return xerrors.Errorf("Failed to open DB. err: %w", err)
}
Expand Down
11 changes: 3 additions & 8 deletions commands/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,10 @@ func searchExploit(_ *cobra.Command, _ []string) error {
return xerrors.Errorf("Failed to SetLogger. err: %w", err)
}

driver, locked, err := db.NewDB(
viper.GetString("dbtype"),
viper.GetString("dbpath"),
viper.GetBool("debug-sql"),
db.Option{},
)
driver, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{})
if err != nil {
if locked {
return xerrors.Errorf("Failed to initialize DB. Close DB connection before fetching. err: %w", err)
if xerrors.Is(err, db.ErrDBLocked) {
return xerrors.Errorf("Failed to open DB. Close DB connection before fetching. err: %w", err)
}
return xerrors.Errorf("Failed to open DB. err: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions commands/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ func executeServer(_ *cobra.Command, _ []string) (err error) {
return xerrors.Errorf("Failed to SetLogger. err: %w", err)
}

driver, locked, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{})
driver, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{})
if err != nil {
if locked {
return xerrors.Errorf("Failed to initialize DB. Close DB connection before fetching. err: %w", err)
if xerrors.Is(err, db.ErrDBLocked) {
return xerrors.Errorf("Failed to open DB. Close DB connection before fetching. err: %w", err)
}
return xerrors.Errorf("Failed to open DB. err: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion commands/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var versionCmd = &cobra.Command{
Use: "version",
Short: "Show version",
Long: `Show version`,
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, _ []string) {
fmt.Printf("go-exploitdb %s %s\n", config.Version, config.Revision)
},
}
21 changes: 9 additions & 12 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// DB :
type DB interface {
Name() string
OpenDB(dbType, dbPath string, debugSQL bool, option Option) (bool, error)
OpenDB(dbType, dbPath string, debugSQL bool, option Option) error
CloseDB() error
MigrateDB() error
GetExploitByID(string) ([]models.Exploit, error)
Expand All @@ -33,31 +33,28 @@ type Option struct {
}

// NewDB :
func NewDB(dbType string, dbPath string, debugSQL bool, option Option) (driver DB, locked bool, err error) {
func NewDB(dbType string, dbPath string, debugSQL bool, option Option) (driver DB, err error) {
if driver, err = newDB(dbType); err != nil {
return driver, false, xerrors.Errorf("Failed to new db. err: %w", err)
return driver, xerrors.Errorf("Failed to new db. err: %w", err)
}

if locked, err := driver.OpenDB(dbType, dbPath, debugSQL, option); err != nil {
if locked {
return nil, true, err
}
return nil, false, err
if err := driver.OpenDB(dbType, dbPath, debugSQL, option); err != nil {
return nil, xerrors.Errorf("Failed to open db. err: %w", err)
}

isV1, err := driver.IsExploitModelV1()
if err != nil {
return nil, false, xerrors.Errorf("Failed to IsExploitModelV1. err: %w", err)
return nil, xerrors.Errorf("Failed to IsExploitModelV1. err: %w", err)
}
if isV1 {
return nil, false, xerrors.New("Failed to NewDB. Since SchemaVersion is incompatible, delete Database and fetch again.")
return nil, xerrors.New("Failed to NewDB. Since SchemaVersion is incompatible, delete Database and fetch again.")
}

if err := driver.MigrateDB(); err != nil {
return driver, false, xerrors.Errorf("Failed to migrate db. err: %w", err)
return driver, xerrors.Errorf("Failed to migrate db. err: %w", err)
}

return driver, false, nil
return driver, nil
}

func newDB(dbType string) (DB, error) {
Expand Down
Loading

0 comments on commit dd6a015

Please sign in to comment.