Shared Azure middleware between Azure Monitor and Prometheus datasources (#46002)
* Scopes in Azure middleware * Enable Azure middleware without feature flag * Use common Azure middleware in Azure Monitor * Apply feature flag to JsonData configuration of Azure auth * Enforce feature flag in Prometheus datasource * Prometheus provider tests * Datasource service tests * Fix http client provider tests * Pass sdkhttpclient.Options by reference * Add middleware to httpclient.Options * Remove dependency on Grafana settings * Unit-tests updated * Fix ds_proxy_test * Fix service_test
This commit is contained in:
@@ -1,17 +1,21 @@
|
||||
package azuremonitor
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
||||
"github.com/grafana/grafana/pkg/infra/httpclient"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/azcredentials"
|
||||
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/deprecated"
|
||||
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_httpCliProvider(t *testing.T) {
|
||||
cfg := &setting.Cfg{}
|
||||
func TestHttpClient_Middlewares(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
route types.AzRoute
|
||||
@@ -19,18 +23,6 @@ func Test_httpCliProvider(t *testing.T) {
|
||||
expectedMiddlewares int
|
||||
Err require.ErrorAssertionFunc
|
||||
}{
|
||||
{
|
||||
name: "creates an HTTP client with a middleware due to the scope",
|
||||
route: types.AzRoute{
|
||||
URL: "http://route",
|
||||
Scopes: []string{"http://route/.default"},
|
||||
},
|
||||
model: types.DatasourceInfo{
|
||||
Credentials: &azcredentials.AzureClientSecretCredentials{},
|
||||
},
|
||||
expectedMiddlewares: 1,
|
||||
Err: require.NoError,
|
||||
},
|
||||
{
|
||||
name: "creates an HTTP client with a middleware due to an app key",
|
||||
route: types.AzRoute{
|
||||
@@ -61,7 +53,7 @@ func Test_httpCliProvider(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
m, err := getMiddlewares(tt.route, tt.model, cfg)
|
||||
m, err := getMiddlewares(tt.route, tt.model)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Cannot test that the cli middleware works properly since the azcore sdk
|
||||
@@ -72,3 +64,63 @@ func Test_httpCliProvider(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpClient_AzureCredentials(t *testing.T) {
|
||||
model := types.DatasourceInfo{
|
||||
Credentials: &azcredentials.AzureManagedIdentityCredentials{},
|
||||
}
|
||||
|
||||
cfg := &setting.Cfg{}
|
||||
provider := &fakeHttpClientProvider{}
|
||||
|
||||
t.Run("should have Azure middleware when scopes provided", func(t *testing.T) {
|
||||
route := types.AzRoute{
|
||||
URL: deprecated.AzAppInsights.URL,
|
||||
Scopes: []string{"https://management.azure.com/.default"},
|
||||
}
|
||||
|
||||
_, err := newHTTPClient(route, model, cfg, provider)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.NotNil(t, provider.opts)
|
||||
require.NotNil(t, provider.opts.Middlewares)
|
||||
assert.Len(t, provider.opts.Middlewares, 1)
|
||||
})
|
||||
|
||||
t.Run("should not have Azure middleware when scopes are not provided", func(t *testing.T) {
|
||||
route := types.AzRoute{
|
||||
URL: deprecated.AzAppInsights.URL,
|
||||
Scopes: []string{},
|
||||
}
|
||||
|
||||
_, err := newHTTPClient(route, model, cfg, provider)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.NotNil(t, provider.opts)
|
||||
|
||||
if provider.opts.Middlewares != nil {
|
||||
assert.Len(t, provider.opts.Middlewares, 0)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type fakeHttpClientProvider struct {
|
||||
httpclient.Provider
|
||||
|
||||
opts sdkhttpclient.Options
|
||||
}
|
||||
|
||||
func (p *fakeHttpClientProvider) New(opts ...sdkhttpclient.Options) (*http.Client, error) {
|
||||
p.opts = opts[0]
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (p *fakeHttpClientProvider) GetTransport(opts ...sdkhttpclient.Options) (http.RoundTripper, error) {
|
||||
p.opts = opts[0]
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (p *fakeHttpClientProvider) GetTLSConfig(opts ...sdkhttpclient.Options) (*tls.Config, error) {
|
||||
p.opts = opts[0]
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user