-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #846 from r4f4/azure-adal-deprecation
CORS-2467: dns: azure: use azidentity with an adapter
- Loading branch information
Showing
184 changed files
with
19,392 additions
and
396 deletions.
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
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 |
---|---|---|
@@ -1,21 +1,67 @@ | ||
package client | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azcore" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azidentity" | ||
"github.com/Azure/go-autorest/autorest" | ||
"github.com/Azure/go-autorest/autorest/adal" | ||
"github.com/Azure/go-autorest/autorest/azure" | ||
"github.com/jongio/azidext/go/azidext" | ||
) | ||
|
||
func getAuthorizerForResource(config Config) (autorest.Authorizer, error) { | ||
oauthConfig, err := adal.NewOAuthConfig( | ||
config.Environment.ActiveDirectoryEndpoint, config.TenantID) | ||
var cloudConfig cloud.Configuration | ||
switch config.Environment { | ||
case azure.ChinaCloud: | ||
cloudConfig = cloud.AzureChina | ||
// GermanCloud was closed on Oct 29, 2021 | ||
// https://learn.microsoft.com/en-us/azure/active-directory/develop/authentication-national-cloud | ||
// case azure.GermanCloud: | ||
// return nil, nil | ||
case azure.USGovernmentCloud: | ||
cloudConfig = cloud.AzureGovernment | ||
case azure.PublicCloud: | ||
cloudConfig = cloud.AzurePublic | ||
default: // AzureStackCloud | ||
cloudConfig = cloud.Configuration{ | ||
ActiveDirectoryAuthorityHost: config.Environment.ActiveDirectoryEndpoint, | ||
Services: map[cloud.ServiceName]cloud.ServiceConfiguration{ | ||
cloud.ResourceManager: { | ||
Audience: config.Environment.TokenAudience, | ||
Endpoint: config.Environment.ResourceManagerEndpoint, | ||
}, | ||
}, | ||
} | ||
} | ||
options := azidentity.ClientSecretCredentialOptions{ | ||
ClientOptions: azcore.ClientOptions{ | ||
Cloud: cloudConfig, | ||
}, | ||
} | ||
cred, err := azidentity.NewClientSecretCredential(config.TenantID, config.ClientID, config.ClientSecret, &options) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
token, err := adal.NewServicePrincipalToken( | ||
*oauthConfig, config.ClientID, config.ClientSecret, config.Environment.TokenAudience) | ||
if err != nil { | ||
return nil, err | ||
scope := endpointToScope(config.Environment.TokenAudience) | ||
|
||
// Use an adapter so azidentity in the Azure SDK can be used as | ||
// Authorizer when calling the Azure Management Packages, which we | ||
// currently use. Once the Azure SDK clients (found in /sdk) move to | ||
// stable, we can update our clients and they will be able to use the | ||
// creds directly without the authorizer. The schedule is here: | ||
// https://azure.github.io/azure-sdk/releases/latest/index.html#go | ||
authorizer := azidext.NewTokenCredentialAdapter(cred, []string{scope}) | ||
|
||
return authorizer, nil | ||
} | ||
|
||
func endpointToScope(endpoint string) string { | ||
scope := endpoint | ||
if !strings.HasSuffix(scope, "/.default") { | ||
scope += "/.default" | ||
} | ||
return autorest.NewBearerAuthorizer(token), err | ||
return scope | ||
} |
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,38 @@ | ||
package client | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestEndpointToScope(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
endpoint string | ||
expected string | ||
}{ | ||
{ | ||
name: "Default scope is suffixed", | ||
endpoint: "https://graph.microsoft.com", | ||
expected: "https://graph.microsoft.com/.default", | ||
}, | ||
{ | ||
name: "No-OP for a valid scope", | ||
endpoint: "https://graph.microsoft.com/.default", | ||
expected: "https://graph.microsoft.com/.default", | ||
}, | ||
{ | ||
name: "Existing slash is preserved", | ||
endpoint: "https://graph.microsoft.com/", | ||
expected: "https://graph.microsoft.com//.default", | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
scope := endpointToScope(tc.endpoint) | ||
assert.Equal(t, scope, tc.expected) | ||
}) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
vendor/github.com/Azure/azure-sdk-for-go/profiles/2018-03-01/dns/mgmt/dns/models.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.