Skip to content

Commit 0aa9d95

Browse files
authored
Use hash of proxy settings as part of datasource instance cache key (#1133)
1 parent b5221f7 commit 0aa9d95

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

backend/config.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@ func (c *GrafanaCfg) Equal(c2 *GrafanaCfg) bool {
9696
return true
9797
}
9898

99+
// ProxyHash returns the last four bytes of the PDC client key contents,
100+
// if present, for use in datasource instance caching
101+
func (c *GrafanaCfg) ProxyHash() string {
102+
if c == nil {
103+
return ""
104+
}
105+
key := c.config[proxy.PluginSecureSocksProxyClientKeyContents]
106+
start := max(len(key)-4, 0)
107+
return key[start:]
108+
}
109+
99110
type FeatureToggles struct {
100111
// enabled is a set-like map of feature flags that are enabled.
101112
enabled map[string]struct{}

backend/datasource/instance_provider.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,11 @@ func (ip *instanceProvider) GetKey(ctx context.Context, pluginContext backend.Pl
5959
return nil, errors.New("data source instance settings cannot be nil")
6060
}
6161

62-
defaultKey := pluginContext.DataSourceInstanceSettings.ID
63-
if tID := tenant.IDFromContext(ctx); tID != "" {
64-
return fmt.Sprintf("%s#%v", tID, defaultKey), nil
65-
}
62+
dsID := pluginContext.DataSourceInstanceSettings.ID
63+
proxyHash := pluginContext.GrafanaConfig.ProxyHash()
64+
tenantID := tenant.IDFromContext(ctx)
6665

67-
return defaultKey, nil
66+
return fmt.Sprintf("%d#%s#%s", dsID, tenantID, proxyHash), nil
6867
}
6968

7069
func (ip *instanceProvider) NeedsUpdate(_ context.Context, pluginContext backend.PluginContext, cachedInstance instancemgmt.CachedInstance) bool {

backend/datasource/instance_provider_test.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/grafana/grafana-plugin-sdk-go/backend"
99
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt"
10+
"github.com/grafana/grafana-plugin-sdk-go/backend/proxy"
1011
"github.com/stretchr/testify/require"
1112
)
1213

@@ -30,7 +31,43 @@ func TestInstanceProvider(t *testing.T) {
3031
},
3132
})
3233
require.NoError(t, err)
33-
require.Equal(t, int64(4), key)
34+
require.Equal(t, "4##", key)
35+
})
36+
37+
t.Run("When PDC is configured, datasource cache key should include its (so-called) hash", func(t *testing.T) {
38+
cfg := backend.NewGrafanaCfg(map[string]string{
39+
proxy.PluginSecureSocksProxyClientKeyContents: "This should work",
40+
})
41+
key, err := ip.GetKey(context.Background(), backend.PluginContext{
42+
DataSourceInstanceSettings: &backend.DataSourceInstanceSettings{ID: 5},
43+
GrafanaConfig: cfg,
44+
})
45+
require.NoError(t, err)
46+
require.Equal(t, "5##work", key)
47+
})
48+
49+
t.Run("When PDC is configured but the key is empty, no problem", func(t *testing.T) {
50+
cfg := backend.NewGrafanaCfg(map[string]string{
51+
proxy.PluginSecureSocksProxyClientKeyContents: "",
52+
})
53+
key, err := ip.GetKey(context.Background(), backend.PluginContext{
54+
DataSourceInstanceSettings: &backend.DataSourceInstanceSettings{ID: 6},
55+
GrafanaConfig: cfg,
56+
})
57+
require.NoError(t, err)
58+
require.Equal(t, "6##", key)
59+
})
60+
61+
t.Run("When PDC is configured, a too-short key doesn't cause an error", func(t *testing.T) {
62+
cfg := backend.NewGrafanaCfg(map[string]string{
63+
proxy.PluginSecureSocksProxyClientKeyContents: "doh",
64+
})
65+
key, err := ip.GetKey(context.Background(), backend.PluginContext{
66+
DataSourceInstanceSettings: &backend.DataSourceInstanceSettings{ID: 7},
67+
GrafanaConfig: cfg,
68+
})
69+
require.NoError(t, err)
70+
require.Equal(t, "7##doh", key)
3471
})
3572

3673
t.Run("When both the configuration and updated field of current data source instance settings are equal to the cache, should return false", func(t *testing.T) {

0 commit comments

Comments
 (0)