-
Notifications
You must be signed in to change notification settings - Fork 106
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
Add support for Azure authentication #785
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 9 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 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 Microsoft authentication |
This file contains 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 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 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 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 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" | ||
"strings" | ||
|
||
"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-infinity-datasource/pkg/models" | ||
"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" | ||
"github.com/grafana/grafana-plugin-sdk-go/backend/tracing" | ||
) | ||
|
||
func ApplyAzureAuth(ctx context.Context, httpClient *http.Client, settings models.InfinitySettings) (*http.Client, error) { | ||
ctx, span := tracing.DefaultTracer().Start(ctx, "ApplyAzureAuth") | ||
defer span.End() | ||
|
||
if IsAzureAuthConfigured(settings) { | ||
azSettings, err := azsettings.ReadFromEnv() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var credentials azcredentials.AzureCredentials | ||
|
||
switch settings.MicrosoftSettings.AuthType { | ||
case models.MicrosoftAuthTypeClientSecret: | ||
|
||
if strings.TrimSpace(settings.MicrosoftSettings.TenantID) == "" { | ||
return nil, fmt.Errorf("Tenant ID %w ", models.MicrosoftRequiredForClientSecretErrHelp) | ||
jkroepke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if strings.TrimSpace(settings.MicrosoftSettings.ClientID) == "" { | ||
return nil, fmt.Errorf("Client ID %w ", models.MicrosoftRequiredForClientSecretErrHelp) | ||
jkroepke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if strings.TrimSpace(settings.MicrosoftSettings.ClientSecret) == "" { | ||
return nil, fmt.Errorf("Client secret %w ", models.MicrosoftRequiredForClientSecretErrHelp) | ||
jkroepke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
credentials = &azcredentials.AzureClientSecretCredentials{ | ||
AzureCloud: string(settings.MicrosoftSettings.Cloud), | ||
TenantId: settings.MicrosoftSettings.TenantID, | ||
ClientId: settings.MicrosoftSettings.ClientID, | ||
ClientSecret: settings.MicrosoftSettings.ClientSecret, | ||
} | ||
case models.MicrosoftAuthTypeManagedIdentity: | ||
if !azSettings.ManagedIdentityEnabled { | ||
return nil, fmt.Errorf("Managed Identity %w ", models.MicrosoftDisabledAuthErrHelp) | ||
jkroepke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
credentials = &azcredentials.AzureManagedIdentityCredentials{ | ||
// ClientId is optional for managed identity, because it can be inferred from the environment | ||
// https://github.com/grafana/grafana-azure-sdk-go/blob/21e2891b4190eb7c255c8cd275836def8200faf8/aztokenprovider/retriever_msi.go#L20-L30 | ||
ClientId: settings.MicrosoftSettings.ClientID, | ||
} | ||
case models.MicrosoftAuthTypeWorkloadIdentity: | ||
if !azSettings.WorkloadIdentityEnabled { | ||
return nil, fmt.Errorf("Workload Identity %w ", models.MicrosoftDisabledAuthErrHelp) | ||
} | ||
|
||
credentials = &azcredentials.AzureWorkloadIdentityCredentials{} | ||
case models.MicrosoftAuthTypeCurrentUserIdentity: | ||
if !azSettings.UserIdentityEnabled { | ||
return nil, fmt.Errorf("User Identity %w ", models.MicrosoftDisabledAuthErrHelp) | ||
jkroepke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
credentials = &azcredentials.AadCurrentUserCredentials{} | ||
default: | ||
panic(fmt.Errorf("invalid auth type '%s'", settings.MicrosoftSettings.AuthType)) | ||
} | ||
|
||
authOpts := azhttpclient.NewAuthOptions(azSettings) | ||
authOpts.Scopes(settings.MicrosoftSettings.Scopes) | ||
|
||
httpClient.Transport = azhttpclient.AzureMiddleware(authOpts, credentials). | ||
CreateMiddleware(httpclient.Options{}, httpClient.Transport) | ||
} | ||
return httpClient, nil | ||
} | ||
|
||
func IsAzureAuthConfigured(settings models.InfinitySettings) bool { | ||
return settings.AuthenticationMethod == models.AuthenticationMethodMicrosoft | ||
} |
This file contains 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
jkroepke marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains 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 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was the reason for your error you should me at the review.