Skip to content

Commit

Permalink
Add EnvVar AZURE_AUTHORITY_HOST (#8074)
Browse files Browse the repository at this point in the history
* add env authorityhost

* add AuthorityHost tests

* Unset env variable after AuthorityTest to avoid other tests fail

* add default AuthorityHost test

Co-authored-by: wantedfast <[email protected]>
Co-authored-by: Geralt Wang <[email protected]>
  • Loading branch information
3 people authored Apr 6, 2020
1 parent 5df0052 commit b1466cf
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
10 changes: 8 additions & 2 deletions sdk/azidentity/azidentity.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package azidentity
import (
"net/http"
"net/url"
"os"
"time"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
Expand Down Expand Up @@ -117,16 +118,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)
}
}

0 comments on commit b1466cf

Please sign in to comment.