mirror of https://github.com/dapr/dotnet-sdk.git
Bugfix: Crypto ReadOnlyMemory<byte> decryption times out (#1443)
* Bugfix: Removed use of MemoryMarshal as it wasn't decrypting in-memory byte arrays properly (doesn't impact stream encryption/decryption). Signed-off-by: Whit Waldo <whit.waldo@innovian.net> * Added extension method similar to how the CommunityToolkit.HighPerformance project handles the creation of MemoryStreams without an allocation. Restored the use of MemoryMarshal, but throws an exception if the data cannot be accessed now, instead of hanging as it did in a previous iteration. Tested both paths (string and stream) from example project successfully. Signed-off-by: Whit Waldo <whit.waldo@innovian.net> * Added missing using Signed-off-by: Whit Waldo <whit.waldo@innovian.net> --------- Signed-off-by: Whit Waldo <whit.waldo@innovian.net>
This commit is contained in:
parent
ef54d75f70
commit
676c0d7a7f
|
@ -1670,14 +1670,12 @@ internal class DaprClientGrpc : DaprClient
|
|||
ReadOnlyMemory<byte> plaintextBytes, string keyName, EncryptionOptions encryptionOptions,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (MemoryMarshal.TryGetArray(plaintextBytes, out var plaintextSegment) && plaintextSegment.Array != null)
|
||||
{
|
||||
var encryptionResult = await EncryptAsync(vaultResourceName, new MemoryStream(plaintextSegment.Array),
|
||||
keyName, encryptionOptions,
|
||||
cancellationToken);
|
||||
using var memoryStream = plaintextBytes.CreateMemoryStream(true);
|
||||
|
||||
var encryptionResult =
|
||||
await EncryptAsync(vaultResourceName, memoryStream, keyName, encryptionOptions, cancellationToken);
|
||||
|
||||
var bufferedResult = new ArrayBufferWriter<byte>();
|
||||
|
||||
await foreach (var item in encryptionResult.WithCancellation(cancellationToken))
|
||||
{
|
||||
bufferedResult.Write(item.Span);
|
||||
|
@ -1686,10 +1684,6 @@ internal class DaprClientGrpc : DaprClient
|
|||
return bufferedResult.WrittenMemory;
|
||||
}
|
||||
|
||||
throw new ArgumentException("The input instance doesn't have a valid underlying data store.",
|
||||
nameof(plaintextBytes));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
[Obsolete(
|
||||
"The API is currently not stable as it is in the Alpha stage. This attribute will be removed once it is stable.")]
|
||||
|
@ -1895,10 +1889,10 @@ internal class DaprClientGrpc : DaprClient
|
|||
ReadOnlyMemory<byte> ciphertextBytes, string keyName, DecryptionOptions decryptionOptions,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (MemoryMarshal.TryGetArray(ciphertextBytes, out var ciphertextSegment) && ciphertextSegment.Array != null)
|
||||
{
|
||||
var decryptionResult = await DecryptAsync(vaultResourceName, new MemoryStream(ciphertextSegment.Array),
|
||||
keyName, decryptionOptions, cancellationToken);
|
||||
using var memoryStream = ciphertextBytes.CreateMemoryStream(true);
|
||||
|
||||
var decryptionResult =
|
||||
await DecryptAsync(vaultResourceName, memoryStream, keyName, decryptionOptions, cancellationToken);
|
||||
|
||||
var bufferedResult = new ArrayBufferWriter<byte>();
|
||||
await foreach (var item in decryptionResult.WithCancellation(cancellationToken))
|
||||
|
@ -1909,10 +1903,6 @@ internal class DaprClientGrpc : DaprClient
|
|||
return bufferedResult.WrittenMemory;
|
||||
}
|
||||
|
||||
throw new ArgumentException("The input instance doesn't have a valid underlying data store",
|
||||
nameof(ciphertextBytes));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
[Obsolete(
|
||||
"The API is currently not stable as it is in the Alpha stage. This attribute will be removed once it is stable.")]
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
// ------------------------------------------------------------------------
|
||||
// Copyright 2025 The Dapr Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Dapr.Client;
|
||||
|
||||
internal static class ReadOnlyMemoryExtensions
|
||||
{
|
||||
public static MemoryStream CreateMemoryStream(this ReadOnlyMemory<byte> memory, bool isReadOnly)
|
||||
{
|
||||
if (memory.IsEmpty)
|
||||
{
|
||||
return new MemoryStream(Array.Empty<byte>(), !isReadOnly);
|
||||
}
|
||||
|
||||
if (MemoryMarshal.TryGetArray(memory, out ArraySegment<byte> segment))
|
||||
{
|
||||
return new MemoryStream(segment.Array!, segment.Offset, segment.Count, !isReadOnly);
|
||||
}
|
||||
|
||||
throw new ArgumentException(nameof(memory), "Unable to create MemoryStream from provided memory value");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue