Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: Crypto ReadOnlyMemory<byte> decryption times out #1443

Merged
merged 5 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 17 additions & 27 deletions src/Dapr.Client/DaprClientGrpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1670,24 +1670,18 @@ public override async Task<ReadOnlyMemory<byte>> EncryptAsync(string vaultResour
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 bufferedResult = new ArrayBufferWriter<byte>();
var encryptionResult =
await EncryptAsync(vaultResourceName, memoryStream, keyName, encryptionOptions, cancellationToken);

await foreach (var item in encryptionResult.WithCancellation(cancellationToken))
{
bufferedResult.Write(item.Span);
}

return bufferedResult.WrittenMemory;
var bufferedResult = new ArrayBufferWriter<byte>();
await foreach (var item in encryptionResult.WithCancellation(cancellationToken))
{
bufferedResult.Write(item.Span);
}

throw new ArgumentException("The input instance doesn't have a valid underlying data store.",
nameof(plaintextBytes));
return bufferedResult.WrittenMemory;
}

/// <inheritdoc />
Expand Down Expand Up @@ -1895,22 +1889,18 @@ public override async Task<ReadOnlyMemory<byte>> DecryptAsync(string vaultResour
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 bufferedResult = new ArrayBufferWriter<byte>();
await foreach (var item in decryptionResult.WithCancellation(cancellationToken))
{
bufferedResult.Write(item.Span);
}

return bufferedResult.WrittenMemory;
var decryptionResult =
await DecryptAsync(vaultResourceName, memoryStream, keyName, decryptionOptions, cancellationToken);

var bufferedResult = new ArrayBufferWriter<byte>();
await foreach (var item in decryptionResult.WithCancellation(cancellationToken))
{
bufferedResult.Write(item.Span);
}

throw new ArgumentException("The input instance doesn't have a valid underlying data store",
nameof(ciphertextBytes));
return bufferedResult.WrittenMemory;
}

/// <inheritdoc />
Expand Down
36 changes: 36 additions & 0 deletions src/Dapr.Client/Extensions/ReadOnlyMemoryExtensions.cs
Original file line number Diff line number Diff line change
@@ -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)
philliphoff marked this conversation as resolved.
Show resolved Hide resolved
{
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