-
Notifications
You must be signed in to change notification settings - Fork 5
/
fetchjvn.go
119 lines (99 loc) · 2.73 KB
/
fetchjvn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package commands
import (
"flag"
"fmt"
"os"
"time"
"github.com/google/subcommands"
c "github.com/kotakanbe/go-cwe-dictionary/config"
"github.com/kotakanbe/go-cwe-dictionary/golang"
log "github.com/kotakanbe/go-cwe-dictionary/log"
"github.com/kotakanbe/go-cwe-dictionary/models"
"golang.org/x/net/context"
jvn "github.com/kotakanbe/go-cve-dictionary/fetcher/jvn/xml"
cvedictlog "github.com/kotakanbe/go-cve-dictionary/log"
jvnmodels "github.com/kotakanbe/go-cve-dictionary/models"
)
// FetchJVNCmd is Subcommand for fetch Nvd information.
type FetchJVNCmd struct {
debug bool
debugSQL bool
dbpath string
httpProxy string
goconst bool
sqlite3 bool
}
// Name return subcommand name
func (*FetchJVNCmd) Name() string { return "fetchjvn" }
// Synopsis return synopsis
func (*FetchJVNCmd) Synopsis() string { return "Fetch CWE" }
// Usage return usage
func (*FetchJVNCmd) Usage() string {
return `fetch:
fetch
[-dbpath=/path/to/cwe.sqlite3]
[-http-proxy=http://192.168.0.1:8080]
[-sqlite3]
[-debug]
[-debug-sql]
`
}
// 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.sqlite3, "sqlite3", false,
"Print Go Const definition to stdout")
f.BoolVar(&p.goconst, "go", true,
"Print Go Const definition to stdout")
pwd := os.Getenv("PWD")
f.StringVar(&p.dbpath, "dbpath", pwd+"/cwe.sqlite3", "/path/to/sqlite3")
f.StringVar(
&p.httpProxy,
"http-proxy",
"",
"http://proxy-url:port (default: empty)",
)
}
// Execute execute
func (p *FetchJVNCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
c.Conf.Debug = true
c.Conf.DebugSQL = p.debugSQL
if c.Conf.Debug {
log.SetDebug()
}
c.Conf.DBPath = p.dbpath
c.Conf.HTTPProxy = p.httpProxy
if !c.Conf.Validate() {
return subcommands.ExitUsageError
}
cvedictlog.Initialize("/tmp", p.debug, os.Stderr)
urls := []string{}
thisYear := time.Now().Year()
for i := 1998; i <= thisYear; i++ {
url := fmt.Sprintf("https://jvndb.jvn.jp/ja/rss/years/jvndb_%d.rdf", i)
urls = append(urls, url)
}
urls = append(urls,
"https://jvndb.jvn.jp/ja/rss/jvndb.rdf",
"https://jvndb.jvn.jp/ja/rss/jvndb_new.rdf")
needUpdates := []jvnmodels.FeedMeta{}
for _, u := range urls {
needUpdates = append(needUpdates, jvnmodels.FeedMeta{URL: u})
}
items, err := jvn.Fetch(needUpdates)
if err != nil {
log.Errorf("Failed to fetch JVN: %s", err)
return subcommands.ExitUsageError
}
cwes := models.ConvertToModelJVN(items)
code, err := golang.GenerateJVN(cwes.Uniq())
if err != nil {
log.Error(err)
return subcommands.ExitFailure
}
fmt.Println(code)
return subcommands.ExitSuccess
}