diff --git a/src/SecureStore.Contrib.Configuration/SecureStore.Contrib.Configuration.csproj b/src/SecureStore.Contrib.Configuration/SecureStore.Contrib.Configuration.csproj index 3e00157..6015f7f 100644 --- a/src/SecureStore.Contrib.Configuration/SecureStore.Contrib.Configuration.csproj +++ b/src/SecureStore.Contrib.Configuration/SecureStore.Contrib.Configuration.csproj @@ -33,7 +33,7 @@ - + diff --git a/src/SecureStore.Contrib.Configuration/SecureStoreConfigurationProvider.cs b/src/SecureStore.Contrib.Configuration/SecureStoreConfigurationProvider.cs index cf07280..dd73c34 100644 --- a/src/SecureStore.Contrib.Configuration/SecureStoreConfigurationProvider.cs +++ b/src/SecureStore.Contrib.Configuration/SecureStoreConfigurationProvider.cs @@ -48,7 +48,10 @@ public override void Load(Stream stream) throw new FileNotFoundException(error.ToString()); } - manager.LoadKeyFromFile(file.PhysicalPath); + using (var keyStream = file.CreateReadStream()) + { + manager.LoadKeyFromStream(keyStream); + } break; case KeyType.Password: manager.LoadKeyFromPassword(source.Key); diff --git a/test/SecureStore.Contrib.Configuration.Tests/SecureStore.Contrib.Configuration.Tests.csproj b/test/SecureStore.Contrib.Configuration.Tests/SecureStore.Contrib.Configuration.Tests.csproj index 06c9be0..7df6a02 100644 --- a/test/SecureStore.Contrib.Configuration.Tests/SecureStore.Contrib.Configuration.Tests.csproj +++ b/test/SecureStore.Contrib.Configuration.Tests/SecureStore.Contrib.Configuration.Tests.csproj @@ -1,16 +1,17 @@ - + netcoreapp3.1 - + true false + - + all @@ -22,6 +23,10 @@ + + + + diff --git a/test/SecureStore.Contrib.Configuration.Tests/SecureStoreConfigurationExtensionsTests.cs b/test/SecureStore.Contrib.Configuration.Tests/SecureStoreConfigurationExtensionsTests.cs index b809961..fe2e9cb 100644 --- a/test/SecureStore.Contrib.Configuration.Tests/SecureStoreConfigurationExtensionsTests.cs +++ b/test/SecureStore.Contrib.Configuration.Tests/SecureStoreConfigurationExtensionsTests.cs @@ -76,7 +76,7 @@ public void AddSecureStoreFile_ThrowsIfFileDoesNotExistAtKey() // Act and Assert var ex = Assert.Throws(() => new ConfigurationBuilder().AddSecureStoreFile(path, keyPath, KeyType.File).Build()); - Assert.StartsWith($"Could not find file ", ex.Message); + Assert.StartsWith($"The configuration key file '{keyPath}' was not found", ex.Message); // Cleanup File.Delete(path); diff --git a/test/SecureStore.Contrib.Configuration.Tests/SecureStoreConfigurationProviderTests.cs b/test/SecureStore.Contrib.Configuration.Tests/SecureStoreConfigurationProviderTests.cs index 4a2c92b..847d963 100644 --- a/test/SecureStore.Contrib.Configuration.Tests/SecureStoreConfigurationProviderTests.cs +++ b/test/SecureStore.Contrib.Configuration.Tests/SecureStoreConfigurationProviderTests.cs @@ -1,3 +1,5 @@ +using Microsoft.Extensions.FileProviders; + namespace SecureStore.Contrib.Configuration.Tests { using System; @@ -6,9 +8,11 @@ namespace SecureStore.Contrib.Configuration.Tests using NeoSmart.SecureStore; using Xunit; - public class SecureStoreConfigurationProviderTests + public class SecureStoreConfigurationProviderTests : IDisposable { - private static string Password => "P@$$w0rD!"; + private static readonly string EmbeddedKeyName = "embedded.key"; + private static readonly string Password = "P@$$w0rD!"; + private readonly string _storePath; private static readonly Dictionary SecureData = new Dictionary { @@ -17,58 +21,68 @@ public class SecureStoreConfigurationProviderTests {"foo3", "bar3"} }; - private void CreateTestStore(string storePath, string key, KeyType type) + public SecureStoreConfigurationProviderTests() { - using (var sman = SecretsManager.CreateStore()) - { - if (type == KeyType.Password) - { - sman.LoadKeyFromPassword(key); - } - else - { - sman.GenerateKey(); - } - - foreach (var secretKey in SecureData.Keys) - { - sman.Set(secretKey, SecureData[secretKey]); - } - - sman.SaveStore(storePath); - sman.ExportKey(key); - } + _storePath = Path.GetTempFileName(); + } + + public void Dispose() + { + File.Delete(_storePath); } [Fact] public void LoadStreamUsingKeyFile() { - var storePath = Path.GetTempFileName(); var keyPath = Path.GetTempFileName(); + CreateTestStore(_storePath, keyPath, KeyType.File); + var configurationSource = new SecureStoreConfigurationSource + { + KeyType = KeyType.File, + Key = keyPath, + Optional = true + }; + configurationSource.ResolveKeyFileProvider(); + var provider = new SecureStoreConfigurationProvider(configurationSource); + + using (var stream = new FileStream(_storePath, FileMode.Open, FileAccess.Read)) + { + provider.Load(stream); + } - CreateTestStore(storePath, keyPath, KeyType.File); + Assert.All(SecureData, item => Assert.Equal(provider.Get(item.Key), item.Value)); + File.Delete(keyPath); + } + [Fact] + public void LoadStreamUsingEmbeddedKeyFile() + { + var assembly = typeof(SecureStoreConfigurationProviderTests).Assembly; + var names = assembly.GetManifestResourceNames(); + using (var key = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.{EmbeddedKeyName}")!) + { + CreateTestStore(_storePath, key); + } var provider = new SecureStoreConfigurationProvider(new SecureStoreConfigurationSource { + KeyFileProvider = new ManifestEmbeddedFileProvider(assembly), KeyType = KeyType.File, - Key = keyPath, + Key = EmbeddedKeyName, Optional = true }); - using (var stream = new FileStream(storePath, FileMode.Open, FileAccess.Read)) + using (var stream = new FileStream(_storePath, FileMode.Open, FileAccess.Read)) { provider.Load(stream); } - File.Delete(storePath); - File.Delete(keyPath); + Assert.All(SecureData, item => Assert.Equal(provider.Get(item.Key), item.Value)); } [Fact] public void LoadStreamUsingPassword() { - var storePath = Path.GetTempFileName(); - CreateTestStore(storePath, Password, KeyType.Password); + CreateTestStore(_storePath, Password, KeyType.Password); var provider = new SecureStoreConfigurationProvider(new SecureStoreConfigurationSource { @@ -77,36 +91,67 @@ public void LoadStreamUsingPassword() Optional = true }); - using (var stream = new FileStream(storePath, FileMode.Open, FileAccess.Read)) + using (var stream = new FileStream(_storePath, FileMode.Open, FileAccess.Read)) { provider.Load(stream); } - File.Delete(storePath); + Assert.All(SecureData, item => Assert.Equal(provider.Get(item.Key), item.Value)); } [Fact] public void LoadStreamUsingPassword_ThrowsIfKeyTypeNotInRange() { - var storePath = Path.GetTempFileName(); - CreateTestStore(storePath, Password, KeyType.Password); + CreateTestStore(_storePath, Password, KeyType.Password); var source = new SecureStoreConfigurationSource { - KeyType = (KeyType) 3, + KeyType = (KeyType)3, Key = Password, Optional = true }; var provider = new SecureStoreConfigurationProvider(source); - using (var stream = new FileStream(storePath, FileMode.Open, FileAccess.Read)) + using (var stream = new FileStream(_storePath, FileMode.Open, FileAccess.Read)) { var ex = Assert.Throws(() => provider.Load(stream)); Assert.Equal(nameof(source.KeyType), ex.ParamName); } + } + + private void CreateTestStore(string storePath, string key, KeyType type) + { + using var sman = SecretsManager.CreateStore(); + if (type == KeyType.Password) + { + sman.LoadKeyFromPassword(key); + } + else + { + sman.GenerateKey(); + } + + foreach (var secretKey in SecureData.Keys) + { + sman.Set(secretKey, SecureData[secretKey]); + } + + sman.SaveStore(storePath); + sman.ExportKey(key); + } + + private void CreateTestStore(string storePath, Stream key) + { + using var sman = SecretsManager.CreateStore(); + sman.LoadKeyFromStream(key); + + foreach (var secretKey in SecureData.Keys) + { + sman.Set(secretKey, SecureData[secretKey]); + } - File.Delete(storePath); + sman.SaveStore(storePath); } } } \ No newline at end of file diff --git a/test/SecureStore.Contrib.Configuration.Tests/embedded.key b/test/SecureStore.Contrib.Configuration.Tests/embedded.key new file mode 100644 index 0000000..1ae7804 --- /dev/null +++ b/test/SecureStore.Contrib.Configuration.Tests/embedded.key @@ -0,0 +1,3 @@ +-----BEGIN PRIVATE KEY----- +xRx4F6zq7k3/w+hmapDpo44huBupZrCbkyqQqdAKF5I= +-----END PRIVATE KEY-----