Skip to content

Commit e90aeb0

Browse files
authored
chore: remove guard.net from other secret providers (#436)
* chore: remove guard.net from other secret providers * pr-fix: correct missing arg exception checks
1 parent 3b7e738 commit e90aeb0

File tree

11 files changed

+175
-148
lines changed

11 files changed

+175
-148
lines changed

src/Arcus.Security.Providers.CommandLine/CommandLineSecretProvider.cs

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Threading.Tasks;
33
using Arcus.Security.Core;
4-
using GuardNet;
54
using Microsoft.Extensions.Configuration;
65
using Microsoft.Extensions.Configuration.CommandLine;
76

@@ -21,8 +20,7 @@ public class CommandLineSecretProvider : ISyncSecretProvider
2120
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="configurationProvider"/> is <c>null</c>.</exception>
2221
public CommandLineSecretProvider(CommandLineConfigurationProvider configurationProvider)
2322
{
24-
Guard.NotNull(configurationProvider, nameof(configurationProvider), "Requires a command line configuration provider instance to load the command arguments as secrets");
25-
_configurationProvider = configurationProvider;
23+
_configurationProvider = configurationProvider ?? throw new ArgumentNullException(nameof(configurationProvider));
2624
}
2725

2826
/// <summary>
@@ -33,8 +31,6 @@ public CommandLineSecretProvider(CommandLineConfigurationProvider configurationP
3331
/// <exception cref="ArgumentException">Thrown when the <paramref name="secretName"/> is blank.</exception>
3432
public Task<Secret> GetSecretAsync(string secretName)
3533
{
36-
Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name to look up the command line argument secret");
37-
3834
Secret secret = GetSecret(secretName);
3935
return Task.FromResult(secret);
4036
}
@@ -47,8 +43,6 @@ public Task<Secret> GetSecretAsync(string secretName)
4743
/// <exception cref="ArgumentException">Thrown when the <paramref name="secretName"/> is blank.</exception>
4844
public Task<string> GetRawSecretAsync(string secretName)
4945
{
50-
Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name to look up the command line argument secret");
51-
5246
string rawSecret = GetRawSecret(secretName);
5347
return Task.FromResult(rawSecret);
5448
}
@@ -62,8 +56,6 @@ public Task<string> GetRawSecretAsync(string secretName)
6256
/// <exception cref="SecretNotFoundException">Thrown when the secret was not found, using the given name.</exception>
6357
public Secret GetSecret(string secretName)
6458
{
65-
Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name to look up the command line argument secret");
66-
6759
string secretValue = GetRawSecret(secretName);
6860
if (secretValue is null)
6961
{
@@ -82,7 +74,10 @@ public Secret GetSecret(string secretName)
8274
/// <exception cref="SecretNotFoundException">Thrown when the secret was not found, using the given name.</exception>
8375
public string GetRawSecret(string secretName)
8476
{
85-
Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name to look up the command line argument secret");
77+
if (string.IsNullOrWhiteSpace(secretName))
78+
{
79+
throw new ArgumentException("Requires a non-blank secret name to look up the command line argument secret", nameof(secretName));
80+
}
8681

8782
if (_configurationProvider.TryGet(secretName, out string secretValue))
8883
{

src/Arcus.Security.Providers.CommandLine/Extensions/SecretStoreBuilderExtensions.cs

+10-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using Arcus.Security.Providers.CommandLine;
3-
using GuardNet;
43
using Microsoft.Extensions.Configuration.CommandLine;
54

65
// ReSharper disable once CheckNamespace
@@ -19,9 +18,6 @@ public static class SecretStoreBuilderExtensions
1918
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="builder"/> or <paramref name="arguments"/> is <c>null</c>.</exception>
2019
public static SecretStoreBuilder AddCommandLine(this SecretStoreBuilder builder, string[] arguments)
2120
{
22-
Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the command line arguments as secrets to the secret store");
23-
Guard.NotNull(arguments, nameof(arguments), "Requires a set of command line arguments to be set as secret in the secret store");
24-
2521
return AddCommandLine(builder, arguments, name: null);
2622
}
2723

@@ -34,9 +30,6 @@ public static SecretStoreBuilder AddCommandLine(this SecretStoreBuilder builder,
3430
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="builder"/> or <paramref name="arguments"/> is <c>null</c>.</exception>
3531
public static SecretStoreBuilder AddCommandLine(this SecretStoreBuilder builder, string[] arguments, string name)
3632
{
37-
Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the command line arguments as secrets to the secret store");
38-
Guard.NotNull(arguments, nameof(arguments), "Requires a set of command line arguments to be set as secret in the secret store");
39-
4033
return AddCommandLine(builder, arguments, name, mutateSecretName: null);
4134
}
4235

@@ -49,9 +42,6 @@ public static SecretStoreBuilder AddCommandLine(this SecretStoreBuilder builder,
4942
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="builder"/> or <paramref name="arguments"/> is <c>null</c>.</exception>
5043
public static SecretStoreBuilder AddCommandLine(this SecretStoreBuilder builder, string[] arguments, Func<string, string> mutateSecretName)
5144
{
52-
Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the command line arguments as secrets to the secret store");
53-
Guard.NotNull(arguments, nameof(arguments), "Requires a set of command line arguments to be set as secret in the secret store");
54-
5545
return AddCommandLine(builder, arguments, name: null, mutateSecretName: mutateSecretName);
5646
}
5747

@@ -65,9 +55,16 @@ public static SecretStoreBuilder AddCommandLine(this SecretStoreBuilder builder,
6555
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="builder"/> or <paramref name="arguments"/> is <c>null</c>.</exception>
6656
public static SecretStoreBuilder AddCommandLine(this SecretStoreBuilder builder, string[] arguments, string name, Func<string, string> mutateSecretName)
6757
{
68-
Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the command line arguments as secrets to the secret store");
69-
Guard.NotNull(arguments, nameof(arguments), "Requires a set of command line arguments to be set as secret in the secret store");
70-
58+
if (builder is null)
59+
{
60+
throw new ArgumentNullException(nameof(builder));
61+
}
62+
63+
if (arguments is null)
64+
{
65+
throw new ArgumentNullException(nameof(arguments));
66+
}
67+
7168
var configProvider = new CommandLineConfigurationProvider(arguments);
7269
configProvider.Load();
7370

src/Arcus.Security.Providers.DockerSecrets/DockerSecretsSecretProvider.cs

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Arcus.Security.Core;
2-
using GuardNet;
32
using Microsoft.Extensions.Configuration.KeyPerFile;
43
using System;
54
using System.IO;
@@ -23,9 +22,15 @@ public class DockerSecretsSecretProvider : ISyncSecretProvider
2322
/// <exception cref="DirectoryNotFoundException">Thrown when the <paramref name="secretsDirectoryPath"/> is not found on the system.</exception>
2423
public DockerSecretsSecretProvider(string secretsDirectoryPath)
2524
{
26-
Guard.NotNullOrWhitespace(secretsDirectoryPath, nameof(secretsDirectoryPath), "Requires a directory path inside the Docker container where the secrets are located");
27-
Guard.For(() => !Path.IsPathRooted(secretsDirectoryPath),
28-
new ArgumentException("Requires an absolute directory path inside the Docker container to located the secrets", nameof(secretsDirectoryPath)));
25+
if (string.IsNullOrWhiteSpace(secretsDirectoryPath))
26+
{
27+
throw new ArgumentException("Requires a directory path inside the Docker container where the secrets are located", nameof(secretsDirectoryPath));
28+
}
29+
30+
if (!Path.IsPathRooted(secretsDirectoryPath))
31+
{
32+
throw new ArgumentException("Requires an absolute directory path inside the Docker container to located the secrets", nameof(secretsDirectoryPath));
33+
}
2934

3035
if (!Directory.Exists(secretsDirectoryPath))
3136
{
@@ -54,8 +59,6 @@ public DockerSecretsSecretProvider(string secretsDirectoryPath)
5459
/// <exception cref="SecretNotFoundException">The secret was not found, using the given name</exception>
5560
public Task<Secret> GetSecretAsync(string secretName)
5661
{
57-
Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name to retrieve a Docker secret");
58-
5962
Secret secret = GetSecret(secretName);
6063
return Task.FromResult(secret);
6164
}
@@ -70,8 +73,6 @@ public Task<Secret> GetSecretAsync(string secretName)
7073
/// <exception cref="SecretNotFoundException">The secret was not found, using the given name</exception>
7174
public Task<string> GetRawSecretAsync(string secretName)
7275
{
73-
Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name to retrieve a Docker secret");
74-
7576
string secretValue = GetRawSecret(secretName);
7677
return Task.FromResult(secretValue);
7778
}
@@ -85,8 +86,6 @@ public Task<string> GetRawSecretAsync(string secretName)
8586
/// <exception cref="SecretNotFoundException">Thrown when the secret was not found, using the given name.</exception>
8687
public Secret GetSecret(string secretName)
8788
{
88-
Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name to retrieve a Docker secret");
89-
9089
string secretValue = GetRawSecret(secretName);
9190
if (secretValue is null)
9291
{
@@ -105,7 +104,10 @@ public Secret GetSecret(string secretName)
105104
/// <exception cref="SecretNotFoundException">Thrown when the secret was not found, using the given name.</exception>
106105
public string GetRawSecret(string secretName)
107106
{
108-
Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Requires a non-blank secret name to retrieve a Docker secret");
107+
if (string.IsNullOrWhiteSpace(secretName))
108+
{
109+
throw new ArgumentException("Requires a non-blank secret name to retrieve a Docker secret", nameof(secretName));
110+
}
109111

110112
if (_provider.TryGet(secretName, out string value))
111113
{

src/Arcus.Security.Providers.DockerSecrets/Extensions/SecretStoreBuilderExtensions.cs

+14-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.IO;
33
using Arcus.Security.Providers.DockerSecrets;
4-
using GuardNet;
54
using Microsoft.Extensions.Configuration.KeyPerFile;
65
using Microsoft.Extensions.FileProviders;
76

@@ -23,16 +22,6 @@ public static class SecretStoreBuilderExtensions
2322
/// <exception cref="ArgumentException">Throw when the <paramref name="directoryPath"/> is blank or is not an absolute path.</exception>
2423
public static SecretStoreBuilder AddDockerSecrets(this SecretStoreBuilder builder, string directoryPath, Func<string, string> mutateSecretName = null)
2524
{
26-
Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the Docker secrets to");
27-
Guard.NotNullOrWhitespace(directoryPath, nameof(directoryPath), "Requires a non-blank directory path inside the Docker container to locate the secrets");
28-
Guard.For(() => !Path.IsPathRooted(directoryPath),
29-
new ArgumentException("Requires an absolute directory path inside the Docker container to located the secrets", nameof(directoryPath)));
30-
31-
if (!Directory.Exists(directoryPath))
32-
{
33-
throw new DirectoryNotFoundException($"The directory {directoryPath} which is configured as secretsDirectoryPath does not exist.");
34-
}
35-
3625
return AddDockerSecrets(builder, directoryPath, name: null, mutateSecretName: mutateSecretName);
3726
}
3827

@@ -52,10 +41,20 @@ public static SecretStoreBuilder AddDockerSecrets(
5241
string name,
5342
Func<string, string> mutateSecretName)
5443
{
55-
Guard.NotNull(builder, nameof(builder), "Requires a secret store builder to add the Docker secrets to");
56-
Guard.NotNullOrWhitespace(directoryPath, nameof(directoryPath), "Requires a non-blank directory path inside the Docker container to locate the secrets");
57-
Guard.For(() => !Path.IsPathRooted(directoryPath),
58-
new ArgumentException("Requires an absolute directory path inside the Docker container to located the secrets", nameof(directoryPath)));
44+
if (builder is null)
45+
{
46+
throw new ArgumentNullException(nameof(builder));
47+
}
48+
49+
if (string.IsNullOrWhiteSpace(directoryPath))
50+
{
51+
throw new ArgumentException("Requires a non-blank directory path inside the Docker container to locate the secrets", nameof(directoryPath));
52+
}
53+
54+
if (!Path.IsPathRooted(directoryPath))
55+
{
56+
throw new ArgumentException("Requires an absolute directory path inside the Docker container to located the secrets", nameof(directoryPath));
57+
}
5958

6059
if (!Directory.Exists(directoryPath))
6160
{

src/Arcus.Security.Providers.HashiCorp/Configuration/HashiCorpVaultKubernetesOptions.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using GuardNet;
1+
using System;
22
using VaultSharp.V1.AuthMethods;
33

44
namespace Arcus.Security.Providers.HashiCorp.Configuration
@@ -19,7 +19,11 @@ public string KubernetesMountPoint
1919
get => _kubernetesMountPoint;
2020
set
2121
{
22-
Guard.NotNullOrWhitespace(value, nameof(value), "Requires a non-blank mount point for the Kubernetes authentication");
22+
if (string.IsNullOrWhiteSpace(value))
23+
{
24+
throw new ArgumentException("Requires a non-blank mount point for the Kubernetes authentication", nameof(value));
25+
}
26+
2327
_kubernetesMountPoint = value;
2428
}
2529
}

src/Arcus.Security.Providers.HashiCorp/Configuration/HashiCorpVaultOptions.cs

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using GuardNet;
32
using VaultSharp.V1.SecretsEngines;
43

54
namespace Arcus.Security.Providers.HashiCorp.Configuration
@@ -21,7 +20,11 @@ public string KeyValueMountPoint
2120
get => _keyValueMountPoint;
2221
set
2322
{
24-
Guard.NotNullOrWhitespace(value, nameof(value), "Requires a non-blank point where the KeyVault secret engine is mounted");
23+
if (string.IsNullOrWhiteSpace(value))
24+
{
25+
throw new ArgumentException("Requires a non-blank point where the KeyVault secret engine is mounted", nameof(value));
26+
}
27+
2528
_keyValueMountPoint = value;
2629
}
2730
}
@@ -35,7 +38,11 @@ public VaultKeyValueSecretEngineVersion KeyValueVersion
3538
get => _engineVersion;
3639
set
3740
{
38-
Guard.For<ArgumentOutOfRangeException>(() => !Enum.IsDefined(typeof(VaultKeyValueSecretEngineVersion), value), "Requires the client API version to be either V1 or V2");
41+
if (!Enum.IsDefined(typeof(VaultKeyValueSecretEngineVersion), value))
42+
{
43+
throw new ArgumentException("Requires the client API version to be either V1 or V2", nameof(value));
44+
}
45+
3946
_engineVersion = value;
4047
}
4148
}

src/Arcus.Security.Providers.HashiCorp/Configuration/HashiCorpVaultUserPassOptions.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using GuardNet;
1+
using System;
22
using VaultSharp.V1.AuthMethods;
33

44
namespace Arcus.Security.Providers.HashiCorp.Configuration
@@ -19,7 +19,11 @@ public string UserPassMountPoint
1919
get => _userPassMountPoint;
2020
set
2121
{
22-
Guard.NotNullOrWhitespace(value, nameof(value), "Requires a non-blank mount point for the UserPass authentication");
22+
if (string.IsNullOrWhiteSpace(value))
23+
{
24+
throw new ArgumentException("Requires a non-blank mount point for the UserPass authentication", nameof(value));
25+
}
26+
2327
_userPassMountPoint = value;
2428
}
2529
}

0 commit comments

Comments
 (0)