github.com/adacta-ru/mattermost-server@v5.11.1+incompatible/app/plugin_key_value_store.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"crypto/sha256"
     8  	"encoding/base64"
     9  	"net/http"
    10  
    11  	"github.com/mattermost/mattermost-server/mlog"
    12  	"github.com/mattermost/mattermost-server/model"
    13  )
    14  
    15  func getKeyHash(key string) string {
    16  	hash := sha256.New()
    17  	hash.Write([]byte(key))
    18  	return base64.StdEncoding.EncodeToString(hash.Sum(nil))
    19  }
    20  
    21  func (a *App) SetPluginKey(pluginId string, key string, value []byte) *model.AppError {
    22  	return a.SetPluginKeyWithExpiry(pluginId, key, value, 0)
    23  }
    24  
    25  func (a *App) SetPluginKeyWithExpiry(pluginId string, key string, value []byte, expireInSeconds int64) *model.AppError {
    26  	if expireInSeconds > 0 {
    27  		expireInSeconds = model.GetMillis() + (expireInSeconds * 1000)
    28  	}
    29  
    30  	kv := &model.PluginKeyValue{
    31  		PluginId: pluginId,
    32  		Key:      key,
    33  		Value:    value,
    34  		ExpireAt: expireInSeconds,
    35  	}
    36  
    37  	if result := <-a.Srv.Store.Plugin().SaveOrUpdate(kv); result.Err != nil {
    38  		mlog.Error("Failed to set plugin key value", mlog.String("plugin_id", pluginId), mlog.String("key", key), mlog.Err(result.Err))
    39  		return result.Err
    40  	}
    41  
    42  	// Clean up a previous entry using the hashed key, if it exists.
    43  	if result := <-a.Srv.Store.Plugin().Delete(pluginId, getKeyHash(key)); result.Err != nil {
    44  		mlog.Error("Failed to clean up previously hashed plugin key value", mlog.String("plugin_id", pluginId), mlog.String("key", key), mlog.Err(result.Err))
    45  	}
    46  
    47  	return nil
    48  }
    49  
    50  func (a *App) GetPluginKey(pluginId string, key string) ([]byte, *model.AppError) {
    51  	if result := <-a.Srv.Store.Plugin().Get(pluginId, key); result.Err == nil {
    52  		return result.Data.(*model.PluginKeyValue).Value, nil
    53  	} else if result.Err.StatusCode != http.StatusNotFound {
    54  		mlog.Error("Failed to query plugin key value", mlog.String("plugin_id", pluginId), mlog.String("key", key), mlog.Err(result.Err))
    55  		return nil, result.Err
    56  	}
    57  
    58  	// Lookup using the hashed version of the key for keys written prior to v5.6.
    59  	if result := <-a.Srv.Store.Plugin().Get(pluginId, getKeyHash(key)); result.Err == nil {
    60  		return result.Data.(*model.PluginKeyValue).Value, nil
    61  	} else if result.Err.StatusCode != http.StatusNotFound {
    62  		mlog.Error("Failed to query plugin key value using hashed key", mlog.String("plugin_id", pluginId), mlog.String("key", key), mlog.Err(result.Err))
    63  		return nil, result.Err
    64  	}
    65  
    66  	return nil, nil
    67  }
    68  
    69  func (a *App) DeletePluginKey(pluginId string, key string) *model.AppError {
    70  	if result := <-a.Srv.Store.Plugin().Delete(pluginId, getKeyHash(key)); result.Err != nil {
    71  		mlog.Error("Failed to delete plugin key value", mlog.String("plugin_id", pluginId), mlog.String("key", key), mlog.Err(result.Err))
    72  		return result.Err
    73  	}
    74  
    75  	// Also delete the key without hashing
    76  	if result := <-a.Srv.Store.Plugin().Delete(pluginId, key); result.Err != nil {
    77  		mlog.Error("Failed to delete plugin key value using hashed key", mlog.String("plugin_id", pluginId), mlog.String("key", key), mlog.Err(result.Err))
    78  		return result.Err
    79  	}
    80  
    81  	return nil
    82  }
    83  
    84  func (a *App) DeleteAllKeysForPlugin(pluginId string) *model.AppError {
    85  	if result := <-a.Srv.Store.Plugin().DeleteAllForPlugin(pluginId); result.Err != nil {
    86  		mlog.Error("Failed to delete all plugin key values", mlog.String("plugin_id", pluginId), mlog.Err(result.Err))
    87  		return result.Err
    88  	}
    89  
    90  	return nil
    91  }
    92  
    93  func (a *App) DeleteAllExpiredPluginKeys() *model.AppError {
    94  	if a.Srv == nil {
    95  		return nil
    96  	}
    97  
    98  	if result := <-a.Srv.Store.Plugin().DeleteAllExpired(); result.Err != nil {
    99  		mlog.Error("Failed to delete all expired plugin key values", mlog.Err(result.Err))
   100  		return result.Err
   101  	}
   102  
   103  	return nil
   104  }
   105  
   106  func (a *App) ListPluginKeys(pluginId string, page, perPage int) ([]string, *model.AppError) {
   107  	result := <-a.Srv.Store.Plugin().List(pluginId, page*perPage, perPage)
   108  
   109  	if result.Err != nil {
   110  		mlog.Error("Failed to list plugin key values", mlog.Int("page", page), mlog.Int("perPage", perPage), mlog.Err(result.Err))
   111  		return nil, result.Err
   112  	}
   113  
   114  	return result.Data.([]string), nil
   115  }