Azure: Update Grafana Azure SDK and middleware (#62050)

* Update middleware

* Update Grafana Azure SDK

* Client options to client builder

* Fix tests

* maputil from SDK
This commit is contained in:
Sergey Kostrukov
2023-02-02 08:58:10 -08:00
committed by GitHub
parent 9fa28c11c5
commit 2f218ab928
7 changed files with 40 additions and 28 deletions

2
go.mod
View File

@@ -67,7 +67,7 @@ require (
github.com/grafana/alerting v0.0.0-20230125210216-facc6b27b9e0
github.com/grafana/cuetsy v0.1.5
github.com/grafana/grafana-aws-sdk v0.12.0
github.com/grafana/grafana-azure-sdk-go v1.5.1
github.com/grafana/grafana-azure-sdk-go v1.6.0
github.com/grafana/grafana-plugin-sdk-go v0.147.0
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/hashicorp/go-hclog v1.2.0

4
go.sum
View File

@@ -1260,8 +1260,8 @@ github.com/grafana/go-mssqldb v0.0.0-20210326084033-d0ce3c521036 h1:GplhUk6Xes5J
github.com/grafana/go-mssqldb v0.0.0-20210326084033-d0ce3c521036/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
github.com/grafana/grafana-aws-sdk v0.12.0 h1:eUjFdFZeZE+nyu/RMRz+qFxTBew69ToLBrbRhTbjkfM=
github.com/grafana/grafana-aws-sdk v0.12.0/go.mod h1:rCXLYoMpPqF90U7XqgVJ1HIAopFVF0bB3SXBVEJIm3I=
github.com/grafana/grafana-azure-sdk-go v1.5.1 h1:d+wwqvWLYvPeGltxU50LNjvCykyJjoyd5exMYJW4nLM=
github.com/grafana/grafana-azure-sdk-go v1.5.1/go.mod h1:OJJuBJ3MOoaq2mqD6xlPsArpL2R5j80TrDqPYr35Zak=
github.com/grafana/grafana-azure-sdk-go v1.6.0 h1:lxvH/mVY7gKBtJKhZ4B/6tIZFY7Jth97HxBA38olaxs=
github.com/grafana/grafana-azure-sdk-go v1.6.0/go.mod h1:X4PdEQIYgHfn0KTa2ZTKvufhNz6jbCEKUQPZIlcyOGw=
github.com/grafana/grafana-google-sdk-go v0.0.0-20211104130251-b190293eaf58 h1:2ud7NNM7LrGPO4x0NFR8qLq68CqI4SmB7I2yRN2w9oE=
github.com/grafana/grafana-google-sdk-go v0.0.0-20211104130251-b190293eaf58/go.mod h1:Vo2TKWfDVmNTELBUM+3lkrZvFtBws0qSZdXhQxRdJrE=
github.com/grafana/grafana-plugin-sdk-go v0.94.0/go.mod h1:3VXz4nCv6wH5SfgB3mlW39s+c+LetqSCjFj7xxPC5+M=

View File

@@ -68,9 +68,9 @@ type Service struct {
tracer tracing.Tracer
}
func getDatasourceService(cfg *setting.Cfg, clientProvider *httpclient.Provider, dsInfo types.DatasourceInfo, routeName string, httpClientOptions httpclient.Options) (types.DatasourceService, error) {
func getDatasourceService(settings *backend.DataSourceInstanceSettings, cfg *setting.Cfg, clientProvider *httpclient.Provider, dsInfo types.DatasourceInfo, routeName string) (types.DatasourceService, error) {
route := dsInfo.Routes[routeName]
client, err := newHTTPClient(route, dsInfo, cfg, clientProvider, httpClientOptions)
client, err := newHTTPClient(route, dsInfo, settings, cfg, clientProvider)
if err != nil {
return types.DatasourceService{}, err
}
@@ -92,11 +92,6 @@ func NewInstanceSettings(cfg *setting.Cfg, clientProvider *httpclient.Provider,
return nil, fmt.Errorf("error reading settings: %w", err)
}
httpClientOpts, err := settings.HTTPClientOptions()
if err != nil {
return nil, fmt.Errorf("error getting http options: %w", err)
}
azMonitorSettings := types.AzureMonitorSettings{}
err = json.Unmarshal(settings.JSONData, &azMonitorSettings)
if err != nil {
@@ -130,7 +125,7 @@ func NewInstanceSettings(cfg *setting.Cfg, clientProvider *httpclient.Provider,
}
for routeName := range executors {
service, err := getDatasourceService(cfg, clientProvider, model, routeName, httpClientOpts)
service, err := getDatasourceService(&settings, cfg, clientProvider, model, routeName)
if err != nil {
return nil, err
}

View File

@@ -6,16 +6,21 @@ import (
"github.com/grafana/grafana-azure-sdk-go/azcredentials"
"github.com/grafana/grafana-azure-sdk-go/azhttpclient"
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana/pkg/infra/httpclient"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/types"
)
func newHTTPClient(route types.AzRoute, model types.DatasourceInfo, cfg *setting.Cfg, clientProvider httpclient.Provider, httpClientOptions sdkhttpclient.Options) (*http.Client, error) {
func newHTTPClient(route types.AzRoute, model types.DatasourceInfo, settings *backend.DataSourceInstanceSettings, cfg *setting.Cfg, clientProvider httpclient.Provider) (*http.Client, error) {
clientOpts, err := settings.HTTPClientOptions()
if err != nil {
return nil, fmt.Errorf("error getting HTTP options: %w", err)
}
for header, value := range route.Headers {
httpClientOptions.Headers[header] = value
clientOpts.Headers[header] = value
}
// Use Azure credentials if the route has OAuth scopes configured
@@ -23,8 +28,11 @@ func newHTTPClient(route types.AzRoute, model types.DatasourceInfo, cfg *setting
if cred, ok := model.Credentials.(*azcredentials.AzureClientSecretCredentials); ok && cred.ClientSecret == "" {
return nil, fmt.Errorf("unable to initialize HTTP Client: clientSecret not found")
}
azhttpclient.AddAzureAuthentication(&httpClientOptions, cfg.Azure, model.Credentials, route.Scopes)
authOpts := azhttpclient.NewAuthOptions(cfg.Azure)
authOpts.Scopes(route.Scopes)
azhttpclient.AddAzureAuthentication(&clientOpts, authOpts, model.Credentials)
}
return clientProvider.New(httpClientOptions)
return clientProvider.New(clientOpts)
}

View File

@@ -2,10 +2,12 @@ package azuremonitor
import (
"crypto/tls"
"encoding/json"
"net/http"
"testing"
"github.com/grafana/grafana-azure-sdk-go/azcredentials"
"github.com/grafana/grafana-plugin-sdk-go/backend"
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
"github.com/grafana/grafana/pkg/infra/httpclient"
@@ -20,6 +22,16 @@ func TestHttpClient_AzureCredentials(t *testing.T) {
Credentials: &azcredentials.AzureManagedIdentityCredentials{},
}
jsonData, _ := json.Marshal(map[string]interface{}{
"httpHeaderName1": "GrafanaHeader",
})
settings := &backend.DataSourceInstanceSettings{
JSONData: jsonData,
DecryptedSecureJSONData: map[string]string{
"httpHeaderValue1": "GrafanaValue",
},
}
cfg := &setting.Cfg{}
provider := &fakeHttpClientProvider{}
@@ -28,7 +40,7 @@ func TestHttpClient_AzureCredentials(t *testing.T) {
Scopes: []string{"https://management.azure.com/.default"},
}
_, err := newHTTPClient(route, model, cfg, provider, sdkhttpclient.Options{})
_, err := newHTTPClient(route, model, settings, cfg, provider)
require.NoError(t, err)
require.NotNil(t, provider.opts)
@@ -41,7 +53,7 @@ func TestHttpClient_AzureCredentials(t *testing.T) {
Scopes: []string{},
}
_, err := newHTTPClient(route, model, cfg, provider, sdkhttpclient.Options{})
_, err := newHTTPClient(route, model, settings, cfg, provider)
require.NoError(t, err)
assert.NotNil(t, provider.opts)
@@ -57,17 +69,12 @@ func TestHttpClient_AzureCredentials(t *testing.T) {
"AzureHeader": "AzureValue",
},
}
opts := sdkhttpclient.Options{
Headers: map[string]string{
"GrafanaHeader": "GrafanaValue",
},
}
res := map[string]string{
"GrafanaHeader": "GrafanaValue",
"AzureHeader": "AzureValue",
}
_, err := newHTTPClient(route, model, cfg, provider, opts)
_, err := newHTTPClient(route, model, settings, cfg, provider)
require.NoError(t, err)
assert.NotNil(t, provider.opts)

View File

@@ -8,11 +8,11 @@ import (
"github.com/grafana/grafana-azure-sdk-go/azcredentials"
"github.com/grafana/grafana-azure-sdk-go/azhttpclient"
"github.com/grafana/grafana-azure-sdk-go/azsettings"
"github.com/grafana/grafana-azure-sdk-go/util/maputil"
"github.com/grafana/grafana-plugin-sdk-go/backend"
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
"github.com/grafana/grafana/pkg/tsdb/prometheus/utils"
"github.com/grafana/grafana/pkg/util/maputil"
)
var (
@@ -23,7 +23,7 @@ var (
}
)
func ConfigureAzureAuthentication(settings backend.DataSourceInstanceSettings, azureSettings *azsettings.AzureSettings, opts *sdkhttpclient.Options) error {
func ConfigureAzureAuthentication(settings backend.DataSourceInstanceSettings, azureSettings *azsettings.AzureSettings, clientOpts *sdkhttpclient.Options) error {
jsonData, err := utils.GetJsonData(settings)
if err != nil {
return fmt.Errorf("failed to get jsonData: %w", err)
@@ -47,7 +47,9 @@ func ConfigureAzureAuthentication(settings backend.DataSourceInstanceSettings, a
}
}
azhttpclient.AddAzureAuthentication(opts, azureSettings, credentials, scopes)
authOpts := azhttpclient.NewAuthOptions(azureSettings)
authOpts.Scopes(scopes)
azhttpclient.AddAzureAuthentication(clientOpts, authOpts, credentials)
}
return nil

View File

@@ -20,7 +20,7 @@ import (
func CreateTransportOptions(settings backend.DataSourceInstanceSettings, cfg *setting.Cfg, logger log.Logger) (*sdkhttpclient.Options, error) {
opts, err := settings.HTTPClientOptions()
if err != nil {
return nil, err
return nil, fmt.Errorf("error getting HTTP options: %w", err)
}
jsonData, err := utils.GetJsonData(settings)