public async Task CachedSecretProvider_GetTwoSecretValues_WithinCacheInterval_ShouldReturnTheSameValues() { // Arrange string expectedFirstSecret = Guid.NewGuid().ToString("N"); string expectedSecondSecret = Guid.NewGuid().ToString("N"); var spyTestProvider = new TestSecretProviderStub(expectedFirstSecret); TimeSpan cacheInterval = TimeSpan.FromSeconds(3); const string keyName = "MyValue"; ICachedSecretProvider cachedSecretProvider = new CachedSecretProvider( spyTestProvider, new CacheConfiguration(cacheInterval), new MemoryCache(new MemoryCacheOptions())); // Act string actualFirst = await cachedSecretProvider.Get(keyName); await Task.Delay(TimeSpan.FromSeconds(1)); spyTestProvider.SecretValue = expectedSecondSecret; string actualSecond = await cachedSecretProvider.Get(keyName); // Assert Assert.Equal(expectedFirstSecret, actualFirst); Assert.Equal(expectedFirstSecret, actualSecond); Assert.Equal(1, spyTestProvider.CallsMadeSinceCreation); }
public async Task CachedSecretProvider_GetTwoSecretsAsync_SkippedCache_ShouldReturnDifferentValues() { // Arrange string expectedFirstSecret = Guid.NewGuid().ToString("N"); string expectedSecondSecret = Guid.NewGuid().ToString("N"); var spyTestProvider = new TestSecretProviderStub(expectedFirstSecret); TimeSpan cacheInterval = TimeSpan.FromSeconds(3); const string keyName = "MyValue"; ICachedSecretProvider cachedSecretProvider = new CachedSecretProvider( spyTestProvider, new CacheConfiguration(cacheInterval), new MemoryCache(new MemoryCacheOptions())); // Act Secret actualFirst = await cachedSecretProvider.GetSecretAsync(keyName, ignoreCache : true); await Task.Delay(TimeSpan.FromMilliseconds(100)); spyTestProvider.SecretValue = expectedSecondSecret; Secret actualSecond = await cachedSecretProvider.GetSecretAsync(keyName, ignoreCache : true); // Assert Assert.True(actualFirst != null, "actualFirst != null"); Assert.True(actualSecond != null, "actualSecond != null"); Assert.Equal(expectedFirstSecret, actualFirst.Value); Assert.Equal(expectedSecondSecret, actualSecond.Value); Assert.Equal(2, spyTestProvider.CallsMadeSinceCreation); }
public async Task CacheSecretProvider_GetSecretAsync_InvalidateSecret_ShouldRequestNewSecretEvenWithinCacheDuration() { // Arrange string expectedFirstSecret = Guid.NewGuid().ToString("N"); string expectedSecondSecret = Guid.NewGuid().ToString("N"); var testProviderStub = new TestSecretProviderStub(expectedFirstSecret); var cacheInterval = TimeSpan.FromSeconds(10); const string keyName = "MySecret"; var cachedSecretProvider = new CachedSecretProvider( testProviderStub, new CacheConfiguration(cacheInterval)); Secret actualFirst = await cachedSecretProvider.GetSecretAsync(keyName); testProviderStub.SecretValue = expectedSecondSecret; // Act await cachedSecretProvider.InvalidateSecretAsync(keyName); // Assert Secret actualSecond = await cachedSecretProvider.GetSecretAsync(keyName); Assert.True(actualFirst != null, "actualFirst != null"); Assert.True(actualSecond != null, "actualSecond != null"); Assert.Equal(expectedFirstSecret, actualFirst.Value); Assert.Equal(expectedSecondSecret, actualSecond.Value); Assert.Equal(2, testProviderStub.CallsMadeSinceCreation); }
public void CachedSecretProvider_CreateWithOnlySecretProvider_ShouldSucceed() { // Arrange string secretKeyValue = Guid.NewGuid().ToString("N"); var testSecretProvider = new TestSecretProviderStub(secretKeyValue); // Act & Assert var _ = new CachedSecretProvider(testSecretProvider); }
public void CachedSecretProvider_CreateWithoutCache_ShouldSucceed() { // Arrange string secretKeyValue = Guid.NewGuid().ToString("N"); var testSecretProvider = new TestSecretProviderStub(secretKeyValue); var cacheConfiguration = new CacheConfiguration(TimeSpan.MaxValue); // Act & Assert var _ = new CachedSecretProvider(testSecretProvider, cacheConfiguration); }
public void CachedSecretProvider_CreateWithCorrectArguments_ShouldSucceed() { // Arrange string secretKeyValue = Guid.NewGuid().ToString("N"); var testSecretProvider = new TestSecretProviderStub(secretKeyValue); var memCache = new MemoryCache(new MemoryCacheOptions()); var cacheConfiguration = new CacheConfiguration(TimeSpan.MaxValue); // Act & Assert ICachedSecretProvider cachedSecretProvider = new CachedSecretProvider(testSecretProvider, cacheConfiguration, memCache); Assert.NotNull(cachedSecretProvider); }
/// <summary> /// Adds Azure Key Vault as a secret source. /// </summary> /// <param name="builder">The builder to create the secret store.</param> /// <param name="authentication">The requested authentication type for connecting to the Azure Key Vault instance.</param> /// <param name="configuration">The configuration related to the Azure Key Vault instance to use.</param> /// <param name="cacheConfiguration">The configuration to control how the caching will be done.</param> public static SecretStoreBuilder AddAzureKeyVault( this SecretStoreBuilder builder, IKeyVaultAuthentication authentication, IKeyVaultConfiguration configuration, ICacheConfiguration cacheConfiguration) { Guard.NotNull(builder, nameof(builder)); Guard.NotNull(authentication, nameof(authentication)); Guard.NotNull(configuration, nameof(configuration)); var keyVaultSecretProvider = new KeyVaultSecretProvider(authentication, configuration); if (cacheConfiguration != null) { var cachedSecretProvider = new CachedSecretProvider(keyVaultSecretProvider, cacheConfiguration); return(builder.AddProvider(cachedSecretProvider)); } return(builder.AddProvider(keyVaultSecretProvider)); }