Skip to content

Commit

Permalink
Use hash of proxy settings as part of datasource instance cache key (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
njvrzm authored Nov 20, 2024
1 parent b5221f7 commit 0aa9d95
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
11 changes: 11 additions & 0 deletions backend/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ func (c *GrafanaCfg) Equal(c2 *GrafanaCfg) bool {
return true
}

// ProxyHash returns the last four bytes of the PDC client key contents,
// if present, for use in datasource instance caching
func (c *GrafanaCfg) ProxyHash() string {
if c == nil {
return ""
}
key := c.config[proxy.PluginSecureSocksProxyClientKeyContents]
start := max(len(key)-4, 0)
return key[start:]
}

type FeatureToggles struct {
// enabled is a set-like map of feature flags that are enabled.
enabled map[string]struct{}
Expand Down
9 changes: 4 additions & 5 deletions backend/datasource/instance_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,11 @@ func (ip *instanceProvider) GetKey(ctx context.Context, pluginContext backend.Pl
return nil, errors.New("data source instance settings cannot be nil")
}

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

return defaultKey, nil
return fmt.Sprintf("%d#%s#%s", dsID, tenantID, proxyHash), nil
}

func (ip *instanceProvider) NeedsUpdate(_ context.Context, pluginContext backend.PluginContext, cachedInstance instancemgmt.CachedInstance) bool {
Expand Down
39 changes: 38 additions & 1 deletion backend/datasource/instance_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt"
"github.com/grafana/grafana-plugin-sdk-go/backend/proxy"
"github.com/stretchr/testify/require"
)

Expand All @@ -30,7 +31,43 @@ func TestInstanceProvider(t *testing.T) {
},
})
require.NoError(t, err)
require.Equal(t, int64(4), key)
require.Equal(t, "4##", key)
})

t.Run("When PDC is configured, datasource cache key should include its (so-called) hash", func(t *testing.T) {
cfg := backend.NewGrafanaCfg(map[string]string{
proxy.PluginSecureSocksProxyClientKeyContents: "This should work",
})
key, err := ip.GetKey(context.Background(), backend.PluginContext{
DataSourceInstanceSettings: &backend.DataSourceInstanceSettings{ID: 5},
GrafanaConfig: cfg,
})
require.NoError(t, err)
require.Equal(t, "5##work", key)
})

t.Run("When PDC is configured but the key is empty, no problem", func(t *testing.T) {
cfg := backend.NewGrafanaCfg(map[string]string{
proxy.PluginSecureSocksProxyClientKeyContents: "",
})
key, err := ip.GetKey(context.Background(), backend.PluginContext{
DataSourceInstanceSettings: &backend.DataSourceInstanceSettings{ID: 6},
GrafanaConfig: cfg,
})
require.NoError(t, err)
require.Equal(t, "6##", key)
})

t.Run("When PDC is configured, a too-short key doesn't cause an error", func(t *testing.T) {
cfg := backend.NewGrafanaCfg(map[string]string{
proxy.PluginSecureSocksProxyClientKeyContents: "doh",
})
key, err := ip.GetKey(context.Background(), backend.PluginContext{
DataSourceInstanceSettings: &backend.DataSourceInstanceSettings{ID: 7},
GrafanaConfig: cfg,
})
require.NoError(t, err)
require.Equal(t, "7##doh", key)
})

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) {
Expand Down

0 comments on commit 0aa9d95

Please sign in to comment.