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

ocu-267 - Leveraging go-enum #468

Merged
merged 4 commits into from
Nov 5, 2024
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ install-go-fmt-tools:

fmt-go:
@echo "Tidying up Go files"
@gofmt -l -w $$(find . -name *.go -not -path "./vendor/*" | xargs -n 1 dirname | uniq)
@goimports -local $$(cat go.mod | grep -E "^module" | sed 's/module //') -w $$(find . -name *.go -not -path "./vendor/*" | xargs -n 1 dirname | uniq)
@gofmt -l -w $$(find . -name *.go -not -path "**/vendor/*" | xargs -n 1 dirname | uniq)
@goimports -local $$(cat go.mod | grep -E "^module" | sed 's/module //') -w $$(find . -name *.go -not -path "**/vendor/*" | xargs -n 1 dirname | uniq)

install-md-fmt-tools:
@npm ci
Expand Down
23 changes: 7 additions & 16 deletions sdk/component/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ import (
)

const (
logLevelDebug RunnerConfigLoggingLevel = "debug"
logLevelInfo RunnerConfigLoggingLevel = "info"
logLevelError RunnerConfigLoggingLevel = "error"
logLevelWarn RunnerConfigLoggingLevel = "warn"

// Err reasons.
errReasonCannotBeEmpty = "cannot be empty"
errReasonUnsupportedValue = "unsupported value"
Expand Down Expand Up @@ -47,13 +42,9 @@ type (
storerConfig runnerConfigStorer
}

// RunnerConfigLoggingLevel is used to represent log levels.
RunnerConfigLoggingLevel string

// RunnerConfigLogging contains the configuration related with the runner logger.
RunnerConfigLogging struct {
Level RunnerConfigLoggingLevel

Level RunnerConfigLoggingLevel
Logger Logger
}

Expand Down Expand Up @@ -91,10 +82,6 @@ func (er ErrInvalidRunnerConfig) Error() string {
return fmt.Sprintf("invalid configuration, field '%s': %s", er.FieldName, er.Reason)
}

func (rl RunnerConfigLoggingLevel) String() string {
return string(rl)
}

func (rc *RunnerConfig) isValid() error {
switch {
case rc.SDKVersion == "":
Expand Down Expand Up @@ -190,7 +177,7 @@ func RunnerWithStorer(stType string, store Storer) RunnerOption {
}
}
r.config.storerConfig.store = store
r.config.storerConfig.storeType = storeTypeLocal
r.config.storerConfig.storeType = StoreTypeLocal
return nil
}
}
Expand Down Expand Up @@ -221,7 +208,11 @@ func newRunnerConfig() (*RunnerConfig, error) {
// --- END - BASIC ENV - END ---

// --- BEGIN - LOGGING ENV - BEGIN ---
logLevel, err := fromEnvOrDefault(envVarKeyLoggingLogLevel, logLevelDebug.String(), withFallbackToDefaultOnError(true))
logLevel, err := fromEnvOrDefault(
envVarKeyLoggingLogLevel,
RunnerConfigLoggingLevelDebug.String(),
withFallbackToDefaultOnError(true),
)
if err != nil {
return nil, errors.Errorf("could not lookup environment for '%s': %w", envVarKeyLoggingLogLevel, err)
}
Expand Down
3 changes: 1 addition & 2 deletions sdk/component/enricher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import (
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"

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

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

Expand Down
10 changes: 10 additions & 0 deletions sdk/component/enum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package component

type (
// RunnerConfigLoggingLevel is used to represent log levels.
// ENUM(debug, info, error, warn)
RunnerConfigLoggingLevel string

// ENUM(local)
storeType string
)
83 changes: 83 additions & 0 deletions sdk/component/enum_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions sdk/component/internal/storer/local/sqlite/enum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package sqlite

type (
// ENUM(findings, instance_id, updated_at)
columnName string
)
49 changes: 49 additions & 0 deletions sdk/component/internal/storer/local/sqlite/enum_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 7 additions & 19 deletions sdk/component/internal/storer/local/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,7 @@ import (
ocsf "github.com/smithy-security/smithy/sdk/gen/com/github/ocsf/ocsf_schema/v1"
)

const (
errInvalidConstructorEmptyReason = "cannot be empty"

columnNameFindings columnName = "findings"
columnNameinstanceID columnName = "instance_id"
columnNameUpdatedAt columnName = "updated_at"
)
const errInvalidConstructorEmptyReason = "cannot be empty"

type (
manager struct {
Expand All @@ -33,8 +27,6 @@ type (

managerOption func(*manager) error

columnName string

// ErrInvalidConstructor should be used for invalid manager constructor errors.
ErrInvalidConstructor struct {
argName string
Expand All @@ -53,10 +45,6 @@ func ManagerWithClock(clock clockwork.Clock) managerOption {
}
}

func (cn columnName) String() string {
return string(cn)
}

func (e ErrInvalidConstructor) Error() string {
return fmt.Sprintf("invalid argument '%s': %s", e.argName, e.reason)
}
Expand Down Expand Up @@ -113,7 +101,7 @@ func (m *manager) Read(ctx context.Context, instanceID uuid.UUID) ([]*ocsf.Vulne
err = stmt.
QueryRowContext(
ctx,
sql.Named(columnNameinstanceID.String(), instanceID.String()),
sql.Named(ColumnNameInstanceId.String(), instanceID.String()),
).
Scan(&jsonFindingsStr)
if err != nil {
Expand Down Expand Up @@ -159,8 +147,8 @@ func (m *manager) Write(ctx context.Context, instanceID uuid.UUID, findings []*o
defer stmt.Close()

if _, err = stmt.Exec(
sql.Named(columnNameinstanceID.String(), instanceID.String()),
sql.Named(columnNameFindings.String(), jsonFindings),
sql.Named(ColumnNameInstanceId.String(), instanceID.String()),
sql.Named(ColumnNameFindings.String(), jsonFindings),
); err != nil {
return errors.Errorf("could not insert findings: %w", err)
}
Expand Down Expand Up @@ -192,9 +180,9 @@ func (m *manager) Update(ctx context.Context, instanceID uuid.UUID, findings []*
defer stmt.Close()

res, err := stmt.Exec(
sql.Named(columnNameinstanceID.String(), instanceID.String()),
sql.Named(columnNameUpdatedAt.String(), m.clock.Now().UTC().Format(time.RFC3339)),
sql.Named(columnNameFindings.String(), jsonFindings),
sql.Named(ColumnNameInstanceId.String(), instanceID.String()),
sql.Named(ColumnNameUpdatedAt.String(), m.clock.Now().UTC().Format(time.RFC3339)),
sql.Named(ColumnNameFindings.String(), jsonFindings),
)
if err != nil {
return errors.Errorf("could not update findings: %w", err)
Expand Down
10 changes: 5 additions & 5 deletions sdk/component/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func ContextWithLogger(ctx context.Context, logger Logger) context.Context {
func LoggerFromContext(ctx context.Context) Logger {
logger := ctx.Value(ctxLoggerKey)
if logger == nil {
l, _ := newDefaultLogger(logLevelDebug)
l, _ := newDefaultLogger(RunnerConfigLoggingLevelDebug)
return l
}
return logger.(Logger)
Expand All @@ -96,13 +96,13 @@ func newDefaultLogger(level RunnerConfigLoggingLevel) (*defaultLogger, error) {
var logLevel slog.Level

switch level {
case logLevelDebug:
case RunnerConfigLoggingLevelDebug:
logLevel = slog.LevelDebug
case logLevelInfo:
case RunnerConfigLoggingLevelInfo:
logLevel = slog.LevelInfo
case logLevelError:
case RunnerConfigLoggingLevelError:
logLevel = slog.LevelError
case logLevelWarn:
case RunnerConfigLoggingLevelWarn:
logLevel = slog.LevelWarn
default:
return nil, errors.Errorf("unknown logger level: %s", level)
Expand Down
8 changes: 2 additions & 6 deletions sdk/component/storer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@ import (
"github.com/smithy-security/smithy/sdk/component/internal/storer/local/sqlite"
)

type storeType string

const storeTypeLocal storeType = "local"

func isAllowedStoreType(st storeType) bool {
return st == storeTypeLocal
return st == StoreTypeLocal
}

func newStorer(conf runnerConfigStorer) (Storer, error) {
if conf.storeType == storeTypeLocal {
if conf.storeType == StoreTypeLocal {
localMgr, err := sqlite.NewManager(conf.dbDSN)
if err != nil {
return nil, errors.Errorf("unable to initialize local sqlite manager: %w", err)
Expand Down
3 changes: 1 addition & 2 deletions sdk/component/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import (
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"

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

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

func runTargetHelper(t *testing.T, ctx context.Context, target component.Target, store component.Storer) error {
Expand Down
Loading