Skip to content

Commit

Permalink
Updated encrypt/decrypt options so the streaming block size no longer…
Browse files Browse the repository at this point in the history
… uses a uint. Added validation in its place to ensure the value provided is never less than or equal to 0.

Signed-off-by: Whit Waldo <whit.waldo@innovian.net>
  • Loading branch information
WhitWaldo committed Jan 3, 2024
1 parent 8c65ed1 commit c0530cd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
27 changes: 24 additions & 3 deletions src/Dapr.Client/CryptographyOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#nullable enable
using System;

namespace Dapr.Client
{
/// <summary>
Expand All @@ -20,14 +22,23 @@ public EncryptionOptions(KeyWrapAlgorithm keyWrapAlgorithm)
/// </summary>
public KeyWrapAlgorithm KeyWrapAlgorithm { get; set; }

private int streamingBlockSizeInBytes = 4 * 1024; // 4 KB
/// <summary>
/// The size of the block in bytes used to send data to the sidecar for cryptography operations.
/// </summary>
/// <remarks>
/// This defaults to 4KB and generally should not exceed 64KB.
/// </remarks>
public uint StreamingBlockSizeInBytes { get; set; } = 4 * 1024;

public int StreamingBlockSizeInBytes
{
get => streamingBlockSizeInBytes;
set
{
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(value, 0, nameof(value));

Check failure on line 37 in src/Dapr.Client/CryptographyOptions.cs

View workflow job for this annotation

GitHub Actions / Test .NET 8.0

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThanOrEqual'

Check failure on line 37 in src/Dapr.Client/CryptographyOptions.cs

View workflow job for this annotation

GitHub Actions / Test .NET 8.0

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThanOrEqual'

Check failure on line 37 in src/Dapr.Client/CryptographyOptions.cs

View workflow job for this annotation

GitHub Actions / Build

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThanOrEqual'

Check failure on line 37 in src/Dapr.Client/CryptographyOptions.cs

View workflow job for this annotation

GitHub Actions / Build

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThanOrEqual'

Check failure on line 37 in src/Dapr.Client/CryptographyOptions.cs

View workflow job for this annotation

GitHub Actions / run integration tests (8.0)

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThanOrEqual'

Check failure on line 37 in src/Dapr.Client/CryptographyOptions.cs

View workflow job for this annotation

GitHub Actions / run integration tests (8.0)

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThanOrEqual'

Check failure on line 37 in src/Dapr.Client/CryptographyOptions.cs

View workflow job for this annotation

GitHub Actions / Test .NET 7.0

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThanOrEqual'

Check failure on line 37 in src/Dapr.Client/CryptographyOptions.cs

View workflow job for this annotation

GitHub Actions / Test .NET 7.0

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThanOrEqual'

Check failure on line 37 in src/Dapr.Client/CryptographyOptions.cs

View workflow job for this annotation

GitHub Actions / run integration tests (6.0)

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThanOrEqual'

Check failure on line 37 in src/Dapr.Client/CryptographyOptions.cs

View workflow job for this annotation

GitHub Actions / run integration tests (6.0)

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThanOrEqual'
streamingBlockSizeInBytes = value;
}
}

/// <summary>
/// The optional name (and optionally a version) of the key specified to use during decryption.
/// </summary>
Expand All @@ -44,9 +55,19 @@ public EncryptionOptions(KeyWrapAlgorithm keyWrapAlgorithm)
/// </summary>
public class DecryptionOptions
{
private int streamingBlockSizeInBytes = 4 * 1024; // 4KB
/// <summary>
/// The size of the block in bytes used to send data to the sidecar for cryptography operations.
/// </summary>
public uint StreamingBlockSizeInBytes { get; set; } = 4 * 1024;
public int StreamingBlockSizeInBytes
{
get => streamingBlockSizeInBytes;
set
{
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(value, 0, nameof(value));

Check failure on line 67 in src/Dapr.Client/CryptographyOptions.cs

View workflow job for this annotation

GitHub Actions / Test .NET 8.0

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThanOrEqual'

Check failure on line 67 in src/Dapr.Client/CryptographyOptions.cs

View workflow job for this annotation

GitHub Actions / Test .NET 8.0

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThanOrEqual'

Check failure on line 67 in src/Dapr.Client/CryptographyOptions.cs

View workflow job for this annotation

GitHub Actions / Build

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThanOrEqual'

Check failure on line 67 in src/Dapr.Client/CryptographyOptions.cs

View workflow job for this annotation

GitHub Actions / Build

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThanOrEqual'

Check failure on line 67 in src/Dapr.Client/CryptographyOptions.cs

View workflow job for this annotation

GitHub Actions / run integration tests (8.0)

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThanOrEqual'

Check failure on line 67 in src/Dapr.Client/CryptographyOptions.cs

View workflow job for this annotation

GitHub Actions / run integration tests (8.0)

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThanOrEqual'

Check failure on line 67 in src/Dapr.Client/CryptographyOptions.cs

View workflow job for this annotation

GitHub Actions / Test .NET 7.0

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThanOrEqual'

Check failure on line 67 in src/Dapr.Client/CryptographyOptions.cs

View workflow job for this annotation

GitHub Actions / Test .NET 7.0

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThanOrEqual'

Check failure on line 67 in src/Dapr.Client/CryptographyOptions.cs

View workflow job for this annotation

GitHub Actions / run integration tests (6.0)

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThanOrEqual'

Check failure on line 67 in src/Dapr.Client/CryptographyOptions.cs

View workflow job for this annotation

GitHub Actions / run integration tests (6.0)

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThanOrEqual'

streamingBlockSizeInBytes = value;
}
}
}
}
8 changes: 4 additions & 4 deletions src/Dapr.Client/DaprClientGrpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1413,7 +1413,7 @@ private async Task<ReadOnlyMemory<byte>> EncryptAsync(string vaultResourceName,
{
//Stream the plaintext data to the sidecar in chunks
Task.FromResult(SendPlaintextStreamAsync(plaintextStream,
(int)encryptionOptions.StreamingBlockSizeInBytes, duplexStream, encryptRequestOptions,
encryptionOptions.StreamingBlockSizeInBytes, duplexStream, encryptRequestOptions,
cancellationToken)),
//At the same time, retrieve the encrypted response from the sidecar
Task.FromResult(RetrieveEncryptedStreamAsync(duplexStream, cancellationToken))
Expand Down Expand Up @@ -1469,7 +1469,7 @@ public override async IAsyncEnumerable<byte[]> EncryptStreamAsync(string vaultRe
var tasks = new Task<IAsyncEnumerable<byte[]>>[]
{
//Stream the plaintext data to the sidecar in chunks
Task.FromResult(SendPlaintextStreamAsync(plaintextStream, (int)encryptionOptions.StreamingBlockSizeInBytes, duplexStream, encryptRequestOptions, cancellationToken)),
Task.FromResult(SendPlaintextStreamAsync(plaintextStream, encryptionOptions.StreamingBlockSizeInBytes, duplexStream, encryptRequestOptions, cancellationToken)),
//At the same time, retrieve the encrypted response from the sidecar
Task.FromResult(RetrieveEncryptedStreamAsync(duplexStream, cancellationToken))
};
Expand Down Expand Up @@ -1562,7 +1562,7 @@ public override async IAsyncEnumerable<byte[]> DecryptStreamAsync(string vaultRe
var tasks = new Task<IAsyncEnumerable<byte[]>>[]
{
//Stream the plaintext data to the sidecar in chunks
Task.FromResult(SendCiphertextStreamAsync(ciphertextStream, (int)decryptionOptions.StreamingBlockSizeInBytes, duplexStream, decryptRequestOptions, cancellationToken)),
Task.FromResult(SendCiphertextStreamAsync(ciphertextStream, decryptionOptions.StreamingBlockSizeInBytes, duplexStream, decryptRequestOptions, cancellationToken)),
//At the same time, retrieve the encrypted response from the sidecar
Task.FromResult(RetrieveDecryptedStreamAsync(duplexStream, cancellationToken))
};
Expand Down Expand Up @@ -1674,7 +1674,7 @@ private async Task<ReadOnlyMemory<byte>> DecryptAsync(string vaultResourceName,
var tasks = new Task<IAsyncEnumerable<byte[]>>[]
{
Task.FromResult(SendCiphertextStreamAsync(ciphertextStream,
(int)decryptionOptions.StreamingBlockSizeInBytes, duplexStream, decryptRequestOptions,
decryptionOptions.StreamingBlockSizeInBytes, duplexStream, decryptRequestOptions,
cancellationToken)),
Task.FromResult(RetrieveDecryptedStreamAsync(duplexStream, cancellationToken))
};
Expand Down

0 comments on commit c0530cd

Please sign in to comment.