diff --git a/sdk/component/examples/enricher/main.go b/sdk/component/examples/enricher/main.go new file mode 100644 index 000000000..1349efd90 --- /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.LoggerFromContext(ctx).Info("Closing enricher.") + return nil +} + +func (s sampleEnricher) Read(ctx context.Context) ([]*ocsf.VulnerabilityFinding, error) { + component.LoggerFromContext(ctx).Info("Read.") + return make([]*ocsf.VulnerabilityFinding, 0, 10), nil +} + +func (s sampleEnricher) Update(ctx context.Context, findings []*ocsf.VulnerabilityFinding) error { + component.LoggerFromContext(ctx).Info("Update.") + return nil +} + +func (s sampleEnricher) Annotate(ctx context.Context, findings []*ocsf.VulnerabilityFinding) ([]*ocsf.VulnerabilityFinding, error) { + component.LoggerFromContext(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..30f85432f --- /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.LoggerFromContext(ctx).Info("Closing filter.") + return nil +} + +func (s sampleFilter) Read(ctx context.Context) ([]*ocsf.VulnerabilityFinding, error) { + component.LoggerFromContext(ctx).Info("Read.") + return make([]*ocsf.VulnerabilityFinding, 0, 100), nil +} + +func (s sampleFilter) Update(ctx context.Context, findings []*ocsf.VulnerabilityFinding) error { + component.LoggerFromContext(ctx).Info("Update.") + return nil +} + +func (s sampleFilter) Filter(ctx context.Context, findings []*ocsf.VulnerabilityFinding) ([]*ocsf.VulnerabilityFinding, bool, error) { + component.LoggerFromContext(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..8c5dd357b --- /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.LoggerFromContext(ctx).Info("Closing reporter.") + return nil +} + +func (s sampleReporter) Read(ctx context.Context) ([]*ocsf.VulnerabilityFinding, error) { + component.LoggerFromContext(ctx).Info("Read.") + return make([]*ocsf.VulnerabilityFinding, 0, 100), nil +} + +func (s sampleReporter) Report(ctx context.Context, findings []*ocsf.VulnerabilityFinding) error { + component.LoggerFromContext(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..042f6a41d --- /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.LoggerFromContext(ctx).Info("Closing scanner.") + return nil +} + +func (s sampleScanner) Store(ctx context.Context, findings []*ocsf.VulnerabilityFinding) error { + component.LoggerFromContext(ctx).Info("Storing.") + return nil +} + +func (s sampleScanner) Scan(ctx context.Context) ([]component.Unmarshaler, error) { + component.LoggerFromContext(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.LoggerFromContext(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..d5c4d5cbc --- /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.LoggerFromContext(ctx).Info("Closing target.") + return nil +} + +func (s sampleTarget) Prepare(ctx context.Context) error { + component.LoggerFromContext(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) + } +}