diff --git a/sdk/component/conf.go b/sdk/component/conf.go index 676761ea0..912bad072 100644 --- a/sdk/component/conf.go +++ b/sdk/component/conf.go @@ -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" @@ -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 } @@ -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 == "": @@ -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 } } @@ -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) } diff --git a/sdk/component/enricher_test.go b/sdk/component/enricher_test.go index af9892478..ae259ae9a 100644 --- a/sdk/component/enricher_test.go +++ b/sdk/component/enricher_test.go @@ -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" ) diff --git a/sdk/component/enum.go b/sdk/component/enum.go new file mode 100644 index 000000000..1fbfe639f --- /dev/null +++ b/sdk/component/enum.go @@ -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 +) diff --git a/sdk/component/enum_enum.go b/sdk/component/enum_enum.go new file mode 100644 index 000000000..9b5cf836d --- /dev/null +++ b/sdk/component/enum_enum.go @@ -0,0 +1,83 @@ +// Code generated by go-enum DO NOT EDIT. +// Version: +// Revision: +// Build Date: +// Built By: + +package component + +import ( + "errors" + "fmt" +) + +const ( + // RunnerConfigLoggingLevelDebug is a RunnerConfigLoggingLevel of type debug. + RunnerConfigLoggingLevelDebug RunnerConfigLoggingLevel = "debug" + // RunnerConfigLoggingLevelInfo is a RunnerConfigLoggingLevel of type info. + RunnerConfigLoggingLevelInfo RunnerConfigLoggingLevel = "info" + // RunnerConfigLoggingLevelError is a RunnerConfigLoggingLevel of type error. + RunnerConfigLoggingLevelError RunnerConfigLoggingLevel = "error" + // RunnerConfigLoggingLevelWarn is a RunnerConfigLoggingLevel of type warn. + RunnerConfigLoggingLevelWarn RunnerConfigLoggingLevel = "warn" +) + +var ErrInvalidRunnerConfigLoggingLevel = errors.New("not a valid RunnerConfigLoggingLevel") + +// String implements the Stringer interface. +func (x RunnerConfigLoggingLevel) String() string { + return string(x) +} + +// IsValid provides a quick way to determine if the typed value is +// part of the allowed enumerated values +func (x RunnerConfigLoggingLevel) IsValid() bool { + _, err := ParseRunnerConfigLoggingLevel(string(x)) + return err == nil +} + +var _RunnerConfigLoggingLevelValue = map[string]RunnerConfigLoggingLevel{ + "debug": RunnerConfigLoggingLevelDebug, + "info": RunnerConfigLoggingLevelInfo, + "error": RunnerConfigLoggingLevelError, + "warn": RunnerConfigLoggingLevelWarn, +} + +// ParseRunnerConfigLoggingLevel attempts to convert a string to a RunnerConfigLoggingLevel. +func ParseRunnerConfigLoggingLevel(name string) (RunnerConfigLoggingLevel, error) { + if x, ok := _RunnerConfigLoggingLevelValue[name]; ok { + return x, nil + } + return RunnerConfigLoggingLevel(""), fmt.Errorf("%s is %w", name, ErrInvalidRunnerConfigLoggingLevel) +} + +const ( + // StoreTypeLocal is a storeType of type local. + StoreTypeLocal storeType = "local" +) + +var ErrInvalidstoreType = errors.New("not a valid storeType") + +// String implements the Stringer interface. +func (x storeType) String() string { + return string(x) +} + +// IsValid provides a quick way to determine if the typed value is +// part of the allowed enumerated values +func (x storeType) IsValid() bool { + _, err := ParsestoreType(string(x)) + return err == nil +} + +var _storeTypeValue = map[string]storeType{ + "local": StoreTypeLocal, +} + +// ParsestoreType attempts to convert a string to a storeType. +func ParsestoreType(name string) (storeType, error) { + if x, ok := _storeTypeValue[name]; ok { + return x, nil + } + return storeType(""), fmt.Errorf("%s is %w", name, ErrInvalidstoreType) +} diff --git a/sdk/component/internal/storer/local/sqlite/enum.go b/sdk/component/internal/storer/local/sqlite/enum.go new file mode 100644 index 000000000..bde170686 --- /dev/null +++ b/sdk/component/internal/storer/local/sqlite/enum.go @@ -0,0 +1,6 @@ +package sqlite + +type ( + // ENUM(findings, instance_id, updated_at) + columnName string +) diff --git a/sdk/component/internal/storer/local/sqlite/enum_enum.go b/sdk/component/internal/storer/local/sqlite/enum_enum.go new file mode 100644 index 000000000..6c66455fe --- /dev/null +++ b/sdk/component/internal/storer/local/sqlite/enum_enum.go @@ -0,0 +1,49 @@ +// Code generated by go-enum DO NOT EDIT. +// Version: +// Revision: +// Build Date: +// Built By: + +package sqlite + +import ( + "errors" + "fmt" +) + +const ( + // ColumnNameFindings is a columnName of type findings. + ColumnNameFindings columnName = "findings" + // ColumnNameInstanceId is a columnName of type instance_id. + ColumnNameInstanceId columnName = "instance_id" + // ColumnNameUpdatedAt is a columnName of type updated_at. + ColumnNameUpdatedAt columnName = "updated_at" +) + +var ErrInvalidcolumnName = errors.New("not a valid columnName") + +// String implements the Stringer interface. +func (x columnName) String() string { + return string(x) +} + +// IsValid provides a quick way to determine if the typed value is +// part of the allowed enumerated values +func (x columnName) IsValid() bool { + _, err := ParsecolumnName(string(x)) + return err == nil +} + +var _columnNameValue = map[string]columnName{ + "findings": ColumnNameFindings, + "instance_id": ColumnNameInstanceId, + "updated_at": ColumnNameUpdatedAt, +} + +// ParsecolumnName attempts to convert a string to a columnName. +func ParsecolumnName(name string) (columnName, error) { + if x, ok := _columnNameValue[name]; ok { + return x, nil + } + return columnName(""), fmt.Errorf("%s is %w", name, ErrInvalidcolumnName) +} diff --git a/sdk/component/internal/storer/local/sqlite/sqlite.go b/sdk/component/internal/storer/local/sqlite/sqlite.go index 8af26a0d3..20a2f72ff 100644 --- a/sdk/component/internal/storer/local/sqlite/sqlite.go +++ b/sdk/component/internal/storer/local/sqlite/sqlite.go @@ -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 { @@ -33,8 +27,6 @@ type ( managerOption func(*manager) error - columnName string - // ErrInvalidConstructor should be used for invalid manager constructor errors. ErrInvalidConstructor struct { argName string @@ -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) } @@ -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 { @@ -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) } @@ -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) diff --git a/sdk/component/logger.go b/sdk/component/logger.go index 1cde5516f..a4394d713 100644 --- a/sdk/component/logger.go +++ b/sdk/component/logger.go @@ -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) @@ -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) diff --git a/sdk/component/storer.go b/sdk/component/storer.go index 126920a65..5dd69145c 100644 --- a/sdk/component/storer.go +++ b/sdk/component/storer.go @@ -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) diff --git a/sdk/component/target_test.go b/sdk/component/target_test.go index b782a8a33..7950a7b9d 100644 --- a/sdk/component/target_test.go +++ b/sdk/component/target_test.go @@ -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 {