From c9f7ebabbb8a9f8559e4e5b916ae2cb1b483658b Mon Sep 17 00:00:00 2001 From: Chris Schinnerl Date: Fri, 15 Mar 2024 10:43:21 +0100 Subject: [PATCH 1/3] stores: add setting type --- stores/settingsdb.go | 10 +++++----- stores/types.go | 30 ++++++++++++++++++++++++++++++ stores/types_test.go | 14 ++++++++++++++ 3 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 stores/types_test.go diff --git a/stores/settingsdb.go b/stores/settingsdb.go index 3b1eafc31..f7ba1e82d 100644 --- a/stores/settingsdb.go +++ b/stores/settingsdb.go @@ -14,8 +14,8 @@ type ( dbSetting struct { Model - Key string `gorm:"unique;index;NOT NULL"` - Value string `gorm:"NOT NULL"` + Key string `gorm:"unique;index;NOT NULL"` + Value setting `gorm:"NOT NULL"` } ) @@ -52,8 +52,8 @@ func (s *SQLStore) Setting(ctx context.Context, key string) (string, error) { } else if err != nil { return "", err } - s.settings[key] = entry.Value - return entry.Value, nil + s.settings[key] = string(entry.Value) + return string(entry.Value), nil } // Settings implements the bus.SettingStore interface. @@ -74,7 +74,7 @@ func (s *SQLStore) UpdateSetting(ctx context.Context, key, value string) error { DoUpdates: clause.AssignmentColumns([]string{"value"}), }).Create(&dbSetting{ Key: key, - Value: value, + Value: setting(value), }).Error if err != nil { return err diff --git a/stores/types.go b/stores/types.go index 42a8d29e4..4e4be2e45 100644 --- a/stores/types.go +++ b/stores/types.go @@ -35,8 +35,38 @@ type ( balance big.Int unsigned64 uint64 // used for storing large uint64 values in sqlite secretKey []byte + setting string ) +// GormDataType implements gorm.GormDataTypeInterface. +func (setting) GormDataType() string { + return "string" +} + +// String implements fmt.Stringer to prevent "s3authentication" settings from +// getting leaked. +func (s setting) String() string { + if strings.Contains(string(s), "v4Keypairs") { + return "*****" + } + return string(s) +} + +// Scan scans value into the setting +func (s *setting) Scan(value interface{}) error { + str, ok := value.(string) + if !ok { + return errors.New(fmt.Sprint("failed to unmarshal setting value:", value)) + } + *s = setting(str) + return nil +} + +// Value returns an key value, implements driver.Valuer interface. +func (s setting) Value() (driver.Value, error) { + return string(s), nil +} + // GormDataType implements gorm.GormDataTypeInterface. func (secretKey) GormDataType() string { return "bytes" diff --git a/stores/types_test.go b/stores/types_test.go new file mode 100644 index 000000000..c985dd012 --- /dev/null +++ b/stores/types_test.go @@ -0,0 +1,14 @@ +package stores + +import "testing" + +func TestTypeSetting(t *testing.T) { + s1 := setting("some setting") + s2 := setting("v4Keypairs") + + if s1.String() != "some setting" { + t.Fatal("unexpected string") + } else if s2.String() != "*****" { + t.Fatal("unexpected string") + } +} From deb4af4c4fd2939b665919e3227e64bce182a7d5 Mon Sep 17 00:00:00 2001 From: Chris Schinnerl Date: Fri, 15 Mar 2024 11:26:24 +0100 Subject: [PATCH 2/3] stores: add []byte case to Scan --- stores/types.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/stores/types.go b/stores/types.go index 4e4be2e45..a8f753077 100644 --- a/stores/types.go +++ b/stores/types.go @@ -54,11 +54,14 @@ func (s setting) String() string { // Scan scans value into the setting func (s *setting) Scan(value interface{}) error { - str, ok := value.(string) - if !ok { - return errors.New(fmt.Sprint("failed to unmarshal setting value:", value)) + switch value := value.(type) { + case string: + *s = setting(value) + case []byte: + *s = setting(value) + default: + return fmt.Errorf("failed to unmarshal setting value from type %t", value) } - *s = setting(str) return nil } From 5440f904448dfb4b541bbc636b4793010e24a28c Mon Sep 17 00:00:00 2001 From: Chris Schinnerl Date: Mon, 18 Mar 2024 10:51:41 +0100 Subject: [PATCH 3/3] stores: fix docstring --- stores/types.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stores/types.go b/stores/types.go index a8f753077..a2628d834 100644 --- a/stores/types.go +++ b/stores/types.go @@ -65,7 +65,7 @@ func (s *setting) Scan(value interface{}) error { return nil } -// Value returns an key value, implements driver.Valuer interface. +// Value returns a setting value, implements driver.Valuer interface. func (s setting) Value() (driver.Value, error) { return string(s), nil }