Skip to content

Commit

Permalink
chore: remove guard.net from other secret providers
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnmoreels committed Sep 27, 2024
1 parent 3b7e738 commit 4271c5b
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 148 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -21,8 +20,7 @@ public class CommandLineSecretProvider : ISyncSecretProvider
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="configurationProvider"/> is <c>null</c>.</exception>
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));
}

/// <summary>
Expand All @@ -33,8 +31,6 @@ public CommandLineSecretProvider(CommandLineConfigurationProvider configurationP
/// <exception cref="ArgumentException">Thrown when the <paramref name="secretName"/> is blank.</exception>
public Task<Secret> 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);
}
Expand All @@ -47,8 +43,6 @@ public Task<Secret> GetSecretAsync(string secretName)
/// <exception cref="ArgumentException">Thrown when the <paramref name="secretName"/> is blank.</exception>
public Task<string> 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);
}
Expand All @@ -62,8 +56,6 @@ public Task<string> GetRawSecretAsync(string secretName)
/// <exception cref="SecretNotFoundException">Thrown when the secret was not found, using the given name.</exception>
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)
{
Expand All @@ -82,7 +74,10 @@ public Secret GetSecret(string secretName)
/// <exception cref="SecretNotFoundException">Thrown when the secret was not found, using the given name.</exception>
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))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using Arcus.Security.Providers.CommandLine;
using GuardNet;
using Microsoft.Extensions.Configuration.CommandLine;

// ReSharper disable once CheckNamespace
Expand All @@ -19,9 +18,6 @@ public static class SecretStoreBuilderExtensions
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="builder"/> or <paramref name="arguments"/> is <c>null</c>.</exception>
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);
}

Expand All @@ -34,9 +30,6 @@ public static SecretStoreBuilder AddCommandLine(this SecretStoreBuilder builder,
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="builder"/> or <paramref name="arguments"/> is <c>null</c>.</exception>
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);
}

Expand All @@ -49,9 +42,6 @@ public static SecretStoreBuilder AddCommandLine(this SecretStoreBuilder builder,
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="builder"/> or <paramref name="arguments"/> is <c>null</c>.</exception>
public static SecretStoreBuilder AddCommandLine(this SecretStoreBuilder builder, string[] arguments, Func<string, string> 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);
}

Expand All @@ -65,9 +55,16 @@ public static SecretStoreBuilder AddCommandLine(this SecretStoreBuilder builder,
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="builder"/> or <paramref name="arguments"/> is <c>null</c>.</exception>
public static SecretStoreBuilder AddCommandLine(this SecretStoreBuilder builder, string[] arguments, string name, Func<string, string> 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();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Arcus.Security.Core;
using GuardNet;
using Microsoft.Extensions.Configuration.KeyPerFile;
using System;
using System.IO;
Expand All @@ -23,9 +22,15 @@ public class DockerSecretsSecretProvider : ISyncSecretProvider
/// <exception cref="DirectoryNotFoundException">Thrown when the <paramref name="secretsDirectoryPath"/> is not found on the system.</exception>
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))
{
Expand Down Expand Up @@ -54,8 +59,6 @@ public DockerSecretsSecretProvider(string secretsDirectoryPath)
/// <exception cref="SecretNotFoundException">The secret was not found, using the given name</exception>
public Task<Secret> 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);
}
Expand All @@ -70,8 +73,6 @@ public Task<Secret> GetSecretAsync(string secretName)
/// <exception cref="SecretNotFoundException">The secret was not found, using the given name</exception>
public Task<string> 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);
}
Expand All @@ -85,8 +86,6 @@ public Task<string> GetRawSecretAsync(string secretName)
/// <exception cref="SecretNotFoundException">Thrown when the secret was not found, using the given name.</exception>
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)
{
Expand All @@ -105,7 +104,10 @@ public Secret GetSecret(string secretName)
/// <exception cref="SecretNotFoundException">Thrown when the secret was not found, using the given name.</exception>
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))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -23,16 +22,6 @@ public static class SecretStoreBuilderExtensions
/// <exception cref="ArgumentException">Throw when the <paramref name="directoryPath"/> is blank or is not an absolute path.</exception>
public static SecretStoreBuilder AddDockerSecrets(this SecretStoreBuilder builder, string directoryPath, Func<string, string> 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);
}

Expand All @@ -52,10 +41,20 @@ public static SecretStoreBuilder AddDockerSecrets(
string name,
Func<string, string> 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))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using GuardNet;
using System;
using VaultSharp.V1.AuthMethods;

namespace Arcus.Security.Providers.HashiCorp.Configuration
Expand All @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using GuardNet;
using VaultSharp.V1.SecretsEngines;

namespace Arcus.Security.Providers.HashiCorp.Configuration
Expand All @@ -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;
}
}
Expand All @@ -35,7 +38,11 @@ public VaultKeyValueSecretEngineVersion KeyValueVersion
get => _engineVersion;
set
{
Guard.For<ArgumentOutOfRangeException>(() => !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;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using GuardNet;
using System;
using VaultSharp.V1.AuthMethods;

namespace Arcus.Security.Providers.HashiCorp.Configuration
Expand All @@ -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;
}
}
Expand Down
Loading

0 comments on commit 4271c5b

Please sign in to comment.