3
3
using System . Linq ;
4
4
using System . Threading . Tasks ;
5
5
using Arcus . Security . Core . Caching . Configuration ;
6
- using GuardNet ;
7
6
using Microsoft . Extensions . Caching . Memory ;
8
7
9
8
namespace Arcus . Security . Core . Caching
@@ -29,16 +28,15 @@ public class CachedSecretProvider : ICachedSecretProvider, IVersionedSecretProvi
29
28
/// <exception cref="ArgumentOutOfRangeException">Thrown when the <see cref="ICacheConfiguration.Duration"/> is not a positive time duration.</exception>
30
29
public CachedSecretProvider ( ISecretProvider secretProvider , ICacheConfiguration cacheConfiguration , IMemoryCache memoryCache )
31
30
{
32
- Guard . NotNull ( secretProvider , nameof ( secretProvider ) , "Requires a secret provider instance to include caching while retrieving secrets" ) ;
33
- Guard . NotNull ( memoryCache , nameof ( memoryCache ) , "Requires a memory caching implementation to include caching while retrieving secrets" ) ;
34
- Guard . NotNull ( cacheConfiguration , nameof ( cacheConfiguration ) , "Requires a configuration instance to influence the caching during the retrieval of secrets" ) ;
35
- Guard . NotLessThan ( cacheConfiguration . Duration , TimeSpan . Zero , nameof ( cacheConfiguration ) ,
36
- "Requires a positive time duration in the cache configuration in which the caching should take place" ) ;
37
-
38
- _secretProvider = secretProvider ;
39
- _cacheConfiguration = cacheConfiguration ;
40
-
41
- MemoryCache = memoryCache ;
31
+ _secretProvider = secretProvider ?? throw new ArgumentNullException ( nameof ( secretProvider ) ) ;
32
+ _cacheConfiguration = cacheConfiguration ?? throw new ArgumentNullException ( nameof ( cacheConfiguration ) ) ;
33
+
34
+ if ( _cacheConfiguration . Duration < TimeSpan . Zero )
35
+ {
36
+ throw new ArgumentOutOfRangeException ( nameof ( cacheConfiguration ) , cacheConfiguration . Duration , "Requires a positive time duration in the cache configuration in which the caching should take place" ) ;
37
+ }
38
+
39
+ MemoryCache = memoryCache ?? throw new ArgumentNullException ( nameof ( memoryCache ) ) ;
42
40
43
41
CacheEntry = new MemoryCacheEntryOptions ( )
44
42
// Keep in cache for this time, reset time if accessed.
@@ -93,8 +91,11 @@ public CachedSecretProvider(ISecretProvider secretProvider) :
93
91
/// <exception cref="SecretNotFoundException">The secret was not found, using the given name</exception>
94
92
public Task < string > GetRawSecretAsync ( string secretName )
95
93
{
96
- Guard . NotNullOrWhitespace ( secretName , nameof ( secretName ) , "Requires a non-blank secret name to look up the secret" ) ;
97
-
94
+ if ( string . IsNullOrWhiteSpace ( secretName ) )
95
+ {
96
+ throw new ArgumentException ( "Requires a non-blank secret name to look up the secret" , nameof ( secretName ) ) ;
97
+ }
98
+
98
99
return GetRawSecretAsync ( secretName , ignoreCache : false ) ;
99
100
}
100
101
@@ -108,7 +109,10 @@ public Task<string> GetRawSecretAsync(string secretName)
108
109
/// <exception cref="SecretNotFoundException">The secret was not found, using the given name</exception>
109
110
public Task < Secret > GetSecretAsync ( string secretName )
110
111
{
111
- Guard . NotNullOrWhitespace ( secretName , nameof ( secretName ) , "Requires a non-blank secret name to look up the secret" ) ;
112
+ if ( string . IsNullOrWhiteSpace ( secretName ) )
113
+ {
114
+ throw new ArgumentException ( "Requires a non-blank secret name to look up the secret" , nameof ( secretName ) ) ;
115
+ }
112
116
113
117
return GetSecretAsync ( secretName , ignoreCache : false ) ;
114
118
}
@@ -124,7 +128,10 @@ public Task<Secret> GetSecretAsync(string secretName)
124
128
/// <exception cref="SecretNotFoundException">The secret was not found, using the given name</exception>
125
129
public async Task < string > GetRawSecretAsync ( string secretName , bool ignoreCache )
126
130
{
127
- Guard . NotNullOrWhitespace ( secretName , nameof ( secretName ) , "Requires a non-blank secret name to look up the secret" ) ;
131
+ if ( string . IsNullOrWhiteSpace ( secretName ) )
132
+ {
133
+ throw new ArgumentException ( "Requires a non-blank secret name to look up the secret" , nameof ( secretName ) ) ;
134
+ }
128
135
129
136
Secret secret = await GetSecretAsync ( secretName , ignoreCache ) ;
130
137
return secret ? . Value ;
@@ -141,7 +148,10 @@ public async Task<string> GetRawSecretAsync(string secretName, bool ignoreCache)
141
148
/// <exception cref="SecretNotFoundException">The secret was not found, using the given name</exception>
142
149
public async Task < Secret > GetSecretAsync ( string secretName , bool ignoreCache )
143
150
{
144
- Guard . NotNullOrWhitespace ( secretName , nameof ( secretName ) , "Requires a non-blank secret name to look up the secret" ) ;
151
+ if ( string . IsNullOrWhiteSpace ( secretName ) )
152
+ {
153
+ throw new ArgumentException ( "Requires a non-blank secret name to look up the secret" , nameof ( secretName ) ) ;
154
+ }
145
155
146
156
if ( TryGetValueFromCache ( secretName , ignoreCache , out Secret [ ] cachedSecret ) )
147
157
{
@@ -177,8 +187,15 @@ private bool TryGetValueFromCache(string secretName, bool ignoreCache, out Secre
177
187
/// <exception cref="SecretNotFoundException">Thrown when no secret was not found, using the given <paramref name="secretName"/>.</exception>
178
188
public async Task < IEnumerable < string > > GetRawSecretsAsync ( string secretName , int amountOfVersions )
179
189
{
180
- Guard . NotNullOrWhitespace ( secretName , nameof ( secretName ) , "Requires a non-blank secret name to look up the versioned secrets" ) ;
181
- Guard . NotLessThan ( amountOfVersions , 1 , nameof ( amountOfVersions ) , "Requires at least 1 secret version to look up the versioned secrets" ) ;
190
+ if ( string . IsNullOrWhiteSpace ( secretName ) )
191
+ {
192
+ throw new ArgumentException ( "Requires a non-blank secret name to look up the secret" , nameof ( secretName ) ) ;
193
+ }
194
+
195
+ if ( amountOfVersions < 1 )
196
+ {
197
+ throw new ArgumentOutOfRangeException ( nameof ( amountOfVersions ) , amountOfVersions , "Requires at least 1 secret version to look up the versioned secrets" ) ;
198
+ }
182
199
183
200
IEnumerable < Secret > secrets = await GetSecretsAsync ( secretName , amountOfVersions ) ;
184
201
return secrets ? . Select ( secret => secret ? . Value ) . ToArray ( ) ;
@@ -194,8 +211,15 @@ public async Task<IEnumerable<string>> GetRawSecretsAsync(string secretName, int
194
211
/// <exception cref="SecretNotFoundException">Thrown when no secret was not found, using the given <paramref name="secretName"/>.</exception>
195
212
public async Task < IEnumerable < Secret > > GetSecretsAsync ( string secretName , int amountOfVersions )
196
213
{
197
- Guard . NotNullOrWhitespace ( secretName , nameof ( secretName ) , "Requires a non-blank secret name to look up the versioned secrets" ) ;
198
- Guard . NotLessThan ( amountOfVersions , 1 , nameof ( amountOfVersions ) , "Requires at least 1 secret version to look up the versioned secrets" ) ;
214
+ if ( string . IsNullOrWhiteSpace ( secretName ) )
215
+ {
216
+ throw new ArgumentException ( "Requires a non-blank secret name to look up the secret" , nameof ( secretName ) ) ;
217
+ }
218
+
219
+ if ( amountOfVersions < 1 )
220
+ {
221
+ throw new ArgumentOutOfRangeException ( nameof ( amountOfVersions ) , amountOfVersions , "Requires at least 1 secret version to look up the versioned secrets" ) ;
222
+ }
199
223
200
224
if ( _secretProvider is IVersionedSecretProvider versionProvider )
201
225
{
@@ -224,7 +248,10 @@ public async Task<IEnumerable<Secret>> GetSecretsAsync(string secretName, int am
224
248
/// <param name="secretName">The name of the secret that should be removed from the cache.</param>
225
249
public Task InvalidateSecretAsync ( string secretName )
226
250
{
227
- Guard . NotNullOrWhitespace ( secretName , nameof ( secretName ) , "Requires a non-blank secret name to invalidate the cached secret" ) ;
251
+ if ( string . IsNullOrWhiteSpace ( secretName ) )
252
+ {
253
+ throw new ArgumentException ( "Requires a non-blank secret name to invalidate the secret" , nameof ( secretName ) ) ;
254
+ }
228
255
229
256
MemoryCache . Remove ( secretName ) ;
230
257
return Task . CompletedTask ;
@@ -239,7 +266,10 @@ public Task InvalidateSecretAsync(string secretName)
239
266
/// <exception cref="SecretNotFoundException">Thrown when the secret was not found, using the given name.</exception>
240
267
public string GetRawSecret ( string secretName )
241
268
{
242
- Guard . NotNullOrWhitespace ( secretName , nameof ( secretName ) , "Requires a non-blank secret name to look up the secret" ) ;
269
+ if ( string . IsNullOrWhiteSpace ( secretName ) )
270
+ {
271
+ throw new ArgumentException ( "Requires a non-blank secret name to look up the secret" , nameof ( secretName ) ) ;
272
+ }
243
273
244
274
Secret secret = GetSecret ( secretName ) ;
245
275
return secret ? . Value ;
@@ -254,7 +284,10 @@ public string GetRawSecret(string secretName)
254
284
/// <exception cref="SecretNotFoundException">Thrown when the secret was not found, using the given name.</exception>
255
285
public Secret GetSecret ( string secretName )
256
286
{
257
- Guard . NotNullOrWhitespace ( secretName , nameof ( secretName ) , "Requires a non-blank secret name to look up the secret" ) ;
287
+ if ( string . IsNullOrWhiteSpace ( secretName ) )
288
+ {
289
+ throw new ArgumentException ( "Requires a non-blank secret name to look up the secret" , nameof ( secretName ) ) ;
290
+ }
258
291
259
292
if ( TryGetValueFromCache ( secretName , ignoreCache : false , out Secret [ ] cachedSecrets ) )
260
293
{
0 commit comments