Skip to content

Commit

Permalink
F Updating examples and components' code to comply with new vulnerabi…
Browse files Browse the repository at this point in the history
…lity type and storages.
  • Loading branch information
andream16 committed Dec 15, 2024
1 parent 0a6e053 commit 6e073da
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 126 deletions.
16 changes: 6 additions & 10 deletions sdk/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ import (
"context"

"github.com/smithy-security/smithy/sdk/component/uuid"
finding "github.com/smithy-security/smithy/sdk/component/vulnerability-finding"
ocsf "github.com/smithy-security/smithy/sdk/gen/ocsf_schema/v1"
)

type VulnerabilityFinding struct {
ID uint64
Finding *ocsf.VulnerabilityFinding
}

// Helpers interfaces for common functionalities.
type (
// Validator allows validating vulnerability findings by a specified criteria.
Expand All @@ -23,13 +19,13 @@ type (
// Reader allows reading vulnerability findings from a storage.
Reader interface {
// Read reads vulnerability findings from a storage.
Read(ctx context.Context, instanceID uuid.UUID) ([]*VulnerabilityFinding, error)
Read(ctx context.Context, instanceID uuid.UUID) ([]*finding.VulnerabilityFinding, error)
}

// Updater allows updating vulnerability findings in an underlying storage.
Updater interface {
// Update updates existing vulnerability findings.
Update(ctx context.Context, instanceID uuid.UUID, findings []*VulnerabilityFinding) error
Update(ctx context.Context, instanceID uuid.UUID, findings []*finding.VulnerabilityFinding) error
}

// Writer allows writing non-existent vulnerability findings in an underlying storage.
Expand Down Expand Up @@ -72,19 +68,19 @@ type (
Filter interface {
// Filter returns filtered findings from the supplied ones applying some criteria.
// It returns false if no findings have been filtered out.
Filter(ctx context.Context, findings []*VulnerabilityFinding) ([]*VulnerabilityFinding, bool, error)
Filter(ctx context.Context, findings []*finding.VulnerabilityFinding) ([]*finding.VulnerabilityFinding, bool, error)
}

// Enricher allows enriching vulnerability findings by some criteria.
Enricher interface {
// Annotate enriches vulnerability findings by some criteria.
Annotate(ctx context.Context, findings []*VulnerabilityFinding) ([]*VulnerabilityFinding, error)
Annotate(ctx context.Context, findings []*finding.VulnerabilityFinding) ([]*finding.VulnerabilityFinding, error)
}

// Reporter advertises behaviours for reporting vulnerability findings.
Reporter interface {
// Report reports vulnerability findings on a specified destination.
// i.e. raises them as tickets on your favourite ticketing system.
Report(ctx context.Context, findings []*VulnerabilityFinding) error
Report(ctx context.Context, findings []*finding.VulnerabilityFinding) error
}
)
83 changes: 7 additions & 76 deletions sdk/component/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"context"
"testing"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"

"github.com/smithy-security/smithy/sdk/component"
finding "github.com/smithy-security/smithy/sdk/component/vulnerability-finding"
ocsf "github.com/smithy-security/smithy/sdk/gen/ocsf_schema/v1"
)

Expand All @@ -23,101 +23,32 @@ type (
testFilter struct{}
)

func (t testFilter) Read(
ctx context.Context,
instanceID uuid.UUID,
) ([]*ocsf.VulnerabilityFinding, error) {
return nil, nil
}

func (t testFilter) Filter(
ctx context.Context,
findings []*ocsf.VulnerabilityFinding,
) ([]*ocsf.VulnerabilityFinding, bool, error) {
return nil, false, nil
}

func (t testFilter) Close(ctx context.Context) error {
return nil
}

func (t testFilter) Update(
ctx context.Context,
instanceID uuid.UUID,
findings []*ocsf.VulnerabilityFinding,
) error {
return nil
}

func (t testReporter) Read(
ctx context.Context,
instanceID uuid.UUID,
) ([]*ocsf.VulnerabilityFinding, error) {
return nil, nil
}

func (t testReporter) Report(ctx context.Context, findings []*ocsf.VulnerabilityFinding) error {
return nil
}

func (t testReporter) Close(ctx context.Context) error {
return nil
}

func (t testEnricher) Read(
ctx context.Context,
instanceID uuid.UUID,
) ([]*ocsf.VulnerabilityFinding, error) {
return nil, nil
findings []*finding.VulnerabilityFinding,
) ([]*finding.VulnerabilityFinding, bool, error) {
return nil, true, nil
}

func (t testEnricher) Update(
ctx context.Context,
instanceID uuid.UUID,
findings []*ocsf.VulnerabilityFinding,
) error {
func (t testReporter) Report(ctx context.Context, findings []*finding.VulnerabilityFinding) error {
return nil
}

func (t testEnricher) Annotate(
ctx context.Context,
findings []*ocsf.VulnerabilityFinding,
) ([]*ocsf.VulnerabilityFinding, error) {
findings []*finding.VulnerabilityFinding,
) ([]*finding.VulnerabilityFinding, error) {
return nil, nil
}

func (t testEnricher) Close(ctx context.Context) error {
return nil
}

func (t testScanner) Write(
ctx context.Context,
instanceID uuid.UUID,
findings []*ocsf.VulnerabilityFinding,
) error {
return nil
}

func (t testScanner) Validate(finding *ocsf.VulnerabilityFinding) error {
return nil
}

func (t testScanner) Transform(ctx context.Context) ([]*ocsf.VulnerabilityFinding, error) {
return nil, nil
}

func (t testScanner) Close(ctx context.Context) error {
return nil
}

func (t testTarget) Prepare(ctx context.Context) error {
return nil
}

func (t testTarget) Close(ctx context.Context) error {
return nil
}

func TestImplementations(t *testing.T) {
assert.Implements(t, (*component.Target)(nil), testTarget{})
assert.Implements(t, (*component.Scanner)(nil), testScanner{})
Expand Down
12 changes: 6 additions & 6 deletions sdk/component/enricher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/smithy-security/smithy/sdk/component"
"github.com/smithy-security/smithy/sdk/component/internal/mocks"
"github.com/smithy-security/smithy/sdk/component/uuid"
ocsf "github.com/smithy-security/smithy/sdk/gen/ocsf_schema/v1"
vf "github.com/smithy-security/smithy/sdk/component/vulnerability-finding"
)

func runEnricherHelper(
Expand Down Expand Up @@ -41,8 +41,8 @@ func TestRunEnricher(t *testing.T) {
mockCtx = gomock.AssignableToTypeOf(ctx)
mockStore = mocks.NewMockStorer(ctrl)
mockEnricher = mocks.NewMockEnricher(ctrl)
vulns = make([]*ocsf.VulnerabilityFinding, 0, 2)
enrichedVulns = make([]*ocsf.VulnerabilityFinding, 0, 2)
vulns = make([]*vf.VulnerabilityFinding, 0, 2)
enrichedVulns = make([]*vf.VulnerabilityFinding, 0, 2)
)

t.Run("it should run a enricher correctly and enrich out one finding", func(t *testing.T) {
Expand Down Expand Up @@ -80,7 +80,7 @@ func TestRunEnricher(t *testing.T) {
EXPECT().
Annotate(mockCtx, vulns).
DoAndReturn(
func(ctx context.Context, vulns []*ocsf.VulnerabilityFinding) ([]*ocsf.VulnerabilityFinding, error) {
func(ctx context.Context, vulns []*vf.VulnerabilityFinding) ([]*vf.VulnerabilityFinding, error) {
cancel()
return enrichedVulns, nil
}),
Expand All @@ -91,7 +91,7 @@ func TestRunEnricher(t *testing.T) {
func(
ctx context.Context,
instanceID uuid.UUID,
vulns []*ocsf.VulnerabilityFinding,
vulns []*vf.VulnerabilityFinding,
) error {
<-ctx.Done()
return nil
Expand Down Expand Up @@ -182,7 +182,7 @@ func TestRunEnricher(t *testing.T) {
EXPECT().
Annotate(mockCtx, vulns).
DoAndReturn(
func(ctx context.Context, vulns []*ocsf.VulnerabilityFinding) ([]*ocsf.VulnerabilityFinding, error) {
func(ctx context.Context, vulns []*vf.VulnerabilityFinding) ([]*vf.VulnerabilityFinding, error) {
panic(errAnnotation)
return enrichedVulns, nil
}),
Expand Down
6 changes: 3 additions & 3 deletions sdk/component/examples/enricher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"time"

"github.com/smithy-security/smithy/sdk/component"
ocsf "github.com/smithy-security/smithy/sdk/gen/ocsf_schema/v1"
vf "github.com/smithy-security/smithy/sdk/component/vulnerability-finding"
)

type sampleEnricher struct{}

func (s sampleEnricher) Annotate(ctx context.Context, findings []*ocsf.VulnerabilityFinding) ([]*ocsf.VulnerabilityFinding, error) {
func (s sampleEnricher) Annotate(ctx context.Context, findings []*vf.VulnerabilityFinding) ([]*vf.VulnerabilityFinding, error) {
component.LoggerFromContext(ctx).Info("Annotate.")
return make([]*ocsf.VulnerabilityFinding, 0, 10), nil
return make([]*vf.VulnerabilityFinding, 0, 10), nil
}

func main() {
Expand Down
6 changes: 3 additions & 3 deletions sdk/component/examples/filter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"time"

"github.com/smithy-security/smithy/sdk/component"
ocsf "github.com/smithy-security/smithy/sdk/gen/ocsf_schema/v1"
vf "github.com/smithy-security/smithy/sdk/component/vulnerability-finding"
)

type sampleFilter struct{}

func (s sampleFilter) Filter(ctx context.Context, findings []*ocsf.VulnerabilityFinding) ([]*ocsf.VulnerabilityFinding, bool, error) {
func (s sampleFilter) Filter(ctx context.Context, findings []*vf.VulnerabilityFinding) ([]*vf.VulnerabilityFinding, bool, error) {
component.LoggerFromContext(ctx).Info("Filter.")
return make([]*ocsf.VulnerabilityFinding, 0, 80), true, nil
return make([]*vf.VulnerabilityFinding, 0, 80), true, nil
}

func main() {
Expand Down
4 changes: 2 additions & 2 deletions sdk/component/examples/reporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"time"

"github.com/smithy-security/smithy/sdk/component"
ocsf "github.com/smithy-security/smithy/sdk/gen/ocsf_schema/v1"
vf "github.com/smithy-security/smithy/sdk/component/vulnerability-finding"
)

type sampleReporter struct{}

func (s sampleReporter) Report(ctx context.Context, findings []*ocsf.VulnerabilityFinding) error {
func (s sampleReporter) Report(ctx context.Context, findings []*vf.VulnerabilityFinding) error {
component.LoggerFromContext(ctx).Info("Report.")
return nil
}
Expand Down
12 changes: 6 additions & 6 deletions sdk/component/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/smithy-security/smithy/sdk/component"
"github.com/smithy-security/smithy/sdk/component/internal/mocks"
"github.com/smithy-security/smithy/sdk/component/uuid"
ocsf "github.com/smithy-security/smithy/sdk/gen/ocsf_schema/v1"
vf "github.com/smithy-security/smithy/sdk/component/vulnerability-finding"
)

func runFilterHelper(
Expand Down Expand Up @@ -40,8 +40,8 @@ func TestRunFilter(t *testing.T) {
mockCtx = gomock.AssignableToTypeOf(ctx)
mockStore = mocks.NewMockStorer(ctrl)
mockFilter = mocks.NewMockFilter(ctrl)
vulns = make([]*ocsf.VulnerabilityFinding, 0, 2)
filteredVulns = make([]*ocsf.VulnerabilityFinding, 0, 1)
vulns = make([]*vf.VulnerabilityFinding, 0, 2)
filteredVulns = make([]*vf.VulnerabilityFinding, 0, 1)
)

t.Run("it should run a filter correctly and filter out one finding", func(t *testing.T) {
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestRunFilter(t *testing.T) {
mockFilter.
EXPECT().
Filter(mockCtx, vulns).
DoAndReturn(func(ctx context.Context, vulns []*ocsf.VulnerabilityFinding) ([]*ocsf.VulnerabilityFinding, bool, error) {
DoAndReturn(func(ctx context.Context, vulns []*vf.VulnerabilityFinding) ([]*vf.VulnerabilityFinding, bool, error) {
cancel()
return filteredVulns, true, nil
}),
Expand All @@ -108,7 +108,7 @@ func TestRunFilter(t *testing.T) {
func(
ctx context.Context,
instanceID uuid.UUID,
vulns []*ocsf.VulnerabilityFinding,
vulns []*vf.VulnerabilityFinding,
) error {
<-ctx.Done()
return nil
Expand Down Expand Up @@ -196,7 +196,7 @@ func TestRunFilter(t *testing.T) {
mockFilter.
EXPECT().
Filter(mockCtx, vulns).
DoAndReturn(func(ctx context.Context, vulns []*ocsf.VulnerabilityFinding) ([]*ocsf.VulnerabilityFinding, bool, error) {
DoAndReturn(func(ctx context.Context, vulns []*vf.VulnerabilityFinding) ([]*vf.VulnerabilityFinding, bool, error) {
panic(errFilter)
return filteredVulns, true, nil
}),
Expand Down
Loading

0 comments on commit 6e073da

Please sign in to comment.