You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Setting Up and Initializing Column Encryption with MSSQL Always Encrypted #23970
I'd love if it would be possible to initialize the Column Encryption from application layer, so that you just add an annotation like [ColumnEncryption] to every column that should be encrypted, so that it is picked up by migrations which build the necessary T-SQL.
For now you have to either do it yourself in SSMS or Powershell or by some complex T-SQL that you have to hack into your migrations.
This looks like it would involve supporting the ENCRYPTED WITH ... syntax when creating columns. Doing that depends on having an encryption key predefined - we should be very careful with what we provide in this area (if at all).
thanks for your fast answer :-)
What would be the problem about that?
Besides that it would be possible as well to either create new encryption keys, dependent on the Config or let the user define some, etc. It would greatly enhance the convenience of EF Core if it would be possible to include those things in a clean and pre-defined way instead of having to care about that yourself.
We have to be extremely careful not to have any sensitive data slip into the migrations or model snapshot - that would be a security concern. I'm not familiar enough with SQL Server Always Encrypted to know exactly how the encryption key should be managed, we'd have to carefully research this.
On the other hand, the CREATE TABLE clause does seem pretty straightforward.
It would be enough to support scenarios where the key is generated automatically by the database or already there. The Powershell-Scripts I use for now to initialize the encryption don't contain any sensitive data so it shouldn't be required.
Note that it seems possible to use ALTER TABLE to add Always Encrypted to existing columns (e.g. this). Assuming that's indeed the case, people can use raw SQL in their migrations to set Always Encrypted after their tables/columns are created by EF Core.
I would love to have better support for Always Encrypted in EF Core. I want to Unit Test against a real Database since some operators are not supported with an encrypted Database. But i cant figure out a fast and reliable way to create a new Database to use in each of my Unit Tests.
Altering the Column Encryption after the Column is created is not possible with Raw Sql.
Using Migrations and changing the AddColumn statement could be possible but using migrations is slower then just calling EnsureCreated.
Right now im calling a PowerShell script to set up the encryption keys and changing the columns to encrypted. But this is slow too.
I wish there was a way to add custom Sql to the Model Builder so the columns get created with encryption enabled when i call EnsureCreated.
Using Migrations and changing the AddColumn statement could be possible but using migrations is slower then just calling EnsureCreated.
Do you mean in terms of development speed, or runtime speed? Because running (minimal) migrations vs. EnsureCreated really shouldn't have any meaningful difference. I'd advise simply using migrations for this kind of case.
Letting people know that I second this (and by extension that someone else cares). I think it would be beneficial to look at this together with the SQL Server tooling team though. As it stands the tooling for SQL Server Always Encrypted is somewhat lacking, and only runs on PowerShell 5.
+1. I also would love to see this.
I understand it's possible to insert this into migrations myself. But I don't hand-edit migrations often, so am worried about taking long to learn+debug. I decided not to use Always Encrypted until it is supported with EF CORE code-first. Instead I wrote the code to encrypt/decrypt (in a deterministic way - to enable some EF/DB content-matching queries to still work) whenever writing and reading the affected properties.
@kandaker modifying/hand-editing migrations is both really easy and also recommended (for cases when it is needed); I'd definitely recommend against using a different encryption method just to avoid doing it.
Activity
roji commentedon Jan 25, 2021
This looks like it would involve supporting the
ENCRYPTED WITH ...syntax when creating columns. Doing that depends on having an encryption key predefined - we should be very careful with what we provide in this area (if at all).daniels7 commentedon Jan 25, 2021
Hello @roji ,
thanks for your fast answer :-)
What would be the problem about that?
Besides that it would be possible as well to either create new encryption keys, dependent on the Config or let the user define some, etc. It would greatly enhance the convenience of EF Core if it would be possible to include those things in a clean and pre-defined way instead of having to care about that yourself.
roji commentedon Jan 25, 2021
We have to be extremely careful not to have any sensitive data slip into the migrations or model snapshot - that would be a security concern. I'm not familiar enough with SQL Server Always Encrypted to know exactly how the encryption key should be managed, we'd have to carefully research this.
On the other hand, the CREATE TABLE clause does seem pretty straightforward.
daniels7 commentedon Jan 26, 2021
It would be enough to support scenarios where the key is generated automatically by the database or already there. The Powershell-Scripts I use for now to initialize the encryption don't contain any sensitive data so it shouldn't be required.
roji commentedon Feb 24, 2022
Note that it seems possible to use ALTER TABLE to add Always Encrypted to existing columns (e.g. this). Assuming that's indeed the case, people can use raw SQL in their migrations to set Always Encrypted after their tables/columns are created by EF Core.
mblumenauer commentedon Jun 27, 2022
I would love to have better support for Always Encrypted in EF Core. I want to Unit Test against a real Database since some operators are not supported with an encrypted Database. But i cant figure out a fast and reliable way to create a new Database to use in each of my Unit Tests.
Altering the Column Encryption after the Column is created is not possible with Raw Sql.
Using Migrations and changing the AddColumn statement could be possible but using migrations is slower then just calling EnsureCreated.
Right now im calling a PowerShell script to set up the encryption keys and changing the columns to encrypted. But this is slow too.
I wish there was a way to add custom Sql to the Model Builder so the columns get created with encryption enabled when i call EnsureCreated.
ajcvickers commentedon Jun 29, 2022
@mblumenauer I filed #28343 as a possible approach for this.
roji commentedon Jun 29, 2022
Do you mean in terms of development speed, or runtime speed? Because running (minimal) migrations vs. EnsureCreated really shouldn't have any meaningful difference. I'd advise simply using migrations for this kind of case.
fepettersen commentedon Jun 2, 2023
Letting people know that I second this (and by extension that someone else cares). I think it would be beneficial to look at this together with the SQL Server tooling team though. As it stands the tooling for SQL Server Always Encrypted is somewhat lacking, and only runs on PowerShell 5.
kandaker commentedon Sep 25, 2023
+1. I also would love to see this.
I understand it's possible to insert this into migrations myself. But I don't hand-edit migrations often, so am worried about taking long to learn+debug. I decided not to use Always Encrypted until it is supported with EF CORE code-first. Instead I wrote the code to encrypt/decrypt (in a deterministic way - to enable some EF/DB content-matching queries to still work) whenever writing and reading the affected properties.
roji commentedon Sep 25, 2023
@kandaker modifying/hand-editing migrations is both really easy and also recommended (for cases when it is needed); I'd definitely recommend against using a different encryption method just to avoid doing it.
Ogglas commentedon Oct 25, 2023
This would be really good!