2022-02-03 10:06:31 -06:00
|
|
|
package promclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-04-01 06:26:49 -05:00
|
|
|
"net/url"
|
|
|
|
"path"
|
2022-02-03 10:06:31 -06:00
|
|
|
|
2022-04-04 04:23:13 -05:00
|
|
|
"github.com/grafana/grafana-azure-sdk-go/azcredentials"
|
|
|
|
"github.com/grafana/grafana-azure-sdk-go/azhttpclient"
|
2022-02-03 10:06:31 -06:00
|
|
|
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
2022-04-04 04:23:13 -05:00
|
|
|
|
2022-04-01 06:26:49 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
2022-02-03 10:06:31 -06:00
|
|
|
"github.com/grafana/grafana/pkg/util/maputil"
|
|
|
|
)
|
|
|
|
|
2022-04-01 06:26:49 -05:00
|
|
|
func (p *Provider) configureAzureAuthentication(opts *sdkhttpclient.Options) error {
|
|
|
|
// Azure authentication is experimental (#35857)
|
|
|
|
if !p.features.IsEnabled(featuremgmt.FlagPrometheusAzureAuth) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-03 10:06:31 -06:00
|
|
|
credentials, err := azcredentials.FromDatasourceData(p.jsonData, p.settings.DecryptedSecureJSONData)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("invalid Azure credentials: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if credentials != nil {
|
2022-04-01 06:26:49 -05:00
|
|
|
resourceIdStr, err := maputil.GetStringOptional(p.jsonData, "azureEndpointResourceId")
|
2022-02-03 10:06:31 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-04-01 06:26:49 -05:00
|
|
|
} else if resourceIdStr == "" {
|
|
|
|
err := fmt.Errorf("endpoint resource ID (audience) not provided")
|
|
|
|
return err
|
2022-02-03 10:06:31 -06:00
|
|
|
}
|
|
|
|
|
2022-04-01 06:26:49 -05:00
|
|
|
resourceId, err := url.Parse(resourceIdStr)
|
|
|
|
if err != nil || resourceId.Scheme == "" || resourceId.Host == "" {
|
|
|
|
err := fmt.Errorf("endpoint resource ID (audience) '%s' invalid", resourceIdStr)
|
|
|
|
return err
|
2022-02-03 10:06:31 -06:00
|
|
|
}
|
2022-04-01 06:26:49 -05:00
|
|
|
|
|
|
|
resourceId.Path = path.Join(resourceId.Path, ".default")
|
|
|
|
scopes := []string{resourceId.String()}
|
|
|
|
|
|
|
|
azhttpclient.AddAzureAuthentication(opts, p.cfg.Azure, credentials, scopes)
|
2022-02-03 10:06:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|