Skip to content

Commit

Permalink
UD-1132: Update Trivy Plugin Status
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Conner <[email protected]>
  • Loading branch information
knrc committed Jan 23, 2024
1 parent 5ac8e28 commit a603fd4
Show file tree
Hide file tree
Showing 7 changed files with 277 additions and 9 deletions.
32 changes: 32 additions & 0 deletions api/zora/v1alpha1/clusterscan_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,35 @@ func (in *PluginReference) PluginKey(defaultNamespace string) types.NamespacedNa
return types.NamespacedName{Name: in.Name, Namespace: ns}
}

type ProcessedScanStatus struct {
// The status of the last scan.
Status string `json:"status,omitempty"`
// The message for the last scan.
Message string `json:"message,omitempty"`
// True is the scan is currently suspended.
Suspend bool `json:"suspend,omitempty"`
// The status of the last scan.
ID string `json:"id,omitempty"`
}

// PluginStatus defines the observed state of Plugin
type PluginScanProcessedStatus struct {
// The scan status information.
Scan *ProcessedScanStatus `json:"scan,omitempty"`
// The number of misconfiguration issues discovered in the last successful scan.
IssueCount *int `json:"issueCount,omitempty"`
// When the last successful scan occurred.
LastSuccessfulScanTime *metav1.Time `json:"lastSuccessfulScanTime,omitempty"`
// When the last scan finished.
LastFinishedScanTime *metav1.Time `json:"lastFinishedScanTime,omitempty"`
// When the next scan will occurr.
NextScheduleScanTime *metav1.Time `json:"nextScheduleScanTime,omitempty"`
// The schedule of the scan.
Schedule string `json:"schedule,omitempty"`
// The Scan ID of the last successful scan.
LastSuccessfulScanID string `json:"lastSuccessfulScanID,omitempty"`
}

type PluginScanProcessedResources map[string]string

// ClusterScanStatus defines the observed state of ClusterScan
Expand Down Expand Up @@ -112,6 +141,9 @@ type ClusterScanStatus struct {

// Resource versions of processed misconfigurations
ProcessedMisconfigurations map[string]PluginScanProcessedResources `json:"processedMisconfigurations,omitempty"`

// Processed Status information for each plugin
ProcessedPluginStatus map[string]*PluginScanProcessedStatus `json:"processedPluginStatus,omitempty"`
}

// GetPluginStatus returns a PluginScanStatus of a plugin
Expand Down
68 changes: 68 additions & 0 deletions api/zora/v1alpha1/zz_generated.deepcopy.go

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

45 changes: 45 additions & 0 deletions charts/zora/crds/zora.undistro.io_clusterscans.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,51 @@ spec:
type: object
description: Resource versions of processed misconfigurations
type: object
processedPluginStatus:
additionalProperties:
description: PluginStatus defines the observed state of Plugin
properties:
issueCount:
description: The number of misconfiguration issues discovered
in the last successful scan.
type: integer
lastFinishedScanTime:
description: When the last scan finished.
format: date-time
type: string
lastSuccessfulScanID:
description: The Scan ID of the last successful scan.
type: string
lastSuccessfulScanTime:
description: When the last successful scan occurred.
format: date-time
type: string
nextScheduleScanTime:
description: When the next scan will occurr.
format: date-time
type: string
scan:
description: The scan status information.
properties:
id:
description: The status of the last scan.
type: string
message:
description: The message for the last scan.
type: string
status:
description: The status of the last scan.
type: string
suspend:
description: True is the scan is currently suspended.
type: boolean
type: object
schedule:
description: The schedule of the scan.
type: string
type: object
description: Processed Status information for each plugin
type: object
processedVulnerabilities:
additionalProperties:
additionalProperties:
Expand Down
45 changes: 45 additions & 0 deletions config/crd/bases/zora.undistro.io_clusterscans.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,51 @@ spec:
type: object
description: Resource versions of processed misconfigurations
type: object
processedPluginStatus:
additionalProperties:
description: PluginStatus defines the observed state of Plugin
properties:
issueCount:
description: The number of misconfiguration issues discovered
in the last successful scan.
type: integer
lastFinishedScanTime:
description: When the last scan finished.
format: date-time
type: string
lastSuccessfulScanID:
description: The Scan ID of the last successful scan.
type: string
lastSuccessfulScanTime:
description: When the last successful scan occurred.
format: date-time
type: string
nextScheduleScanTime:
description: When the next scan will occurr.
format: date-time
type: string
scan:
description: The scan status information.
properties:
id:
description: The status of the last scan.
type: string
message:
description: The message for the last scan.
type: string
status:
description: The status of the last scan.
type: string
suspend:
description: True is the scan is currently suspended.
type: boolean
type: object
schedule:
description: The schedule of the scan.
type: string
type: object
description: Processed Status information for each plugin
type: object
processedVulnerabilities:
additionalProperties:
additionalProperties:
Expand Down
21 changes: 21 additions & 0 deletions internal/saas/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Client interface {
PutClusterScan(ctx context.Context, namespace, name string, pluginStatus map[string]*PluginStatus) error
DeleteClusterScan(ctx context.Context, namespace, name string) error
PutVulnerabilityReport(ctx context.Context, namespace, name string, vulnReport v1alpha1.VulnerabilityReport) error
PutClusterStatus(ctx context.Context, namespace, name string, pluginStatus map[string]*PluginStatus) error
}

type client struct {
Expand Down Expand Up @@ -158,6 +159,26 @@ func (r *client) DeleteClusterScan(ctx context.Context, namespace, name string)
return validateStatus(res)
}

func (r *client) PutClusterStatus(ctx context.Context, namespace, name string, pluginStatus map[string]*PluginStatus) error {
u := r.clusterURL(namespace, name, "status")
b, err := json.Marshal(pluginStatus)
if err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPut, u, bytes.NewReader(b))
if err != nil {
return err
}
req.Header.Set("content-type", "application/json")
req.Header.Set(versionHeader, r.version)
res, err := r.client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
return validateStatus(res)
}

func (r *client) clusterURL(namespace, name string, extra ...string) string {
p := path.Join(r.baseURL.Path, fmt.Sprintf(clusterPathF, namespace, name))
if len(extra) > 0 {
Expand Down
2 changes: 2 additions & 0 deletions internal/saas/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type PluginStatus struct {
LastFinishedScanTime *metav1.Time `json:"lastFinishedScanTime"`
NextScheduleScanTime *metav1.Time `json:"nextScheduleScanTime"`
Schedule string `json:"schedule"`
LastSuccessfulScanID string `json:"lastSuccessfulScanID"`
}

type NamespacedName struct {
Expand Down Expand Up @@ -158,6 +159,7 @@ func NewScanStatus(scans []v1alpha1.ClusterScan) (map[string]*PluginStatus, *int
pluginStatus[p].Scan.Suspend = pointer.BoolDeref(cs.Spec.Suspend, false)
pluginStatus[p].Schedule = cs.Spec.Schedule
pluginStatus[p].Scan.ID = s.LastScanID
pluginStatus[p].LastSuccessfulScanID = s.LastSuccessfulScanID

if s.TotalIssues != nil {
if pluginStatus[p].IssueCount == nil {
Expand Down
Loading

0 comments on commit a603fd4

Please sign in to comment.