Skip to content
This repository was archived by the owner on Oct 14, 2020. It is now read-only.

Commit 6d8dfe0

Browse files
committed
Add errored state for scans from scanning and parsing state
1 parent 845c18a commit 6d8dfe0

File tree

5 files changed

+53
-12
lines changed

5 files changed

+53
-12
lines changed

operator/apis/execution/v1/scan_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ type ScanSpec struct {
3636
// ScanStatus defines the observed state of Scan
3737
type ScanStatus struct {
3838
State string `json:"state,omitempty"`
39+
40+
ErrorDescription string `json:"errorDescription,omitempty"`
41+
3942
// RawResultType determines which kind of ParseDefinition will be used to turn the raw results of the scanner into findings
4043
RawResultType string `json:"rawResultType,omitempty"`
4144
// RawResultFile Filename of the result file of the scanner. e.g. `nmap-result.xml`

operator/config/crd/bases/execution.experimental.securecodebox.io_scans.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ spec:
6969
status:
7070
description: ScanStatus defines the observed state of Scan
7171
properties:
72+
errorDescription:
73+
type: string
7274
findings:
7375
description: FindingStats contains the general stats about the results
7476
of the scan

operator/config/samples/execution_v1_scan/nmap_localhost.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ metadata:
55
spec:
66
scanType: "nmap"
77
parameters:
8-
- "-Pn"
9-
- localhost
8+
- "-FAOSJDBN"
9+
- http://localhost

operator/controllers/execution/scan_controller.go

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,31 @@ func (r *ScanReconciler) getJob(name, namespace string) (*batch.Job, error) {
140140
return &job, nil
141141
}
142142

143-
func (r *ScanReconciler) checkIfJobIsCompleted(name, namespace string) (bool, error) {
143+
type jobCompletionType string
144+
145+
const (
146+
completed jobCompletionType = "Completed"
147+
failed jobCompletionType = "Failed"
148+
incomplete jobCompletionType = "Incomplete"
149+
unkown jobCompletionType = "Unkown"
150+
)
151+
152+
func (r *ScanReconciler) checkIfJobIsCompleted(name, namespace string) (jobCompletionType, error) {
144153
job, err := r.getJob(name, namespace)
145154
if err != nil {
146-
return false, err
155+
return unkown, err
147156
}
148-
return (job != nil && job.Status.Succeeded != 0), nil
157+
if job == nil {
158+
return unkown, errors.New("Both Job and error were nil. This isn't really expected")
159+
}
160+
161+
if job.Status.Succeeded != 0 {
162+
return completed, nil
163+
}
164+
if job.Status.Failed != 0 {
165+
return failed, nil
166+
}
167+
return unkown, nil
149168
}
150169

151170
func (r *ScanReconciler) startScan(scan *executionv1.Scan) error {
@@ -219,20 +238,28 @@ func (r *ScanReconciler) startScan(scan *executionv1.Scan) error {
219238
func (r *ScanReconciler) checkIfScanIsCompleted(scan *executionv1.Scan) error {
220239
ctx := context.Background()
221240

222-
done, err := r.checkIfJobIsCompleted(fmt.Sprintf("scan-%s", scan.Name), scan.Namespace)
241+
status, err := r.checkIfJobIsCompleted(fmt.Sprintf("scan-%s", scan.Name), scan.Namespace)
223242
if err != nil {
224243
return err
225244
}
226245

227-
// TODO: Handle job failure cases
228-
if done {
246+
switch status {
247+
case completed:
229248
r.Log.V(7).Info("Scan is completed")
230249
scan.Status.State = "ScanCompleted"
231250
if err := r.Status().Update(ctx, scan); err != nil {
232251
r.Log.Error(err, "unable to update Scan status")
233252
return err
234253
}
254+
case failed:
255+
scan.Status.State = "Errored"
256+
scan.Status.ErrorDescription = "Failed to run the Scan Container, check k8s Job and its logs for more details"
257+
if err := r.Status().Update(ctx, scan); err != nil {
258+
r.Log.Error(err, "unable to update Scan status")
259+
return err
260+
}
235261
}
262+
// Either Incomplete or Unkown, nothing we can do, other then giving it some more time...
236263
return nil
237264
}
238265

@@ -357,19 +384,26 @@ func (r *ScanReconciler) startParser(scan *executionv1.Scan) error {
357384
func (r *ScanReconciler) checkIfParsingIsCompleted(scan *executionv1.Scan) error {
358385
ctx := context.Background()
359386

360-
done, err := r.checkIfJobIsCompleted(fmt.Sprintf("parse-%s", scan.Name), scan.Namespace)
387+
status, err := r.checkIfJobIsCompleted(fmt.Sprintf("scan-%s", scan.Name), scan.Namespace)
361388
if err != nil {
362389
return err
363390
}
364391

365-
// TODO: Handle job failure cases
366-
if done {
367-
r.Log.V(7).Info("Scan is completed")
392+
switch status {
393+
case completed:
394+
r.Log.V(7).Info("Parsing is completed")
368395
scan.Status.State = "ParseCompleted"
369396
if err := r.Status().Update(ctx, scan); err != nil {
370397
r.Log.Error(err, "unable to update Scan status")
371398
return err
372399
}
400+
case failed:
401+
scan.Status.State = "Errored"
402+
scan.Status.ErrorDescription = "Failed to run the Parser. This is likely a Bug, we would like to know about. Please open up a Issue on GitHub."
403+
if err := r.Status().Update(ctx, scan); err != nil {
404+
r.Log.Error(err, "unable to update Scan status")
405+
return err
406+
}
373407
}
374408

375409
return nil

operator/crds/execution.experimental.securecodebox.io_scans.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ spec:
6969
status:
7070
description: ScanStatus defines the observed state of Scan
7171
properties:
72+
errorDescription:
73+
type: string
7274
findings:
7375
description: FindingStats contains the general stats about the results
7476
of the scan

0 commit comments

Comments
 (0)