From 2a87ba8b5f791b7326e381a2dd523d3a21b550da Mon Sep 17 00:00:00 2001 From: Tom Yao Date: Fri, 20 Mar 2020 17:17:16 +0800 Subject: [PATCH 1/8] add environment variable Authority Host --- sdk/identity/Azure.Identity/src/EnvironmentVariables.cs | 1 + sdk/identity/Azure.Identity/src/TokenCredentialOptions.cs | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/sdk/identity/Azure.Identity/src/EnvironmentVariables.cs b/sdk/identity/Azure.Identity/src/EnvironmentVariables.cs index a4d0e09398dc3..a2057c087ab4e 100644 --- a/sdk/identity/Azure.Identity/src/EnvironmentVariables.cs +++ b/sdk/identity/Azure.Identity/src/EnvironmentVariables.cs @@ -21,5 +21,6 @@ internal class EnvironmentVariables public static string ProgramFilesX86 => Environment.GetEnvironmentVariable("ProgramFiles(x86)"); public static string ProgramFiles => Environment.GetEnvironmentVariable("ProgramFiles"); + public static string AuthorityHost => Environment.GetEnvironmentVariable("AZURE_AUTHORITY_HOST"); } } diff --git a/sdk/identity/Azure.Identity/src/TokenCredentialOptions.cs b/sdk/identity/Azure.Identity/src/TokenCredentialOptions.cs index 5f7b02caa6dd5..66ba07017911b 100644 --- a/sdk/identity/Azure.Identity/src/TokenCredentialOptions.cs +++ b/sdk/identity/Azure.Identity/src/TokenCredentialOptions.cs @@ -21,7 +21,12 @@ public class TokenCredentialOptions : ClientOptions /// public TokenCredentialOptions() { - AuthorityHost = KnownAuthorityHosts.AzureCloud; + + string environmentAuthorityHost = EnvironmentVariables.AuthorityHost; + + Uri authorityHostUri = string.IsNullOrEmpty(environmentAuthorityHost) ? KnownAuthorityHosts.AzureCloud : new Uri(environmentAuthorityHost); + + AuthorityHost = authorityHostUri; } } } From e9e1b0e5f7ee0b2b97c72b3221745afab9918812 Mon Sep 17 00:00:00 2001 From: Tom Yao Date: Fri, 20 Mar 2020 17:21:28 +0800 Subject: [PATCH 2/8] remove space --- sdk/identity/Azure.Identity/src/TokenCredentialOptions.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/sdk/identity/Azure.Identity/src/TokenCredentialOptions.cs b/sdk/identity/Azure.Identity/src/TokenCredentialOptions.cs index 66ba07017911b..3bf5268fd9bfa 100644 --- a/sdk/identity/Azure.Identity/src/TokenCredentialOptions.cs +++ b/sdk/identity/Azure.Identity/src/TokenCredentialOptions.cs @@ -21,7 +21,6 @@ public class TokenCredentialOptions : ClientOptions /// public TokenCredentialOptions() { - string environmentAuthorityHost = EnvironmentVariables.AuthorityHost; Uri authorityHostUri = string.IsNullOrEmpty(environmentAuthorityHost) ? KnownAuthorityHosts.AzureCloud : new Uri(environmentAuthorityHost); From f4aac73a3934b998729509509eaf49efb4d9c66c Mon Sep 17 00:00:00 2001 From: Tom Yao Date: Mon, 23 Mar 2020 10:24:00 +0800 Subject: [PATCH 3/8] remove redundant code --- sdk/identity/Azure.Identity/src/TokenCredentialOptions.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/sdk/identity/Azure.Identity/src/TokenCredentialOptions.cs b/sdk/identity/Azure.Identity/src/TokenCredentialOptions.cs index 3bf5268fd9bfa..0c922579ee620 100644 --- a/sdk/identity/Azure.Identity/src/TokenCredentialOptions.cs +++ b/sdk/identity/Azure.Identity/src/TokenCredentialOptions.cs @@ -21,9 +21,7 @@ public class TokenCredentialOptions : ClientOptions /// public TokenCredentialOptions() { - string environmentAuthorityHost = EnvironmentVariables.AuthorityHost; - - Uri authorityHostUri = string.IsNullOrEmpty(environmentAuthorityHost) ? KnownAuthorityHosts.AzureCloud : new Uri(environmentAuthorityHost); + Uri authorityHostUri = string.IsNullOrEmpty(EnvironmentVariables.AuthorityHost) ? KnownAuthorityHosts.AzureCloud : new Uri(EnvironmentVariables.AuthorityHost); AuthorityHost = authorityHostUri; } From 26b9731e9e9872eb5711c8d8826dce87cbe2ed7a Mon Sep 17 00:00:00 2001 From: Tom Yao Date: Mon, 23 Mar 2020 19:20:17 +0800 Subject: [PATCH 4/8] remove space --- sdk/identity/Azure.Identity/src/TokenCredentialOptions.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/sdk/identity/Azure.Identity/src/TokenCredentialOptions.cs b/sdk/identity/Azure.Identity/src/TokenCredentialOptions.cs index 0c922579ee620..cc16e5c04f2db 100644 --- a/sdk/identity/Azure.Identity/src/TokenCredentialOptions.cs +++ b/sdk/identity/Azure.Identity/src/TokenCredentialOptions.cs @@ -22,7 +22,6 @@ public class TokenCredentialOptions : ClientOptions public TokenCredentialOptions() { Uri authorityHostUri = string.IsNullOrEmpty(EnvironmentVariables.AuthorityHost) ? KnownAuthorityHosts.AzureCloud : new Uri(EnvironmentVariables.AuthorityHost); - AuthorityHost = authorityHostUri; } } From e1b6ee209611eaeae23e72574a94ac4f733a4953 Mon Sep 17 00:00:00 2001 From: Tom Yao Date: Thu, 26 Mar 2020 15:19:17 +0800 Subject: [PATCH 5/8] read environment variable using get accessor --- .../Azure.Identity/src/TokenCredentialOptions.cs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/sdk/identity/Azure.Identity/src/TokenCredentialOptions.cs b/sdk/identity/Azure.Identity/src/TokenCredentialOptions.cs index cc16e5c04f2db..507dac14564dc 100644 --- a/sdk/identity/Azure.Identity/src/TokenCredentialOptions.cs +++ b/sdk/identity/Azure.Identity/src/TokenCredentialOptions.cs @@ -11,18 +11,14 @@ namespace Azure.Identity /// public class TokenCredentialOptions : ClientOptions { + private Uri _authorityHost; /// /// The host of the Azure Active Directory authority. The default is https://login.microsoftonline.com/. For well known authority hosts for Azure cloud instances see . /// - public Uri AuthorityHost { get; set; } - - /// - /// Creates an instance of with default settings. - /// - public TokenCredentialOptions() + public Uri AuthorityHost { - Uri authorityHostUri = string.IsNullOrEmpty(EnvironmentVariables.AuthorityHost) ? KnownAuthorityHosts.AzureCloud : new Uri(EnvironmentVariables.AuthorityHost); - AuthorityHost = authorityHostUri; + get { return _authorityHost ?? (EnvironmentVariables.AuthorityHost != null ? new Uri(EnvironmentVariables.AuthorityHost) : KnownAuthorityHosts.AzureCloud); } + set { _authorityHost = value; } } } } From f78b7d332958309d58256b8254c7ecf58d0cf91e Mon Sep 17 00:00:00 2001 From: Tom Yao Date: Fri, 3 Apr 2020 09:57:28 +0800 Subject: [PATCH 6/8] add tests of environment authority host. --- .../tests/TokenCredentialOptionsTests.cs | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 sdk/identity/Azure.Identity/tests/TokenCredentialOptionsTests.cs diff --git a/sdk/identity/Azure.Identity/tests/TokenCredentialOptionsTests.cs b/sdk/identity/Azure.Identity/tests/TokenCredentialOptionsTests.cs new file mode 100644 index 0000000000000..9e10beca9f09e --- /dev/null +++ b/sdk/identity/Azure.Identity/tests/TokenCredentialOptionsTests.cs @@ -0,0 +1,69 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using Azure.Core.Testing; +using NUnit.Framework; + +namespace Azure.Identity.Tests +{ + public class TokenCredentialOptionsTests : ClientTestBase + { + public TokenCredentialOptionsTests(bool isAsync) : base(isAsync) + { + } + + [Test] + public void InvalidEnvAuthorityHost() + { + using (new TestEnvVar("AZURE_AUTHORITY_HOST", "mock-env-authority-host")) + { + TokenCredentialOptions option = new TokenCredentialOptions(); + + Assert.Throws(() => { Uri finalUri = option.AuthorityHost; }); + } + } + + [Test] + public void EnvAuthorityHost() + { + string envHostValue = KnownAuthorityHosts.AzureChinaCloud.ToString(); + + using (new TestEnvVar("AZURE_AUTHORITY_HOST", envHostValue)) + { + TokenCredentialOptions option = new TokenCredentialOptions(); + Uri finalUri = option.AuthorityHost; + + Assert.AreEqual(finalUri, new Uri(envHostValue)); + } + } + + [Test] + public void CustomAuthorityHost() + { + string envHostValue = KnownAuthorityHosts.AzureGermanCloud.ToString(); + + using (new TestEnvVar("AZURE_AUTHORITY_HOST", envHostValue)) + { + Uri customUri = KnownAuthorityHosts.AzureChinaCloud; + + TokenCredentialOptions option = new TokenCredentialOptions() { AuthorityHost = customUri }; + Uri finalUri = option.AuthorityHost; + + Assert.AreNotEqual(finalUri, new Uri(envHostValue)); + Assert.AreEqual(finalUri, customUri); + } + } + + [Test] + public void DefaultAuthorityHost() + { + using (new TestEnvVar("AZURE_AUTHORITY_HOST", null)) + { + TokenCredentialOptions option = new TokenCredentialOptions(); + + Assert.AreEqual(option.AuthorityHost, KnownAuthorityHosts.AzureCloud); + } + } + } +} From 9df1400c8392a1384ca141e58e492c88be809af2 Mon Sep 17 00:00:00 2001 From: Tom Yao Date: Fri, 3 Apr 2020 10:41:18 +0800 Subject: [PATCH 7/8] use meaningful variable name --- .../tests/TokenCredentialOptionsTests.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sdk/identity/Azure.Identity/tests/TokenCredentialOptionsTests.cs b/sdk/identity/Azure.Identity/tests/TokenCredentialOptionsTests.cs index 9e10beca9f09e..159bb3084d5cf 100644 --- a/sdk/identity/Azure.Identity/tests/TokenCredentialOptionsTests.cs +++ b/sdk/identity/Azure.Identity/tests/TokenCredentialOptionsTests.cs @@ -20,7 +20,7 @@ public void InvalidEnvAuthorityHost() { TokenCredentialOptions option = new TokenCredentialOptions(); - Assert.Throws(() => { Uri finalUri = option.AuthorityHost; }); + Assert.Throws(() => { Uri authHost = option.AuthorityHost; }); } } @@ -32,9 +32,9 @@ public void EnvAuthorityHost() using (new TestEnvVar("AZURE_AUTHORITY_HOST", envHostValue)) { TokenCredentialOptions option = new TokenCredentialOptions(); - Uri finalUri = option.AuthorityHost; + Uri authHost = option.AuthorityHost; - Assert.AreEqual(finalUri, new Uri(envHostValue)); + Assert.AreEqual(authHost, new Uri(envHostValue)); } } @@ -48,10 +48,10 @@ public void CustomAuthorityHost() Uri customUri = KnownAuthorityHosts.AzureChinaCloud; TokenCredentialOptions option = new TokenCredentialOptions() { AuthorityHost = customUri }; - Uri finalUri = option.AuthorityHost; + Uri authHost = option.AuthorityHost; - Assert.AreNotEqual(finalUri, new Uri(envHostValue)); - Assert.AreEqual(finalUri, customUri); + Assert.AreNotEqual(authHost, new Uri(envHostValue)); + Assert.AreEqual(authHost, customUri); } } From 9a80ef98e6e583288f13d685f3eb711fc3d82844 Mon Sep 17 00:00:00 2001 From: Tom Yao Date: Sat, 4 Apr 2020 07:52:11 +0800 Subject: [PATCH 8/8] Add the [NonParallelizable] attribute to tests which update environment variables. --- .../Azure.Identity/tests/TokenCredentialOptionsTests.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sdk/identity/Azure.Identity/tests/TokenCredentialOptionsTests.cs b/sdk/identity/Azure.Identity/tests/TokenCredentialOptionsTests.cs index 159bb3084d5cf..002d4f6b01089 100644 --- a/sdk/identity/Azure.Identity/tests/TokenCredentialOptionsTests.cs +++ b/sdk/identity/Azure.Identity/tests/TokenCredentialOptionsTests.cs @@ -13,6 +13,7 @@ public TokenCredentialOptionsTests(bool isAsync) : base(isAsync) { } + [NonParallelizable] [Test] public void InvalidEnvAuthorityHost() { @@ -24,6 +25,7 @@ public void InvalidEnvAuthorityHost() } } + [NonParallelizable] [Test] public void EnvAuthorityHost() { @@ -38,6 +40,7 @@ public void EnvAuthorityHost() } } + [NonParallelizable] [Test] public void CustomAuthorityHost() { @@ -55,6 +58,7 @@ public void CustomAuthorityHost() } } + [NonParallelizable] [Test] public void DefaultAuthorityHost() {