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

fix(codeqlExecuteScan): added waiting for the SARIF file upload #4409

Merged
merged 14 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from 11 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
49 changes: 44 additions & 5 deletions cmd/codeqlExecuteScan.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package cmd

import (
"bytes"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"time"

"github.com/SAP/jenkins-library/pkg/codeql"
"github.com/SAP/jenkins-library/pkg/command"
Expand Down Expand Up @@ -160,7 +162,7 @@ func getToken(config *codeqlExecuteScanOptions) (bool, string) {
return false, ""
}

func uploadResults(config *codeqlExecuteScanOptions, repoInfo RepoInfo, token string, utils codeqlExecuteScanUtils) error {
func uploadResults(config *codeqlExecuteScanOptions, repoInfo RepoInfo, token string, utils codeqlExecuteScanUtils) (string, error) {
cmd := []string{"github", "upload-results", "--sarif=" + filepath.Join(config.ModulePath, "target", "codeqlReport.sarif")}

if config.GithubToken != "" {
Expand All @@ -185,13 +187,46 @@ func uploadResults(config *codeqlExecuteScanOptions, repoInfo RepoInfo, token st

//if no git pramas are passed(commitId, reference, serverUrl, repository), then codeql tries to auto populate it based on git information of the checkout repository.
//It also depends on the orchestrator. Some orchestrator keep git information and some not.

var buffer bytes.Buffer
utils.Stdout(&buffer)
err := execute(utils, cmd, GeneralConfig.Verbose)
if err != nil {
log.Entry().Error("failed to upload sarif results")
return err
return "", err
}
utils.Stdout(log.Writer())

return nil
url := buffer.String()
return strings.TrimSpace(url), nil
vlkl-sap marked this conversation as resolved.
Show resolved Hide resolved
}

func waitSarifUploaded(config *codeqlExecuteScanOptions, codeqlSarifUploader codeql.CodeqlSarifUploader) error {
var sarifUploadComplete = "complete"
var sarifUploadFailed = "failed"
maxRetries := config.SarifCheckMaxRetries
retryInterval := time.Duration(config.SarifCheckRetryInterval) * time.Second

log.Entry().Info("waiting for the SARIF to upload")
for i := 1; i <= maxRetries; i++ {
sarifStatus, err := codeqlSarifUploader.GetSarifStatus()
if err != nil {
log.Entry().Errorf("error while checking sarif status: %s, retrying in %d seconds... (retry %d/%d)", err, retryInterval, i, maxRetries)
time.Sleep(retryInterval)
continue
}
vlkl-sap marked this conversation as resolved.
Show resolved Hide resolved
if sarifStatus.ProcessingStatus == sarifUploadComplete {
return nil
}
if sarifStatus.ProcessingStatus == sarifUploadFailed {
for e := range sarifStatus.Errors {
log.Entry().Error(e)
}
return errors.New("failed to upload sarif file")
}
time.Sleep(retryInterval)
}
return errors.New("failed to check sarif uploading status: max retries reached")
}

func runCodeqlExecuteScan(config *codeqlExecuteScanOptions, telemetryData *telemetry.CustomData, utils codeqlExecuteScanUtils) ([]piperutils.Path, error) {
Expand Down Expand Up @@ -275,11 +310,15 @@ func runCodeqlExecuteScan(config *codeqlExecuteScanOptions, telemetryData *telem
return reports, errors.New("failed running upload-results as githubToken was not specified")
}

err = uploadResults(config, repoInfo, token, utils)
sarifUrl, err := uploadResults(config, repoInfo, token, utils)
if err != nil {

return reports, err
}
codeqlSarifUploader := codeql.NewCodeqlSarifUploaderInstance(sarifUrl, token)
err = waitSarifUploaded(config, &codeqlSarifUploader)
if err != nil {
return reports, errors.Wrap(err, "failed to upload sarif")
}

if config.CheckForCompliance {
codeqlScanAuditInstance := codeql.NewCodeqlScanAuditInstance(repoInfo.serverUrl, repoInfo.owner, repoInfo.repo, token, []string{})
Expand Down
22 changes: 22 additions & 0 deletions cmd/codeqlExecuteScan_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 78 additions & 6 deletions cmd/codeqlExecuteScan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ package cmd
import (
"fmt"
"testing"
"time"

"github.com/SAP/jenkins-library/pkg/codeql"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/SAP/jenkins-library/pkg/orchestrator"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -45,12 +48,6 @@ func TestRunCodeqlExecuteScan(t *testing.T) {
assert.Error(t, err)
})

t.Run("Check for compliace fails as repository not specified", func(t *testing.T) {
config := codeqlExecuteScanOptions{BuildTool: "maven", ModulePath: "./", UploadResults: true, GithubToken: "test", CheckForCompliance: true}
_, err := runCodeqlExecuteScan(&config, nil, newCodeqlExecuteScanTestsUtils())
assert.Error(t, err)
})

t.Run("Custom buildtool", func(t *testing.T) {
config := codeqlExecuteScanOptions{BuildTool: "custom", Language: "javascript", ModulePath: "./"}
_, err := runCodeqlExecuteScan(&config, nil, newCodeqlExecuteScanTestsUtils())
Expand Down Expand Up @@ -339,3 +336,78 @@ func TestCreateToolRecordCodeql(t *testing.T) {
assert.Error(t, err)
})
}

func TestWaitSarifUploaded(t *testing.T) {
t.Parallel()
config := codeqlExecuteScanOptions{SarifCheckRetryInterval: 1, SarifCheckMaxRetries: 5}
t.Run("Fast complete upload", func(t *testing.T) {
codeqlScanAuditMock := CodeqlSarifUploaderMock{counter: 0}
timerStart := time.Now()
err := waitSarifUploaded(&config, &codeqlScanAuditMock)
assert.Less(t, time.Now().Sub(timerStart), time.Second)
assert.NoError(t, err)
})
t.Run("Long completed upload", func(t *testing.T) {
codeqlScanAuditMock := CodeqlSarifUploaderMock{counter: 2}
timerStart := time.Now()
err := waitSarifUploaded(&config, &codeqlScanAuditMock)
assert.GreaterOrEqual(t, time.Now().Sub(timerStart), time.Second*2)
assert.NoError(t, err)
})
t.Run("Failed upload", func(t *testing.T) {
codeqlScanAuditMock := CodeqlSarifUploaderMock{counter: -1}
err := waitSarifUploaded(&config, &codeqlScanAuditMock)
assert.Error(t, err)
assert.ErrorContains(t, err, "failed to upload sarif file")
})
t.Run("Error while checking sarif uploading", func(t *testing.T) {
codeqlScanAuditErrorMock := CodeqlSarifUploaderErrorMock{counter: 10}
err := waitSarifUploaded(&config, &codeqlScanAuditErrorMock)
assert.Error(t, err)
assert.ErrorContains(t, err, "max retries reached")
})
t.Run("Completed upload after getting errors from server", func(t *testing.T) {
codeqlScanAuditErrorMock := CodeqlSarifUploaderErrorMock{counter: 3}
err := waitSarifUploaded(&config, &codeqlScanAuditErrorMock)
assert.NoError(t, err)
})
}

type CodeqlSarifUploaderMock struct {
counter int
}

func (c *CodeqlSarifUploaderMock) GetSarifStatus() (codeql.SarifFileInfo, error) {
if c.counter == 0 {
return codeql.SarifFileInfo{
ProcessingStatus: "complete",
Errors: nil,
}, nil
}
if c.counter == -1 {
return codeql.SarifFileInfo{
ProcessingStatus: "failed",
Errors: []string{"upload error"},
}, nil
}
c.counter--
return codeql.SarifFileInfo{
ProcessingStatus: "pending",
Errors: nil,
}, nil
}

type CodeqlSarifUploaderErrorMock struct {
counter int
}

func (c *CodeqlSarifUploaderErrorMock) GetSarifStatus() (codeql.SarifFileInfo, error) {
if c.counter == 0 {
return codeql.SarifFileInfo{
ProcessingStatus: "complete",
Errors: nil,
}, nil
}
c.counter--
return codeql.SarifFileInfo{}, errors.New("test error")
}
10 changes: 0 additions & 10 deletions pkg/codeql/codeql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,13 @@ func (g *githubCodeqlScanningMock) ListAlertsForRepo(ctx context.Context, owner,
return alerts, &response, nil
}

func (g *githubCodeqlScanningMock) ListAnalysesForRepo(ctx context.Context, owner, repo string, opts *github.AnalysesListOptions) ([]*github.ScanningAnalysis, *github.Response, error) {
resultsCount := 3
analysis := []*github.ScanningAnalysis{{ResultsCount: &resultsCount}}
return analysis, nil, nil
}

type githubCodeqlScanningErrorMock struct {
}

func (g *githubCodeqlScanningErrorMock) ListAlertsForRepo(ctx context.Context, owner, repo string, opts *github.AlertListOptions) ([]*github.Alert, *github.Response, error) {
return []*github.Alert{}, nil, errors.New("Some error")
}

func (g *githubCodeqlScanningErrorMock) ListAnalysesForRepo(ctx context.Context, owner, repo string, opts *github.AnalysesListOptions) ([]*github.ScanningAnalysis, *github.Response, error) {
return []*github.ScanningAnalysis{}, nil, errors.New("Some error")
}

func TestGetVulnerabilitiesFromClient(t *testing.T) {
ctx := context.Background()
t.Parallel()
Expand Down
60 changes: 60 additions & 0 deletions pkg/codeql/sarif_upload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package codeql

import (
"encoding/json"
"io"
"net/http"
)

type CodeqlSarifUploader interface {
GetSarifStatus() (SarifFileInfo, error)
}

func NewCodeqlSarifUploaderInstance(url, token string) CodeqlSarifUploaderInstance {
return CodeqlSarifUploaderInstance{
url: url,
token: token,
}
}

type CodeqlSarifUploaderInstance struct {
url string
token string
}

func (codeqlSarifUploader *CodeqlSarifUploaderInstance) GetSarifStatus() (SarifFileInfo, error) {
return getSarifUploadingStatus(codeqlSarifUploader.url, codeqlSarifUploader.token)
}

type SarifFileInfo struct {
ProcessingStatus string `json:"processing_status"`
Errors []string `json:"errors"`
}

func getSarifUploadingStatus(sarifURL, token string) (SarifFileInfo, error) {
client := http.Client{}
req, err := http.NewRequest("GET", sarifURL, nil)
if err != nil {
return SarifFileInfo{}, err
}
req.Header.Add("Authorization", "Bearer "+token)
req.Header.Add("Accept", "application/vnd.github+json")
req.Header.Add("X-GitHub-Api-Version", "2022-11-28")

resp, err := client.Do(req)
if err != nil {
return SarifFileInfo{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return SarifFileInfo{}, err
}

sarifInfo := SarifFileInfo{}
err = json.Unmarshal(body, &sarifInfo)
if err != nil {
return SarifFileInfo{}, err
}
return sarifInfo, nil
}
16 changes: 16 additions & 0 deletions resources/metadata/codeqlExecuteScan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,22 @@ spec:
- STAGES
- STEPS
default: false
- name: sarifCheckMaxRetries
type: int
description: "Sets max retries for check SARIF file is uploaded when server doesn't response."

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose: "Maximum number of retries when waiting for the server to finish processing the SARIF upload. Only relevant, if checkForCompliance is enabled."

scope:
- PARAMETERS
- STAGES
- STEPS
default: 5
- name: sarifCheckRetryInterval
type: int
descriptoin: "Sets interval in seconds to poll the server to check if SARIF is uploaded."
vlkl-sap marked this conversation as resolved.
Show resolved Hide resolved
scope:
- PARAMETERS
- STAGES
- STEPS
default: 10
- name: threads
type: string
description: "Use this many threads for the codeql operations."
Expand Down