Skip to content
This repository was archived by the owner on Feb 12, 2025. It is now read-only.

Commit 9038e4a

Browse files
feat: allow getting authorizer from existing file settings (#709)
* feat: allow getting authorizer from existing file settings Signed-off-by: Tareq Sharafy <tareq.sha@gmail.com> * fix test Signed-off-by: Tareq Sharafy <tareq.sha@gmail.com> Co-authored-by: Joel Hendrix <jhendrix@microsoft.com>
1 parent 79575dd commit 9038e4a

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

autorest/azure/auth/auth.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,17 @@ func NewAuthorizerFromFile(resourceBaseURI string) (autorest.Authorizer, error)
250250
if err != nil {
251251
return nil, err
252252
}
253+
return settings.GetAuthorizer(resourceBaseURI)
254+
}
255+
256+
// GetAuthorizer create an Authorizer in the following order.
257+
// 1. Client credentials
258+
// 2. Client certificate
259+
// resourceBaseURI - used to determine the resource type
260+
func (settings FileSettings) GetAuthorizer(resourceBaseURI string) (autorest.Authorizer, error) {
261+
if resourceBaseURI == "" {
262+
resourceBaseURI = azure.PublicCloud.ServiceManagementEndpoint
263+
}
253264
if a, err := settings.ClientCredentialsAuthorizer(resourceBaseURI); err == nil {
254265
return a, err
255266
}

autorest/azure/auth/auth_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,26 @@ func TestFileClientCertificateAuthorizer(t *testing.T) {
268268
}
269269
}
270270

271+
func TestFileGetAuthorizerClientCert(t *testing.T) {
272+
t.Setenv("AZURE_AUTH_LOCATION", "./testdata/credsutf8.json")
273+
settings, err := GetSettingsFromFile()
274+
if err != nil {
275+
t.Logf("failed to load file settings: %v", err)
276+
t.Fail()
277+
}
278+
// add certificate settings
279+
settings.Values[CertificatePath] = "~/fake/path/cert.pfx"
280+
settings.Values[CertificatePassword] = "fake-password"
281+
auth, err := settings.GetAuthorizer("https://management.azure.com")
282+
if err != nil {
283+
t.Logf("failed to get authorizer: %v", err)
284+
t.Fail()
285+
}
286+
if _, ok := auth.(*autorest.BearerAuthorizer); !ok {
287+
t.Fatalf("unexpected authorizer type %T", auth)
288+
}
289+
}
290+
271291
func TestMultitenantClientCredentials(t *testing.T) {
272292
setDefaultEnv()
273293
os.Setenv(AuxiliaryTenantIDs, "aux-tenant-1;aux-tenant-2;aux-tenant3")

0 commit comments

Comments
 (0)