efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Setting Up and Initializing Column Encryption with MSSQL Always Encrypted

Open daniels7 opened this issue 5 years ago • 12 comments

Hello,

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.

Greetings, Daniel

daniels7 avatar Jan 25 '21 16:01 daniels7

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).

roji avatar Jan 25 '21 16:01 roji

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.

daniels7 avatar Jan 25 '21 16:01 daniels7

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.

roji avatar Jan 25 '21 16:01 roji

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.

daniels7 avatar Jan 26 '21 09:01 daniels7

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.

roji avatar Feb 24 '22 08:02 roji

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.

mblumenauer avatar Jun 27 '22 16:06 mblumenauer

@mblumenauer I filed #28343 as a possible approach for this.

ajcvickers avatar Jun 29 '22 10:06 ajcvickers

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.

roji avatar Jun 29 '22 10:06 roji

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.

fepettersen avatar Jun 02 '23 11:06 fepettersen

+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 avatar Sep 25 '23 17:09 kandaker

@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.

roji avatar Sep 25 '23 18:09 roji

This would be really good!

Ogglas avatar Oct 25 '23 12:10 Ogglas