Skip to content

fix(cmd/saas): add timeout option #2183

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 6 commits into from
Apr 23, 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
19 changes: 11 additions & 8 deletions saas/saas.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ import (
)

// Writer writes results to SaaS
type Writer struct{}
type Writer struct {
Cnf config.SaasConf
Proxy string
TimeoutSec int
}

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

payload := payload{
GroupID: config.Conf.Saas.GroupID,
Token: config.Conf.Saas.Token,
GroupID: w.Cnf.GroupID,
Token: w.Cnf.Token,
ScannedBy: hostname,
ScannedIPv4s: strings.Join(ipv4s, ", "),
ScannedIPv6s: strings.Join(ipv6s, ", "),
Expand All @@ -68,16 +72,15 @@ func (w Writer) Write(rs ...models.ScanResult) error {
return xerrors.Errorf("Failed to Marshal to JSON: %w", err)
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, config.Conf.Saas.URL, bytes.NewBuffer(body))
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(w.TimeoutSec)*time.Second)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, w.Cnf.URL, bytes.NewBuffer(body))
defer cancel()
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
// TODO Don't use global variable
client, err := util.GetHTTPClient(config.Conf.HTTPProxy)
client, err := util.GetHTTPClient(w.Proxy)
if err != nil {
return err
}
Expand Down Expand Up @@ -107,7 +110,7 @@ func (w Writer) Write(rs ...models.ScanResult) error {
return xerrors.Errorf("Failed to load config. err: %w", err)
}
// For S3 upload of aws sdk
if err := os.Setenv("HTTPS_PROXY", config.Conf.HTTPProxy); err != nil {
if err := os.Setenv("HTTPS_PROXY", w.Proxy); err != nil {
return xerrors.Errorf("Failed to set HTTP proxy: %s", err)
}

Expand Down
12 changes: 11 additions & 1 deletion subcmds/saas.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
// SaaSCmd is subcommand for FutureVuls
type SaaSCmd struct {
configPath string
timeoutSec int
}

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

f.IntVar(&p.timeoutSec, "timeout", 10,
"Number of seconds for uploading scan reports to saas",
)

f.StringVar(
&config.Conf.HTTPProxy, "http-proxy", "",
"http://proxy-url:port (default: empty)")
Expand Down Expand Up @@ -114,7 +120,11 @@ func (p *SaaSCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
return subcommands.ExitFailure
}

var w reporter.ResultWriter = saas.Writer{}
w := saas.Writer{
Cnf: config.Conf.Saas,
Proxy: config.Conf.HTTPProxy,
TimeoutSec: p.timeoutSec,
}
if err := w.Write(res...); err != nil {
logging.Log.Errorf("Failed to upload. err: %+v", err)
return subcommands.ExitFailure
Expand Down
Loading