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 #8040

Closed
wants to merge 7 commits into from
Closed
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
12 changes: 10 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,23 @@ 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) {
var authorityHost string
envauthorityHost := os.Getenv("AZURE_AUTHORITY_HOST")
authorityHost = defaultAuthorityHost
if 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
31 changes: 31 additions & 0 deletions sdk/azidentity/chained_token_credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,37 @@ func TestChainedTokenCredential_GetTokenFail(t *testing.T) {
}
}

func TestChainedTokenCredential_GetTokenWithUnavailableCredentialInChain(t *testing.T) {
srv, close := mock.NewServer()
defer close()
srv.AppendError(&CredentialUnavailableError{CredentialType: "MockCredential", Message: "Mocking a credential unavailable error"})
srv.AppendResponse(mock.WithBody([]byte(accessTokenRespSuccess)))
testURL := srv.URL()
secCred, err := NewClientSecretCredential(tenantID, clientID, wrongSecret, &TokenCredentialOptions{HTTPClient: srv, AuthorityHost: &testURL})
if err != nil {
t.Fatalf("Unable to create credential. Received: %v", err)
}
// The chain has the same credential twice, since it doesn't matter what credential we add to the chain as long as it is not a nil credential.
// Most credentials will not be instantiated if the conditions do not exist to allow them to be used, thus returning a
// CredentialUnavailable error from the constructor. In order to test the CredentialUnavailable functionality for
// ChainedTokenCredential we have to mock with two valid credentials, but the first will fail since the first response queued
// in the test server is a CredentialUnavailable error.
cred, err := NewChainedTokenCredential(secCred, secCred)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
tk, err := cred.GetToken(context.Background(), azcore.TokenRequestOptions{Scopes: []string{scope}})
if err != nil {
t.Fatalf("Received an error when attempting to get a token but expected none")
}
if tk.Token != tokenValue {
t.Fatalf("Received an incorrect access token")
}
if tk.ExpiresOn.IsZero() {
t.Fatalf("Received an incorrect time in the response")
}
}

func TestBearerPolicy_ChainedTokenCredential(t *testing.T) {
err := initEnvironmentVarsForTest()
if err != nil {
Expand Down