From 5ddb78e2635098e9f06b16430a8be2991d08ab18 Mon Sep 17 00:00:00 2001 From: Tareq Sharafy Date: Fri, 2 Sep 2022 14:57:49 +0300 Subject: [PATCH 1/2] feat: allow getting authorizer from existing file settings Signed-off-by: Tareq Sharafy --- autorest/azure/auth/auth.go | 11 +++++++++++ autorest/azure/auth/auth_test.go | 17 +++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/autorest/azure/auth/auth.go b/autorest/azure/auth/auth.go index 2f1a99818..4d9f2e7f0 100644 --- a/autorest/azure/auth/auth.go +++ b/autorest/azure/auth/auth.go @@ -250,6 +250,17 @@ func NewAuthorizerFromFile(resourceBaseURI string) (autorest.Authorizer, error) if err != nil { return nil, err } + return settings.GetAuthorizer(resourceBaseURI) +} + +// GetAuthorizer create an Authorizer in the following order. +// 1. Client credentials +// 2. Client certificate +// resourceBaseURI - used to determine the resource type +func (settings FileSettings) GetAuthorizer(resourceBaseURI string) (autorest.Authorizer, error) { + if resourceBaseURI == "" { + resourceBaseURI = azure.PublicCloud.ServiceManagementEndpoint + } if a, err := settings.ClientCredentialsAuthorizer(resourceBaseURI); err == nil { return a, err } diff --git a/autorest/azure/auth/auth_test.go b/autorest/azure/auth/auth_test.go index 4f111a8f0..742cf4196 100644 --- a/autorest/azure/auth/auth_test.go +++ b/autorest/azure/auth/auth_test.go @@ -268,6 +268,23 @@ func TestFileClientCertificateAuthorizer(t *testing.T) { } } +func TestFileGetAuthorizerClientCert(t *testing.T) { + os.Setenv("AZURE_AUTH_LOCATION", "./testdata/credsutf8.json") + settings, err := GetSettingsFromFile() + if err != nil { + t.Logf("failed to load file settings: %v", err) + t.Fail() + } + // add certificate settings + settings.Values[CertificatePath] = "~/fake/path/cert.pfx" + settings.Values[CertificatePassword] = "fake-password" + _, err = settings.GetAuthorizer("https://management.azure.com") + if err == nil { + t.Log("unexpected nil error") + t.Fail() + } +} + func TestMultitenantClientCredentials(t *testing.T) { setDefaultEnv() os.Setenv(AuxiliaryTenantIDs, "aux-tenant-1;aux-tenant-2;aux-tenant3") From 1f15e3dde48d925fcd0b98d9a09b9325b865a72e Mon Sep 17 00:00:00 2001 From: Joel Hendrix Date: Tue, 17 Jan 2023 10:58:16 -0800 Subject: [PATCH 2/2] fix test --- autorest/azure/auth/auth_test.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/autorest/azure/auth/auth_test.go b/autorest/azure/auth/auth_test.go index 742cf4196..0bc140a5c 100644 --- a/autorest/azure/auth/auth_test.go +++ b/autorest/azure/auth/auth_test.go @@ -269,7 +269,7 @@ func TestFileClientCertificateAuthorizer(t *testing.T) { } func TestFileGetAuthorizerClientCert(t *testing.T) { - os.Setenv("AZURE_AUTH_LOCATION", "./testdata/credsutf8.json") + t.Setenv("AZURE_AUTH_LOCATION", "./testdata/credsutf8.json") settings, err := GetSettingsFromFile() if err != nil { t.Logf("failed to load file settings: %v", err) @@ -278,11 +278,14 @@ func TestFileGetAuthorizerClientCert(t *testing.T) { // add certificate settings settings.Values[CertificatePath] = "~/fake/path/cert.pfx" settings.Values[CertificatePassword] = "fake-password" - _, err = settings.GetAuthorizer("https://management.azure.com") - if err == nil { - t.Log("unexpected nil error") + auth, err := settings.GetAuthorizer("https://management.azure.com") + if err != nil { + t.Logf("failed to get authorizer: %v", err) t.Fail() } + if _, ok := auth.(*autorest.BearerAuthorizer); !ok { + t.Fatalf("unexpected authorizer type %T", auth) + } } func TestMultitenantClientCredentials(t *testing.T) {