From 1296035b1819a05795586bf9ffe5534efeabf108 Mon Sep 17 00:00:00 2001 From: andream16 Date: Wed, 23 Oct 2024 19:25:45 +0100 Subject: [PATCH] Adding examples. --- sdk/component/examples/enricher/main.go | 41 +++++++++++++++++++ sdk/component/examples/filter/main.go | 41 +++++++++++++++++++ sdk/component/examples/reporter/main.go | 36 +++++++++++++++++ sdk/component/examples/scanner/main.go | 53 +++++++++++++++++++++++++ sdk/component/examples/target/main.go | 30 ++++++++++++++ 5 files changed, 201 insertions(+) create mode 100644 sdk/component/examples/enricher/main.go create mode 100644 sdk/component/examples/filter/main.go create mode 100644 sdk/component/examples/reporter/main.go create mode 100644 sdk/component/examples/scanner/main.go create mode 100644 sdk/component/examples/target/main.go diff --git a/sdk/component/examples/enricher/main.go b/sdk/component/examples/enricher/main.go new file mode 100644 index 000000000..7047c4cbe --- /dev/null +++ b/sdk/component/examples/enricher/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "context" + "log" + "time" + + "github.com/smithy-security/smithy/sdk/component" + ocsf "github.com/smithy-security/smithy/sdk/gen/com/github/ocsf/ocsf_schema/v1" +) + +type sampleEnricher struct{} + +func (s sampleEnricher) Close(ctx context.Context) error { + component.FromContext(ctx).Info("Closing enricher.") + return nil +} + +func (s sampleEnricher) Read(ctx context.Context) ([]*ocsf.VulnerabilityFinding, error) { + component.FromContext(ctx).Info("Read.") + return make([]*ocsf.VulnerabilityFinding, 0, 10), nil +} + +func (s sampleEnricher) Update(ctx context.Context, findings []*ocsf.VulnerabilityFinding) error { + component.FromContext(ctx).Info("Update.") + return nil +} + +func (s sampleEnricher) Annotate(ctx context.Context, findings []*ocsf.VulnerabilityFinding) ([]*ocsf.VulnerabilityFinding, error) { + component.FromContext(ctx).Info("Annotate.") + return make([]*ocsf.VulnerabilityFinding, 0, 10), nil +} + +func main() { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) + defer cancel() + + if err := component.RunEnricher(ctx, sampleEnricher{}, component.RunnerWithComponentName("sample-enricher")); err != nil { + log.Fatalf("unexpected run error: %v", err) + } +} diff --git a/sdk/component/examples/filter/main.go b/sdk/component/examples/filter/main.go new file mode 100644 index 000000000..7957f2418 --- /dev/null +++ b/sdk/component/examples/filter/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "context" + "log" + "time" + + "github.com/smithy-security/smithy/sdk/component" + ocsf "github.com/smithy-security/smithy/sdk/gen/com/github/ocsf/ocsf_schema/v1" +) + +type sampleFilter struct{} + +func (s sampleFilter) Close(ctx context.Context) error { + component.FromContext(ctx).Info("Closing filter.") + return nil +} + +func (s sampleFilter) Read(ctx context.Context) ([]*ocsf.VulnerabilityFinding, error) { + component.FromContext(ctx).Info("Read.") + return make([]*ocsf.VulnerabilityFinding, 0, 100), nil +} + +func (s sampleFilter) Update(ctx context.Context, findings []*ocsf.VulnerabilityFinding) error { + component.FromContext(ctx).Info("Update.") + return nil +} + +func (s sampleFilter) Filter(ctx context.Context, findings []*ocsf.VulnerabilityFinding) ([]*ocsf.VulnerabilityFinding, bool, error) { + component.FromContext(ctx).Info("Filter.") + return make([]*ocsf.VulnerabilityFinding, 0, 80), true, nil +} + +func main() { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) + defer cancel() + + if err := component.RunFilter(ctx, sampleFilter{}, component.RunnerWithComponentName("sample-filter")); err != nil { + log.Fatalf("unexpected run error: %v", err) + } +} diff --git a/sdk/component/examples/reporter/main.go b/sdk/component/examples/reporter/main.go new file mode 100644 index 000000000..bc172ddb9 --- /dev/null +++ b/sdk/component/examples/reporter/main.go @@ -0,0 +1,36 @@ +package main + +import ( + "context" + "log" + "time" + + "github.com/smithy-security/smithy/sdk/component" + ocsf "github.com/smithy-security/smithy/sdk/gen/com/github/ocsf/ocsf_schema/v1" +) + +type sampleReporter struct{} + +func (s sampleReporter) Close(ctx context.Context) error { + component.FromContext(ctx).Info("Closing reporter.") + return nil +} + +func (s sampleReporter) Read(ctx context.Context) ([]*ocsf.VulnerabilityFinding, error) { + component.FromContext(ctx).Info("Read.") + return make([]*ocsf.VulnerabilityFinding, 0, 100), nil +} + +func (s sampleReporter) Report(ctx context.Context, findings []*ocsf.VulnerabilityFinding) error { + component.FromContext(ctx).Info("Report.") + return nil +} + +func main() { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) + defer cancel() + + if err := component.RunReporter(ctx, sampleReporter{}, component.RunnerWithComponentName("sample-reporter")); err != nil { + log.Fatalf("unexpected run error: %v", err) + } +} diff --git a/sdk/component/examples/scanner/main.go b/sdk/component/examples/scanner/main.go new file mode 100644 index 000000000..42d2331ef --- /dev/null +++ b/sdk/component/examples/scanner/main.go @@ -0,0 +1,53 @@ +package main + +import ( + "context" + "log" + "time" + + "github.com/smithy-security/smithy/sdk/component" + ocsf "github.com/smithy-security/smithy/sdk/gen/com/github/ocsf/ocsf_schema/v1" +) + +type ( + sampleScanner struct{} + + sampleRawVuln struct{} +) + +func (s sampleRawVuln) Unmarshal() (*ocsf.VulnerabilityFinding, error) { + return &ocsf.VulnerabilityFinding{}, nil +} + +func (s sampleScanner) Close(ctx context.Context) error { + component.FromContext(ctx).Info("Closing scanner.") + return nil +} + +func (s sampleScanner) Store(ctx context.Context, findings []*ocsf.VulnerabilityFinding) error { + component.FromContext(ctx).Info("Storing.") + return nil +} + +func (s sampleScanner) Scan(ctx context.Context) ([]component.Unmarshaler, error) { + component.FromContext(ctx).Info("Scanning.") + var rawVulns = make([]component.Unmarshaler, 0, 10) + for i := 0; i < 10; i++ { + rawVulns = append(rawVulns, sampleRawVuln{}) + } + return rawVulns, nil +} + +func (s sampleScanner) Transform(ctx context.Context, payload component.Unmarshaler) (*ocsf.VulnerabilityFinding, error) { + component.FromContext(ctx).Info("Transforming.") + return &ocsf.VulnerabilityFinding{}, nil +} + +func main() { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) + defer cancel() + + if err := component.RunScanner(ctx, sampleScanner{}, component.RunnerWithComponentName("sample-scanner")); err != nil { + log.Fatalf("unexpected run error: %v", err) + } +} diff --git a/sdk/component/examples/target/main.go b/sdk/component/examples/target/main.go new file mode 100644 index 000000000..caad3fe74 --- /dev/null +++ b/sdk/component/examples/target/main.go @@ -0,0 +1,30 @@ +package main + +import ( + "context" + "log" + "time" + + "github.com/smithy-security/smithy/sdk/component" +) + +type sampleTarget struct{} + +func (s sampleTarget) Close(ctx context.Context) error { + component.FromContext(ctx).Info("Closing target.") + return nil +} + +func (s sampleTarget) Prepare(ctx context.Context) error { + component.FromContext(ctx).Info("Preparing.") + return nil +} + +func main() { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) + defer cancel() + + if err := component.RunTarget(ctx, sampleTarget{}, component.RunnerWithComponentName("sample-target")); err != nil { + log.Fatalf("unexpected run error: %v", err) + } +}