Skip to content

Commit 3b7e738

Browse files
authored
chore: remove guard.net from core and keyvault secret provider (#435)
* chore: remove guard.net from core and keyvault secret provider * pr-fix: fix remaining expected exception types
1 parent e0b4f97 commit 3b7e738

15 files changed

+359
-279
lines changed

src/Arcus.Security.Core/Arcus.Security.Core.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
<ItemGroup>
2828
<PackageReference Include="Arcus.Observability.Telemetry.Core" Version="[3.0.0,4.0.0)" />
29-
<PackageReference Include="Guard.Net" Version="3.0.0" />
3029
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="6.0.0" />
3130
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />
3231
</ItemGroup>

src/Arcus.Security.Core/Caching/CachedSecretProvider.cs

+56-23
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Linq;
44
using System.Threading.Tasks;
55
using Arcus.Security.Core.Caching.Configuration;
6-
using GuardNet;
76
using Microsoft.Extensions.Caching.Memory;
87

98
namespace Arcus.Security.Core.Caching
@@ -29,16 +28,15 @@ public class CachedSecretProvider : ICachedSecretProvider, IVersionedSecretProvi
2928
/// <exception cref="ArgumentOutOfRangeException">Thrown when the <see cref="ICacheConfiguration.Duration"/> is not a positive time duration.</exception>
3029
public CachedSecretProvider(ISecretProvider secretProvider, ICacheConfiguration cacheConfiguration, IMemoryCache memoryCache)
3130
{
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));
4240

4341
CacheEntry = new MemoryCacheEntryOptions()
4442
// Keep in cache for this time, reset time if accessed.
@@ -93,8 +91,11 @@ public CachedSecretProvider(ISecretProvider secretProvider) :
9391
/// <exception cref="SecretNotFoundException">The secret was not found, using the given name</exception>
9492
public Task<string> GetRawSecretAsync(string secretName)
9593
{
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+
9899
return GetRawSecretAsync(secretName, ignoreCache: false);
99100
}
100101

@@ -108,7 +109,10 @@ public Task<string> GetRawSecretAsync(string secretName)
108109
/// <exception cref="SecretNotFoundException">The secret was not found, using the given name</exception>
109110
public Task<Secret> GetSecretAsync(string secretName)
110111
{
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+
}
112116

113117
return GetSecretAsync(secretName, ignoreCache: false);
114118
}
@@ -124,7 +128,10 @@ public Task<Secret> GetSecretAsync(string secretName)
124128
/// <exception cref="SecretNotFoundException">The secret was not found, using the given name</exception>
125129
public async Task<string> GetRawSecretAsync(string secretName, bool ignoreCache)
126130
{
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+
}
128135

129136
Secret secret = await GetSecretAsync(secretName, ignoreCache);
130137
return secret?.Value;
@@ -141,7 +148,10 @@ public async Task<string> GetRawSecretAsync(string secretName, bool ignoreCache)
141148
/// <exception cref="SecretNotFoundException">The secret was not found, using the given name</exception>
142149
public async Task<Secret> GetSecretAsync(string secretName, bool ignoreCache)
143150
{
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+
}
145155

146156
if (TryGetValueFromCache(secretName, ignoreCache, out Secret[] cachedSecret))
147157
{
@@ -177,8 +187,15 @@ private bool TryGetValueFromCache(string secretName, bool ignoreCache, out Secre
177187
/// <exception cref="SecretNotFoundException">Thrown when no secret was not found, using the given <paramref name="secretName"/>.</exception>
178188
public async Task<IEnumerable<string>> GetRawSecretsAsync(string secretName, int amountOfVersions)
179189
{
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+
}
182199

183200
IEnumerable<Secret> secrets = await GetSecretsAsync(secretName, amountOfVersions);
184201
return secrets?.Select(secret => secret?.Value).ToArray();
@@ -194,8 +211,15 @@ public async Task<IEnumerable<string>> GetRawSecretsAsync(string secretName, int
194211
/// <exception cref="SecretNotFoundException">Thrown when no secret was not found, using the given <paramref name="secretName"/>.</exception>
195212
public async Task<IEnumerable<Secret>> GetSecretsAsync(string secretName, int amountOfVersions)
196213
{
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+
}
199223

200224
if (_secretProvider is IVersionedSecretProvider versionProvider)
201225
{
@@ -224,7 +248,10 @@ public async Task<IEnumerable<Secret>> GetSecretsAsync(string secretName, int am
224248
/// <param name="secretName">The name of the secret that should be removed from the cache.</param>
225249
public Task InvalidateSecretAsync(string secretName)
226250
{
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+
}
228255

229256
MemoryCache.Remove(secretName);
230257
return Task.CompletedTask;
@@ -239,7 +266,10 @@ public Task InvalidateSecretAsync(string secretName)
239266
/// <exception cref="SecretNotFoundException">Thrown when the secret was not found, using the given name.</exception>
240267
public string GetRawSecret(string secretName)
241268
{
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+
}
243273

244274
Secret secret = GetSecret(secretName);
245275
return secret?.Value;
@@ -254,7 +284,10 @@ public string GetRawSecret(string secretName)
254284
/// <exception cref="SecretNotFoundException">Thrown when the secret was not found, using the given name.</exception>
255285
public Secret GetSecret(string secretName)
256286
{
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+
}
258291

259292
if (TryGetValueFromCache(secretName, ignoreCache: false, out Secret[] cachedSecrets))
260293
{

0 commit comments

Comments
 (0)