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

Do not add parameters for sp_describe_parameter_encryption when not provided #1115

Merged
merged 9 commits into from
Jul 10, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 1 addition & 3 deletions doc/samples/AzureKeyVaultProviderExample_2_0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,5 @@ public CustomerRecord(int id, string fName, string lName)
}
}
}

//</Snippet1>

}
//</Snippet1>
Original file line number Diff line number Diff line change
Expand Up @@ -5829,11 +5829,22 @@ private SqlParameter BuildStoredProcedureStatementForColumnEncryption(string sto
{
Debug.Assert(CommandType == CommandType.StoredProcedure, "BuildStoredProcedureStatementForColumnEncryption() should only be called for stored procedures");
Debug.Assert(!string.IsNullOrWhiteSpace(storedProcedureName), "storedProcedureName cannot be null or empty in BuildStoredProcedureStatementForColumnEncryption");
Debug.Assert(parameters != null, "parameters cannot be null in BuildStoredProcedureStatementForColumnEncryption");

StringBuilder execStatement = new StringBuilder();
execStatement.Append(@"EXEC ");

if (parameters is null)
{
execStatement.Append(ParseAndQuoteIdentifier(storedProcedureName, false));
return new SqlParameter(
null,
((execStatement.Length << 1) <= TdsEnums.TYPE_SIZE_LIMIT) ? SqlDbType.NVarChar : SqlDbType.NText,
execStatement.Length)
{
Value = execStatement.ToString()
};
}

// Find the return value parameter (if any).
SqlParameter returnValueParameter = null;
foreach (SqlParameter parameter in parameters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6781,11 +6781,22 @@ private SqlParameter BuildStoredProcedureStatementForColumnEncryption(string sto
{
Debug.Assert(CommandType == CommandType.StoredProcedure, "BuildStoredProcedureStatementForColumnEncryption() should only be called for stored procedures");
Debug.Assert(!string.IsNullOrWhiteSpace(storedProcedureName), "storedProcedureName cannot be null or empty in BuildStoredProcedureStatementForColumnEncryption");
Debug.Assert(parameters != null, "parameters cannot be null in BuildStoredProcedureStatementForColumnEncryption");

StringBuilder execStatement = new StringBuilder();
execStatement.Append(@"EXEC ");

if (parameters is null)
{
execStatement.Append(ParseAndQuoteIdentifier(storedProcedureName, false));
return new SqlParameter(
null,
((execStatement.Length << 1) <= TdsEnums.TYPE_SIZE_LIMIT) ? SqlDbType.NVarChar : SqlDbType.NText,
execStatement.Length)
{
Value = execStatement.ToString()
};
}

// Find the return value parameter (if any).
SqlParameter returnValueParameter = null;
foreach (SqlParameter parameter in parameters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,53 @@ public void TestExecuteReaderWithCommandBehavior(string connection, CommandBehav
});
}


[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringSetupForAE))]
[ClassData(typeof(AEConnectionStringProvider))]
public void TestEnclaveStoredProceduresWithAndWithoutParameters(string connectionString)
{
using SqlConnection sqlConnection = new(connectionString);
sqlConnection.Open();

using SqlCommand sqlCommand = new("", sqlConnection, transaction: null,
columnEncryptionSetting: SqlCommandColumnEncryptionSetting.Enabled);

string procWithoutParams = DataTestUtility.GetUniqueName("EnclaveWithoutParams", withBracket: false);
string procWithParam = DataTestUtility.GetUniqueName("EnclaveWithParams", withBracket: false);

try
{
sqlCommand.CommandText = $"CREATE PROCEDURE {procWithoutParams} AS SELECT * FROM [{_tableName}];";
sqlCommand.ExecuteNonQuery();
sqlCommand.CommandText = $"CREATE PROCEDURE {procWithParam} @id INT AS SELECT * FROM [{_tableName}] WHERE CustomerId = @id";
sqlCommand.ExecuteNonQuery();

sqlCommand.CommandText = procWithoutParams;
sqlCommand.CommandType = CommandType.StoredProcedure;
using (SqlDataReader reader = sqlCommand.ExecuteReader())
{
Assert.Equal(3, reader.VisibleFieldCount);
johnnypham marked this conversation as resolved.
Show resolved Hide resolved
}

sqlCommand.CommandText = procWithParam;
sqlCommand.CommandType = CommandType.StoredProcedure;
Exception ex = Assert.Throws<SqlException>(() => sqlCommand.ExecuteReader());
string expectedMsg = $"Procedure or function '{procWithParam}' expects parameter '@id', which was not supplied.";

Assert.Equal(expectedMsg, ex.Message);

sqlCommand.Parameters.AddWithValue("@id", 0);
using (SqlDataReader reader = sqlCommand.ExecuteReader())
{
Assert.Equal(3, reader.VisibleFieldCount);
}
}
finally
{
DropHelperProcedures(new[] { procWithoutParams, procWithParam }, connectionString);
}
}

[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringSetupForAE))]
[ClassData(typeof(AEConnectionStringProvider))]
public void TestPrepareWithExecuteNonQuery(string connection)
Expand Down Expand Up @@ -2262,15 +2309,17 @@ public void TestSystemProvidersHavePrecedenceOverInstanceLevelProviders(string c
connection.Open();
using SqlCommand command = CreateCommandThatRequiresSystemKeyStoreProvider(connection);
connection.RegisterColumnEncryptionKeyStoreProvidersOnConnection(customKeyStoreProviders);
command.ExecuteReader();
SqlDataReader reader = command.ExecuteReader();
Assert.Equal(3, reader.VisibleFieldCount);
}

using (SqlConnection connection = new(connectionString))
{
connection.Open();
using SqlCommand command = CreateCommandThatRequiresSystemKeyStoreProvider(connection);
command.RegisterColumnEncryptionKeyStoreProvidersOnCommand(customKeyStoreProviders);
command.ExecuteReader();
SqlDataReader reader = command.ExecuteReader();
Assert.Equal(3, reader.VisibleFieldCount);
}
}

Expand Down