2020-02-19 12:17:05 -06:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/components/securejsondata"
|
|
|
|
)
|
|
|
|
|
2020-11-17 04:51:31 -06:00
|
|
|
// clearPluginSettingDecryptionCache clears the datasource decryption cache.
|
|
|
|
func clearPluginSettingDecryptionCache() {
|
|
|
|
pluginSettingDecryptionCache.Lock()
|
|
|
|
defer pluginSettingDecryptionCache.Unlock()
|
|
|
|
|
|
|
|
pluginSettingDecryptionCache.cache = make(map[int64]cachedDecryptedJSON)
|
|
|
|
}
|
|
|
|
|
2020-02-19 12:17:05 -06:00
|
|
|
func TestPluginSettingDecryptionCache(t *testing.T) {
|
|
|
|
t.Run("When plugin settings hasn't been updated, encrypted JSON should be fetched from cache", func(t *testing.T) {
|
2020-11-17 04:51:31 -06:00
|
|
|
clearPluginSettingDecryptionCache()
|
2020-02-19 12:17:05 -06:00
|
|
|
|
|
|
|
ps := PluginSetting{
|
|
|
|
Id: 1,
|
|
|
|
JsonData: map[string]interface{}{},
|
|
|
|
SecureJsonData: securejsondata.GetEncryptedJsonData(map[string]string{
|
|
|
|
"password": "password",
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate cache
|
2020-11-17 04:51:31 -06:00
|
|
|
password, ok := ps.DecryptedValues()["password"]
|
2020-02-19 12:17:05 -06:00
|
|
|
require.Equal(t, "password", password)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
ps.SecureJsonData = securejsondata.GetEncryptedJsonData(map[string]string{
|
|
|
|
"password": "",
|
|
|
|
})
|
|
|
|
|
|
|
|
require.Equal(t, "password", password)
|
|
|
|
require.True(t, ok)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("When plugin settings is updated, encrypted JSON should not be fetched from cache", func(t *testing.T) {
|
2020-11-17 04:51:31 -06:00
|
|
|
clearPluginSettingDecryptionCache()
|
2020-02-19 12:17:05 -06:00
|
|
|
|
|
|
|
ps := PluginSetting{
|
|
|
|
Id: 1,
|
|
|
|
JsonData: map[string]interface{}{},
|
|
|
|
SecureJsonData: securejsondata.GetEncryptedJsonData(map[string]string{
|
|
|
|
"password": "password",
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate cache
|
2020-11-17 04:51:31 -06:00
|
|
|
password, ok := ps.DecryptedValues()["password"]
|
2020-02-19 12:17:05 -06:00
|
|
|
require.Equal(t, "password", password)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
ps.SecureJsonData = securejsondata.GetEncryptedJsonData(map[string]string{
|
|
|
|
"password": "",
|
|
|
|
})
|
|
|
|
ps.Updated = time.Now()
|
|
|
|
|
2020-11-17 04:51:31 -06:00
|
|
|
password, ok = ps.DecryptedValues()["password"]
|
2020-02-19 12:17:05 -06:00
|
|
|
require.Empty(t, password)
|
|
|
|
require.True(t, ok)
|
|
|
|
})
|
|
|
|
}
|