diff --git a/src/Arcus.Security.Providers.CommandLine/CommandLineSecretProvider.cs b/src/Arcus.Security.Providers.CommandLine/CommandLineSecretProvider.cs index 1c6f3d4d..957398b4 100644 --- a/src/Arcus.Security.Providers.CommandLine/CommandLineSecretProvider.cs +++ b/src/Arcus.Security.Providers.CommandLine/CommandLineSecretProvider.cs @@ -1,7 +1,6 @@ using System; using System.Threading.Tasks; using Arcus.Security.Core; -using GuardNet; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration.CommandLine; @@ -21,8 +20,7 @@ public class CommandLineSecretProvider : ISyncSecretProvider /// Thrown when the is null. public CommandLineSecretProvider(CommandLineConfigurationProvider configurationProvider) { - Guard.NotNull(configurationProvider, nameof(configurationProvider), "Requires a command line configuration provider instance to load the command arguments as secrets"); - _configurationProvider = configurationProvider; + _configurationProvider = configurationProvider ?? throw new ArgumentNullException(nameof(configurationProvider)); } /// @@ -33,8 +31,6 @@ public CommandLineSecretProvider(CommandLineConfigurationProvider configurationP /// Thrown when the is blank. public Task GetSecretAsync(string secretName) { - Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name to look up the command line argument secret"); - Secret secret = GetSecret(secretName); return Task.FromResult(secret); } @@ -47,8 +43,6 @@ public Task GetSecretAsync(string secretName) /// Thrown when the is blank. public Task GetRawSecretAsync(string secretName) { - Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name to look up the command line argument secret"); - string rawSecret = GetRawSecret(secretName); return Task.FromResult(rawSecret); } @@ -62,8 +56,6 @@ public Task GetRawSecretAsync(string secretName) /// Thrown when the secret was not found, using the given name. public Secret GetSecret(string secretName) { - Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name to look up the command line argument secret"); - string secretValue = GetRawSecret(secretName); if (secretValue is null) { @@ -82,7 +74,10 @@ public Secret GetSecret(string secretName) /// Thrown when the secret was not found, using the given name. public string GetRawSecret(string secretName) { - Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name to look up the command line argument secret"); + if (string.IsNullOrWhiteSpace(secretName)) + { + throw new ArgumentException("Requires a non-blank secret name to look up the command line argument secret", nameof(secretName)); + } if (_configurationProvider.TryGet(secretName, out string secretValue)) { diff --git a/src/Arcus.Security.Providers.CommandLine/Extensions/SecretStoreBuilderExtensions.cs b/src/Arcus.Security.Providers.CommandLine/Extensions/SecretStoreBuilderExtensions.cs index 3d8a9c2e..da15fb0d 100644 --- a/src/Arcus.Security.Providers.CommandLine/Extensions/SecretStoreBuilderExtensions.cs +++ b/src/Arcus.Security.Providers.CommandLine/Extensions/SecretStoreBuilderExtensions.cs @@ -1,6 +1,5 @@ using System; using Arcus.Security.Providers.CommandLine; -using GuardNet; using Microsoft.Extensions.Configuration.CommandLine; // ReSharper disable once CheckNamespace @@ -19,9 +18,6 @@ public static class SecretStoreBuilderExtensions /// Thrown when the or is null. public static SecretStoreBuilder AddCommandLine(this SecretStoreBuilder builder, string[] arguments) { - Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the command line arguments as secrets to the secret store"); - Guard.NotNull(arguments, nameof(arguments), "Requires a set of command line arguments to be set as secret in the secret store"); - return AddCommandLine(builder, arguments, name: null); } @@ -34,9 +30,6 @@ public static SecretStoreBuilder AddCommandLine(this SecretStoreBuilder builder, /// Thrown when the or is null. public static SecretStoreBuilder AddCommandLine(this SecretStoreBuilder builder, string[] arguments, string name) { - Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the command line arguments as secrets to the secret store"); - Guard.NotNull(arguments, nameof(arguments), "Requires a set of command line arguments to be set as secret in the secret store"); - return AddCommandLine(builder, arguments, name, mutateSecretName: null); } @@ -49,9 +42,6 @@ public static SecretStoreBuilder AddCommandLine(this SecretStoreBuilder builder, /// Thrown when the or is null. public static SecretStoreBuilder AddCommandLine(this SecretStoreBuilder builder, string[] arguments, Func mutateSecretName) { - Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the command line arguments as secrets to the secret store"); - Guard.NotNull(arguments, nameof(arguments), "Requires a set of command line arguments to be set as secret in the secret store"); - return AddCommandLine(builder, arguments, name: null, mutateSecretName: mutateSecretName); } @@ -65,9 +55,16 @@ public static SecretStoreBuilder AddCommandLine(this SecretStoreBuilder builder, /// Thrown when the or is null. public static SecretStoreBuilder AddCommandLine(this SecretStoreBuilder builder, string[] arguments, string name, Func mutateSecretName) { - Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the command line arguments as secrets to the secret store"); - Guard.NotNull(arguments, nameof(arguments), "Requires a set of command line arguments to be set as secret in the secret store"); - + if (builder is null) + { + throw new ArgumentNullException(nameof(builder)); + } + + if (arguments is null) + { + throw new ArgumentNullException(nameof(arguments)); + } + var configProvider = new CommandLineConfigurationProvider(arguments); configProvider.Load(); diff --git a/src/Arcus.Security.Providers.DockerSecrets/DockerSecretsSecretProvider.cs b/src/Arcus.Security.Providers.DockerSecrets/DockerSecretsSecretProvider.cs index a25cc585..640e794e 100644 --- a/src/Arcus.Security.Providers.DockerSecrets/DockerSecretsSecretProvider.cs +++ b/src/Arcus.Security.Providers.DockerSecrets/DockerSecretsSecretProvider.cs @@ -1,5 +1,4 @@ using Arcus.Security.Core; -using GuardNet; using Microsoft.Extensions.Configuration.KeyPerFile; using System; using System.IO; @@ -23,9 +22,15 @@ public class DockerSecretsSecretProvider : ISyncSecretProvider /// Thrown when the is not found on the system. public DockerSecretsSecretProvider(string secretsDirectoryPath) { - Guard.NotNullOrWhitespace(secretsDirectoryPath, nameof(secretsDirectoryPath), "Requires a directory path inside the Docker container where the secrets are located"); - Guard.For(() => !Path.IsPathRooted(secretsDirectoryPath), - new ArgumentException("Requires an absolute directory path inside the Docker container to located the secrets", nameof(secretsDirectoryPath))); + if (string.IsNullOrWhiteSpace(secretsDirectoryPath)) + { + throw new ArgumentException("Requires a directory path inside the Docker container where the secrets are located", nameof(secretsDirectoryPath)); + } + + if (!Path.IsPathRooted(secretsDirectoryPath)) + { + throw new ArgumentException("Requires an absolute directory path inside the Docker container to located the secrets", nameof(secretsDirectoryPath)); + } if (!Directory.Exists(secretsDirectoryPath)) { @@ -54,8 +59,6 @@ public DockerSecretsSecretProvider(string secretsDirectoryPath) /// The secret was not found, using the given name public Task GetSecretAsync(string secretName) { - Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name to retrieve a Docker secret"); - Secret secret = GetSecret(secretName); return Task.FromResult(secret); } @@ -70,8 +73,6 @@ public Task GetSecretAsync(string secretName) /// The secret was not found, using the given name public Task GetRawSecretAsync(string secretName) { - Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name to retrieve a Docker secret"); - string secretValue = GetRawSecret(secretName); return Task.FromResult(secretValue); } @@ -85,8 +86,6 @@ public Task GetRawSecretAsync(string secretName) /// Thrown when the secret was not found, using the given name. public Secret GetSecret(string secretName) { - Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name to retrieve a Docker secret"); - string secretValue = GetRawSecret(secretName); if (secretValue is null) { @@ -105,7 +104,10 @@ public Secret GetSecret(string secretName) /// Thrown when the secret was not found, using the given name. public string GetRawSecret(string secretName) { - Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name to retrieve a Docker secret"); + if (string.IsNullOrWhiteSpace(secretName)) + { + throw new ArgumentException("Requires a non-blank secret name to retrieve a Docker secret", nameof(secretName)); + } if (_provider.TryGet(secretName, out string value)) { diff --git a/src/Arcus.Security.Providers.DockerSecrets/Extensions/SecretStoreBuilderExtensions.cs b/src/Arcus.Security.Providers.DockerSecrets/Extensions/SecretStoreBuilderExtensions.cs index 27e6498a..69bbf5ab 100644 --- a/src/Arcus.Security.Providers.DockerSecrets/Extensions/SecretStoreBuilderExtensions.cs +++ b/src/Arcus.Security.Providers.DockerSecrets/Extensions/SecretStoreBuilderExtensions.cs @@ -1,7 +1,6 @@ using System; using System.IO; using Arcus.Security.Providers.DockerSecrets; -using GuardNet; using Microsoft.Extensions.Configuration.KeyPerFile; using Microsoft.Extensions.FileProviders; @@ -23,16 +22,6 @@ public static class SecretStoreBuilderExtensions /// Throw when the is blank or is not an absolute path. public static SecretStoreBuilder AddDockerSecrets(this SecretStoreBuilder builder, string directoryPath, Func mutateSecretName = null) { - Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the Docker secrets to"); - Guard.NotNullOrWhitespace(directoryPath, nameof(directoryPath), "Requires a non-blank directory path inside the Docker container to locate the secrets"); - Guard.For(() => !Path.IsPathRooted(directoryPath), - new ArgumentException("Requires an absolute directory path inside the Docker container to located the secrets", nameof(directoryPath))); - - if (!Directory.Exists(directoryPath)) - { - throw new DirectoryNotFoundException($"The directory {directoryPath} which is configured as secretsDirectoryPath does not exist."); - } - return AddDockerSecrets(builder, directoryPath, name: null, mutateSecretName: mutateSecretName); } @@ -52,10 +41,20 @@ public static SecretStoreBuilder AddDockerSecrets( string name, Func mutateSecretName) { - Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the Docker secrets to"); - Guard.NotNullOrWhitespace(directoryPath, nameof(directoryPath), "Requires a non-blank directory path inside the Docker container to locate the secrets"); - Guard.For(() => !Path.IsPathRooted(directoryPath), - new ArgumentException("Requires an absolute directory path inside the Docker container to located the secrets", nameof(directoryPath))); + if (builder is null) + { + throw new ArgumentNullException(nameof(builder)); + } + + if (string.IsNullOrWhiteSpace(directoryPath)) + { + throw new ArgumentException("Requires a non-blank directory path inside the Docker container to locate the secrets", nameof(directoryPath)); + } + + if (!Path.IsPathRooted(directoryPath)) + { + throw new ArgumentException("Requires an absolute directory path inside the Docker container to located the secrets", nameof(directoryPath)); + } if (!Directory.Exists(directoryPath)) { diff --git a/src/Arcus.Security.Providers.HashiCorp/Configuration/HashiCorpVaultKubernetesOptions.cs b/src/Arcus.Security.Providers.HashiCorp/Configuration/HashiCorpVaultKubernetesOptions.cs index be37c6c0..c148981b 100644 --- a/src/Arcus.Security.Providers.HashiCorp/Configuration/HashiCorpVaultKubernetesOptions.cs +++ b/src/Arcus.Security.Providers.HashiCorp/Configuration/HashiCorpVaultKubernetesOptions.cs @@ -1,4 +1,4 @@ -using GuardNet; +using System; using VaultSharp.V1.AuthMethods; namespace Arcus.Security.Providers.HashiCorp.Configuration @@ -19,7 +19,11 @@ public string KubernetesMountPoint get => _kubernetesMountPoint; set { - Guard.NotNullOrWhitespace(value, nameof(value), "Requires a non-blank mount point for the Kubernetes authentication"); + if (string.IsNullOrWhiteSpace(value)) + { + throw new ArgumentException("Requires a non-blank mount point for the Kubernetes authentication", nameof(value)); + } + _kubernetesMountPoint = value; } } diff --git a/src/Arcus.Security.Providers.HashiCorp/Configuration/HashiCorpVaultOptions.cs b/src/Arcus.Security.Providers.HashiCorp/Configuration/HashiCorpVaultOptions.cs index 901ab18f..95ea8b0b 100644 --- a/src/Arcus.Security.Providers.HashiCorp/Configuration/HashiCorpVaultOptions.cs +++ b/src/Arcus.Security.Providers.HashiCorp/Configuration/HashiCorpVaultOptions.cs @@ -1,5 +1,4 @@ using System; -using GuardNet; using VaultSharp.V1.SecretsEngines; namespace Arcus.Security.Providers.HashiCorp.Configuration @@ -21,7 +20,11 @@ public string KeyValueMountPoint get => _keyValueMountPoint; set { - Guard.NotNullOrWhitespace(value, nameof(value), "Requires a non-blank point where the KeyVault secret engine is mounted"); + if (string.IsNullOrWhiteSpace(value)) + { + throw new ArgumentException("Requires a non-blank point where the KeyVault secret engine is mounted", nameof(value)); + } + _keyValueMountPoint = value; } } @@ -35,7 +38,11 @@ public VaultKeyValueSecretEngineVersion KeyValueVersion get => _engineVersion; set { - Guard.For(() => !Enum.IsDefined(typeof(VaultKeyValueSecretEngineVersion), value), "Requires the client API version to be either V1 or V2"); + if (!Enum.IsDefined(typeof(VaultKeyValueSecretEngineVersion), value)) + { + throw new ArgumentException("Requires the client API version to be either V1 or V2", nameof(value)); + } + _engineVersion = value; } } diff --git a/src/Arcus.Security.Providers.HashiCorp/Configuration/HashiCorpVaultUserPassOptions.cs b/src/Arcus.Security.Providers.HashiCorp/Configuration/HashiCorpVaultUserPassOptions.cs index 55e1488e..317621b1 100644 --- a/src/Arcus.Security.Providers.HashiCorp/Configuration/HashiCorpVaultUserPassOptions.cs +++ b/src/Arcus.Security.Providers.HashiCorp/Configuration/HashiCorpVaultUserPassOptions.cs @@ -1,4 +1,4 @@ -using GuardNet; +using System; using VaultSharp.V1.AuthMethods; namespace Arcus.Security.Providers.HashiCorp.Configuration @@ -19,7 +19,11 @@ public string UserPassMountPoint get => _userPassMountPoint; set { - Guard.NotNullOrWhitespace(value, nameof(value), "Requires a non-blank mount point for the UserPass authentication"); + if (string.IsNullOrWhiteSpace(value)) + { + throw new ArgumentException("Requires a non-blank mount point for the UserPass authentication", nameof(value)); + } + _userPassMountPoint = value; } } diff --git a/src/Arcus.Security.Providers.HashiCorp/Extensions/SecretStoreBuilderExtensions.cs b/src/Arcus.Security.Providers.HashiCorp/Extensions/SecretStoreBuilderExtensions.cs index 70a2e730..7f9cd9e5 100644 --- a/src/Arcus.Security.Providers.HashiCorp/Extensions/SecretStoreBuilderExtensions.cs +++ b/src/Arcus.Security.Providers.HashiCorp/Extensions/SecretStoreBuilderExtensions.cs @@ -2,7 +2,6 @@ using System.Net; using Arcus.Security.Core; using Arcus.Security.Providers.HashiCorp.Configuration; -using GuardNet; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; @@ -45,13 +44,6 @@ public static SecretStoreBuilder AddHashiCorpVaultWithUserPass( string password, string secretPath) { - Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the HashiCorp Vault secret provider"); - Guard.NotNullOrWhitespace(vaultServerUriWithPort, nameof(vaultServerUriWithPort), "Requires a valid HashiCorp Vault URI with HTTP port to connect to the running HashiCorp Vault"); - Guard.NotNullOrWhitespace(username, nameof(username), "Requires a username for the UserPass authentication during connecting with the HashiCorp Vault"); - Guard.NotNullOrWhitespace(password, nameof(password), "Requires a password for the UserPass authentication during connecting with the HashiCorp Vault"); - Guard.NotNullOrWhitespace(secretPath, nameof(secretPath), "Requires a path where the HashiCorp Vault secrets are stored"); - Guard.For(() => !Uri.IsWellFormedUriString(vaultServerUriWithPort, UriKind.RelativeOrAbsolute), "Requires a HashiCorp Vault server URI with HTTP port"); - return AddHashiCorpVaultWithUserPass(builder, vaultServerUriWithPort, username, password, secretPath, configureOptions: null); } @@ -83,13 +75,6 @@ public static SecretStoreBuilder AddHashiCorpVaultWithUserPass( string secretPath, Action configureOptions) { - Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the HashiCorp Vault secret provider"); - Guard.NotNullOrWhitespace(vaultServerUriWithPort, nameof(vaultServerUriWithPort), "Requires a valid HashiCorp Vault URI with HTTP port to connect to the running HashiCorp Vault"); - Guard.NotNullOrWhitespace(username, nameof(username), "Requires a username for the UserPass authentication during connecting with the HashiCorp Vault"); - Guard.NotNullOrWhitespace(password, nameof(password), "Requires a password for the UserPass authentication during connecting with the HashiCorp Vault"); - Guard.NotNullOrWhitespace(secretPath, nameof(secretPath), "Requires a path where the HashiCorp Vault secrets are stored"); - Guard.For(() => !Uri.IsWellFormedUriString(vaultServerUriWithPort, UriKind.RelativeOrAbsolute), "Requires a HashiCorp Vault server URI with HTTP port"); - return AddHashiCorpVaultWithUserPass(builder, vaultServerUriWithPort, username, password, secretPath, configureOptions, name: null, mutateSecretName: null); } @@ -125,12 +110,30 @@ public static SecretStoreBuilder AddHashiCorpVaultWithUserPass( string name, Func mutateSecretName) { - Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the HashiCorp Vault secret provider"); - Guard.NotNullOrWhitespace(vaultServerUriWithPort, nameof(vaultServerUriWithPort), "Requires a valid HashiCorp Vault URI with HTTP port to connect to the running HashiCorp Vault"); - Guard.NotNullOrWhitespace(username, nameof(username), "Requires a username for the UserPass authentication during connecting with the HashiCorp Vault"); - Guard.NotNullOrWhitespace(password, nameof(password), "Requires a password for the UserPass authentication during connecting with the HashiCorp Vault"); - Guard.NotNullOrWhitespace(secretPath, nameof(secretPath), "Requires a path where the HashiCorp Vault secrets are stored"); - Guard.For(() => !Uri.IsWellFormedUriString(vaultServerUriWithPort, UriKind.RelativeOrAbsolute), "Requires a HashiCorp Vault server URI with HTTP port"); + if (string.IsNullOrWhiteSpace(vaultServerUriWithPort)) + { + throw new ArgumentException("Requires a valid HashiCorp Vault URI with HTTP port to connect to the running HashiCorp Vault", nameof(vaultServerUriWithPort)); + } + + if (string.IsNullOrWhiteSpace(username)) + { + throw new ArgumentException("Requires a username for the UserPass authentication during connecting with the HashiCorp Vault", nameof(username)); + } + + if (string.IsNullOrWhiteSpace(password)) + { + throw new ArgumentException("Requires a password for the UserPass authentication during connecting with the HashiCorp Vault", nameof(password)); + } + + if (string.IsNullOrWhiteSpace(secretPath)) + { + throw new ArgumentException("Requires a path where the HashiCorp Vault secrets are stored", nameof(secretPath)); + } + + if (!Uri.IsWellFormedUriString(vaultServerUriWithPort, UriKind.RelativeOrAbsolute)) + { + throw new ArgumentException("Requires a HashiCorp Vault server URI with HTTP port", nameof(vaultServerUriWithPort)); + } var options = new HashiCorpVaultUserPassOptions(); configureOptions?.Invoke(options); @@ -175,12 +178,6 @@ public static SecretStoreBuilder AddHashiCorpVaultWithKubernetes( string jsonWebToken, string secretPath) { - Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the HashiCorp Vault secret provider"); - Guard.NotNullOrWhitespace(vaultServerUriWithPort, nameof(vaultServerUriWithPort), "Requires a valid HashiCorp Vault URI with HTTP port to connect to the running HashiCorp Vault"); - Guard.NotNullOrWhitespace(jsonWebToken, nameof(jsonWebToken), "Requires a valid Json Web Token (JWT) during the Kubernetes authentication procedure"); - Guard.NotNullOrWhitespace(secretPath, nameof(secretPath), "Requires a path where the HashiCorp Vault secrets are stored"); - Guard.For(() => !Uri.IsWellFormedUriString(vaultServerUriWithPort, UriKind.RelativeOrAbsolute), "Requires a HashiCorp Vault server URI with HTTP port"); - return AddHashiCorpVaultWithKubernetes(builder, vaultServerUriWithPort, roleName, jsonWebToken, secretPath, configureOptions: null, name: null, mutateSecretName: null); } @@ -220,11 +217,25 @@ public static SecretStoreBuilder AddHashiCorpVaultWithKubernetes( string name, Func mutateSecretName) { - Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the HashiCorp Vault secret provider"); - Guard.NotNullOrWhitespace(vaultServerUriWithPort, nameof(vaultServerUriWithPort), "Requires a valid HashiCorp Vault URI with HTTP port to connect to the running HashiCorp Vault"); - Guard.NotNullOrWhitespace(jsonWebToken, nameof(jsonWebToken), "Requires a valid Json Web Token (JWT) during the Kubernetes authentication procedure"); - Guard.NotNullOrWhitespace(secretPath, nameof(secretPath), "Requires a path where the HashiCorp Vault secrets are stored"); - Guard.For(() => !Uri.IsWellFormedUriString(vaultServerUriWithPort, UriKind.RelativeOrAbsolute), "Requires a HashiCorp Vault server URI with HTTP port"); + if (string.IsNullOrWhiteSpace(vaultServerUriWithPort)) + { + throw new ArgumentException("Requires a valid HashiCorp Vault URI with HTTP port to connect to the running HashiCorp Vault", nameof(vaultServerUriWithPort)); + } + + if (string.IsNullOrWhiteSpace(jsonWebToken)) + { + throw new ArgumentException("Requires a valid Json Web Token (JWT) during the Kubernetes authentication procedure", nameof(jsonWebToken)); + } + + if (string.IsNullOrWhiteSpace(secretPath)) + { + throw new ArgumentException("Requires a path where the HashiCorp Vault secrets are stored", nameof(secretPath)); + } + + if (!Uri.IsWellFormedUriString(vaultServerUriWithPort, UriKind.RelativeOrAbsolute)) + { + throw new ArgumentException("Requires a HashiCorp Vault server URI with HTTP port", nameof(vaultServerUriWithPort)); + } var options = new HashiCorpVaultKubernetesOptions(); configureOptions?.Invoke(options); @@ -262,13 +273,6 @@ public static SecretStoreBuilder AddHashiCorpVault( VaultClientSettings settings, string secretPath) { - Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the HashiCorp Vault secret provider"); - Guard.NotNull(settings, nameof(settings), "Requires HashiCorp Vault settings to correctly connect to the running HashiCorp Vault"); - Guard.NotNullOrWhitespace(settings.VaultServerUriWithPort, nameof(settings.VaultServerUriWithPort), "Requires a non-blank HashiCorp Vault settings to have a valid URI with HTTP port"); - Guard.NotNull(settings.AuthMethodInfo, nameof(settings.AuthMethodInfo), "Requires the HashiCorp Vault settings to have an authentication method configured"); - Guard.NotNullOrWhitespace(secretPath, nameof(secretPath), "Requires a secret path to look for secret values"); - Guard.For(() => !Uri.IsWellFormedUriString(settings.VaultServerUriWithPort, UriKind.RelativeOrAbsolute), "Requires a HashiCorp Vault server URI with HTTP port"); - return AddHashiCorpVault(builder, settings, secretPath, configureOptions: null, name: null, mutateSecretName: null); } @@ -301,13 +305,6 @@ public static SecretStoreBuilder AddHashiCorpVault( string name, Func mutateSecretName) { - Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the HashiCorp Vault secret provider"); - Guard.NotNull(settings, nameof(settings), "Requires HashiCorp Vault settings to correctly connect to the running HashiCorp Vault"); - Guard.NotNullOrWhitespace(settings.VaultServerUriWithPort, nameof(settings.VaultServerUriWithPort), "Requires a non-blank HashiCorp Vault settings to have a valid URI with HTTP port"); - Guard.NotNull(settings.AuthMethodInfo, nameof(settings.AuthMethodInfo), "Requires the HashiCorp Vault settings to have an authentication method configured"); - Guard.NotNullOrWhitespace(secretPath, nameof(secretPath), "Requires a secret path to look for secret values"); - Guard.For(() => !Uri.IsWellFormedUriString(settings.VaultServerUriWithPort, UriKind.RelativeOrAbsolute), "Requires a HashiCorp Vault server URI with HTTP port"); - var options = new HashiCorpVaultOptions(); configureOptions?.Invoke(options); @@ -335,9 +332,6 @@ public static SecretStoreBuilder AddHashiCorpVault( Func implementationFactory) where TSecretProvider : HashiCorpSecretProvider { - Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the HashiCorp Vault secret provider"); - Guard.NotNull(implementationFactory, nameof(implementationFactory), "Requires a factory function to create a HashiCorp KeyValue Vault secret provider implementation"); - return AddHashiCorpVault(builder, implementationFactory, name: null, mutateSecretName: null); } @@ -362,9 +356,11 @@ public static SecretStoreBuilder AddHashiCorpVault( Func mutateSecretName) where TSecretProvider : HashiCorpSecretProvider { - Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the HashiCorp Vault secret provider"); - Guard.NotNull(implementationFactory, nameof(implementationFactory), "Requires a factory function to create a HashiCorp KeyValue Vault secret provider implementation"); - + if (implementationFactory is null) + { + throw new ArgumentNullException(nameof(implementationFactory)); + } + AddHashiCorpCriticalExceptions(builder); return builder.AddProvider(implementationFactory, options => @@ -381,6 +377,26 @@ private static SecretStoreBuilder AddHashiCorpVault( HashiCorpVaultOptions options, Action configureSecretProviderOptions) { + if (settings is null) + { + throw new ArgumentNullException(nameof(settings)); + } + + if (settings.AuthMethodInfo is null) + { + throw new ArgumentNullException(nameof(settings), "Requires a authentication method to connect to the HashiCorp Vault"); + } + + if (string.IsNullOrWhiteSpace(settings.VaultServerUriWithPort)) + { + throw new ArgumentException("Requires a HashiCorp Vault server URI with HTTP port", nameof(settings)); + } + + if (!Uri.IsWellFormedUriString(settings.VaultServerUriWithPort, UriKind.RelativeOrAbsolute)) + { + throw new ArgumentException("Requires a HashiCorp Vault server URI with HTTP port", nameof(settings)); + } + AddHashiCorpCriticalExceptions(builder); return builder.AddProvider(serviceProvider => diff --git a/src/Arcus.Security.Providers.HashiCorp/HashiCorpSecretProvider.cs b/src/Arcus.Security.Providers.HashiCorp/HashiCorpSecretProvider.cs index 8c5fd642..736f8cfc 100644 --- a/src/Arcus.Security.Providers.HashiCorp/HashiCorpSecretProvider.cs +++ b/src/Arcus.Security.Providers.HashiCorp/HashiCorpSecretProvider.cs @@ -4,7 +4,6 @@ using Arcus.Observability.Telemetry.Core; using Arcus.Security.Core; using Arcus.Security.Providers.HashiCorp.Configuration; -using GuardNet; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using VaultSharp; @@ -46,10 +45,30 @@ public HashiCorpSecretProvider( HashiCorpVaultOptions options, ILogger logger) { - Guard.NotNull(settings, nameof(settings), "Requires HashiCorp settings to successfully connect to the Vault"); - Guard.NotNull(settings.AuthMethodInfo, nameof(settings.AuthMethodInfo), "Requires a authentication method to connect to the HashiCorp Vault"); - Guard.NotNullOrWhitespace(secretPath, nameof(secretPath), "Requires a path where the HashiCorp Vault KeyValue secret engine should look for secrets"); - Guard.For(() => !Uri.IsWellFormedUriString(settings.VaultServerUriWithPort, UriKind.RelativeOrAbsolute), "Requires a HashiCorp Vault server URI with HTTP port"); + if (settings is null) + { + throw new ArgumentNullException(nameof(settings)); + } + + if (settings.AuthMethodInfo is null) + { + throw new ArgumentNullException(nameof(settings), "Requires a authentication method to connect to the HashiCorp Vault"); + } + + if (string.IsNullOrWhiteSpace(secretPath)) + { + throw new ArgumentException("Requires a path where the HashiCorp Vault KeyValue secret engine should look for secrets", nameof(secretPath)); + } + + if (string.IsNullOrWhiteSpace(settings.VaultServerUriWithPort)) + { + throw new ArgumentException("Requires a HashiCorp Vault server URI with HTTP port", nameof(settings)); + } + + if (!Uri.IsWellFormedUriString(settings.VaultServerUriWithPort, UriKind.RelativeOrAbsolute)) + { + throw new ArgumentException("Requires a HashiCorp Vault server URI with HTTP port", nameof(settings)); + } Options = options; SecretPath = secretPath; @@ -85,9 +104,6 @@ public HashiCorpSecretProvider( /// Thrown when the is blank. public virtual async Task GetRawSecretAsync(string secretName) { - Guard.NotNullOrWhitespace(secretName, nameof(secretName), - $"Requires a non-blank secret name to look up the secret in the HashiCorp Vault {Options.KeyValueVersion} KeyValue secret engine"); - Secret secret = await GetSecretAsync(secretName); return secret?.Value; } @@ -100,9 +116,6 @@ public virtual async Task GetRawSecretAsync(string secretName) /// Thrown when the is blank. public virtual async Task GetSecretAsync(string secretName) { - Guard.NotNullOrWhitespace(secretName, nameof(secretName), - $"Requires a non-blank secret name to look up the secret in the HashiCorp Vault {Options.KeyValueVersion} KeyValue secret engine"); - SecretData result = await GetTrackedSecretAsync(secretName); if (result.Data.TryGetValue(secretName, out object value) && value != null) @@ -128,8 +141,10 @@ public virtual async Task GetSecretAsync(string secretName) /// protected async Task GetTrackedSecretAsync(string secretName) { - Guard.NotNullOrWhitespace(secretName, nameof(secretName), - $"Requires a non-blank secret name to look up the secret in the HashiCorp Vault {Options.KeyValueVersion} KeyValue secret engine"); + if (string.IsNullOrWhiteSpace(secretName)) + { + throw new ArgumentException($"Requires a non-blank secret name to look up the secret in the HashiCorp Vault {Options.KeyValueVersion} KeyValue secret engine", nameof(secretName)); + } var context = new Dictionary { diff --git a/src/Arcus.Security.Providers.UserSecrets/Extensions/SecretStoreBuilderExtensions.cs b/src/Arcus.Security.Providers.UserSecrets/Extensions/SecretStoreBuilderExtensions.cs index 9edd5ce8..7cfaf767 100644 --- a/src/Arcus.Security.Providers.UserSecrets/Extensions/SecretStoreBuilderExtensions.cs +++ b/src/Arcus.Security.Providers.UserSecrets/Extensions/SecretStoreBuilderExtensions.cs @@ -3,7 +3,6 @@ using System.Reflection; using Arcus.Security.Core; using Arcus.Security.Providers.UserSecrets; -using GuardNet; using Microsoft.Extensions.Configuration.Json; using Microsoft.Extensions.Configuration.UserSecrets; using Microsoft.Extensions.FileProviders; @@ -31,8 +30,6 @@ public static SecretStoreBuilder AddUserSecrets( this SecretStoreBuilder builder, Func mutateSecretName = null) where T : class { - Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the user secrets"); - return AddUserSecrets(builder, options => options.MutateSecretName = mutateSecretName); } @@ -51,8 +48,6 @@ public static SecretStoreBuilder AddUserSecrets( string name, Func mutateSecretName) where T : class { - Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the user secrets"); - return AddUserSecrets(builder, options => { options.Name = name; @@ -80,9 +75,6 @@ private static SecretStoreBuilder AddUserSecrets( /// Thrown when does not have a valid . public static SecretStoreBuilder AddUserSecrets(this SecretStoreBuilder builder, Assembly assembly, Func mutateSecretName = null) { - Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the user secrets"); - Guard.NotNull(assembly, nameof(assembly), "Requires an assembly to retrieve the user secrets ID which locates the local user secrets"); - return AddUserSecrets(builder, assembly, options => options.MutateSecretName = mutateSecretName); } @@ -103,9 +95,6 @@ public static SecretStoreBuilder AddUserSecrets( string name, Func mutateSecretName) { - Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the user secrets"); - Guard.NotNull(assembly, nameof(assembly), "Requires an assembly to retrieve the user secrets ID which locates the local user secrets"); - return AddUserSecrets(builder, assembly, options => { options.Name = name; @@ -121,6 +110,11 @@ private static SecretStoreBuilder AddUserSecrets(this SecretStoreBuilder builder private static string GetUserSecretsIdFromTypeAssembly(Assembly assembly) { + if (assembly is null) + { + throw new ArgumentNullException(nameof(assembly)); + } + var attribute = assembly.GetCustomAttribute(); if (attribute is null) { @@ -144,9 +138,6 @@ private static string GetUserSecretsIdFromTypeAssembly(Assembly assembly) /// Thrown when the is blank. public static SecretStoreBuilder AddUserSecrets(this SecretStoreBuilder builder, string userSecretsId, Func mutateSecretName = null) { - Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the user secrets"); - Guard.NotNullOrWhitespace(userSecretsId, nameof(userSecretsId), "Requires a non-blank user secrets ID to locate the user secrets"); - return AddUserSecrets(builder, userSecretsId, options => options.MutateSecretName = mutateSecretName); } @@ -166,9 +157,6 @@ public static SecretStoreBuilder AddUserSecrets( string name, Func mutateSecretName) { - Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the user secrets"); - Guard.NotNullOrWhitespace(userSecretsId, nameof(userSecretsId), "Requires a non-blank user secrets ID to locate the user secrets"); - return AddUserSecrets(builder, userSecretsId, options => { options.Name = name; @@ -189,6 +177,11 @@ private static SecretStoreBuilder AddUserSecrets(SecretStoreBuilder builder, str private static string GetUserSecretsDirectoryPath(string usersSecretsId) { + if (string.IsNullOrWhiteSpace(usersSecretsId)) + { + throw new ArgumentException("Requires a non-blank user secret ID to determine the local path of the users secrets", nameof(usersSecretsId)); + } + string secretPath = PathHelper.GetSecretsPathFromSecretsId(usersSecretsId); string directoryPath = Path.GetDirectoryName(secretPath); diff --git a/src/Arcus.Security.Providers.UserSecrets/UserSecretsSecretProvider.cs b/src/Arcus.Security.Providers.UserSecrets/UserSecretsSecretProvider.cs index ddcc8b6f..e958ed2f 100644 --- a/src/Arcus.Security.Providers.UserSecrets/UserSecretsSecretProvider.cs +++ b/src/Arcus.Security.Providers.UserSecrets/UserSecretsSecretProvider.cs @@ -1,7 +1,6 @@ using System; using System.Threading.Tasks; using Arcus.Security.Core; -using GuardNet; using Microsoft.Extensions.Configuration.Json; namespace Arcus.Security.Providers.UserSecrets @@ -20,8 +19,7 @@ public class UserSecretsSecretProvider : ISyncSecretProvider /// Thrown when the is null. public UserSecretsSecretProvider(JsonConfigurationProvider jsonProvider) { - Guard.NotNull(jsonProvider, nameof(jsonProvider), "Requires a JSON configuration instance to provide user secrets"); - _jsonProvider = jsonProvider; + _jsonProvider = jsonProvider ?? throw new ArgumentNullException(nameof(jsonProvider)); } /// @@ -34,8 +32,6 @@ public UserSecretsSecretProvider(JsonConfigurationProvider jsonProvider) /// The secret was not found, using the given name public Task GetSecretAsync(string secretName) { - Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name to look up the user secret value"); - Secret secret = GetSecret(secretName); return Task.FromResult(secret); } @@ -50,8 +46,6 @@ public Task GetSecretAsync(string secretName) /// The secret was not found, using the given name public Task GetRawSecretAsync(string secretName) { - Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name to look up the user secret value"); - string secretValue = GetRawSecret(secretName); return Task.FromResult(secretValue); } @@ -65,8 +59,6 @@ public Task GetRawSecretAsync(string secretName) /// Thrown when the secret was not found, using the given name. public Secret GetSecret(string secretName) { - Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name to look up the user secret value"); - string secretValue = GetRawSecret(secretName); if (secretValue is null) { @@ -85,7 +77,10 @@ public Secret GetSecret(string secretName) /// Thrown when the secret was not found, using the given name. public string GetRawSecret(string secretName) { - Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name to look up the user secret value"); + if (string.IsNullOrWhiteSpace(secretName)) + { + throw new ArgumentException("Requires a non-blank secret name to look up the user secret value", nameof(secretName)); + } if (_jsonProvider.TryGet(secretName, out string value)) {