Skip to content
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

Implement embedded storage backend reader and writer - Part 1 #455

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions sdk/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package component
import (
"context"

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

Expand All @@ -17,19 +18,19 @@ type (
// Reader allows reading vulnerability findings from a storage.
Reader interface {
// Read reads vulnerability findings from a storage.
Read(ctx context.Context) ([]*ocsf.VulnerabilityFinding, error)
Read(ctx context.Context, workflowID uuid.UUID) ([]*ocsf.VulnerabilityFinding, error)
}

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

// Writer allows writing non-existent vulnerability findings in an underlying storage.
Writer interface {
// Write writes non-existing vulnerability findings.
Write(ctx context.Context, findings []*ocsf.VulnerabilityFinding) error
Write(ctx context.Context, workflowID uuid.UUID, findings []*ocsf.VulnerabilityFinding) error
}

// Closer allows to define behaviours to close component dependencies gracefully.
Expand Down
44 changes: 36 additions & 8 deletions sdk/component/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/assert"

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

Expand All @@ -22,23 +23,36 @@ type (
testFilter struct{}
)

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

func (t testFilter) Filter(ctx context.Context, findings []*ocsf.VulnerabilityFinding) ([]*ocsf.VulnerabilityFinding, bool, error) {
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, findings []*ocsf.VulnerabilityFinding) error {
func (t testFilter) Update(
ctx context.Context,
workflowID uuid.UUID,
findings []*ocsf.VulnerabilityFinding,
) error {
return nil
}

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

Expand All @@ -50,23 +64,37 @@ func (t testReporter) Close(ctx context.Context) error {
return nil
}

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

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

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

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

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

Expand Down
33 changes: 33 additions & 0 deletions sdk/component/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/smithy-security/smithy/sdk"
"github.com/smithy-security/smithy/sdk/component/internal/uuid"
)

const (
Expand All @@ -19,6 +20,7 @@ const (

// Env vars.
envVarKeyComponentName = "SMITHY_COMPONENT_NAME"
envVarKeyWorkflowID = "SMITHY_WORKFLOW_ID"
envVarKeyLoggingLogLevel = "SMITHY_LOG_LEVEL"
envVarKeyBackedStoreType = "SMITHY_BACKEND_STORE_TYPE"
)
Expand All @@ -29,6 +31,7 @@ type (
RunnerConfig struct {
SDKVersion string
ComponentName string
WorkflowID uuid.UUID

Logging RunnerConfigLogging
PanicHandler PanicHandler
Expand Down Expand Up @@ -98,6 +101,11 @@ func (rc *RunnerConfig) isValid() error {
FieldName: "component_name",
Reason: errReasonCannotBeEmpty,
}
case rc.WorkflowID.IsNil():
return ErrInvalidRunnerConfig{
FieldName: "workflow_id",
Reason: errReasonCannotBeNil,
}
case rc.Logging.Logger == nil:
return ErrInvalidRunnerConfig{
FieldName: "logger",
Expand Down Expand Up @@ -146,6 +154,20 @@ func RunnerWithComponentName(name string) RunnerOption {
}
}

// RunnerWithWorkflowID allows customising the workflow id.
func RunnerWithWorkflowID(id uuid.UUID) RunnerOption {
return func(r *runner) error {
if id.IsNil() {
return ErrRunnerOption{
OptionName: "workflow id",
Reason: errReasonCannotBeEmpty,
}
}
r.config.WorkflowID = id
return nil
}
}

// RunnerWithStorer can be used to customise the underlying storage.
func RunnerWithStorer(stType string, store Storer) RunnerOption {
return func(r *runner) error {
Expand Down Expand Up @@ -181,6 +203,16 @@ func newRunnerConfig() (*RunnerConfig, error) {
if err != nil {
return nil, fmt.Errorf("could not lookup environment for '%s': %w", envVarKeyComponentName, err)
}

workflowIDStr, err := fromEnvOrDefault(envVarKeyWorkflowID, "", withFallbackToDefaultOnError(true))
if err != nil {
return nil, fmt.Errorf("could not lookup environment for '%s': %w", envVarKeyWorkflowID, err)
}

workflowID, err := uuid.Parse(workflowIDStr)
if err != nil {
return nil, fmt.Errorf("could not parse workflow ID '%s': %w", workflowIDStr, err)
}
// --- END - BASIC ENV - END ---

// --- BEGIN - LOGGING ENV - BEGIN ---
Expand Down Expand Up @@ -222,6 +254,7 @@ func newRunnerConfig() (*RunnerConfig, error) {
return &RunnerConfig{
ComponentName: componentName,
SDKVersion: sdk.Version,
WorkflowID: workflowID,
Logging: RunnerConfigLogging{
Level: RunnerConfigLoggingLevel(logLevel),
Logger: logger,
Expand Down
9 changes: 5 additions & 4 deletions sdk/component/enricher.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ func RunEnricher(ctx context.Context, enricher Enricher, opts ...RunnerOption) e
ctx,
func(ctx context.Context, cfg *RunnerConfig) error {
var (
logger = LoggerFromContext(ctx).With(logKeyComponentType, "enricher")
store = cfg.storerConfig.store
workflowID = cfg.WorkflowID
logger = LoggerFromContext(ctx).With(logKeyComponentType, "enricher")
store = cfg.storerConfig.store
)

defer func() {
Expand All @@ -24,7 +25,7 @@ func RunEnricher(ctx context.Context, enricher Enricher, opts ...RunnerOption) e
logger.Debug("preparing to execute enricher component...")
logger.Debug("preparing to execute read step...")

findings, err := store.Read(ctx)
findings, err := store.Read(ctx, workflowID)
if err != nil {
logger.With(logKeyError, err.Error()).Error("reading step failed")
return fmt.Errorf("could not read: %w", err)
Expand All @@ -44,7 +45,7 @@ func RunEnricher(ctx context.Context, enricher Enricher, opts ...RunnerOption) e
logger.Debug("enricher step completed!")
logger.Debug("preparing to execute update step...")

if err := store.Update(ctx, enrichedFindings); err != nil {
if err := store.Update(ctx, workflowID, enrichedFindings); err != nil {
logger.With(logKeyError, err.Error()).Error("updating step failed")
return fmt.Errorf("could not update: %w", err)
}
Expand Down
Loading