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 5 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
53 changes: 48 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,50 @@ 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())

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

return nil
func waitSarifUploaded(codeqlSarifUploader codeql.CodeqlSarifUploader) error {
var sarifUploadComplete = "complete"
var sarifUploadFailed = "failed"
var maxRetries = 5
vlkl-sap marked this conversation as resolved.
Show resolved Hide resolved
var retryCount = 0
for {
sarifStatus, err := codeqlSarifUploader.GetSarifStatus()
if err != nil {
if retryCount < maxRetries {
retryCount++
log.Entry().Errorf("error while checking sarif status, retrying in 10 seconds... (retry %d/%d)", retryCount, maxRetries)
time.Sleep(time.Second * 10)
continue
} else {
return err
}
vlkl-sap marked this conversation as resolved.
Show resolved Hide resolved
}
if len(sarifStatus.Errors) > 0 {
vlkl-sap marked this conversation as resolved.
Show resolved Hide resolved
for e := range sarifStatus.Errors {
log.Entry().Error(e)
}
}
if sarifStatus.ProcessingStatus == sarifUploadComplete {
return nil
}
if sarifStatus.ProcessingStatus == sarifUploadFailed {
return errors.New("failed to upload sarif file")
}
time.Sleep(time.Second * 10)
}
}

func runCodeqlExecuteScan(config *codeqlExecuteScanOptions, telemetryData *telemetry.CustomData, utils codeqlExecuteScanUtils) ([]piperutils.Path, error) {
Expand Down Expand Up @@ -275,11 +314,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(&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
77 changes: 77 additions & 0 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 @@ -339,3 +342,77 @@ func TestCreateToolRecordCodeql(t *testing.T) {
assert.Error(t, err)
})
}

func TestWaitSarifUploaded(t *testing.T) {
t.Parallel()
t.Run("Fast complete upload", func(t *testing.T) {
codeqlScanAuditMock := CodeqlSarifUploaderMock{counter: 0}
timerStart := time.Now()
err := waitSarifUploaded(&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(&codeqlScanAuditMock)
assert.GreaterOrEqual(t, time.Now().Sub(timerStart), time.Second*20)
assert.NoError(t, err)
})
t.Run("Failed upload", func(t *testing.T) {
codeqlScanAuditMock := CodeqlSarifUploaderMock{counter: -1}
err := waitSarifUploaded(&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(&codeqlScanAuditErrorMock)
assert.Error(t, err)
assert.ErrorContains(t, err, "test error")
})
t.Run("Completed upload after getting errors from server", func(t *testing.T) {
codeqlScanAuditErrorMock := CodeqlSarifUploaderErrorMock{counter: 3}
err := waitSarifUploaded(&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")
}
50 changes: 50 additions & 0 deletions pkg/codeql/codeql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package codeql

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

sapgithub "github.com/SAP/jenkins-library/pkg/github"
"github.com/google/go-github/v45/github"
Expand All @@ -11,6 +14,10 @@ type CodeqlScanAudit interface {
GetVulnerabilities(analyzedRef string, state string) error
}

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

type githubCodeqlScanningService interface {
ListAlertsForRepo(ctx context.Context, owner, repo string, opts *github.AlertListOptions) ([]*github.Alert, *github.Response, error)
}
Expand Down Expand Up @@ -43,6 +50,49 @@ func (codeqlScanAudit *CodeqlScanAuditInstance) GetVulnerabilities(analyzedRef s
return getVulnerabilitiesFromClient(ctx, client.CodeScanning, analyzedRef, codeqlScanAudit)
}

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)

sarifInfo := SarifFileInfo{}
err = json.Unmarshal(body, &sarifInfo)
if err != nil {
return SarifFileInfo{}, err
}
return sarifInfo, nil
}

func getVulnerabilitiesFromClient(ctx context.Context, codeScanning githubCodeqlScanningService, analyzedRef string, codeqlScanAudit *CodeqlScanAuditInstance) (CodeqlScanning, error) {
page := 1
audited := 0
Expand Down
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