-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSecureStoreConfigurationProviderTests.cs
More file actions
157 lines (133 loc) · 4.96 KB
/
SecureStoreConfigurationProviderTests.cs
File metadata and controls
157 lines (133 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using Microsoft.Extensions.FileProviders;
namespace SecureStore.Contrib.Configuration.Tests
{
using System;
using System.Collections.Generic;
using System.IO;
using NeoSmart.SecureStore;
using Xunit;
public class SecureStoreConfigurationProviderTests : IDisposable
{
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>
{
{"foo1", "bar1"},
{"foo2", "bar2"},
{"foo3", "bar3"}
};
public SecureStoreConfigurationProviderTests()
{
_storePath = Path.GetTempFileName();
}
public void Dispose()
{
File.Delete(_storePath);
}
[Fact]
public void LoadStreamUsingKeyFile()
{
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);
}
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 = EmbeddedKeyName,
Optional = true
});
using (var stream = new FileStream(_storePath, FileMode.Open, FileAccess.Read))
{
provider.Load(stream);
}
Assert.All(SecureData, item => Assert.Equal(provider.Get(item.Key), item.Value));
}
[Fact]
public void LoadStreamUsingPassword()
{
CreateTestStore(_storePath, Password, KeyType.Password);
var provider = new SecureStoreConfigurationProvider(new SecureStoreConfigurationSource
{
KeyType = KeyType.Password,
Key = Password,
Optional = true
});
using (var stream = new FileStream(_storePath, FileMode.Open, FileAccess.Read))
{
provider.Load(stream);
}
Assert.All(SecureData, item => Assert.Equal(provider.Get(item.Key), item.Value));
}
[Fact]
public void LoadStreamUsingPassword_ThrowsIfKeyTypeNotInRange()
{
CreateTestStore(_storePath, Password, KeyType.Password);
var source = new SecureStoreConfigurationSource
{
KeyType = (KeyType)3,
Key = Password,
Optional = true
};
var provider = new SecureStoreConfigurationProvider(source);
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]);
}
sman.SaveStore(storePath);
}
}
}