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

Commit

Permalink
refactor: ScanResult states
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgacsal committed Jun 7, 2023
1 parent 559be9d commit c0ebeaa
Show file tree
Hide file tree
Showing 15 changed files with 225 additions and 204 deletions.
21 changes: 11 additions & 10 deletions api/models/models.gen.go

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

2 changes: 1 addition & 1 deletion api/models/scanresult.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (r *TargetScanResult) IsDone() (bool, bool) {
var ok bool
var state TargetScanStateState

if state, ok = r.GetGeneralState(); ok && state == TargetScanStateStateDONE {
if state, ok = r.GetGeneralState(); ok && state == TargetScanStateStateDone {
done = true
}

Expand Down
21 changes: 11 additions & 10 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1665,10 +1665,10 @@ components:
ResourceCleanupState:
type: string
enum:
- DONE
- PENDING
- SKIPPED
- FAILED
- Pending
- Skipped
- Failed
- Done

TargetScanResult:
type: object
Expand Down Expand Up @@ -1760,12 +1760,13 @@ components:
state:
type: string
enum:
- NOT_SCANNED
- INIT
- ATTACHED
- IN_PROGRESS
- ABORTED
- DONE
- NotScanned
- Pending
- Scheduled
- ReadyToScan
- InProgress
- Aborted
- Done
lastTransitionTime:
type: string
format: date-time
Expand Down
191 changes: 95 additions & 96 deletions api/server/server.gen.go

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ var (
logger *logrus.Entry
output string

server string
scanResultID string
mountVolume bool
waitForServerAttached bool
server string
scanResultID string
mountVolume bool
waitForReadyState bool
)

// rootCmd represents the base command when called without any subcommands.
Expand Down Expand Up @@ -86,8 +86,8 @@ var rootCmd = &cobra.Command{
// Start watching for abort event
cli.WatchForAbort(ctx, cancel, DefaultWatcherInterval)

if waitForServerAttached {
if err := cli.WaitForVolumeAttachment(abortCtx); err != nil {
if waitForReadyState {
if err := cli.WaitForReadyState(abortCtx); err != nil {
err = fmt.Errorf("failed to wait for block device being attached: %w", err)
if e := cli.MarkDone(ctx, []error{err}); e != nil {
logger.Errorf("Failed to update scan result stat to completed with errors: %v", e)
Expand Down Expand Up @@ -150,7 +150,7 @@ func init() {
rootCmd.PersistentFlags().StringVar(&server, "server", "", "VMClarity server to export scan results to, for example: http://localhost:9999/api")
rootCmd.PersistentFlags().StringVar(&scanResultID, "scan-result-id", "", "the ScanResult ID to export the scan results to")
rootCmd.PersistentFlags().BoolVar(&mountVolume, "mount-attached-volume", false, "discover for an attached volume and mount it before the scan")
rootCmd.PersistentFlags().BoolVar(&waitForServerAttached, "wait-for-server-attached", false, "wait for the VMClarity server to attach the volume")
rootCmd.PersistentFlags().BoolVar(&waitForReadyState, "wait-for-ready", false, "wait until the ScanResult state is set to ReadyToScan")

// TODO(sambetts) we may have to change this to our own validation when
// we add the CI/CD scenario and there isn't an existing scan-result-id
Expand Down
14 changes: 7 additions & 7 deletions cli/pkg/presenter/vmclarity.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (v *VMClarityPresenter) ExportSbomResult(ctx context.Context, res families.
}
}

state := models.TargetScanStateStateDONE
state := models.TargetScanStateStateDone
scanResult.Status.Sbom.State = &state
scanResult.Status.Sbom.Errors = &errs

Expand Down Expand Up @@ -140,7 +140,7 @@ func (v *VMClarityPresenter) ExportVulResult(ctx context.Context, res families.F
scanResult.Summary.TotalVulnerabilities = utils.GetVulnerabilityTotalsPerSeverity(scanResult.Vulnerabilities.Vulnerabilities)
}

state := models.TargetScanStateStateDONE
state := models.TargetScanStateStateDone
scanResult.Status.Vulnerabilities.State = &state
scanResult.Status.Vulnerabilities.Errors = &errs

Expand Down Expand Up @@ -185,7 +185,7 @@ func (v *VMClarityPresenter) ExportSecretsResult(ctx context.Context, res famili
}
}

state := models.TargetScanStateStateDONE
state := models.TargetScanStateStateDone
scanResult.Status.Secrets.State = &state
scanResult.Status.Secrets.Errors = &errs

Expand Down Expand Up @@ -226,7 +226,7 @@ func (v *VMClarityPresenter) ExportMalwareResult(ctx context.Context, res famili
}
}

state := models.TargetScanStateStateDONE
state := models.TargetScanStateStateDone
scanResult.Status.Malware.State = &state
scanResult.Status.Malware.Errors = &errs

Expand Down Expand Up @@ -269,7 +269,7 @@ func (v *VMClarityPresenter) ExportExploitsResult(ctx context.Context, res famil
}
}

state := models.TargetScanStateStateDONE
state := models.TargetScanStateStateDone
scanResult.Status.Exploits.State = &state
scanResult.Status.Exploits.Errors = &errs

Expand Down Expand Up @@ -316,7 +316,7 @@ func (v *VMClarityPresenter) ExportMisconfigurationResult(ctx context.Context, r
}
}

state := models.TargetScanStateStateDONE
state := models.TargetScanStateStateDone
scanResult.Status.Misconfigurations.State = &state
scanResult.Status.Misconfigurations.Errors = &errs

Expand Down Expand Up @@ -357,7 +357,7 @@ func (v *VMClarityPresenter) ExportRootkitResult(ctx context.Context, res famili
}
}

state := models.TargetScanStateStateDONE
state := models.TargetScanStateStateDone
scanResult.Status.Rootkits.State = &state
scanResult.Status.Rootkits.Errors = &errs

Expand Down
2 changes: 1 addition & 1 deletion cli/pkg/state/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

type LocalState struct{}

func (l *LocalState) WaitForVolumeAttachment(context.Context) error {
func (l *LocalState) WaitForReadyState(context.Context) error {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion cli/pkg/state/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

type Manager interface {
WaitForVolumeAttachment(context.Context) error
WaitForReadyState(context.Context) error
MarkInProgress(context.Context) error
MarkFamilyScanInProgress(context.Context, types.FamilyType) error
MarkDone(context.Context, []error) error
Expand Down
32 changes: 16 additions & 16 deletions cli/pkg/state/vmclarity.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type VMClarityState struct {
}

// nolint:cyclop
func (v *VMClarityState) WaitForVolumeAttachment(ctx context.Context) error {
func (v *VMClarityState) WaitForReadyState(ctx context.Context) error {
logger := log.GetLoggerFromContextOrDiscard(ctx)

timer := time.NewTicker(DefaultWaitForVolRetryInterval)
Expand All @@ -61,13 +61,13 @@ func (v *VMClarityState) WaitForVolumeAttachment(ctx context.Context) error {
}

switch *status.General.State {
case models.TargetScanStateStateINIT:
case models.TargetScanStateStateABORTED:
case models.TargetScanStateStatePending, models.TargetScanStateStateScheduled:
case models.TargetScanStateStateAborted:
// Do nothing as WaitForAborted is responsible for handling this case
case models.TargetScanStateStateATTACHED, models.TargetScanStateStateINPROGRESS:
case models.TargetScanStateStateReadyToScan, models.TargetScanStateStateInProgress:
return nil
case models.TargetScanStateStateDONE, models.TargetScanStateStateNOTSCANNED:
return fmt.Errorf("failed to wait for volume attachment as ScanResult is in %s state", *status.General.State)
case models.TargetScanStateStateDone, models.TargetScanStateStateNotScanned:
return fmt.Errorf("failed to wait for ScanResult become ready as it is in %s state", *status.General.State)
}
case <-ctx.Done():
return fmt.Errorf("waiting for volume ready was canceled: %w", ctx.Err())
Expand All @@ -88,7 +88,7 @@ func (v *VMClarityState) MarkInProgress(ctx context.Context) error {
scanResult.Status.General = &models.TargetScanState{}
}

state := models.TargetScanStateStateINPROGRESS
state := models.TargetScanStateStateInProgress
scanResult.Status.General.State = &state
scanResult.Status.General.LastTransitionTime = utils.PointerTo(time.Now())

Expand All @@ -113,7 +113,7 @@ func (v *VMClarityState) MarkDone(ctx context.Context, errors []error) error {
scanResult.Status.General = &models.TargetScanState{}
}

state := models.TargetScanStateStateDONE
state := models.TargetScanStateStateDone
scanResult.Status.General.State = &state
scanResult.Status.General.LastTransitionTime = utils.PointerTo(time.Now())

Expand Down Expand Up @@ -179,7 +179,7 @@ func (v *VMClarityState) markExploitsScanInProgress(ctx context.Context) error {
scanResult.Status.Exploits = &models.TargetScanState{}
}

state := models.TargetScanStateStateINPROGRESS
state := models.TargetScanStateStateInProgress
scanResult.Status.Exploits.State = &state
scanResult.Status.Exploits.LastTransitionTime = utils.PointerTo(time.Now())

Expand All @@ -204,7 +204,7 @@ func (v *VMClarityState) markSecretsScanInProgress(ctx context.Context) error {
scanResult.Status.Secrets = &models.TargetScanState{}
}

state := models.TargetScanStateStateINPROGRESS
state := models.TargetScanStateStateInProgress
scanResult.Status.Secrets.State = &state
scanResult.Status.Secrets.LastTransitionTime = utils.PointerTo(time.Now())

Expand All @@ -229,7 +229,7 @@ func (v *VMClarityState) markSBOMScanInProgress(ctx context.Context) error {
scanResult.Status.Sbom = &models.TargetScanState{}
}

state := models.TargetScanStateStateINPROGRESS
state := models.TargetScanStateStateInProgress
scanResult.Status.Sbom.State = &state
scanResult.Status.Sbom.LastTransitionTime = utils.PointerTo(time.Now())

Expand All @@ -254,7 +254,7 @@ func (v *VMClarityState) markVulnerabilitiesScanInProgress(ctx context.Context)
scanResult.Status.Vulnerabilities = &models.TargetScanState{}
}

state := models.TargetScanStateStateINPROGRESS
state := models.TargetScanStateStateInProgress
scanResult.Status.Vulnerabilities.State = &state
scanResult.Status.Vulnerabilities.LastTransitionTime = utils.PointerTo(time.Now())

Expand All @@ -279,7 +279,7 @@ func (v *VMClarityState) markMalwareScanInProgress(ctx context.Context) error {
scanResult.Status.Malware = &models.TargetScanState{}
}

state := models.TargetScanStateStateINPROGRESS
state := models.TargetScanStateStateInProgress
scanResult.Status.Malware.State = &state
scanResult.Status.Malware.LastTransitionTime = utils.PointerTo(time.Now())

Expand All @@ -304,7 +304,7 @@ func (v *VMClarityState) markMisconfigurationsScanInProgress(ctx context.Context
scanResult.Status.Misconfigurations = &models.TargetScanState{}
}

state := models.TargetScanStateStateINPROGRESS
state := models.TargetScanStateStateInProgress
scanResult.Status.Misconfigurations.State = &state
scanResult.Status.Misconfigurations.LastTransitionTime = utils.PointerTo(time.Now())

Expand All @@ -329,7 +329,7 @@ func (v *VMClarityState) markRootkitsScanInProgress(ctx context.Context) error {
scanResult.Status.Rootkits = &models.TargetScanState{}
}

state := models.TargetScanStateStateINPROGRESS
state := models.TargetScanStateStateInProgress
scanResult.Status.Rootkits.State = &state
scanResult.Status.Rootkits.LastTransitionTime = utils.PointerTo(time.Now())

Expand All @@ -354,7 +354,7 @@ func (v *VMClarityState) IsAborted(ctx context.Context) (bool, error) {
return false, errors.New("failed to get general state of scan result")
}

if state == models.TargetScanStateStateABORTED {
if state == models.TargetScanStateStateAborted {
return true, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func New(config Config) *ScanResultProcessor {

// Returns true if TargetScanStatus.State is DONE and there are no Errors.
func statusCompletedWithNoErrors(tss *models.TargetScanState) bool {
return tss != nil && tss.State != nil && *tss.State == models.TargetScanStateStateDONE && (tss.Errors == nil || len(*tss.Errors) == 0)
return tss != nil && tss.State != nil && *tss.State == models.TargetScanStateStateDone && (tss.Errors == nil || len(*tss.Errors) == 0)
}

// nolint:cyclop
Expand Down
Loading

0 comments on commit c0ebeaa

Please sign in to comment.