Skip to content

Commit

Permalink
Adding examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
andream16 committed Oct 24, 2024
1 parent 3e8d90e commit 1e548c1
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 0 deletions.
41 changes: 41 additions & 0 deletions sdk/component/examples/enricher/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
41 changes: 41 additions & 0 deletions sdk/component/examples/filter/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
36 changes: 36 additions & 0 deletions sdk/component/examples/reporter/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
53 changes: 53 additions & 0 deletions sdk/component/examples/scanner/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
30 changes: 30 additions & 0 deletions sdk/component/examples/target/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 1e548c1

Please sign in to comment.