Skip to content

Commit 6df9536

Browse files
authored
Merge pull request #54 from appknox/custom-timeout
timeout support added
2 parents 3b69b96 + c5ce9ca commit 6df9536

File tree

6 files changed

+40
-15
lines changed

6 files changed

+40
-15
lines changed

appknox/sarif_generator.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ type Help struct {
9090
Markdown string `json:"markdown,omitempty"`
9191
}
9292

93-
func GenerateSARIFGivenFileID(client *Client, fileID int, riskThreshold int) (SARIF, error) {
93+
func GenerateSARIFGivenFileID(client *Client, fileID int, riskThreshold int,staticScanTimeout time.Duration) (SARIF, error) {
9494
ctx := context.Background()
9595
var sarifReportProgess int
9696
start := time.Now()
@@ -109,7 +109,7 @@ func GenerateSARIFGivenFileID(client *Client, fileID int, riskThreshold int) (SA
109109
decor.Name("] "),
110110
),
111111
)
112-
112+
113113
for sarifReportProgess < 100 {
114114
file, _, err := client.Files.GetByID(ctx, fileID)
115115
if err != nil {
@@ -118,7 +118,8 @@ func GenerateSARIFGivenFileID(client *Client, fileID int, riskThreshold int) (SA
118118
}
119119
sarifReportProgess = file.StaticScanProgress
120120
bar.SetCurrent(int64(sarifReportProgess), time.Since(start))
121-
if time.Since(start) > 15*time.Minute {
121+
122+
if time.Since(start) > staticScanTimeout {
122123
err := errors.New("Request timed out")
123124
PrintError(err)
124125
os.Exit(1)
@@ -263,7 +264,7 @@ func GenerateSARIFGivenFileID(client *Client, fileID int, riskThreshold int) (SA
263264
}
264265

265266
func PrintError(err error) {
266-
panic("unimplemented")
267+
panic(err)
267268
}
268269

269270
func GenerateSARIFFileContent(sarif SARIF) (string, error) {

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("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+
"timeout", "t", 30, "Static scan timeout in minutes for the CI check (default: 30)")
62+
5663
}

cmd/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7-
7+
88
// "github.com/appknox/appknox-go/appknox"
99
"github.com/spf13/cobra"
1010
"github.com/spf13/viper"
@@ -39,7 +39,7 @@ func init() {
3939
viper.BindPFlag("host", RootCmd.PersistentFlags().Lookup("host"))
4040
viper.BindEnv("host", "APPKNOX_API_HOST")
4141

42-
42+
// Define flags globally here for all subcommands
4343
RootCmd.PersistentFlags().String("region", "", "Region names, e.g., global, saudi, uae. By default, global is used")
4444
viper.BindPFlag("region", RootCmd.PersistentFlags().Lookup("region"))
4545
viper.BindEnv("region", "APPKNOX_API_REGION")

cmd/sarif.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import (
55
"os"
66
"strconv"
77
"strings"
8-
8+
"time"
9+
910
"github.com/appknox/appknox-go/helper"
1011
"github.com/spf13/cobra"
1112
)
@@ -45,7 +46,9 @@ var sarifCmd = &cobra.Command{
4546
os.Exit(1)
4647
}
4748
outputFilePath, _ := cmd.Flags().GetString("output")
48-
helper.ConvertToSARIFReport(fileID,riskThresholdInt,outputFilePath)
49+
timeoutMinutes, _ := cmd.Flags().GetInt("timeout")
50+
timeout := time.Duration(timeoutMinutes) * time.Minute
51+
helper.ConvertToSARIFReport(fileID,riskThresholdInt,outputFilePath,timeout)
4952
},
5053
}
5154

@@ -54,4 +57,6 @@ func init() {
5457
sarifCmd.Flags().StringP(
5558
"risk-threshold", "r", "low", "Risk threshold to fail the command. Available options: low, medium, high")
5659
sarifCmd.PersistentFlags().StringP("output", "o", "report.sarif", "Output file path to save reports")
60+
sarifCmd.Flags().IntP(
61+
"timeout", "t", 30, "Static scan timeout in minutes for the CI check (default: 30)")
5762
}

helper/cicheck.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"fmt"
77
"os"
88
"time"
9-
9+
1010
"github.com/appknox/appknox-go/appknox"
1111
"github.com/appknox/appknox-go/appknox/enums"
1212
"github.com/cheynewallace/tabby"
@@ -15,11 +15,21 @@ import (
1515
)
1616

1717
// ProcessCiCheck takes the list of analyses and print it to CLI.
18-
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+
}
1928
ctx := context.Background()
2029
client := getClient()
2130
var staticScanProgess int
2231
start := time.Now()
32+
fmt.Printf("Starting scan at: %v with timeout of %v\n", start.Format(time.RFC3339), staticScanTimeout)
2333
p := mpb.New(
2434
mpb.WithWidth(60),
2535
mpb.WithRefreshRate(180*time.Millisecond),
@@ -44,7 +54,8 @@ func ProcessCiCheck(fileID, riskThreshold int) {
4454
}
4555
staticScanProgess = file.StaticScanProgress
4656
bar.SetCurrent(int64(staticScanProgess), time.Since(start))
47-
if time.Since(start) > 30*time.Minute {
57+
58+
if time.Since(start) > staticScanTimeout {
4859
err := errors.New("Request timed out")
4960
PrintError(err)
5061
os.Exit(1)

helper/sarif.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package helper
33
import (
44
"fmt"
55
"os"
6-
6+
"time"
7+
78
"github.com/appknox/appknox-go/appknox"
89
)
910

10-
func ConvertToSARIFReport(fileID int, riskThreshold int, filePath string) error {
11+
func ConvertToSARIFReport(fileID int, riskThreshold int, filePath string,staticScanTimeout time.Duration) error {
1112
client := getClient()
12-
sarif, err := appknox.GenerateSARIFGivenFileID(client, fileID, riskThreshold)
13+
sarif, err := appknox.GenerateSARIFGivenFileID(client, fileID, riskThreshold,staticScanTimeout)
1314
if err != nil {
1415
return err
1516
}

0 commit comments

Comments
 (0)