In SQL 2016 and Azure SQL there is a new powerful feature - Always Encrypted which allows to keep the encryption key outside of the database (for increased security).
When dealing with encrypted data at rest we need to be able to change (rotate) the encryption key either on schedule or upon a request.
Microsoft provides a example of a PowerShell script that re-encrypts the all data in a database with a new column encryption key.
I used that example to create my own script.
The first setback with the following cmdlet
When dealing with encrypted data at rest we need to be able to change (rotate) the encryption key either on schedule or upon a request.
Microsoft provides a example of a PowerShell script that re-encrypts the all data in a database with a new column encryption key.
I used that example to create my own script.
The first setback with the following cmdlet
Set-SqlColumnEncryption -ColumnEncryptionSettings $ces -InputObject $database -UseOnlineApproach -MaxDowntimeInSeconds 120 -LogFileDirectory .
was a syntax error
Set-SqlColumnEncryption : A parameter cannot be found that matches parameter name 'UseOnlineApproach'.
It was easy to fix - I removed all the parameters except for ColumnEnxryptionSettings and InputObject.
But then the same cmdlet failed at the execution time with this error.
Set-SqlColumnEncryption : Add or update objects failed due to the following errors:
Error SQL46010: Incorrect syntax near -.
At C:\Users\smizrokhi\Documents\Projects\BVI\Scripts\BOSSprovisioning\RotateCEK.ps1:58 char:1
+ Set-SqlColumnEncryption -ColumnEncryptionSettings $ces -InputObject $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Set-SqlColumnEncryption], DacModelException
+ FullyQualifiedErrorId : EncryptionError,Microsoft.SqlServer.Management.PowerShell.AlwaysEncrypted.SetColumnEncryption
It turned out that the column encryption key name in my was 'CEK-2' and because of the dash character within the name it threw out this SQL incorrect syntax error.
The solution was very simple - enclose he column encryption key name in square brackets.
The original statement
$ces += New-SqlColumnEncryptionSettings -ColumnName $threeColPartName -EncryptionType $columns[$j].EncryptionType -EncryptionKey $newCekName
was modified as following
$ces += New-SqlColumnEncryptionSettings -ColumnName $threeColPartName -EncryptionType $columns[$j].EncryptionType -EncryptionKey "[$newCekName]"
Comments
Post a Comment