Skip to content

feat(server): open db before starting server #2244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions detector/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (

"github.com/future-architect/vuls/config"
"github.com/future-architect/vuls/constant"
"github.com/future-architect/vuls/gost"
"github.com/future-architect/vuls/logging"
"github.com/future-architect/vuls/models"
"github.com/future-architect/vuls/oval"
"golang.org/x/xerrors"
)

Expand Down Expand Up @@ -258,3 +260,64 @@ func loadOneServerScanResult(jsonFile string) (*models.ScanResult, error) {
}
return result, nil
}

// ValidateDBs checks if the databases are accessible and can be closed properly
func ValidateDBs(cveConf config.GoCveDictConf, ovalConf config.GovalDictConf, gostConf config.GostConf, exploitConf config.ExploitConf, metasploitConf config.MetasploitConf, kevulnConf config.KEVulnConf, ctiConf config.CtiConf, logOpts logging.LogOpts) error {
cvec, err := newGoCveDictClient(&cveConf, logOpts)
if err != nil {
return xerrors.Errorf("Failed to new CVE client. err: %w", err)
}
if err := cvec.closeDB(); err != nil {
return xerrors.Errorf("Failed to close CVE DB. err: %w", err)
}

ovalc, err := oval.NewOVALClient(constant.ServerTypePseudo, ovalConf, logOpts)
if err != nil {
return xerrors.Errorf("Failed to new OVAL client. err: %w", err)
}
if err := ovalc.CloseDB(); err != nil {
return xerrors.Errorf("Failed to close OVAL DB. err: %w", err)
}

gostc, err := gost.NewGostClient(gostConf, constant.ServerTypePseudo, logOpts)
if err != nil {
return xerrors.Errorf("Failed to new gost client. err: %w", err)
}
if err := gostc.CloseDB(); err != nil {
return xerrors.Errorf("Failed to close gost DB. err: %w", err)
}

exploitc, err := newGoExploitDBClient(&exploitConf, logOpts)
if err != nil {
return xerrors.Errorf("Failed to new exploit client. err: %w", err)
}
if err := exploitc.closeDB(); err != nil {
return xerrors.Errorf("Failed to close exploit DB. err: %w", err)
}

metasploitc, err := newGoMetasploitDBClient(&metasploitConf, logOpts)
if err != nil {
return xerrors.Errorf("Failed to new metasploit client. err: %w", err)
}
if err := metasploitc.closeDB(); err != nil {
return xerrors.Errorf("Failed to close metasploit DB. err: %w", err)
}

kevulnc, err := newGoKEVulnDBClient(&kevulnConf, logOpts)
if err != nil {
return xerrors.Errorf("Failed to new KEVuln client. err: %w", err)
}
if err := kevulnc.closeDB(); err != nil {
return xerrors.Errorf("Failed to close KEVuln DB. err: %w", err)
}

ctic, err := newGoCTIDBClient(&ctiConf, logOpts)
if err != nil {
return xerrors.Errorf("Failed to new CTI client. err: %w", err)
}
if err := ctic.closeDB(); err != nil {
return xerrors.Errorf("Failed to close CTI DB. err: %w", err)
}

return nil
}
1 change: 1 addition & 0 deletions subcmds/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ func (p *ReportCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}
&config.Conf.Exploit,
&config.Conf.Metasploit,
&config.Conf.KEVuln,
&config.Conf.Cti,
} {
cnf.Init()
}
Expand Down
1 change: 1 addition & 0 deletions subcmds/report_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func (p *ReportCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}
&config.Conf.Exploit,
&config.Conf.Metasploit,
&config.Conf.KEVuln,
&config.Conf.Cti,
} {
cnf.Init()
}
Expand Down
8 changes: 8 additions & 0 deletions subcmds/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/google/subcommands"

"github.com/future-architect/vuls/config"
"github.com/future-architect/vuls/detector"
"github.com/future-architect/vuls/logging"
"github.com/future-architect/vuls/server"
)
Expand Down Expand Up @@ -102,6 +103,7 @@ func (p *ServerCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...interface{}
&config.Conf.Exploit,
&config.Conf.Metasploit,
&config.Conf.KEVuln,
&config.Conf.Cti,
} {
cnf.Init()
}
Expand All @@ -117,6 +119,12 @@ func (p *ServerCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...interface{}
return subcommands.ExitUsageError
}

logging.Log.Info("Validating DBs...")
if err := detector.ValidateDBs(config.Conf.CveDict, config.Conf.OvalDict, config.Conf.Gost, config.Conf.Exploit, config.Conf.Metasploit, config.Conf.KEVuln, config.Conf.Cti, config.Conf.LogOpts); err != nil {
logging.Log.Errorf("Failed to validate DBs. err: %+v", err)
return subcommands.ExitFailure
}

http.Handle("/vuls", server.VulsHandler{
ToLocalFile: p.toLocalFile,
})
Expand Down
Loading