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:
@@ -21,6 +21,7 @@ import (
|
||||
|
||||
"github.com/grafana/grafana-aws-sdk/pkg/awsds"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend/gtime"
|
||||
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/azsettings"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
@@ -302,7 +303,7 @@ type Cfg struct {
|
||||
AWSListMetricsPageLimit int
|
||||
|
||||
// Azure Cloud settings
|
||||
Azure AzureSettings
|
||||
Azure *azsettings.AzureSettings
|
||||
|
||||
// Auth proxy settings
|
||||
AuthProxyEnabled bool
|
||||
@@ -824,6 +825,7 @@ func NewCfg() *Cfg {
|
||||
return &Cfg{
|
||||
Logger: log.New("settings"),
|
||||
Raw: ini.Empty(),
|
||||
Azure: &azsettings.AzureSettings{},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,73 +1,19 @@
|
||||
package setting
|
||||
|
||||
import "strings"
|
||||
|
||||
const (
|
||||
AzurePublic = "AzureCloud"
|
||||
AzureChina = "AzureChinaCloud"
|
||||
AzureUSGovernment = "AzureUSGovernment"
|
||||
AzureGermany = "AzureGermanCloud"
|
||||
)
|
||||
|
||||
type AzureSettings struct {
|
||||
Cloud string
|
||||
ManagedIdentityEnabled bool
|
||||
ManagedIdentityClientId string
|
||||
}
|
||||
import "github.com/grafana/grafana/pkg/tsdb/azuremonitor/azsettings"
|
||||
|
||||
func (cfg *Cfg) readAzureSettings() {
|
||||
azureSettings := &azsettings.AzureSettings{}
|
||||
|
||||
azureSection := cfg.Raw.Section("azure")
|
||||
|
||||
// Cloud
|
||||
cloudName := azureSection.Key("cloud").MustString(AzurePublic)
|
||||
cfg.Azure.Cloud = normalizeAzureCloud(cloudName)
|
||||
cloudName := azureSection.Key("cloud").MustString(azsettings.AzurePublic)
|
||||
azureSettings.Cloud = azsettings.NormalizeAzureCloud(cloudName)
|
||||
|
||||
// Managed Identity
|
||||
cfg.Azure.ManagedIdentityEnabled = azureSection.Key("managed_identity_enabled").MustBool(false)
|
||||
cfg.Azure.ManagedIdentityClientId = azureSection.Key("managed_identity_client_id").String()
|
||||
}
|
||||
azureSettings.ManagedIdentityEnabled = azureSection.Key("managed_identity_enabled").MustBool(false)
|
||||
azureSettings.ManagedIdentityClientId = azureSection.Key("managed_identity_client_id").String()
|
||||
|
||||
func normalizeAzureCloud(cloudName string) string {
|
||||
switch strings.ToLower(cloudName) {
|
||||
// Public
|
||||
case "azurecloud":
|
||||
fallthrough
|
||||
case "azurepublic":
|
||||
fallthrough
|
||||
case "azurepubliccloud":
|
||||
fallthrough
|
||||
case "public":
|
||||
return AzurePublic
|
||||
|
||||
// China
|
||||
case "azurechina":
|
||||
fallthrough
|
||||
case "azurechinacloud":
|
||||
fallthrough
|
||||
case "china":
|
||||
return AzureChina
|
||||
|
||||
// US Government
|
||||
case "azureusgovernment":
|
||||
fallthrough
|
||||
case "azureusgovernmentcloud":
|
||||
fallthrough
|
||||
case "usgov":
|
||||
fallthrough
|
||||
case "usgovernment":
|
||||
return AzureUSGovernment
|
||||
|
||||
// Germany
|
||||
case "azuregermancloud":
|
||||
fallthrough
|
||||
case "azuregermany":
|
||||
fallthrough
|
||||
case "german":
|
||||
fallthrough
|
||||
case "germany":
|
||||
return AzureGermany
|
||||
}
|
||||
|
||||
// Pass the name unchanged if it's not known
|
||||
return cloudName
|
||||
cfg.Azure = azureSettings
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package setting
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/azsettings"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -17,27 +18,27 @@ func TestAzureSettings(t *testing.T) {
|
||||
{
|
||||
name: "should be Public if not set",
|
||||
configuredValue: "",
|
||||
resolvedValue: AzurePublic,
|
||||
resolvedValue: azsettings.AzurePublic,
|
||||
},
|
||||
{
|
||||
name: "should be Public if set to Public",
|
||||
configuredValue: AzurePublic,
|
||||
resolvedValue: AzurePublic,
|
||||
configuredValue: azsettings.AzurePublic,
|
||||
resolvedValue: azsettings.AzurePublic,
|
||||
},
|
||||
{
|
||||
name: "should be Public if set to Public using alternative name",
|
||||
configuredValue: "AzurePublicCloud",
|
||||
resolvedValue: AzurePublic,
|
||||
resolvedValue: azsettings.AzurePublic,
|
||||
},
|
||||
{
|
||||
name: "should be China if set to China",
|
||||
configuredValue: AzureChina,
|
||||
resolvedValue: AzureChina,
|
||||
configuredValue: azsettings.AzureChina,
|
||||
resolvedValue: azsettings.AzureChina,
|
||||
},
|
||||
{
|
||||
name: "should be US Government if set to US Government using alternative name",
|
||||
configuredValue: "usgov",
|
||||
resolvedValue: AzureUSGovernment,
|
||||
resolvedValue: azsettings.AzureUSGovernment,
|
||||
},
|
||||
{
|
||||
name: "should be same as set if not known",
|
||||
|
||||
Reference in New Issue
Block a user