aboutsummaryrefslogtreecommitdiff
path: root/lib/VNLib.Plugins.Extensions.Loading/src/Secrets
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2024-05-15 22:04:43 -0400
committerLibravatar vnugent <public@vaughnnugent.com>2024-05-15 22:04:43 -0400
commit69f13e43dfdd8069459800ccc3039f45fc884814 (patch)
tree90363ca9fa7c89e635393465b62e4e8cbe5e6779 /lib/VNLib.Plugins.Extensions.Loading/src/Secrets
parentc848787d4830a73e9ba93898897282be2f3752f2 (diff)
fix: #3 Defer vault loading until a secret actually needs it
Diffstat (limited to 'lib/VNLib.Plugins.Extensions.Loading/src/Secrets')
-rw-r--r--lib/VNLib.Plugins.Extensions.Loading/src/Secrets/OnDemandSecret.cs15
-rw-r--r--lib/VNLib.Plugins.Extensions.Loading/src/Secrets/PluginSecretStore.cs4
2 files changed, 13 insertions, 6 deletions
diff --git a/lib/VNLib.Plugins.Extensions.Loading/src/Secrets/OnDemandSecret.cs b/lib/VNLib.Plugins.Extensions.Loading/src/Secrets/OnDemandSecret.cs
index 17f3523..edbef8c 100644
--- a/lib/VNLib.Plugins.Extensions.Loading/src/Secrets/OnDemandSecret.cs
+++ b/lib/VNLib.Plugins.Extensions.Loading/src/Secrets/OnDemandSecret.cs
@@ -35,13 +35,20 @@ using System.Collections.Generic;
using VNLib.Utils.Memory;
using VNLib.Utils.Logging;
using VNLib.Utils.Extensions;
+using VNLib.Utils.Resources;
using static VNLib.Plugins.Extensions.Loading.PluginSecretConstants;
namespace VNLib.Plugins.Extensions.Loading
{
- internal sealed class OnDemandSecret(PluginBase plugin, string secretName, IKvVaultClient? vault) : IOnDemandSecret
+ internal sealed class OnDemandSecret(PluginBase plugin, string secretName, Func<IKvVaultClient?> vaultCb) : IOnDemandSecret
{
+ /*
+ * Defer loading vault until needed by a vault secret. This avoids loading the vault client
+ * if no secrets are needed from the vault.
+ */
+ private readonly LazyInitializer<IKvVaultClient?> vault = new(vaultCb);
+
public string SecretName { get; } = secretName ?? throw new ArgumentNullException(nameof(secretName));
///<inheritdoc/>
@@ -175,16 +182,16 @@ namespace VNLib.Plugins.Extensions.Loading
string secret = path[(lastSep + 1)..].ToString();
//Try load client
- _ = vault ?? throw new KeyNotFoundException("Vault client not found");
+ _ = vault.Instance ?? throw new KeyNotFoundException("Vault client not found");
if (async)
{
- Task<ISecretResult?> asTask = Task.Run(() => vault.ReadSecretAsync(secret, mount, secretTableKey));
+ Task<ISecretResult?> asTask = Task.Run(() => vault.Instance.ReadSecretAsync(secret, mount, secretTableKey));
return new ValueTask<ISecretResult?>(asTask);
}
else
{
- ISecretResult? result = vault.ReadSecret(secret, mount, secretTableKey);
+ ISecretResult? result = vault.Instance.ReadSecret(secret, mount, secretTableKey);
return new ValueTask<ISecretResult?>(result);
}
}
diff --git a/lib/VNLib.Plugins.Extensions.Loading/src/Secrets/PluginSecretStore.cs b/lib/VNLib.Plugins.Extensions.Loading/src/Secrets/PluginSecretStore.cs
index 1d366b0..759bd12 100644
--- a/lib/VNLib.Plugins.Extensions.Loading/src/Secrets/PluginSecretStore.cs
+++ b/lib/VNLib.Plugins.Extensions.Loading/src/Secrets/PluginSecretStore.cs
@@ -46,7 +46,7 @@ namespace VNLib.Plugins.Extensions.Loading
/// Gets the ambient vault client for the current plugin
/// if the configuration is loaded, null otherwise
/// </summary>
- /// <returns>The ambient <see cref="IVaultClient"/> if loaded, null otherwise</returns>
+ /// <returns>The ambient <see cref="IKvVaultClient"/> if loaded, null otherwise</returns>
/// <exception cref="KeyNotFoundException"></exception>
/// <exception cref="ObjectDisposedException"></exception>
public IKvVaultClient? GetVaultClient() => LoadingExtensions.GetOrCreateSingleton(_plugin, TryGetVaultLoader);
@@ -114,7 +114,7 @@ namespace VNLib.Plugins.Extensions.Loading
public IOnDemandSecret GetOnDemandSecret(string secretName)
{
ArgumentException.ThrowIfNullOrWhiteSpace(secretName);
- return new OnDemandSecret(_plugin, secretName, GetVaultClient());
+ return new OnDemandSecret(_plugin, secretName, GetVaultClient);
}
///<inheritdoc/>