-
Notifications
You must be signed in to change notification settings - Fork 0
Suseobs store #16
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
base: main
Are you sure you want to change the base?
Suseobs store #16
Changes from all commits
e63fcef
ef2e50e
0b124a5
f20be44
0a1856b
691971c
458c300
7873c8a
e3d84e0
cb75f7f
8b0c563
0788f40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,3 +23,5 @@ testbin/* | |
| *~ | ||
|
|
||
| CRDS.tar.gz | ||
|
|
||
| kubewarden-stackpack/kubewarden-stackpack.sts | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ package cmd | |
| import ( | ||
| "context" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/google/uuid" | ||
| "github.com/kubewarden/audit-scanner/internal/k8s" | ||
|
|
@@ -30,13 +31,24 @@ const ( | |
| //nolint:gocognit,funlen // This function is the CLI entrypoint and it's expected to be long. | ||
| func NewRootCommand() *cobra.Command { | ||
| var ( | ||
| level logconfig.Level // log level. | ||
| outputScan bool // print result of scan as JSON to stdout. | ||
| skippedNs []string // list of namespaces to be skipped from scan. | ||
| insecureSSL bool // skip SSL cert validation when connecting to PolicyServers endpoints. | ||
| disableStore bool // disable storing the results in the k8s cluster. | ||
| level logconfig.Level // log level. | ||
| outputScan bool // print result of scan as JSON to stdout. | ||
| skippedNs []string // list of namespaces to be skipped from scan. | ||
| insecureSSL bool // skip SSL cert validation when connecting to PolicyServers endpoints. | ||
| disableStore bool // disable storing the results in the k8s cluster. | ||
| suseObsURL string // URL to the SUSE OBS API. | ||
| suseObsApiKey string // API key to authenticate with the SUSE OBS API. | ||
| suseObsUrn string // API key to authenticate with the SUSE OBS API. | ||
| suseObsCluster string // API key to authenticate with the SUSE OBS API. | ||
| suseObsRepeatInterval time.Duration | ||
| suseObsExpireInterval time.Duration | ||
| ) | ||
|
|
||
| defaultInterval, err := time.ParseDuration("30m") | ||
| if err != nil { | ||
| log.Logger.Err(err).Msg("cannot parse default suseob interval value ") | ||
| } | ||
|
|
||
| // rootCmd represents the base command when called without any subcommands. | ||
| rootCmd := &cobra.Command{ | ||
| Use: "audit-scanner", | ||
|
|
@@ -112,7 +124,14 @@ There will be a ClusterPolicyReport with results for cluster-wide resources.`, | |
| if err != nil { | ||
| return err | ||
| } | ||
| policyReportStore := report.NewPolicyReportStore(client) | ||
| var policyReportStore report.ReportStore | ||
| if len(suseObsURL) > 0 && len(suseObsApiKey) > 0 && len(suseObsUrn) > 0 && len(suseObsCluster) > 0 { | ||
| log.Debug().Msg("Using SUSE Observability as report store") | ||
| policyReportStore = report.NewSuseObsStore(suseObsApiKey, suseObsURL, suseObsUrn, suseObsCluster, suseObsRepeatInterval, suseObsExpireInterval) | ||
| } else { | ||
| log.Debug().Msg("Using Kubernetes as report store") | ||
| policyReportStore = report.NewPolicyReportStore(client) | ||
| } | ||
|
|
||
| scannerConfig := scanner.Config{ | ||
| PoliciesClient: policiesClient, | ||
|
|
@@ -162,6 +181,12 @@ There will be a ClusterPolicyReport with results for cluster-wide resources.`, | |
| rootCmd.Flags().IntP("parallel-resources", "", defaultParallelResources, "number of resources to scan in parallel") | ||
| rootCmd.Flags().IntP("parallel-policies", "", defaultParallelPolicies, "number of policies to evaluate for a given resource in parallel") | ||
| rootCmd.Flags().IntP("page-size", "", defaultPageSize, "number of resources to fetch from the Kubernetes API server when paginating") | ||
| rootCmd.Flags().StringVar(&suseObsURL, "suseobs-url", "", "URL to the SUSE OBS API") | ||
| rootCmd.Flags().StringVar(&suseObsApiKey, "suseobs-apikey", "", "API key to authenticate with the SUSE OBS API") | ||
| rootCmd.Flags().StringVar(&suseObsUrn, "suseobs-urn", "", "SUSE Observability health check stream urn") | ||
| rootCmd.Flags().StringVar(&suseObsCluster, "suseobs-cluster", "", "SUSE Observability cluster name where audit scanner is running") | ||
| rootCmd.Flags().DurationVar(&suseObsRepeatInterval, "suseobs-repeat-interval", defaultInterval, "The frequency with which audit scanner will send health data to SUSE Observability. Max allowed value is 1800 (30 minutes)") | ||
| rootCmd.Flags().DurationVar(&suseObsExpireInterval, "suseobs-expire-interval", defaultInterval, "The time to wait after the last update before an audit scanner check is deleted by SUSE Observability if the check isn't observed again") | ||
|
Comment on lines
+184
to
+189
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we decide to allow multiple store types. I think we should move the store configuration to a file. |
||
|
|
||
| return rootCmd | ||
| } | ||
|
|
@@ -181,6 +206,8 @@ func startScanner(namespace string, clusterWide bool, scanner *scanner.Scanner) | |
|
|
||
| runUID := uuid.New().String() | ||
| ctx := context.Background() | ||
| scanner.BeforeScan(ctx) | ||
| defer scanner.AfterScan(ctx) | ||
|
Comment on lines
+209
to
+210
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the consistency mode used to send the health checks to SUSE Observabiliry (REPEAT_SNAPSHOT), it necessary to send a payload to start the snapshot and another to close it. All the request between these two request, will be the health checks send by the audit scanner. I've add these functions here just to avoid any the work to handle the concurrency nature of the audit scanner and the need to add some control to avoid multiple start/stop snapshot payloads. If we send more then one of this payload, the health checks will not work as expected. |
||
| if clusterWide { | ||
| // only scan clusterwide | ||
| return scanner.ScanClusterWideResources(ctx, runUID) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,15 @@ import ( | |
| wgpolicy "sigs.k8s.io/wg-policy-prototypes/policy-report/pkg/api/wgpolicyk8s.io/v1alpha2" | ||
| ) | ||
|
|
||
| type ReportStore interface { | ||
| BeforeScanning(ctx context.Context) error | ||
| AfterScanning(ctx context.Context) error | ||
| CreateOrPatchPolicyReport(ctx context.Context, policyReport *wgpolicy.PolicyReport) error | ||
| DeleteOldPolicyReports(ctx context.Context, scanRunID, namespace string) error | ||
| CreateOrPatchClusterPolicyReport(ctx context.Context, clusterPolicyReport *wgpolicy.ClusterPolicyReport) error | ||
| DeleteOldClusterPolicyReports(ctx context.Context, scanRunID string) error | ||
| } | ||
|
|
||
|
Comment on lines
+17
to
+25
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactor this for something more meaningful. As the SUSE Obs is not actually storing the reports... |
||
| // PolicyReportStore is a store for PolicyReport and ClusterPolicyReport. | ||
| type PolicyReportStore struct { | ||
| // client is a controller-runtime client that knows about PolicyReport and ClusterPolicyReport CRDs | ||
|
|
@@ -27,6 +36,15 @@ func NewPolicyReportStore(client client.Client) *PolicyReportStore { | |
| } | ||
| } | ||
|
|
||
| func (s *PolicyReportStore) BeforeScanning(ctx context.Context) error { | ||
| return nil | ||
|
|
||
| } | ||
|
|
||
| func (s *PolicyReportStore) AfterScanning(ctx context.Context) error { | ||
| return nil | ||
| } | ||
|
|
||
| // CreateOrPatchPolicyReport creates or patches a PolicyReport. | ||
| func (s *PolicyReportStore) CreateOrPatchPolicyReport(ctx context.Context, policyReport *wgpolicy.PolicyReport) error { | ||
| oldPolicyReport := &wgpolicy.PolicyReport{ObjectMeta: metav1.ObjectMeta{ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is very ugly. And it needs to be refactored.
What do you think about having multiple stores? For example, the audit scanner could create policy reports and send the data to SUSE Obs.