Skip to content

Commit

Permalink
fix(server): start if no write permmision for logdir (#178)
Browse files Browse the repository at this point in the history
* refactor: remove useless vars

* chore: fix typo

* refactor: remove useless vars

* fix(server): start if no write permmision for logdir
  • Loading branch information
kotakanbe committed Feb 1, 2021
1 parent 9b22ff3 commit a2bfda9
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 122 deletions.
45 changes: 13 additions & 32 deletions commands/fetchjvn.go
Expand Up @@ -19,20 +19,11 @@ import (

// FetchJvnCmd is Subcommand for fetch JVN information.
type FetchJvnCmd struct {
debug bool
debugSQL bool
quiet bool
logDir string
logJSON bool
dbpath string
dbtype string
dumpPath string
latest bool
last2Y bool
years bool
httpProxy string
light bool
force bool
logDir string
logJSON bool
latest bool
last2Y bool
years bool
}

// Name return subcommand name
Expand Down Expand Up @@ -64,19 +55,19 @@ func (*FetchJvnCmd) Usage() string {

// SetFlags set flag
func (p *FetchJvnCmd) SetFlags(f *flag.FlagSet) {
f.BoolVar(&p.debug, "debug", false, "debug mode")
f.BoolVar(&p.debugSQL, "debug-sql", false, "SQL debug mode")
f.BoolVar(&p.quiet, "quiet", false, "quiet mode (no output)")
f.BoolVar(&c.Conf.Debug, "debug", false, "debug mode")
f.BoolVar(&c.Conf.DebugSQL, "debug-sql", false, "SQL debug mode")
f.BoolVar(&c.Conf.Quiet, "quiet", false, "quiet mode (no output)")

defaultLogDir := util.GetDefaultLogDir()
f.StringVar(&p.logDir, "log-dir", defaultLogDir, "/path/to/log")
f.BoolVar(&p.logJSON, "log-json", false, "output log as JSON")

pwd := os.Getenv("PWD")
f.StringVar(&p.dbpath, "dbpath", pwd+"/cve.sqlite3",
f.StringVar(&c.Conf.DBPath, "dbpath", pwd+"/cve.sqlite3",
"/path/to/sqlite3 or SQL connection string")

f.StringVar(&p.dbtype, "dbtype", "sqlite3",
f.StringVar(&c.Conf.DBType, "dbtype", "sqlite3",
"Database type to store data in (sqlite3, mysql, postgres or redis supported)")

f.BoolVar(&p.latest, "latest", false,
Expand All @@ -89,28 +80,18 @@ func (p *FetchJvnCmd) SetFlags(f *flag.FlagSet) {
"Refresh JVN data of specific years.")

f.StringVar(
&p.httpProxy,
&c.Conf.HTTPProxy,
"http-proxy",
"",
"http://proxy-url:port (default: empty)",
)

f.BoolVar(&p.light, "light", false, "Don't collect *HEAVY* CPE relate data")
f.BoolVar(&p.force, "force", false, "Force update")
f.BoolVar(&c.Conf.Light, "light", false, "Don't collect *HEAVY* CPE relate data")
f.BoolVar(&c.Conf.Force, "force", false, "Force update")
}

// Execute execute
func (p *FetchJvnCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
c.Conf.Debug = p.debug
c.Conf.Quiet = p.quiet
c.Conf.DebugSQL = p.debugSQL
c.Conf.DBPath = p.dbpath
c.Conf.DBType = p.dbtype
// c.Conf.DumpPath = p.dumpPath
c.Conf.HTTPProxy = p.httpProxy
c.Conf.Light = p.light
c.Conf.Force = p.force

log.SetLogger(p.logDir, c.Conf.Quiet, c.Conf.Debug, p.logJSON)
if !c.Conf.Validate() {
return subcommands.ExitUsageError
Expand Down
44 changes: 13 additions & 31 deletions commands/fetchnvd.go
Expand Up @@ -20,19 +20,11 @@ import (

// FetchNvdCmd is Subcommand for fetch Nvd information.
type FetchNvdCmd struct {
debug bool
debugSQL bool
quiet bool
logDir string
logJSON bool
dbpath string
dbtype string
Latest bool
last2Y bool
years bool
httpProxy string
light bool
force bool
logDir string
logJSON bool
Latest bool
last2Y bool
years bool
}

// Name return subcommand name
Expand Down Expand Up @@ -67,19 +59,19 @@ For the first time, run the blow command to fetch data for entire period. (It ta

// SetFlags set flag
func (p *FetchNvdCmd) SetFlags(f *flag.FlagSet) {
f.BoolVar(&p.debug, "debug", false, "debug mode")
f.BoolVar(&p.debugSQL, "debug-sql", false, "SQL debug mode")
f.BoolVar(&p.quiet, "quiet", false, "quiet mode (no output)")
f.BoolVar(&c.Conf.Debug, "debug", false, "debug mode")
f.BoolVar(&c.Conf.DebugSQL, "debug-sql", false, "SQL debug mode")
f.BoolVar(&c.Conf.Quiet, "quiet", false, "quiet mode (no output)")

defaultLogDir := util.GetDefaultLogDir()
f.StringVar(&p.logDir, "log-dir", defaultLogDir, "/path/to/log")
f.BoolVar(&p.logJSON, "log-json", false, "output log as JSON")

pwd := os.Getenv("PWD")
f.StringVar(&p.dbpath, "dbpath", pwd+"/cve.sqlite3",
f.StringVar(&c.Conf.DBPath, "dbpath", pwd+"/cve.sqlite3",
"/path/to/sqlite3 or SQL connection string")

f.StringVar(&p.dbtype, "dbtype", "sqlite3",
f.StringVar(&c.Conf.DBType, "dbtype", "sqlite3",
"Database type to store data in (sqlite3, mysql, postgres or redis supported)")

f.BoolVar(&p.last2Y, "last2y", false,
Expand All @@ -92,29 +84,19 @@ func (p *FetchNvdCmd) SetFlags(f *flag.FlagSet) {
"Refresh NVD data of specific years")

f.StringVar(
&p.httpProxy,
&c.Conf.HTTPProxy,
"http-proxy",
"",
"http://proxy-url:port (default: empty)",
)

f.BoolVar(&p.light, "light", false, "Don't collect *HEAVY* CPE relate data")
f.BoolVar(&p.force, "force", false, "Force update")
f.BoolVar(&c.Conf.Light, "light", false, "Don't collect *HEAVY* CPE relate data")
f.BoolVar(&c.Conf.Force, "force", false, "Force update")
}

// Execute execute
func (p *FetchNvdCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
c.Conf.Debug = p.debug
c.Conf.Quiet = p.quiet
log.SetLogger(p.logDir, c.Conf.Quiet, c.Conf.Debug, p.logJSON)

c.Conf.DebugSQL = p.debugSQL
c.Conf.DBPath = p.dbpath
c.Conf.DBType = p.dbtype
c.Conf.HTTPProxy = p.httpProxy
c.Conf.Light = p.light
c.Conf.Force = p.force

if !c.Conf.Validate() {
return subcommands.ExitUsageError
}
Expand Down
24 changes: 7 additions & 17 deletions commands/list.go
Expand Up @@ -20,13 +20,8 @@ import (

// ListCmd is Subcommand for fetch Nvd information.
type ListCmd struct {
debug bool
debugSQL bool
logDir string
logJSON bool
dbpath string
dbtype string
httpProxy string
logDir string
logJSON bool
}

// Name return subcommand name
Expand All @@ -51,22 +46,22 @@ func (*ListCmd) Usage() string {

// SetFlags set flag
func (p *ListCmd) SetFlags(f *flag.FlagSet) {
f.BoolVar(&p.debug, "debug", false, "debug mode")
f.BoolVar(&p.debugSQL, "debug-sql", false, "SQL debug mode")
f.BoolVar(&c.Conf.Debug, "debug", false, "debug mode")
f.BoolVar(&c.Conf.DebugSQL, "debug-sql", false, "SQL debug mode")

defaultLogDir := util.GetDefaultLogDir()
f.StringVar(&p.logDir, "log-dir", defaultLogDir, "/path/to/log")
f.BoolVar(&p.logJSON, "log-json", false, "output log as JSON")

pwd := os.Getenv("PWD")
f.StringVar(&p.dbpath, "dbpath", pwd+"/cve.sqlite3",
f.StringVar(&c.Conf.DBPath, "dbpath", pwd+"/cve.sqlite3",
"/path/to/sqlite3 or SQL connection string")

f.StringVar(&p.dbtype, "dbtype", "sqlite3",
f.StringVar(&c.Conf.DBType, "dbtype", "sqlite3",
"Database type to store data in (sqlite3, mysql, postgres or redis supported)")

f.StringVar(
&p.httpProxy,
&c.Conf.HTTPProxy,
"http-proxy",
"",
"http://proxy-url:port (default: empty)",
Expand All @@ -75,11 +70,6 @@ func (p *ListCmd) SetFlags(f *flag.FlagSet) {

// Execute execute
func (p *ListCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
c.Conf.Debug = p.debug
c.Conf.DebugSQL = p.debugSQL
c.Conf.DBPath = p.dbpath
c.Conf.DBType = p.dbtype
c.Conf.HTTPProxy = p.httpProxy
log.SetLogger(p.logDir, c.Conf.Quiet, c.Conf.Debug, p.logJSON)
if !c.Conf.Validate() {
return subcommands.ExitUsageError
Expand Down
35 changes: 9 additions & 26 deletions commands/server.go
Expand Up @@ -15,16 +15,8 @@ import (

// ServerCmd is Subcommand for CVE dictionary HTTP Server
type ServerCmd struct {
debug bool
debugSQL bool
quiet bool
logDir string
logJSON bool

dbpath string
dbtype string
bind string
port string
logDir string
logJSON bool
}

// Name return subcommand name
Expand Down Expand Up @@ -52,41 +44,32 @@ func (*ServerCmd) Usage() string {

// SetFlags set flag
func (p *ServerCmd) SetFlags(f *flag.FlagSet) {
f.BoolVar(&p.debug, "debug", false, "debug mode (default: false)")
f.BoolVar(&p.debugSQL, "debug-sql", false, "SQL debug mode (default: false)")
f.BoolVar(&p.quiet, "quiet", false, "quiet mode (no output)")
f.BoolVar(&c.Conf.Debug, "debug", false, "debug mode (default: false)")
f.BoolVar(&c.Conf.DebugSQL, "debug-sql", false, "SQL debug mode (default: false)")
f.BoolVar(&c.Conf.Quiet, "quiet", false, "quiet mode (no output)")

defaultLogDir := util.GetDefaultLogDir()
f.StringVar(&p.logDir, "log-dir", defaultLogDir, "/path/to/log")
f.BoolVar(&p.logJSON, "log-json", false, "output log as JSON")

pwd := os.Getenv("PWD")
f.StringVar(&p.dbpath, "dbpath", pwd+"/cve.sqlite3",
f.StringVar(&c.Conf.DBPath, "dbpath", pwd+"/cve.sqlite3",
"/path/to/sqlite3 or SQL connection string")

f.StringVar(&p.dbtype, "dbtype", "sqlite3",
f.StringVar(&c.Conf.DBType, "dbtype", "sqlite3",
"Database type to store data in (sqlite3, mysql, postgres or redis supported)")

f.StringVar(&p.bind,
f.StringVar(&c.Conf.Bind,
"bind",
"127.0.0.1",
"HTTP server bind to IP address (default: loop back interface)")
f.StringVar(&p.port, "port", "1323",
f.StringVar(&c.Conf.Port, "port", "1323",
"HTTP server port number (default: 1323)")
}

// Execute execute
func (p *ServerCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
c.Conf.Debug = p.debug
c.Conf.Quiet = p.quiet
log.SetLogger(p.logDir, c.Conf.Quiet, c.Conf.Debug, p.logJSON)

c.Conf.DebugSQL = p.debugSQL
c.Conf.Bind = p.bind
c.Conf.Port = p.port
c.Conf.DBPath = p.dbpath
c.Conf.DBType = p.dbtype

if !c.Conf.Validate() {
return subcommands.ExitUsageError
}
Expand Down
22 changes: 11 additions & 11 deletions log/log.go
Expand Up @@ -12,19 +12,19 @@ import (

// SetLogger set logger
func SetLogger(logDir string, quiet, debug, logJSON bool) {
stderrHundler := logger.StderrHandler
stderrHandler := logger.StderrHandler
logFormat := logger.LogfmtFormat()
if logJSON {
logFormat = logger.JsonFormatEx(false, true)
stderrHundler = logger.StreamHandler(os.Stderr, logFormat)
stderrHandler = logger.StreamHandler(os.Stderr, logFormat)
}

lvlHundler := logger.LvlFilterHandler(logger.LvlInfo, stderrHundler)
lvlHandler := logger.LvlFilterHandler(logger.LvlInfo, stderrHandler)
if debug {
lvlHundler = logger.LvlFilterHandler(logger.LvlDebug, stderrHundler)
lvlHandler = logger.LvlFilterHandler(logger.LvlDebug, stderrHandler)
}
if quiet {
lvlHundler = logger.LvlFilterHandler(logger.LvlDebug, logger.DiscardHandler())
lvlHandler = logger.LvlFilterHandler(logger.LvlDebug, logger.DiscardHandler())
pp.SetDefaultOutput(ioutil.Discard)
}

Expand All @@ -33,23 +33,23 @@ func SetLogger(logDir string, quiet, debug, logJSON bool) {
logger.Error("Failed to create a log directory", "err", err)
}
}
var hundler logger.Handler
var handler logger.Handler
if _, err := os.Stat(logDir); err == nil {
logPath := filepath.Join(logDir, "cve-dictionary.log")
if err := ioutil.WriteFile(logPath, []byte{}, 0700); err != nil {
logger.Error("Failed to create a log file", "err", err)
hundler = lvlHundler
handler = lvlHandler
} else {
hundler = logger.MultiHandler(
handler = logger.MultiHandler(
logger.Must.FileHandler(logPath, logFormat),
lvlHundler,
lvlHandler,
)
}

} else {
hundler = lvlHundler
handler = lvlHandler
}
logger.Root().SetHandler(hundler)
logger.Root().SetHandler(handler)
}

// Debugf is wrapper function
Expand Down
11 changes: 6 additions & 5 deletions server/server.go
Expand Up @@ -31,12 +31,13 @@ func Start(logDir string, driver db.DB) error {
}
f, err := os.OpenFile(logPath, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
return err
log.Errorf("Failed to create a log file: %s", err)
} else {
defer f.Close()
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Output: f,
}))
}
defer f.Close()
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Output: f,
}))

// Routes
e.GET("/health", health())
Expand Down

0 comments on commit a2bfda9

Please sign in to comment.