Skip to content

Commit

Permalink
feat: allow getting authorizer from existing file settings (#709)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
tareksha and jhendrixMSFT committed Jan 17, 2023
1 parent 79575dd commit 9038e4a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
11 changes: 11 additions & 0 deletions autorest/azure/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
20 changes: 20 additions & 0 deletions autorest/azure/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,26 @@ func TestFileClientCertificateAuthorizer(t *testing.T) {
}
}

func TestFileGetAuthorizerClientCert(t *testing.T) {
t.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"
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) {
setDefaultEnv()
os.Setenv(AuxiliaryTenantIDs, "aux-tenant-1;aux-tenant-2;aux-tenant3")
Expand Down

0 comments on commit 9038e4a

Please sign in to comment.