Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.0" />
<PackageReference Include="SecureStore" Version="1.0.0" />
<PackageReference Include="SecureStore" Version="1.2.2" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.2" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="3.1.2" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Abstractions" Version="3.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="SecureStore" Version="1.0.3" />
<PackageReference Include="SecureStore" Version="1.2.2" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
Expand All @@ -22,6 +23,10 @@
</PackageReference>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="embedded.key" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\SecureStore.Contrib.Configuration\SecureStore.Contrib.Configuration.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void AddSecureStoreFile_ThrowsIfFileDoesNotExistAtKey()
// Act and Assert
var ex = Assert.Throws<FileNotFoundException>(() =>
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Microsoft.Extensions.FileProviders;

namespace SecureStore.Contrib.Configuration.Tests
{
using System;
Expand All @@ -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<string, string> SecureData = new Dictionary<string, string>
{
Expand All @@ -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
{
Expand All @@ -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<ArgumentOutOfRangeException>(() =>
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);
}
}
}
3 changes: 3 additions & 0 deletions test/SecureStore.Contrib.Configuration.Tests/embedded.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-----BEGIN PRIVATE KEY-----
xRx4F6zq7k3/w+hmapDpo44huBupZrCbkyqQqdAKF5I=
-----END PRIVATE KEY-----