Skip to content

Commit 0b51049

Browse files
wadda0714和田皓翔MaineK00n
authored
fix(cmd/saas): add timeout option (#2183)
* add: timeout option to ./vuls saas * fix * fix * fix * add: config and proxy to saas writer * delete comment Co-authored-by: MaineK00n <[email protected]> --------- Co-authored-by: 和田皓翔 <wadahiroka@wadda> Co-authored-by: MaineK00n <[email protected]>
1 parent b4bd2ac commit 0b51049

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

saas/saas.go

+11-8
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ import (
2626
)
2727

2828
// Writer writes results to SaaS
29-
type Writer struct{}
29+
type Writer struct {
30+
Cnf config.SaasConf
31+
Proxy string
32+
TimeoutSec int
33+
}
3034

3135
// TempCredential : TempCredential
3236
type TempCredential struct {
@@ -57,8 +61,8 @@ func (w Writer) Write(rs ...models.ScanResult) error {
5761
hostname, _ := os.Hostname()
5862

5963
payload := payload{
60-
GroupID: config.Conf.Saas.GroupID,
61-
Token: config.Conf.Saas.Token,
64+
GroupID: w.Cnf.GroupID,
65+
Token: w.Cnf.Token,
6266
ScannedBy: hostname,
6367
ScannedIPv4s: strings.Join(ipv4s, ", "),
6468
ScannedIPv6s: strings.Join(ipv6s, ", "),
@@ -68,16 +72,15 @@ func (w Writer) Write(rs ...models.ScanResult) error {
6872
return xerrors.Errorf("Failed to Marshal to JSON: %w", err)
6973
}
7074

71-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
72-
req, err := http.NewRequestWithContext(ctx, http.MethodPost, config.Conf.Saas.URL, bytes.NewBuffer(body))
75+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(w.TimeoutSec)*time.Second)
76+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, w.Cnf.URL, bytes.NewBuffer(body))
7377
defer cancel()
7478
if err != nil {
7579
return err
7680
}
7781
req.Header.Set("Content-Type", "application/json")
7882
req.Header.Set("Accept", "application/json")
79-
// TODO Don't use global variable
80-
client, err := util.GetHTTPClient(config.Conf.HTTPProxy)
83+
client, err := util.GetHTTPClient(w.Proxy)
8184
if err != nil {
8285
return err
8386
}
@@ -107,7 +110,7 @@ func (w Writer) Write(rs ...models.ScanResult) error {
107110
return xerrors.Errorf("Failed to load config. err: %w", err)
108111
}
109112
// For S3 upload of aws sdk
110-
if err := os.Setenv("HTTPS_PROXY", config.Conf.HTTPProxy); err != nil {
113+
if err := os.Setenv("HTTPS_PROXY", w.Proxy); err != nil {
111114
return xerrors.Errorf("Failed to set HTTP proxy: %s", err)
112115
}
113116

subcmds/saas.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
// SaaSCmd is subcommand for FutureVuls
1919
type SaaSCmd struct {
2020
configPath string
21+
timeoutSec int
2122
}
2223

2324
// Name return subcommand name
@@ -35,6 +36,7 @@ func (*SaaSCmd) Usage() string {
3536
[-log-to-file]
3637
[-log-dir=/path/to/log]
3738
[-http-proxy=http://192.168.0.1:8080]
39+
[-timeout=10]
3840
[-debug]
3941
[-quiet]
4042
`
@@ -56,6 +58,10 @@ func (p *SaaSCmd) SetFlags(f *flag.FlagSet) {
5658
f.StringVar(&config.Conf.LogDir, "log-dir", defaultLogDir, "/path/to/log")
5759
f.BoolVar(&config.Conf.LogToFile, "log-to-file", false, "Output log to file")
5860

61+
f.IntVar(&p.timeoutSec, "timeout", 10,
62+
"Number of seconds for uploading scan reports to saas",
63+
)
64+
5965
f.StringVar(
6066
&config.Conf.HTTPProxy, "http-proxy", "",
6167
"http://proxy-url:port (default: empty)")
@@ -114,7 +120,11 @@ func (p *SaaSCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
114120
return subcommands.ExitFailure
115121
}
116122

117-
var w reporter.ResultWriter = saas.Writer{}
123+
w := saas.Writer{
124+
Cnf: config.Conf.Saas,
125+
Proxy: config.Conf.HTTPProxy,
126+
TimeoutSec: p.timeoutSec,
127+
}
118128
if err := w.Write(res...); err != nil {
119129
logging.Log.Errorf("Failed to upload. err: %+v", err)
120130
return subcommands.ExitFailure

0 commit comments

Comments
 (0)