Skip to content
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

feat: allow getting authorizer from existing file settings #709

Merged
merged 3 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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