Skip to content

Commit

Permalink
Merge pull request #979 from mercedes-benz/feature-977-client-statusc…
Browse files Browse the repository at this point in the history
…heck-before-download

Feature 977 client status check before download
  • Loading branch information
sven-dmlr authored Feb 7, 2022
2 parents cd3a112 + 047403d commit 4c19e81
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 18 deletions.
3 changes: 3 additions & 0 deletions sechub-cli/src/mercedes-benz.com/sechub/cli/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ const SechubWaittimeDefaultEnvVar = "SECHUB_WAITTIME_DEFAULT"
// ExecutionStateEnded sechub job has succesfully finished
const ExecutionStateEnded = "ENDED"

// JobStatusOkay - sechub job has a report ready to download
const JobStatusOkay = "OK"

/* ---------------------------------- */
/* -------- Validation -------------- */
/* ---------------------------------- */
Expand Down
2 changes: 2 additions & 0 deletions sechub-cli/src/mercedes-benz.com/sechub/cli/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Context struct {
sechubConfig *SecHubConfig
sourceZipFileChecksum string
sourceZipFileName string
jobStatus *jobStatusResult
}

func (context *Context) isUploadingSourceZip() bool {
Expand All @@ -27,6 +28,7 @@ func (context *Context) isUploadingSourceZip() bool {
func NewContext(config *Config) *Context {
context := new(Context)
context.config = config
context.jobStatus = new(jobStatusResult)

/* setup HTTP client */
tr := &http.Transport{
Expand Down
19 changes: 10 additions & 9 deletions sechub-cli/src/mercedes-benz.com/sechub/cli/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@ func Execute() {
switch context.config.action {
case scanAction:
prepareCreateApproveJob(context)
status := waitForSecHubJobDone(context)
waitForSecHubJobDone(context)
downloadSechubReport(context)
printSecHubJobSummaryAndFailOnTrafficLight(context, status)
printSecHubJobSummaryAndFailOnTrafficLight(context)
case scanAsynchronAction:
prepareCreateApproveJob(context)
fmt.Println(context.config.secHubJobUUID)
case getStatusAction:
_, jsonData := getSecHubJobStatus(context)
jsonData := getSecHubJobStatus(context)
fmt.Println(jsonData)
case getReportAction:
getSecHubJobStatus(context)
downloadSechubReport(context)
case getFalsePositivesAction:
downloadFalsePositivesList(context)
Expand Down Expand Up @@ -132,17 +133,17 @@ func prepareCodeScan(context *Context) {
context.sourceZipFileChecksum = sechubUtil.CreateChecksum(context.sourceZipFileName)
}

func downloadSechubReport(context *Context) string {
fileEnding := ".json"
if context.config.reportFormat == "html" {
fileEnding = ".html"
func downloadSechubReport(context *Context) {
if context.jobStatus.Result != JobStatusOkay {
sechubUtil.LogError("Job " + context.config.secHubJobUUID + " failed on server. Cannot download report.")
os.Exit(ExitCodeFailed)
}

fileEnding := "." + context.config.reportFormat // e.g. .json, .html
fileName := "sechub_report_" + context.config.projectID + "_" + context.config.secHubJobUUID + fileEnding

report := ReportDownload{serverResult: getSecHubJobReport(context), outputFolder: context.config.outputFolder, outputFileName: fileName}
report.save(context)

return "" // Dummy (Error handling is done in report.save method)
}

func downloadFalsePositivesList(context *Context) {
Expand Down
16 changes: 8 additions & 8 deletions sechub-cli/src/mercedes-benz.com/sechub/cli/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ func waitForSecHubJobDone(context *Context) (status jobStatusResult) {
sechubUtil.Log(fmt.Sprintf("Waiting for job %s to be done", context.config.secHubJobUUID), context.config.quiet)

for {
status, _ = getSecHubJobStatus(context)
getSecHubJobStatus(context)

if status.State == ExecutionStateEnded {
if context.jobStatus.State == ExecutionStateEnded {
break
}

Expand All @@ -71,7 +71,7 @@ func waitForSecHubJobDone(context *Context) (status jobStatusResult) {
return status
}

func getSecHubJobStatus(context *Context) (status jobStatusResult, jsonData string) {
func getSecHubJobStatus(context *Context) (jsonData string) {
// request SecHub job state from server
response := sendWithDefaultHeader("GET", buildGetSecHubJobStatusAPICall(context), context)

Expand All @@ -82,15 +82,15 @@ func getSecHubJobStatus(context *Context) (status jobStatusResult, jsonData stri
}

/* transform text to json */
err = json.Unmarshal(data, &status)
err = json.Unmarshal(data, context.jobStatus)
sechubUtil.HandleHTTPError(err, ExitCodeHTTPError)

return status, string(data)
return string(data)
}

func printSecHubJobSummaryAndFailOnTrafficLight(context *Context, status jobStatusResult) {
func printSecHubJobSummaryAndFailOnTrafficLight(context *Context) {
/* Evaluate traffic light */
switch status.TrafficLight {
switch context.jobStatus.TrafficLight {
case "RED":
fmt.Fprintln(os.Stderr, " RED alert - security vulnerabilities identified (critical or high)")
os.Exit(ExitCodeFailed)
Expand All @@ -108,7 +108,7 @@ func printSecHubJobSummaryAndFailOnTrafficLight(context *Context, status jobStat
sechubUtil.LogError("No traffic light available! Please check server logs.")
os.Exit(ExitCodeFailed)
default:
sechubUtil.LogError(fmt.Sprintln("UNKNOWN traffic light:", status.TrafficLight, "- Expected one of: RED, YELLOW, GREEN."))
sechubUtil.LogError(fmt.Sprintln("UNKNOWN traffic light:", context.jobStatus.TrafficLight, "- Expected one of: RED, YELLOW, GREEN."))
os.Exit(ExitCodeFailed)
}
}
2 changes: 1 addition & 1 deletion sechub-cli/src/mercedes-benz.com/sechub/cli/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func newSecHubReportFromBytes(bytes []byte) SecHubReport {
/* transform text to json */
err := json.Unmarshal(bytes, &report)
if err != nil {
sechubUtil.LogError("SecHub configuration json is not valid json")
sechubUtil.LogError("Report data is not valid json")
showHelpHint()
os.Exit(ExitCodeMissingConfigFile)
}
Expand Down

0 comments on commit 4c19e81

Please sign in to comment.