Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Removing all dependency to Guard.NET. #437

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
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
@@ -1,6 +1,5 @@
using System;
using Arcus.Security.Core;
using GuardNet;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand All @@ -22,8 +21,15 @@ public static class IFunctionHostBuilderExtensions
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="functionsHostBuilder"/> or <paramref name="configureSecretStores"/> is <c>null</c>.</exception>
public static IFunctionsHostBuilder ConfigureSecretStore(this IFunctionsHostBuilder functionsHostBuilder, Action<SecretStoreBuilder> configureSecretStores)
{
Guard.NotNull(functionsHostBuilder, nameof(functionsHostBuilder), "Requires a functions host builder to add the secret store");
Guard.NotNull(configureSecretStores, nameof(configureSecretStores), "Requires a function to configure the secret store with potential secret providers");
if (functionsHostBuilder is null)
{
throw new ArgumentNullException(nameof(functionsHostBuilder), "Requires a functions host builder to add the secret store");
}

if (configureSecretStores is null)
{
throw new ArgumentNullException(nameof(configureSecretStores), "Requires a function to configure the secret store with potential secret providers");
}

functionsHostBuilder.Services.AddSecretStore(configureSecretStores);
return functionsHostBuilder;
Expand All @@ -39,8 +45,15 @@ public static IFunctionsHostBuilder ConfigureSecretStore(
this IFunctionsHostBuilder functionsHostBuilder,
Action<FunctionsHostBuilderContext, IConfiguration, SecretStoreBuilder> configureSecretStores)
{
Guard.NotNull(functionsHostBuilder, nameof(functionsHostBuilder), "Requires a functions host builder to add the secret store");
Guard.NotNull(configureSecretStores, nameof(configureSecretStores), "Requires a function to configure the secret store with potential secret providers");
if (functionsHostBuilder is null)
{
throw new ArgumentNullException(nameof(functionsHostBuilder), "Requires a functions host builder to add the secret store");
}

if (configureSecretStores is null)
{
throw new ArgumentNullException(nameof(configureSecretStores), "Requires a function to configure the secret store with potential secret providers");
}

FunctionsHostBuilderContext context = functionsHostBuilder.GetContext();
functionsHostBuilder.Services.AddSecretStore(stores => configureSecretStores(context, context.Configuration, stores));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using GuardNet;

namespace Arcus.Security.Core.Caching.Configuration
{
Expand All @@ -15,7 +14,11 @@ public class CacheConfiguration : ICacheConfiguration
/// <exception cref="ArgumentOutOfRangeException">Thrown when the cache duration is not a positive time duration.</exception>
public CacheConfiguration(TimeSpan duration)
{
Guard.NotLessThan(duration, TimeSpan.Zero, nameof(duration), "Requires a positive time duration in which the caching should take place");
if (duration < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(duration), duration, "Requires a positive time duration in which the caching should take place");
}

Duration = duration;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using Arcus.Security.Core.Caching.Configuration;
using GuardNet;
using Microsoft.Extensions.Caching.Memory;

namespace Arcus.Security.Core.Caching
Expand All @@ -21,9 +20,20 @@ public static class SecretProviderCachingExtensions
/// <exception cref="ArgumentOutOfRangeException">Thrown when the <paramref name="cachingDuration"/> is not a positive time duration.</exception>
public static ICachedSecretProvider WithCaching(this ISecretProvider secretProvider, TimeSpan cachingDuration, IMemoryCache memoryCache)
{
Guard.NotNull(secretProvider, nameof(secretProvider), "Requires a secret provider instance to include caching while retrieving secrets");
Guard.NotLessThan(cachingDuration, TimeSpan.Zero, nameof(cachingDuration), "Requires a positive time duration in which the caching should take place");
Guard.NotNull(memoryCache, nameof(memoryCache), "Requires a memory caching implementation to include caching while retrieving secrets");
if (secretProvider is null)
{
throw new ArgumentNullException(nameof(secretProvider), "Requires a secret provider instance to include caching while retrieving secrets");
}

if (cachingDuration < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(cachingDuration), "Requires a positive time duration in which the caching should take place");
}

if (memoryCache is null)
{
throw new ArgumentNullException(nameof(memoryCache), "Requires a memory caching implementation to include caching while retrieving secrets");
}

return new CachedSecretProvider(secretProvider, new CacheConfiguration(cachingDuration), memoryCache);
}
Expand All @@ -39,8 +49,15 @@ public static ICachedSecretProvider WithCaching(this ISecretProvider secretProvi
/// <exception cref="ArgumentOutOfRangeException">Thrown when the <paramref name="cachingDuration"/> is not a positive time duration.</exception>
public static ICachedSecretProvider WithCaching(this ISecretProvider secretProvider, TimeSpan cachingDuration)
{
Guard.NotNull(secretProvider, nameof(secretProvider), "Requires a secret provider instance to include caching while retrieving secrets");
Guard.NotLessThan(cachingDuration, TimeSpan.Zero, nameof(cachingDuration), "Requires a positive time duration in which the caching should take place");
if (secretProvider is null)
{
throw new ArgumentNullException(nameof(secretProvider), "Requires a secret provider instance to include caching while retrieving secrets");
}

if (cachingDuration < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(cachingDuration), cachingDuration, "Requires a positive time duration in which the caching should take place");
}

return new CachedSecretProvider(secretProvider, new CacheConfiguration(cachingDuration));
}
Expand All @@ -54,7 +71,10 @@ public static ICachedSecretProvider WithCaching(this ISecretProvider secretProvi
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="secretProvider"/> is <c>null</c>.</exception>
public static ICachedSecretProvider WithCaching(this ISecretProvider secretProvider)
{
Guard.NotNull(secretProvider, nameof(secretProvider), "Requires a secret provider instance to include caching while retrieving secrets");
if (secretProvider is null)
{
throw new ArgumentNullException(nameof(secretProvider), "Requires a secret provider instance to include caching while retrieving secrets");
}

return new CachedSecretProvider(secretProvider);
}
Expand Down
23 changes: 18 additions & 5 deletions src/Arcus.Security.Core/Extensions/IHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Arcus.Security.Core;
using GuardNet;
using Microsoft.Extensions.Configuration;
using System;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -21,8 +20,15 @@ public static class IHostBuilderExtensions
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="hostBuilder"/> or <paramref name="configureSecretStores"/> is <c>null</c>.</exception>
public static IHostBuilder ConfigureSecretStore(this IHostBuilder hostBuilder, Action<IConfiguration, SecretStoreBuilder> configureSecretStores)
{
Guard.NotNull(hostBuilder, nameof(hostBuilder), "Requires a host builder to add the secret store");
Guard.NotNull(configureSecretStores, nameof(configureSecretStores), "Requires a function to register the secret providers in the secret store");
if (hostBuilder is null)
{
throw new ArgumentNullException(nameof(hostBuilder), "Requires a host builder to add the secret store");
}

if (configureSecretStores is null)
{
throw new ArgumentNullException(nameof(configureSecretStores), "Requires a function to register the secret providers in the secret store");
}

return ConfigureSecretStore(hostBuilder, (context, config, secretStores) => configureSecretStores(config, secretStores));
}
Expand All @@ -35,8 +41,15 @@ public static IHostBuilder ConfigureSecretStore(this IHostBuilder hostBuilder, A
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="hostBuilder"/> or <paramref name="configureSecretStores"/> is <c>null</c>.</exception>
public static IHostBuilder ConfigureSecretStore(this IHostBuilder hostBuilder, Action<HostBuilderContext, IConfiguration, SecretStoreBuilder> configureSecretStores)
{
Guard.NotNull(hostBuilder, nameof(hostBuilder), "Requires a host builder to add the secret store");
Guard.NotNull(configureSecretStores, nameof(configureSecretStores), "Requires a function to register the secret providers in the secret store");
if (hostBuilder is null)
{
throw new ArgumentNullException(nameof(hostBuilder), "Requires a host builder to add the secret store");
}

if (configureSecretStores is null)
{
throw new ArgumentNullException(nameof(configureSecretStores), "Requires a function to register the secret providers in the secret store");
}

return hostBuilder.ConfigureServices((context, services) =>
{
Expand Down
33 changes: 26 additions & 7 deletions src/Arcus.Security.Core/Extensions/ISecretProviderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GuardNet;

// ReSharper disable once CheckNamespace
namespace Arcus.Security.Core
Expand All @@ -23,8 +22,15 @@ public static class ISecretProviderExtensions
/// <exception cref="SecretNotFoundException">Thrown when the secret was not found, using the given name.</exception>
public static string GetRawSecret(this ISecretProvider secretProvider, string secretName)
{
Guard.NotNull(secretProvider, nameof(secretProvider), "Requires a secret provider to synchronously look up the secret");
Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name to look up the secret");
if (secretProvider is null)
{
throw new ArgumentNullException(nameof(secretProvider), "Requires a secret provider to synchronously look up the secret");
}

if (string.IsNullOrWhiteSpace(secretName))
{
throw new ArgumentException("Requires a non-blank secret name to look up the secret", nameof(secretName));
}

if (secretProvider is ISyncSecretProvider composite)
{
Expand All @@ -46,8 +52,15 @@ public static string GetRawSecret(this ISecretProvider secretProvider, string se
/// <exception cref="SecretNotFoundException">Thrown when the secret was not found, using the given name.</exception>
public static Secret GetSecret(this ISecretProvider secretProvider, string secretName)
{
Guard.NotNull(secretProvider, nameof(secretProvider), "Requires a secret provider to synchronously look up the secret");
Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name to look up the secret");
if (secretProvider is null)
{
throw new ArgumentNullException(nameof(secretProvider
}

if (string.IsNullOrWhiteSpace(secretName))
{
throw new ArgumentException("Requires a non-blank secret name to look up the secret", nameof(secretName));
}

if (secretProvider is ISyncSecretProvider composite)
{
Expand All @@ -73,7 +86,10 @@ public static Secret GetSecret(this ISecretProvider secretProvider, string secre
/// <exception cref="SecretNotFoundException">The secret was not found, using the given name</exception>
public static async Task<IEnumerable<string>> GetRawSecretsAsync(this ISecretProvider secretProvider, string secretName)
{
Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name to look up the secret");
if (string.IsNullOrWhiteSpace(secretName))
{
throw new ArgumentException("Requires a non-blank secret name to look up the secret", nameof(secretName));
}

if (secretProvider is CompositeSecretProvider composite)
{
Expand All @@ -99,7 +115,10 @@ public static async Task<IEnumerable<string>> GetRawSecretsAsync(this ISecretPro
/// <exception cref="SecretNotFoundException">The secret was not found, using the given name</exception>
public static async Task<IEnumerable<Secret>> GetSecretsAsync(this ISecretProvider secretProvider, string secretName)
{
Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name to look up the secret");
if (string.IsNullOrWhiteSpace(secretName))
{
throw new ArgumentException("Requires a non-blank secret name to look up the secret", nameof(secretName));
}

if (secretProvider is CompositeSecretProvider composite)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using Arcus.Security.Core;
using GuardNet;
using Microsoft.Extensions.Hosting;

// ReSharper disable once CheckNamespace
Expand All @@ -20,8 +19,15 @@ public static class IServiceCollectionExtensions
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="services"/> or <paramref name="configureSecretStores"/> is <c>null</c>.</exception>
public static IServiceCollection AddSecretStore(this IServiceCollection services, Action<SecretStoreBuilder> configureSecretStores)
{
Guard.NotNull(services, nameof(services), "Requires a set of services to add the secret store");
Guard.NotNull(configureSecretStores, nameof(configureSecretStores), "Requires a function to register the secret providers in the secret store");
if (services is null)
{
throw new ArgumentNullException(nameof(services), "Requires a set of services to add the secret store");
}

if (configureSecretStores is null)
{
throw new ArgumentNullException(nameof(configureSecretStores), "Requires a function to register the secret providers in the secret store");
}

var builder = new SecretStoreBuilder(services);
configureSecretStores(builder);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using Arcus.Security.Core.Providers;
using GuardNet;
using Microsoft.Extensions.Configuration;

// ReSharper disable once CheckNamespace
Expand All @@ -25,13 +24,7 @@ public static SecretStoreBuilder AddEnvironmentVariables(
EnvironmentVariableTarget target = EnvironmentVariableSecretProvider.DefaultTarget,
string prefix = null,
Func<string, string> mutateSecretName = null)
{
Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the environment secrets");
Guard.For<ArgumentException>(() => !Enum.IsDefined(typeof(EnvironmentVariableTarget), target),
$"Requires an environment variable target of either '{EnvironmentVariableTarget.Process}', '{EnvironmentVariableTarget.Machine}', or '{EnvironmentVariableTarget.User}'");

return AddEnvironmentVariables(builder, target, prefix, name: null, mutateSecretName: mutateSecretName);
}
=> AddEnvironmentVariables(builder, target, prefix, name: null, mutateSecretName: mutateSecretName);

/// <summary>
/// Adds a secret source to the secret store of the application that gets its secrets from the environment.
Expand All @@ -50,9 +43,15 @@ public static SecretStoreBuilder AddEnvironmentVariables(
string name,
Func<string, string> mutateSecretName)
{
Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the environment secrets");
Guard.For<ArgumentException>(() => !Enum.IsDefined(typeof(EnvironmentVariableTarget), target),
$"Requires an environment variable target of either '{EnvironmentVariableTarget.Process}', '{EnvironmentVariableTarget.Machine}', or '{EnvironmentVariableTarget.User}'");
if (builder is null)
{
throw new ArgumentNullException(nameof(builder), "Requires a secret store builder to add the environment secrets");
}

if (!Enum.IsDefined(typeof(EnvironmentVariableTarget), target))
{
throw new ArgumentException($"Requires an environment variable target of either '{EnvironmentVariableTarget.Process}', '{EnvironmentVariableTarget.Machine}', or '{EnvironmentVariableTarget.User}'");
}

return builder.AddProvider(new EnvironmentVariableSecretProvider(target, prefix), options =>
{
Expand All @@ -72,12 +71,7 @@ public static SecretStoreBuilder AddConfiguration(
this SecretStoreBuilder builder,
IConfiguration configuration,
Func<string, string> mutateSecretName = null)
{
Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the configuration secrets");
Guard.NotNull(configuration, nameof(configuration), "Requires a configuration instance to retrieve the secrets from");

return AddConfiguration(builder, configuration, name: null, mutateSecretName: mutateSecretName);
}
=> AddConfiguration(builder, configuration, name: null, mutateSecretName: mutateSecretName);

/// <summary>
/// Adds a secret source to the secret store of the application that gets its secrets from the <see cref="IConfiguration"/>.
Expand All @@ -93,8 +87,15 @@ public static SecretStoreBuilder AddConfiguration(
string name,
Func<string, string> mutateSecretName)
{
Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the configuration secrets");
Guard.NotNull(configuration, nameof(configuration), "Requires a configuration instance to retrieve the secrets from");
if (builder is null)
{
throw new ArgumentNullException(nameof(builder), "Requires a secret store builder to add the configuration secrets");
}

if (configuration is null)
{
throw new ArgumentNullException(nameof(configuration), "Requires a configuration instance to retrieve the secrets from");
}

return builder.AddProvider(new ConfigurationSecretProvider(configuration), options =>
{
Expand Down
Loading
Loading