From 34c4e543f9113bf68fb3d70c84e39d4f54e0a1f5 Mon Sep 17 00:00:00 2001 From: Ben Schumacher Date: Mon, 20 Dec 2021 13:13:50 +0100 Subject: [PATCH] [MM-39999] Increase key length in plugin KV store to 150 (#19002) --- app/plugin_api_tests/test_kv/main.go | 84 ++++++++++++++++++++++++++++ model/plugin_key_value.go | 2 +- model/plugin_key_value_test.go | 2 +- store/sqlstore/plugin_store.go | 2 +- store/sqlstore/upgrade.go | 2 + 5 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 app/plugin_api_tests/test_kv/main.go diff --git a/app/plugin_api_tests/test_kv/main.go b/app/plugin_api_tests/test_kv/main.go new file mode 100644 index 0000000000..5663aaf2a5 --- /dev/null +++ b/app/plugin_api_tests/test_kv/main.go @@ -0,0 +1,84 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +package main + +import ( + "bytes" + "fmt" + + "github.com/mattermost/mattermost-server/v6/app/plugin_api_tests" + "github.com/mattermost/mattermost-server/v6/model" + "github.com/mattermost/mattermost-server/v6/plugin" +) + +type MyPlugin struct { + plugin.MattermostPlugin + configuration plugin_api_tests.BasicConfig +} + +func (p *MyPlugin) OnConfigurationChange() error { + if err := p.API.LoadPluginConfiguration(&p.configuration); err != nil { + return err + } + return nil +} + +func (p *MyPlugin) MessageWillBePosted(_ *plugin.Context, _ *model.Post) (*model.Post, string) { + data := []byte("some data") + + appErr := p.API.KVSet("some_key", data) + if appErr != nil { + return nil, appErr.Error() + } + + rData, appErr := p.API.KVGet("some_key") + if appErr != nil { + return nil, appErr.Error() + } + if !bytes.Equal(data, rData) { + return nil, fmt.Sprintf("Data not equal, expected: %s, got: %s", string(data), string(rData)) + } + + data = []byte("some other data") + + var longKey string + for i := 0; i < model.KeyValueKeyMaxRunes; i++ { + longKey += "k" + } + + appErr = p.API.KVSet(longKey, data) + if appErr != nil { + return nil, appErr.Error() + } + + rData, appErr = p.API.KVGet(longKey) + if appErr != nil { + return nil, appErr.Error() + } + + if !bytes.Equal(data, rData) { + return nil, fmt.Sprintf("Data not equal, expected: %s, got: %s", string(data), string(rData)) + } + + longKey += "extra" + + appErr = p.API.KVSet(longKey, data) + if appErr == nil { + return nil, "Should have gotten an error for a to long key" + } + + rData, appErr = p.API.KVGet(longKey) + if appErr != nil { + return nil, "Should have gotten an error for a to long key" + } + if rData != nil { + return nil, "Returned data should have been nil" + } + + return nil, "OK" +} + +func main() { + plugin.ClientMain(&MyPlugin{}) +} diff --git a/model/plugin_key_value.go b/model/plugin_key_value.go index ad5971dd54..c615bcf7a8 100644 --- a/model/plugin_key_value.go +++ b/model/plugin_key_value.go @@ -10,7 +10,7 @@ import ( const ( KeyValuePluginIdMaxRunes = 190 - KeyValueKeyMaxRunes = 50 + KeyValueKeyMaxRunes = 150 ) type PluginKeyValue struct { diff --git a/model/plugin_key_value_test.go b/model/plugin_key_value_test.go index f5505c9659..f9881d236d 100644 --- a/model/plugin_key_value_test.go +++ b/model/plugin_key_value_test.go @@ -20,6 +20,6 @@ func TestPluginKeyIsValid(t *testing.T) { kv.Key = "" assert.NotNil(t, kv.IsValid()) - kv.Key = "this is an extremely long key and should be invalid and this is being verified in this test" + kv.Key = "this is an extremely long, long, long, long, long, long, long, long, long, long, long, long, long key and should be invalid and this is being verified in this test" assert.NotNil(t, kv.IsValid()) } diff --git a/store/sqlstore/plugin_store.go b/store/sqlstore/plugin_store.go index 8e11bbd1c7..be9523f715 100644 --- a/store/sqlstore/plugin_store.go +++ b/store/sqlstore/plugin_store.go @@ -29,7 +29,7 @@ func newSqlPluginStore(sqlStore *SqlStore) store.PluginStore { for _, db := range sqlStore.GetAllConns() { table := db.AddTableWithName(model.PluginKeyValue{}, "PluginKeyValueStore").SetKeys(false, "PluginId", "Key") table.ColMap("PluginId").SetMaxSize(190) - table.ColMap("Key").SetMaxSize(50) + table.ColMap("Key").SetMaxSize(150) table.ColMap("Value").SetMaxSize(8192) } diff --git a/store/sqlstore/upgrade.go b/store/sqlstore/upgrade.go index 41ef921e4c..0f29d511d6 100644 --- a/store/sqlstore/upgrade.go +++ b/store/sqlstore/upgrade.go @@ -1426,6 +1426,8 @@ func upgradeDatabaseToVersion630(sqlStore *SqlStore) { sqlStore.CreateColumnIfNotExists("Schemes", "DefaultRunAdminRole", "VARCHAR(64)", "VARCHAR(64)", "") sqlStore.CreateColumnIfNotExists("Schemes", "DefaultRunMemberRole", "VARCHAR(64)", "VARCHAR(64)", "") + sqlStore.AlterColumnTypeIfExists("PluginKeyValueStore", "PKey", "VARCHAR(150)", "VARCHAR(150)") + // saveSchemaVersion(sqlStore, Version630) // } }