|
|
|
|
@ -6,6 +6,7 @@ import ( |
|
|
|
|
"crypto/sha256" |
|
|
|
|
"encoding/hex" |
|
|
|
|
"net/http" |
|
|
|
|
"net/url" |
|
|
|
|
"testing" |
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require" |
|
|
|
|
@ -22,7 +23,8 @@ import ( |
|
|
|
|
func Test_HostedGrafanaACHeaderMiddleware(t *testing.T) { |
|
|
|
|
t.Run("Should set Grafana request ID headers if the data source URL is in the allow list", func(t *testing.T) { |
|
|
|
|
cfg := setting.NewCfg() |
|
|
|
|
cfg.IPRangeACAllowedURLs = []string{"https://logs.grafana.net"} |
|
|
|
|
allowedURL := &url.URL{Scheme: "https", Host: "logs.grafana.net"} |
|
|
|
|
cfg.IPRangeACAllowedURLs = []*url.URL{allowedURL} |
|
|
|
|
cfg.IPRangeACSecretKey = "secret" |
|
|
|
|
cdt := clienttest.NewClientDecoratorTest(t, clienttest.WithMiddlewares(NewHostedGrafanaACHeaderMiddleware(cfg))) |
|
|
|
|
|
|
|
|
|
@ -63,7 +65,8 @@ func Test_HostedGrafanaACHeaderMiddleware(t *testing.T) { |
|
|
|
|
|
|
|
|
|
t.Run("Should not set Grafana request ID headers if the data source URL is not in the allow list", func(t *testing.T) { |
|
|
|
|
cfg := setting.NewCfg() |
|
|
|
|
cfg.IPRangeACAllowedURLs = []string{"https://logs.grafana.net"} |
|
|
|
|
allowedURL := &url.URL{Scheme: "https", Host: "logs.grafana.net"} |
|
|
|
|
cfg.IPRangeACAllowedURLs = []*url.URL{allowedURL} |
|
|
|
|
cfg.IPRangeACSecretKey = "secret" |
|
|
|
|
cdt := clienttest.NewClientDecoratorTest(t, clienttest.WithMiddlewares(NewHostedGrafanaACHeaderMiddleware(cfg))) |
|
|
|
|
|
|
|
|
|
@ -85,9 +88,10 @@ func Test_HostedGrafanaACHeaderMiddleware(t *testing.T) { |
|
|
|
|
require.Len(t, cdt.CallResourceReq.Headers[GrafanaSignedRequestID], 0) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
t.Run("Should set Grafana request ID headers if a sanitized data source URL is in the allow list", func(t *testing.T) { |
|
|
|
|
t.Run("Should set Grafana request ID headers if URL scheme and host match a URL from the allow list", func(t *testing.T) { |
|
|
|
|
cfg := setting.NewCfg() |
|
|
|
|
cfg.IPRangeACAllowedURLs = []string{"https://logs.GRAFANA.net/"} |
|
|
|
|
allowedURL := &url.URL{Scheme: "https", Host: "logs.grafana.net"} |
|
|
|
|
cfg.IPRangeACAllowedURLs = []*url.URL{allowedURL} |
|
|
|
|
cfg.IPRangeACSecretKey = "secret" |
|
|
|
|
cdt := clienttest.NewClientDecoratorTest(t, clienttest.WithMiddlewares(NewHostedGrafanaACHeaderMiddleware(cfg))) |
|
|
|
|
|
|
|
|
|
@ -99,19 +103,20 @@ func Test_HostedGrafanaACHeaderMiddleware(t *testing.T) { |
|
|
|
|
err := cdt.Decorator.CallResource(ctx, &backend.CallResourceRequest{ |
|
|
|
|
PluginContext: backend.PluginContext{ |
|
|
|
|
DataSourceInstanceSettings: &backend.DataSourceInstanceSettings{ |
|
|
|
|
URL: "https://logs.grafana.net/abc/../", |
|
|
|
|
URL: "https://logs.grafana.net/abc/../some/path", |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, nopCallResourceSender) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
require.Len(t, cdt.CallResourceReq.Headers[GrafanaRequestID], 0) |
|
|
|
|
require.Len(t, cdt.CallResourceReq.Headers[GrafanaSignedRequestID], 0) |
|
|
|
|
require.Len(t, cdt.CallResourceReq.Headers[GrafanaRequestID], 1) |
|
|
|
|
require.Len(t, cdt.CallResourceReq.Headers[GrafanaSignedRequestID], 1) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
t.Run("Should set Grafana internal request header if the request is internal (doesn't have X-Real-IP header set)", func(t *testing.T) { |
|
|
|
|
cfg := setting.NewCfg() |
|
|
|
|
cfg.IPRangeACAllowedURLs = []string{"https://logs.grafana.net"} |
|
|
|
|
allowedURL := &url.URL{Scheme: "https", Host: "logs.grafana.net"} |
|
|
|
|
cfg.IPRangeACAllowedURLs = []*url.URL{allowedURL} |
|
|
|
|
cfg.IPRangeACSecretKey = "secret" |
|
|
|
|
cdt := clienttest.NewClientDecoratorTest(t, clienttest.WithMiddlewares(NewHostedGrafanaACHeaderMiddleware(cfg))) |
|
|
|
|
|
|
|
|
|
|