-
Notifications
You must be signed in to change notification settings - Fork 123
Add support for Azure authentication #785
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jkroepke
wants to merge
22
commits into
grafana:main
Choose a base branch
from
jkroepke:azure-sdk
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
bfad0f5
Add support for native Azure authentication
jkroepke 4e81794
Merge branch 'main' into azure-sdk
jkroepke 662fa7c
Merge branch 'main' into azure-sdk
jkroepke 9290c4e
Merge branch 'main' into azure-sdk
jkroepke a3f53d0
Merge branch 'refs/heads/main' into azure-sdk
jkroepke 5c11907
go mod tidy
jkroepke 38bf447
Rewrite credential handling
jkroepke cadebe3
Merge branch 'main' into azure-sdk
jkroepke 37a3199
Merge branch 'main' into azure-sdk
jkroepke 7c5873f
fix: error strings must not capitalised
jkroepke 239d9d6
Merge branch 'main' into azure-sdk
jkroepke 6cc9e01
build Grafana Azure SDK
jkroepke c3fdb13
Merge branch 'refs/heads/main' into azure-sdk
jkroepke c717b14
fix mr conflicts
jkroepke 8e253dc
Merge branch 'main' into azure-sdk
jkroepke 4f30d6d
fix cspell
jkroepke 072b36a
Merge branch 'main' into azure-sdk
jkroepke f700aee
fix lint + go mod tidy.
jkroepke 533956e
fix lint + go mod tidy.
jkroepke 9067c89
Merge branch 'main' into azure-sdk
jkroepke bbd3983
build Grafana Azure SDK
jkroepke 1861b75
Merge branch 'main' into azure-sdk
jkroepke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'grafana-infinity-datasource': minor | ||
--- | ||
|
||
Add native Azure authentication |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package infinity | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/grafana/grafana-azure-sdk-go/v2/azcredentials" | ||
"github.com/grafana/grafana-azure-sdk-go/v2/azhttpclient" | ||
"github.com/grafana/grafana-azure-sdk-go/v2/azsettings" | ||
"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" | ||
"github.com/grafana/grafana-plugin-sdk-go/backend/tracing" | ||
"github.com/grafana/grafana-plugin-sdk-go/data/utils/maputil" | ||
|
||
"github.com/grafana/grafana-infinity-datasource/pkg/models" | ||
) | ||
|
||
func ApplyAzureAuth(ctx context.Context, httpClient *http.Client, settings models.InfinitySettings) (*http.Client, error) { | ||
_, span := tracing.DefaultTracer().Start(ctx, "ApplyAzureAuth") | ||
defer span.End() | ||
|
||
if !IsAzureAuthConfigured(settings) { | ||
return httpClient, nil | ||
} | ||
|
||
credentialsObj, err := maputil.GetMapOptional(settings.RawData, "azureCredentials") | ||
if err != nil { | ||
return nil, err | ||
} else if credentialsObj == nil { | ||
return httpClient, nil | ||
} | ||
|
||
azSettings, err := azsettings.ReadFromEnv() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// set authType if not set | ||
// this can happen if the user manually provisions the datasource without setting the authType | ||
if credentialsObj["authType"] == nil || credentialsObj["authType"] == "" { | ||
credentialsObj["authType"] = azcredentials.AzureAuthClientSecret | ||
} | ||
|
||
// set azureCloud if not set | ||
// this can happen if the user manually provisions the datasource without setting the azureCloud | ||
if credentialsObj["azureCloud"] == nil || credentialsObj["azureCloud"] == "" { | ||
credentialsObj["azureCloud"] = azSettings.GetDefaultCloud() | ||
} | ||
|
||
azCredentials, err := azcredentials.FromDatasourceData(settings.RawData, settings.RawSecureData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
authOpts := azhttpclient.NewAuthOptions(azSettings) | ||
|
||
// set scopes | ||
if scopes, ok := credentialsObj["scopes"].([]any); ok { | ||
stringScopes, err := toStringSlice(scopes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
authOpts.Scopes(stringScopes) | ||
} | ||
|
||
httpClient.Transport = azhttpclient.AzureMiddleware(authOpts, azCredentials). | ||
CreateMiddleware(httpclient.Options{}, httpClient.Transport) | ||
|
||
return httpClient, nil | ||
} | ||
|
||
func IsAzureAuthConfigured(settings models.InfinitySettings) bool { | ||
return settings.AuthenticationMethod == models.AuthenticationMethodAzure | ||
} | ||
|
||
func toStringSlice(arr []any) ([]string, error) { | ||
result := make([]string, len(arr)) | ||
for i, v := range arr { | ||
if s, ok := v.(string); ok { | ||
result[i] = s | ||
} else { | ||
return nil, fmt.Errorf("expected string, got %T", v) | ||
} | ||
} | ||
return result, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.