Skip to content
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

timeout support added #54

Merged
merged 4 commits into from
Dec 12, 2024
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
9 changes: 5 additions & 4 deletions appknox/sarif_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ type Help struct {
Markdown string `json:"markdown,omitempty"`
}

func GenerateSARIFGivenFileID(client *Client, fileID int, riskThreshold int) (SARIF, error) {
func GenerateSARIFGivenFileID(client *Client, fileID int, riskThreshold int,staticScanTimeout time.Duration) (SARIF, error) {
ctx := context.Background()
var sarifReportProgess int
start := time.Now()
Expand All @@ -109,7 +109,7 @@ func GenerateSARIFGivenFileID(client *Client, fileID int, riskThreshold int) (SA
decor.Name("] "),
),
)

for sarifReportProgess < 100 {
file, _, err := client.Files.GetByID(ctx, fileID)
if err != nil {
Expand All @@ -118,7 +118,8 @@ func GenerateSARIFGivenFileID(client *Client, fileID int, riskThreshold int) (SA
}
sarifReportProgess = file.StaticScanProgress
bar.SetCurrent(int64(sarifReportProgess), time.Since(start))
if time.Since(start) > 15*time.Minute {

if time.Since(start) > staticScanTimeout {
err := errors.New("Request timed out")
PrintError(err)
os.Exit(1)
Expand Down Expand Up @@ -263,7 +264,7 @@ func GenerateSARIFGivenFileID(client *Client, fileID int, riskThreshold int) (SA
}

func PrintError(err error) {
panic("unimplemented")
panic(err)
}

func GenerateSARIFFileContent(sarif SARIF) (string, error) {
Expand Down
9 changes: 8 additions & 1 deletion cmd/cicheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"strconv"
"strings"
"time"

"github.com/appknox/appknox-go/helper"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -45,12 +46,18 @@ var cicheckCmd = &cobra.Command{
helper.PrintError(err)
os.Exit(1)
}
helper.ProcessCiCheck(fileID, riskThresholdInt)
timeoutMinutes, _ := cmd.Flags().GetInt("timeout")
timeout := time.Duration(timeoutMinutes) * time.Minute

helper.ProcessCiCheck(fileID, riskThresholdInt, timeout)
},
}

func init() {
RootCmd.AddCommand(cicheckCmd)
cicheckCmd.Flags().StringP(
"risk-threshold", "r", "low", "Risk threshold to fail the command. Available options: low, medium, high")
cicheckCmd.Flags().IntP(
"timeout", "t", 30, "Static scan timeout in minutes for the CI check (default: 30)")

}
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"os"
"path/filepath"

// "github.com/appknox/appknox-go/appknox"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -39,7 +39,7 @@ func init() {
viper.BindPFlag("host", RootCmd.PersistentFlags().Lookup("host"))
viper.BindEnv("host", "APPKNOX_API_HOST")


// Define flags globally here for all subcommands
RootCmd.PersistentFlags().String("region", "", "Region names, e.g., global, saudi, uae. By default, global is used")
viper.BindPFlag("region", RootCmd.PersistentFlags().Lookup("region"))
viper.BindEnv("region", "APPKNOX_API_REGION")
Expand Down
9 changes: 7 additions & 2 deletions cmd/sarif.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"os"
"strconv"
"strings"

"time"

"github.com/appknox/appknox-go/helper"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -45,7 +46,9 @@ var sarifCmd = &cobra.Command{
os.Exit(1)
}
outputFilePath, _ := cmd.Flags().GetString("output")
helper.ConvertToSARIFReport(fileID,riskThresholdInt,outputFilePath)
timeoutMinutes, _ := cmd.Flags().GetInt("timeout")
timeout := time.Duration(timeoutMinutes) * time.Minute
helper.ConvertToSARIFReport(fileID,riskThresholdInt,outputFilePath,timeout)
},
}

Expand All @@ -54,4 +57,6 @@ func init() {
sarifCmd.Flags().StringP(
"risk-threshold", "r", "low", "Risk threshold to fail the command. Available options: low, medium, high")
sarifCmd.PersistentFlags().StringP("output", "o", "report.sarif", "Output file path to save reports")
sarifCmd.Flags().IntP(
"timeout", "t", 30, "Static scan timeout in minutes for the CI check (default: 30)")
}
17 changes: 14 additions & 3 deletions helper/cicheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"os"
"time"

"github.com/appknox/appknox-go/appknox"
"github.com/appknox/appknox-go/appknox/enums"
"github.com/cheynewallace/tabby"
Expand All @@ -15,11 +15,21 @@ import (
)

// ProcessCiCheck takes the list of analyses and print it to CLI.
func ProcessCiCheck(fileID, riskThreshold int) {
func ProcessCiCheck(fileID, riskThreshold int, staticScanTimeout time.Duration) {
// Add timeout validation
const minTimeout=1;//1 minute
const maxTimeout=240;//4 hours

if staticScanTimeout < minTimeout*time.Minute || staticScanTimeout > maxTimeout*time.Minute {
errMsg := fmt.Sprintf("Error: timeout must be between %v minute and %v minutes", minTimeout, maxTimeout)
fmt.Println(errMsg) // Print error message to standard output
os.Exit(1)
}
ctx := context.Background()
client := getClient()
var staticScanProgess int
start := time.Now()
fmt.Printf("Starting scan at: %v with timeout of %v\n", start.Format(time.RFC3339), staticScanTimeout)
p := mpb.New(
mpb.WithWidth(60),
mpb.WithRefreshRate(180*time.Millisecond),
Expand All @@ -44,7 +54,8 @@ func ProcessCiCheck(fileID, riskThreshold int) {
}
staticScanProgess = file.StaticScanProgress
bar.SetCurrent(int64(staticScanProgess), time.Since(start))
if time.Since(start) > 30*time.Minute {

if time.Since(start) > staticScanTimeout {
err := errors.New("Request timed out")
PrintError(err)
os.Exit(1)
Expand Down
7 changes: 4 additions & 3 deletions helper/sarif.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package helper
import (
"fmt"
"os"

"time"

"github.com/appknox/appknox-go/appknox"
)

func ConvertToSARIFReport(fileID int, riskThreshold int, filePath string) error {
func ConvertToSARIFReport(fileID int, riskThreshold int, filePath string,staticScanTimeout time.Duration) error {
client := getClient()
sarif, err := appknox.GenerateSARIFGivenFileID(client, fileID, riskThreshold)
sarif, err := appknox.GenerateSARIFGivenFileID(client, fileID, riskThreshold,staticScanTimeout)
if err != nil {
return err
}
Expand Down
Loading