-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
fetch-oracle.go
146 lines (124 loc) · 3.92 KB
/
fetch-oracle.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package commands
import (
"context"
"encoding/xml"
"flag"
"os"
"strings"
"time"
"github.com/google/subcommands"
"github.com/inconshreveable/log15"
c "github.com/kotakanbe/goval-dictionary/config"
"github.com/kotakanbe/goval-dictionary/db"
"github.com/kotakanbe/goval-dictionary/fetcher"
"github.com/kotakanbe/goval-dictionary/models"
"github.com/kotakanbe/goval-dictionary/util"
"github.com/ymomoi/goval-parser/oval"
)
// FetchOracleCmd is Subcommand for fetch Oracle OVAL
type FetchOracleCmd struct {
Debug bool
DebugSQL bool
Quiet bool
LogDir string
LogJSON bool
DBPath string
DBType string
HTTPProxy string
}
// Name return subcommand name
func (*FetchOracleCmd) Name() string { return "fetch-oracle" }
// Synopsis return synopsis
func (*FetchOracleCmd) Synopsis() string { return "Fetch Vulnerability dictionary from Oracle" }
// Usage return usage
func (*FetchOracleCmd) Usage() string {
return `fetch-oracle:
fetch-oracle
[-dbtype=sqlite3|mysql|postgres|redis]
[-dbpath=$PWD/oval.sqlite3 or connection string]
[-http-proxy=http://192.168.0.1:8080]
[-debug]
[-debug-sql]
[-quiet]
[-log-dir=/path/to/log]
[-log-json]
For details, see https://github.com/kotakanbe/goval-dictionary#usage-fetch-oval-data-from-oracle
$ goval-dictionary fetch-oracle
`
}
// SetFlags set flag
func (p *FetchOracleCmd) 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)")
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+"/oval.sqlite3",
"/path/to/sqlite3 or SQL connection string")
f.StringVar(&p.DBType, "dbtype", "sqlite3",
"Database type to store data in (sqlite3, mysql, postgres or redis supported)")
f.StringVar(
&p.HTTPProxy,
"http-proxy",
"",
"http://proxy-url:port (default: empty)",
)
}
// Execute execute
func (p *FetchOracleCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
c.Conf.Quiet = p.Quiet
c.Conf.DebugSQL = p.DebugSQL
c.Conf.Debug = p.Debug
c.Conf.DBPath = p.DBPath
c.Conf.DBType = p.DBType
c.Conf.HTTPProxy = p.HTTPProxy
util.SetLogger(p.LogDir, c.Conf.Quiet, c.Conf.Debug, p.LogJSON)
if !c.Conf.Validate() {
return subcommands.ExitUsageError
}
results, err := fetcher.FetchOracleFiles()
if err != nil {
log15.Error("Failed to fetch files.", "err", err)
return subcommands.ExitFailure
}
var driver db.DB
if driver, err = db.NewDB(c.Oracle, c.Conf.DBType, c.Conf.DBPath, c.Conf.DebugSQL); err != nil {
log15.Error("Failed to new db.", "err", err)
return subcommands.ExitFailure
}
defer driver.CloseDB()
for _, r := range results {
ovalroot := oval.Root{}
if err = xml.Unmarshal(r.Body, &ovalroot); err != nil {
log15.Error("Failed to unmarshal.", "url", r.URL, "err", err)
return subcommands.ExitUsageError
}
log15.Info("Fetched", "URL", r.URL, "OVAL definitions", len(ovalroot.Definitions.Definitions))
// var timeformat = "2006-01-02T15:04:05.999-07:00"
var timeformat = "2006-01-02T15:04:05"
t, err := time.Parse(timeformat, strings.Split(ovalroot.Generator.Timestamp, ".")[0])
if err != nil {
panic(err)
}
ss := strings.Split(r.URL, "/")
fmeta := models.FetchMeta{
Timestamp: t,
FileName: ss[len(ss)-1],
}
roots := models.ConvertOracleToModel(&ovalroot)
for _, root := range roots {
root.Timestamp = time.Now()
if err := driver.InsertOval(&root, fmeta); err != nil {
log15.Error("Failed to insert oval.", "err", err)
return subcommands.ExitFailure
}
}
if err := driver.InsertFetchMeta(fmeta); err != nil {
log15.Error("Failed to insert meta.", "err", err)
return subcommands.ExitFailure
}
}
return subcommands.ExitSuccess
}