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

Add EnvVar AZURE_AUTHORITY_HOST #8074

Merged
merged 11 commits into from
Apr 6, 2020
10 changes: 8 additions & 2 deletions sdk/azidentity/azidentity.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"net/http"
"net/url"
"os"
"time"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
Expand Down Expand Up @@ -118,16 +119,21 @@ type TokenCredentialOptions struct {
// NewIdentityClientOptions initializes an instance of IdentityClientOptions with default settings
// NewIdentityClientOptions initializes an instance of IdentityClientOptions with default settings
func (c *TokenCredentialOptions) setDefaultValues() (*TokenCredentialOptions, error) {
authorityHost := defaultAuthorityHost
if envAuthorityHost := os.Getenv("AZURE_AUTHORITY_HOST"); envAuthorityHost != "" {
authorityHost = envAuthorityHost
}

if c == nil {
defaultAuthorityHostURL, err := url.Parse(defaultAuthorityHost)
defaultAuthorityHostURL, err := url.Parse(authorityHost)
if err != nil {
return nil, err
}
c = &TokenCredentialOptions{AuthorityHost: defaultAuthorityHostURL}
}

if c.AuthorityHost == nil {
defaultAuthorityHostURL, err := url.Parse(defaultAuthorityHost)
defaultAuthorityHostURL, err := url.Parse(authorityHost)
if err != nil {
return nil, err
}
Expand Down
57 changes: 57 additions & 0 deletions sdk/azidentity/azidentity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ package azidentity

import (
"net/url"
"os"
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
)

const (
envHostString = "https://mock.com/"
customHostString = "https://custommock.com/"
)

func Test_AuthorityHost_Parse(t *testing.T) {
_, err := url.Parse(defaultAuthorityHost)
if err != nil {
Expand All @@ -27,3 +33,54 @@ func Test_NonNilTokenCredentialOptsNilAuthorityHost(t *testing.T) {
t.Fatalf("Did not set default authority host")
}
}

func Test_SetEnvAuthorityHost(t *testing.T) {
err := os.Setenv("AZURE_AUTHORITY_HOST", envHostString)
if err != nil {
t.Fatalf("Unexpected error when initializing environment variables: %v", err)
}

opts := &TokenCredentialOptions{}
opts, err = opts.setDefaultValues()
if opts.AuthorityHost.String() != envHostString {
t.Fatalf("Unexpected error when get host from environment vairable: %v", err)
}

// Unset that host environment vairable to avoid other tests failed.
err = os.Unsetenv("AZURE_AUTHORITY_HOST")
if err != nil {
t.Fatalf("Unexpected error when unset environment vairable: %v", err)
}
}

func Test_CustomAuthorityHost(t *testing.T) {
err := os.Setenv("AZURE_AUTHORITY_HOST", envHostString)
if err != nil {
t.Fatalf("Unexpected error when initializing environment variables: %v", err)
}

customHost, err := url.Parse(customHostString)
if err != nil {
t.Fatalf("Received an error: %v", err)
}

opts := &TokenCredentialOptions{AuthorityHost: customHost}
opts, err = opts.setDefaultValues()
if opts.AuthorityHost.String() != customHostString {
t.Fatalf("Unexpected error when get host from environment vairable: %v", err)
}

// Unset that host environment vairable to avoid other tests failed.
err = os.Unsetenv("AZURE_AUTHORITY_HOST")
if err != nil {
t.Fatalf("Unexpected error when unset environment vairable: %v", err)
}
}

func Test_DefaultAuthorityHost(t *testing.T) {
opts := &TokenCredentialOptions{}
opts, err := opts.setDefaultValues()
if opts.AuthorityHost.String() != defaultAuthorityHost {
t.Fatalf("Unexpected error when set default AuthorityHost: %v", err)
}
}