From 7a263bf54b7967ddeb9f6b662339ec1c74546ce8 Mon Sep 17 00:00:00 2001 From: vnugent Date: Sat, 9 Mar 2024 14:19:31 -0500 Subject: refactor: Overhaul secret loading. Remove VaultSharp as a dep --- .../src/Secrets/SecretResult.cs | 45 ++++++++++++++-------- 1 file changed, 28 insertions(+), 17 deletions(-) (limited to 'lib/VNLib.Plugins.Extensions.Loading/src/Secrets/SecretResult.cs') diff --git a/lib/VNLib.Plugins.Extensions.Loading/src/Secrets/SecretResult.cs b/lib/VNLib.Plugins.Extensions.Loading/src/Secrets/SecretResult.cs index 2c231b7..23f2276 100644 --- a/lib/VNLib.Plugins.Extensions.Loading/src/Secrets/SecretResult.cs +++ b/lib/VNLib.Plugins.Extensions.Loading/src/Secrets/SecretResult.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Plugins.Extensions.Loading @@ -25,7 +25,6 @@ using System; using VNLib.Utils; -using VNLib.Utils.Extensions; using VNLib.Utils.Memory; namespace VNLib.Plugins.Extensions.Loading @@ -41,29 +40,41 @@ namespace VNLib.Plugins.Extensions.Loading /// public ReadOnlySpan Result => _secretChars; - - internal SecretResult(ReadOnlySpan value) : this(value.ToArray()) - { } - - private SecretResult(char[] secretChars) - { - _secretChars = secretChars; - } - + private SecretResult(char[] secretChars) => _secretChars = secretChars; /// - protected override void Free() - { - MemoryUtil.InitializeBlock(_secretChars); - } + protected override void Free() => MemoryUtil.InitializeBlock(_secretChars); + /// + /// Copies the data from the provided string into a new secret result + /// then erases the original string + /// + /// The secret string to read + /// The wrapper internal static SecretResult ToSecret(string? result) { - SecretResult res = new(result.AsSpan()); - MemoryUtil.UnsafeZeroMemory(result); + if (result == null) + { + return new SecretResult([]); + } + + //Copy string data into a new char array + SecretResult res = new(result.ToCharArray()); + + //PrivateStringManager will safely erase the original string if it is able to + PrivateStringManager.EraseString(result); + return res; } + /// + /// Copies the data from the provided span into a new secret result + /// by allocating a new array internally + /// + /// The array of characters to copy + /// The wrapped secret + internal static SecretResult ToSecret(ReadOnlySpan secretChars) => new(secretChars.ToArray()); + internal static SecretResult ToSecret(char[] result) => new(result); } } -- cgit