diff --git a/src/Accounts/Accounts.Test/AutosaveTests.cs b/src/Accounts/Accounts.Test/AutosaveTests.cs index 00fb9eec717e..7f8cc7e22837 100644 --- a/src/Accounts/Accounts.Test/AutosaveTests.cs +++ b/src/Accounts/Accounts.Test/AutosaveTests.cs @@ -27,6 +27,7 @@ using Microsoft.Azure.Commands.ScenarioTest; using Microsoft.Azure.Commands.ResourceManager.Common; using Microsoft.Azure.Commands.TestFx.Mocks; +using Moq; namespace Microsoft.Azure.Commands.Profile.Test { @@ -42,6 +43,18 @@ public AutosaveTests(ITestOutputHelper output) ResetState(); } + private void SetMockedAzKeyStore() + { + var storageMocker = new Mock(); + storageMocker.Setup(f => f.Create()).Returns(storageMocker.Object); + storageMocker.Setup(f => f.ReadData()).Returns(new byte[0]); + var keyStore = new AzKeyStore(AzureSession.Instance.ARMProfileDirectory, "keystore.cache", false, false, storageMocker.Object); + AzKeyStore.RegisterJsonConverter(typeof(ServicePrincipalKey), typeof(ServicePrincipalKey).Name); + AzKeyStore.RegisterJsonConverter(typeof(SecureString), typeof(SecureString).Name, new SecureStringConverter()); + AzureSession.Instance.RegisterComponent(AzKeyStore.Name, () => keyStore); + keyStore.LoadStorage(); + } + void ResetState() { @@ -55,12 +68,7 @@ void ResetState() Environment.SetEnvironmentVariable("Azure_PS_Data_Collection", "false"); PowerShellTokenCacheProvider tokenProvider = new InMemoryTokenCacheProvider(); AzureSession.Instance.RegisterComponent(PowerShellTokenCacheProvider.PowerShellTokenCacheProviderKey, () => tokenProvider, true); - - var keyStore = new AzKeyStore(AzureSession.Instance.ARMProfileDirectory, "keystore.cache", false); - AzKeyStore.RegisterJsonConverter(typeof(ServicePrincipalKey), typeof(ServicePrincipalKey).Name); - AzKeyStore.RegisterJsonConverter(typeof(SecureString), typeof(SecureString).Name, new SecureStringConverter()); - AzureSession.Instance.RegisterComponent(AzKeyStore.Name, () => keyStore); - keyStore.LoadStorage(); + SetMockedAzKeyStore(); } [Fact] diff --git a/src/Accounts/Authentication.Test/AzKeyStorageTest.cs b/src/Accounts/Authentication.Test/AzKeyStorageTest.cs index a112fc2c8256..bd0b64b8ebf9 100644 --- a/src/Accounts/Authentication.Test/AzKeyStorageTest.cs +++ b/src/Accounts/Authentication.Test/AzKeyStorageTest.cs @@ -11,28 +11,17 @@ // See the License for the specific language governing permissions and // limitations under the License. // ---------------------------------------------------------------------------------- - -using Azure.Core; -using System; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.Azure.Commands.Common.Authentication; -using Microsoft.Azure.Commands.Common.Authentication.Abstractions; -using Microsoft.Azure.PowerShell.Authenticators; -using Microsoft.Azure.PowerShell.Authenticators.Factories; -using Microsoft.WindowsAzure.Commands.ScenarioTest; -using Moq; -using Xunit; -using Xunit.Abstractions; -using Azure.Identity; using Microsoft.Azure.Commands.ResourceManager.Common; -using Microsoft.Identity.Client.Extensions.Msal; -using System.Collections.Generic; using Microsoft.WindowsAzure.Commands.Common; -using System.Text; -using System.Security; +using Microsoft.WindowsAzure.Commands.ScenarioTest; +using Moq; using Newtonsoft.Json; +using System; +using System.Collections.Generic; using System.Linq; +using System.Security; +using System.Text; +using Xunit; namespace Common.Authenticators.Test { @@ -47,6 +36,7 @@ public AzKeyStorageTest() { storageChecker = new List(); storageMocker = new Mock(); + storageMocker.Setup(f => f.Create()).Returns(storageMocker.Object); storageMocker.Setup(f => f.WriteData(It.IsAny())).Callback((byte[] s) => { storageChecker.Clear(); storageChecker.AddRange(s); }); } @@ -65,11 +55,10 @@ private static bool CompareJsonObjects(string expected, string acutal) [Trait(Category.AcceptanceType, Category.CheckIn)] public void SaveKey() { - using (var store = new AzKeyStore(dummpyPath, keyStoreFileName, false)) + using (var store = new AzKeyStore(dummpyPath, keyStoreFileName, false, true, storageMocker.Object)) { AzKeyStore.RegisterJsonConverter(typeof(ServicePrincipalKey), typeof(ServicePrincipalKey).Name); AzKeyStore.RegisterJsonConverter(typeof(SecureString), typeof(SecureString).Name, new SecureStringConverter()); - store.Storage = storageMocker.Object; IKeyStoreKey servicePrincipalKey = new ServicePrincipalKey("ServicePrincipalSecret", "6c984d31-5b4f-4734-b548-e230a248e347", "54826b22-38d6-4fb2-bad9-b7b93a3e9c5a"); var secret = "secret".ConvertToSecureString(); @@ -93,11 +82,10 @@ public void FindKey() { const string EXPECTED = @"[{""keyType"":""ServicePrincipalKey"",""keyStoreKey"":""{\""appId\"":\""6c984d31-5b4f-4734-b548-e230a248e347\"",\""tenantId\"":\""54826b22-38d6-4fb2-bad9-b7b93a3e9c5a\"",\""name\"":\""ServicePrincipalSecret\""}"",""valueType"":""SecureString"",""keyStoreValue"":""\""secret\""""}]"; storageChecker.AddRange(Encoding.UTF8.GetBytes(EXPECTED)); - using (var store = new AzKeyStore(dummpyPath, keyStoreFileName, false)) + using (var store = new AzKeyStore(dummpyPath, keyStoreFileName, false, true, storageMocker.Object)) { AzKeyStore.RegisterJsonConverter(typeof(ServicePrincipalKey), typeof(ServicePrincipalKey).Name); AzKeyStore.RegisterJsonConverter(typeof(SecureString), typeof(SecureString).Name, new SecureStringConverter()); - store.Storage = storageMocker.Object; storageMocker.Setup(f => f.ReadData()).Returns(storageChecker.ToArray()); store.LoadStorage(); @@ -115,11 +103,10 @@ public void FindNoKey() { const string EXPECTED = @"[{""keyType"":""ServicePrincipalKey"",""keyStoreKey"":""{\""appId\"":\""6c984d31-5b4f-4734-b548-e230a248e347\"",\""tenantId\"":\""54826b22-38d6-4fb2-bad9-b7b93a3e9c5a\"",\""name\"":\""ServicePrincipalSecret\""}"",""valueType"":""SecureString"",""keyStoreValue"":""\""secret\""""}]"; storageChecker.AddRange(Encoding.UTF8.GetBytes(EXPECTED)); - using (var store = new AzKeyStore(dummpyPath, keyStoreFileName)) + using (var store = new AzKeyStore(dummpyPath, keyStoreFileName, false, true, storageMocker.Object)) { AzKeyStore.RegisterJsonConverter(typeof(ServicePrincipalKey), typeof(ServicePrincipalKey).Name); AzKeyStore.RegisterJsonConverter(typeof(SecureString), typeof(SecureString).Name, new SecureStringConverter()); - store.Storage = storageMocker.Object; storageMocker.Setup(f => f.ReadData()).Returns(storageChecker.ToArray()); store.LoadStorage(); @@ -135,11 +122,10 @@ public void RemoveKey() { const string EXPECTED = @"[{""keyType"":""ServicePrincipalKey"",""keyStoreKey"":""{\""appId\"":\""6c984d31-5b4f-4734-b548-e230a248e347\"",\""tenantId\"":\""54826b22-38d6-4fb2-bad9-b7b93a3e9c5a\"",\""name\"":\""ServicePrincipalSecret\""}"",""valueType"":""SecureString"",""keyStoreValue"":""\""secret\""""}]"; storageChecker.AddRange(Encoding.UTF8.GetBytes(EXPECTED)); - using (var store = new AzKeyStore(dummpyPath, keyStoreFileName)) + using (var store = new AzKeyStore(dummpyPath, keyStoreFileName, false, true, storageMocker.Object)) { AzKeyStore.RegisterJsonConverter(typeof(ServicePrincipalKey), typeof(ServicePrincipalKey).Name); AzKeyStore.RegisterJsonConverter(typeof(SecureString), typeof(SecureString).Name, new SecureStringConverter()); - store.Storage = storageMocker.Object; storageMocker.Setup(f => f.ReadData()).Returns(storageChecker.ToArray()); store.LoadStorage(); @@ -159,11 +145,10 @@ public void RemoveNoKey() { const string EXPECTED = @"[{""keyType"":""ServicePrincipalKey"",""keyStoreKey"":""{\""appId\"":\""6c984d31-5b4f-4734-b548-e230a248e347\"",\""tenantId\"":\""54826b22-38d6-4fb2-bad9-b7b93a3e9c5a\"",\""name\"":\""ServicePrincipalSecret\""}"",""valueType"":""SecureString"",""keyStoreValue"":""\""secret\""""}]"; storageChecker.AddRange(Encoding.UTF8.GetBytes(EXPECTED)); - using (var store = new AzKeyStore(dummpyPath, keyStoreFileName)) + using (var store = new AzKeyStore(dummpyPath, keyStoreFileName, false, true, storageMocker.Object)) { AzKeyStore.RegisterJsonConverter(typeof(ServicePrincipalKey), typeof(ServicePrincipalKey).Name); AzKeyStore.RegisterJsonConverter(typeof(SecureString), typeof(SecureString).Name, new SecureStringConverter()); - store.Storage = storageMocker.Object; storageMocker.Setup(f => f.ReadData()).Returns(storageChecker.ToArray()); store.LoadStorage(); diff --git a/src/Accounts/Authentication/KeyStore/AzKeyStore.cs b/src/Accounts/Authentication/KeyStore/AzKeyStore.cs index f5528fbead71..67413c8bf8b3 100644 --- a/src/Accounts/Authentication/KeyStore/AzKeyStore.cs +++ b/src/Accounts/Authentication/KeyStore/AzKeyStore.cs @@ -76,10 +76,10 @@ public AzKeyStore() } - public AzKeyStore(string directory, string fileName, bool loadStorage = true, bool autoSaveEnabled = true) + public AzKeyStore(string directory, string fileName, bool loadStorage = true, bool autoSaveEnabled = true, IStorage inputStorage = null) { autoSave = autoSaveEnabled; - Storage = new StorageWrapper() + Storage = inputStorage ?? new StorageWrapper() { FileName = fileName, Directory = directory