Skip to content

IndexOutOfRangeException is thrown when using version 7.0.0 and Always Encrypted Columns #1180

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

Merged
merged 1 commit into from
May 16, 2023
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
6 changes: 3 additions & 3 deletions src/NServiceBus.SqlServer/NServiceBus.SqlServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
<PackageReference Include="Fody" Version="6.6.3" PrivateAssets="All" />
<PackageReference Include="Janitor.Fody" Version="1.9.0" PrivateAssets="All" />
<PackageReference Include="Obsolete.Fody" Version="5.3.0" PrivateAssets="All" />
<PackageReference Include="Particular.Packaging" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="NServiceBus" Version="[8.0.0, 9)" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.3" />
<PackageReference Include="Particular.Packaging" Version="2.3.0" PrivateAssets="All" />
<PackageReference Include="NServiceBus" Version="[8.0.0, 9.0.0)" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.5" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
namespace NServiceBus.Transport.SqlServer.AcceptanceTests
{
using System;
#if SYSTEMDATASQLCLIENT
using System.Data.SqlClient;
#else
using Microsoft.Data.SqlClient;
#endif
using System.Threading.Tasks;
using AcceptanceTesting;
using AcceptanceTesting.Customization;
using NServiceBus.AcceptanceTests;
using NUnit.Framework;

public class When_using_column_encrypted_connection : NServiceBusAcceptanceTest
{
[Test]
public async Task Should_work()
{
#if SYSTEMDATASQLCLIENT && NET
Assert.Ignore("System.Data.SqlClient doesn't support this setting on .NET (works on .NET Framework)");
#endif
var ctx = await Scenario.Define<Context>()
.WithEndpoint<Endpoint>(b => b.When((bus, c) => bus.SendLocal(new Message())))
.Done(c => c.MessageReceived)
.Run();

Assert.True(ctx.MessageReceived, "Message should be properly received");
}

static string GetConnectionString() =>
Environment.GetEnvironmentVariable("SqlServerTransportConnectionString") ?? @"Data Source=.\SQLEXPRESS;Initial Catalog=nservicebus;Integrated Security=True";

public class Endpoint : EndpointConfigurationBuilder
{
public Endpoint()
{
var transport = new SqlServerTransport(async cancellationToken =>
{
var connectionString = GetConnectionString();

if (!connectionString.EndsWith(";"))
{
connectionString += ";";
}

connectionString += "Column Encryption Setting=enabled";

var connection = new SqlConnection(connectionString);

await connection.OpenAsync(cancellationToken);

return connection;
});

EndpointSetup(new CustomizedServer(transport), (c, sd) =>
{
c.OverridePublicReturnAddress($"{Conventions.EndpointNamingConvention(typeof(Endpoint))}@dbo@nservicebus");
});
}

class Handler : IHandleMessages<Message>
{
readonly Context scenarioContext;
public Handler(Context scenarioContext)
{
this.scenarioContext = scenarioContext;
}

public Task Handle(Message message, IMessageHandlerContext context)
{
scenarioContext.MessageReceived = true;

return Task.FromResult(0);
}
}
}

public class Context : ScenarioContext
{
public bool MessageReceived { get; set; }
}

public class Message : IMessage
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
<PackageReference Include="Fody" Version="6.6.3" PrivateAssets="All" />
<PackageReference Include="Janitor.Fody" Version="1.9.0" PrivateAssets="All" />
<PackageReference Include="Obsolete.Fody" Version="5.3.0" PrivateAssets="All" />
<PackageReference Include="Particular.Packaging" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="NServiceBus" Version="[8.0.0, 9)" />
<PackageReference Include="Particular.Packaging" Version="2.3.0" PrivateAssets="All" />
<PackageReference Include="NServiceBus" Version="[8.0.0, 9.0.0)" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="3.0.1" />
</ItemGroup>

Expand Down
14 changes: 7 additions & 7 deletions src/NServiceBus.Transport.SqlServer/Queuing/MessageRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static async Task<MessageRow> ReadRow(SqlDataReader dataReader, bool isStreamSup
id = await dataReader.GetFieldValueAsync<Guid>(0, cancellationToken).ConfigureAwait(false),
expired = await dataReader.GetFieldValueAsync<int>(1, cancellationToken).ConfigureAwait(false) == 1,
headers = await GetHeaders(dataReader, 2, cancellationToken).ConfigureAwait(false),
bodyBytes = isStreamSupported ? await GetBody(dataReader, 3, cancellationToken).ConfigureAwait(false) : await GetNonStreamBody(dataReader, 5, cancellationToken).ConfigureAwait(false)
bodyBytes = await GetBody(dataReader, 3, isStreamSupported, cancellationToken).ConfigureAwait(false)
};
}

Expand Down Expand Up @@ -80,8 +80,13 @@ static async Task<string> GetHeaders(SqlDataReader dataReader, int headersIndex,
}
}

static async Task<byte[]> GetBody(SqlDataReader dataReader, int bodyIndex, CancellationToken cancellationToken)
static async Task<byte[]> GetBody(SqlDataReader dataReader, int bodyIndex, bool isStreamSupported, CancellationToken cancellationToken)
{
if (!isStreamSupported)
{
return (byte[])dataReader[bodyIndex];
}

// Null values will be returned as an empty (zero bytes) Stream.
using (var outStream = new MemoryStream())
using (var stream = dataReader.GetStream(bodyIndex))
Expand All @@ -92,11 +97,6 @@ static async Task<byte[]> GetBody(SqlDataReader dataReader, int bodyIndex, Cance
}
}

static Task<byte[]> GetNonStreamBody(SqlDataReader dataReader, int bodyIndex, CancellationToken cancellationToken)
{
return Task.FromResult((byte[])dataReader[bodyIndex]);
}

void AddParameter(SqlCommand command, string name, SqlDbType type, object value)
{
command.Parameters.Add(name, type).Value = value ?? DBNull.Value;
Expand Down