Skip to content

Commit 4d4abc3

Browse files
committed
Moved staticscantimeout as a local flag to cicheck
1 parent eaa3f3a commit 4d4abc3

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

appknox/sarif_generator.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/iancoleman/strcase"
1414
"github.com/vbauerster/mpb/v4"
1515
"github.com/vbauerster/mpb/v4/decor"
16-
"github.com/spf13/viper"
1716
)
1817

1918
type SARIF struct {
@@ -120,7 +119,7 @@ func GenerateSARIFGivenFileID(client *Client, fileID int, riskThreshold int) (SA
120119
sarifReportProgess = file.StaticScanProgress
121120
bar.SetCurrent(int64(sarifReportProgess), time.Since(start))
122121

123-
if time.Since(start) > time.Duration(viper.GetInt("timeout")) * time.Minute {
122+
if time.Since(start) > 15*time.Minute {
124123
err := errors.New("Request timed out")
125124
PrintError(err)
126125
os.Exit(1)

cmd/cicheck.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"strconv"
77
"strings"
8+
"time"
89

910
"github.com/appknox/appknox-go/helper"
1011
"github.com/spf13/cobra"
@@ -45,12 +46,18 @@ var cicheckCmd = &cobra.Command{
4546
helper.PrintError(err)
4647
os.Exit(1)
4748
}
48-
helper.ProcessCiCheck(fileID, riskThresholdInt)
49+
timeoutMinutes, _ := cmd.Flags().GetInt("sast-timeout")
50+
timeout := time.Duration(timeoutMinutes) * time.Minute
51+
52+
helper.ProcessCiCheck(fileID, riskThresholdInt, timeout)
4953
},
5054
}
5155

5256
func init() {
5357
RootCmd.AddCommand(cicheckCmd)
5458
cicheckCmd.Flags().StringP(
5559
"risk-threshold", "r", "low", "Risk threshold to fail the command. Available options: low, medium, high")
60+
cicheckCmd.Flags().IntP(
61+
"sast-timeout", "t", 30, "Timeout in minutes for the CI check (default: 30)")
62+
5663
}

cmd/root.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ func init() {
4040
viper.BindEnv("host", "APPKNOX_API_HOST")
4141

4242
// Define flags globally here for all subcommands
43-
RootCmd.PersistentFlags().String("timeout", "", "Timeout for Appknox scanner, default is 30 minutes")
44-
viper.BindPFlag("timeout", RootCmd.PersistentFlags().Lookup("timeout"))
45-
viper.BindEnv("timeout", "APPKNOX_TIMEOUT")
46-
viper.SetDefault("timeout", "30") // Default to 30 minutes
4743
RootCmd.PersistentFlags().String("region", "", "Region names, e.g., global, saudi, uae. By default, global is used")
4844
viper.BindPFlag("region", RootCmd.PersistentFlags().Lookup("region"))
4945
viper.BindEnv("region", "APPKNOX_API_REGION")

helper/cicheck.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,27 @@ import (
99

1010
"github.com/appknox/appknox-go/appknox"
1111
"github.com/appknox/appknox-go/appknox/enums"
12-
"github.com/spf13/viper"
1312
"github.com/cheynewallace/tabby"
1413
"github.com/vbauerster/mpb/v4"
1514
"github.com/vbauerster/mpb/v4/decor"
1615
)
1716

1817
// ProcessCiCheck takes the list of analyses and print it to CLI.
19-
func ProcessCiCheck(fileID, riskThreshold int) {
18+
func ProcessCiCheck(fileID, riskThreshold int, staticScanTimeout time.Duration) {
19+
// Add timeout validation
20+
const minTimeout=1;//1 minute
21+
const maxTimeout=240;//4 hours
22+
23+
if staticScanTimeout < minTimeout*time.Minute || staticScanTimeout > maxTimeout*time.Minute {
24+
errMsg := fmt.Sprintf("Error: timeout must be between %v minute and %v minutes", minTimeout, maxTimeout)
25+
fmt.Println(errMsg) // Print error message to standard output
26+
os.Exit(1)
27+
}
2028
ctx := context.Background()
2129
client := getClient()
2230
var staticScanProgess int
2331
start := time.Now()
24-
timeoutMinutes := viper.GetInt("timeout")
25-
timeout := time.Duration(timeoutMinutes) * time.Minute
26-
fmt.Printf("Starting scan at: %v with timeout of %d minutes\n", start.Format(time.RFC3339), timeoutMinutes)
32+
fmt.Printf("Starting scan at: %v with timeout of %v\n", start.Format(time.RFC3339), staticScanTimeout)
2733
p := mpb.New(
2834
mpb.WithWidth(60),
2935
mpb.WithRefreshRate(180*time.Millisecond),
@@ -49,7 +55,7 @@ func ProcessCiCheck(fileID, riskThreshold int) {
4955
staticScanProgess = file.StaticScanProgress
5056
bar.SetCurrent(int64(staticScanProgess), time.Since(start))
5157

52-
if time.Since(start) > timeout {
58+
if time.Since(start) > staticScanTimeout {
5359
err := errors.New("Request timed out")
5460
PrintError(err)
5561
os.Exit(1)

0 commit comments

Comments
 (0)