Skip to content
This repository was archived by the owner on Aug 7, 2020. It is now read-only.

Commit 855b2f8

Browse files
authored
Invoke-EncryptColumns NullReferencEexception (#14)
* more logging and setting default schema to null to fix things * Comments and docs. * Removed the default from the connection string.
1 parent e166704 commit 855b2f8

File tree

3 files changed

+52
-17
lines changed

3 files changed

+52
-17
lines changed

AlwaysEncryptedSample/Views/Home/Index.cshtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
}
44

55
<div class="jumbotron">
6-
<h1>Alway Encrypted Sample App</h1>
6+
<h1>Always Encrypted Sample App</h1>
77
<p class="lead">Always Encrypted allows clients to encrypt sensitive data inside client applications and never reveal the encryption keys to SQL Server.</p>
88
<p><a href="https://msdn.microsoft.com/en-us/library/mt163865.aspx" class="btn btn-primary btn-lg">Learn more &raquo;</a></p>
99
</div>
@@ -12,7 +12,7 @@
1212
<div class="col-md-12">
1313
<h2>Getting started</h2>
1414
<p>
15-
In order to encrypt the columns you need to run the Encryption.ps1 script in SolutionItems on the created database.
15+
In order to encrypt the columns you need to run the <code lang="powershell">New-EncryptionKeys.ps1</code> and <code lang="powershell">Invoke-EncryptColumns.ps1</code> scripts in the project root folder on the created database.
1616
</p>
1717
</div>
1818
</div>

Invoke-EncryptColumns.ps1

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
[cmdletbinding()]
66
param(
7-
[string] $ConnectionString = "Data Source=localhost,1433;Initial Catalog=AlwaysEncryptedSample;UID=sa;PWD=alwaysB3Encrypt1ng;Application Name=Encryption.ps1;Column Encryption Setting=enabled;",
7+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] [string] $ConnectionString,
88
[string] $AuthSchema = 'Authentication',
99
[string] $AppSchema = 'Purchasing',
10-
[string] $LoggingSchema = 'Logging',
10+
[string] $LogSchema = 'Logging',
1111
[string] $AuthColumnKeyName = "AuthColumnsKey",
1212
[string] $AppColumnKeyName = "AppColumnsKey",
1313
[string] $LogColumnKeyName = "LogColumnsKey",
@@ -18,6 +18,7 @@ param(
1818

1919
try {
2020
$smoDatabase = Get-SqlDatabase -ConnectionString $ConnectionString
21+
$smoDatabase.DefaultSchema = $null # If we don't do this Set-SqlColumnEncryption will not respect the schema set by New-SqlColumnEncryptionSettings
2122
}
2223
catch {
2324
Write-Error $_
@@ -27,19 +28,52 @@ catch {
2728
$encryptionChanges = @()
2829

2930
# Change table [Authentication].[AspNetUsers]
30-
$encryptionChanges += New-SqlColumnEncryptionSettings -ColumnName "$($AuthSchema).AspNetUsers.SSN" -EncryptionType Randomized -EncryptionKey $AuthColumnKeyName
31-
31+
if ($smoDatabase.ColumnEncryptionKeys[$AuthColumnKeyName].Length -Eq 0) {
32+
Write-Warning "Authentication Column Encryption Key $AuthColumnKeyName does not exist."
33+
}
34+
elseif ($smoDatabase.Schemas[$AuthSchema].Length -eq 0) {
35+
Write-Warning "Authentication Schema $AuthSchema does not exist."
36+
}
37+
else {
38+
Write-Debug "Adding ColumnEncryptionSettings for Auth Column Key $AuthColumnKeyName."
39+
$encryptionChanges += New-SqlColumnEncryptionSettings -ColumnName "$($AuthSchema).AspNetUsers.SSN" -EncryptionType Randomized -EncryptionKey $AuthColumnKeyName
40+
}
3241

3342
# Change table [Purchasing].[CreditCards]
34-
$encryptionChanges += New-SqlColumnEncryptionSettings -ColumnName "$($AppSchema).CreditCards.CardNumber" -EncryptionType Randomized -EncryptionKey $AppColumnKeyName
35-
$encryptionChanges += New-SqlColumnEncryptionSettings -ColumnName "$($AppSchema).CreditCards.CCV" -EncryptionType Randomized -EncryptionKey $AppColumnKeyName
43+
if ($smoDatabase.ColumnEncryptionKeys[$AppColumnKeyName].Length -Eq 0) {
44+
Write-Warning "Application Column Encryption Key $AppColumnKeyName does not exist."
45+
}
46+
elseif ($smoDatabase.Schemas[$AppSchema].Length -eq 0) {
47+
Write-Warning "Application Schema $AppSchema does not exist."
48+
}
49+
else {
50+
Write-Debug "Adding ColumnEncryptionSettings for App Column Key $AppColumnKeyName."
51+
$encryptionChanges += New-SqlColumnEncryptionSettings -ColumnName "$($AppSchema).CreditCards.CardNumber" -EncryptionType Randomized -EncryptionKey $AppColumnKeyName
52+
$encryptionChanges += New-SqlColumnEncryptionSettings -ColumnName "$($AppSchema).CreditCards.CCV" -EncryptionType Randomized -EncryptionKey $AppColumnKeyName
53+
}
3654

3755
# Change table [Logging].[Log]
38-
$encryptionChanges += New-SqlColumnEncryptionSettings -ColumnName "$($LoggingSchema).Log.User" -EncryptionType Deterministic -EncryptionKey $LogColumnKeyName
39-
$encryptionChanges += New-SqlColumnEncryptionSettings -ColumnName "$($LoggingSchema).Log.ClientIP" -EncryptionType Deterministic -EncryptionKey $LogColumnKeyName
40-
41-
Set-SqlColumnEncryption `
42-
-ColumnEncryptionSettings $encryptionChanges `
43-
-InputObject $smoDatabase `
44-
-Script:$Script `
45-
-LogFileDirectory $LogFileDirectory
56+
if ($smoDatabase.ColumnEncryptionKeys[$LogColumnKeyName].Length -Eq 0) {
57+
Write-Warning "Logging Column Encryption Key $LogColumnKeyName does not exist."
58+
}
59+
elseif ($smoDatabase.Schemas[$LogSchema].Length -eq 0) {
60+
Write-Warning "Logging Schema $LogSchema does not exist."
61+
}
62+
else {
63+
Write-Debug "Adding ColumnEncryptionSettings for Log Column Key $LogColumnKeyName."
64+
$encryptionChanges += New-SqlColumnEncryptionSettings -ColumnName "$($LogSchema).Log.User" -EncryptionType Deterministic -EncryptionKey $LogColumnKeyName
65+
$encryptionChanges += New-SqlColumnEncryptionSettings -ColumnName "$($LogSchema).Log.ClientIP" -EncryptionType Deterministic -EncryptionKey $LogColumnKeyName
66+
}
67+
68+
69+
if ($encryptionChanges.Length -eq 0) {
70+
Write-Warning "Could not find any column keys or schemas to encrypt."
71+
}
72+
else {
73+
Write-Verbose "Applying Column Encryption to $($encryptionChanges.Length) column(s)."
74+
Set-SqlColumnEncryption `
75+
-ColumnEncryptionSettings $encryptionChanges `
76+
-InputObject $smoDatabase `
77+
-Script:$Script `
78+
-LogFileDirectory $LogFileDirectory
79+
}

New-EncryptionKeys.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
[cmdletbinding()]
66
param(
7-
[string] $ConnectionString = "Data Source=localhost,1433;Initial Catalog=AlwaysEncryptedSample;UID=sa;PWD=alwaysB3Encrypt1ng;Application Name=Encryption.ps1;Column Encryption Setting=enabled;",
7+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] [string] $ConnectionString,
88
[string] $MasterKeyDNSName = "CN=Always Encrypted Sample Cert",
99
[switch] $RemoveExistingCerts,
1010
[string] $MasterKeySQLName = "AlwaysEncryptedSampleCMK",
@@ -56,3 +56,4 @@ New-SqlColumnMasterKey -Name $MasterKeySQLName -InputObject $smoDatabase -Column
5656
-ColumnMasterKey $MasterKeySQLName `
5757
-Name $_ | Out-Null
5858
}
59+

0 commit comments

Comments
 (0)