1
1
param (
2
2
[ValidateSet (" sqlcmd" , " sqlengine" )]
3
3
[string []]$Components ,
4
+ [bool ]$ForceEncryption ,
4
5
[string ]$SaPassword ,
5
6
[ValidateSet (" 2017" )]
6
7
[string ]$Version
9
10
function Wait-ForContainer {
10
11
$checkInterval = 5
11
12
$containerName = " sql"
12
- $timeout = 120
13
+ $timeout = 60
13
14
14
15
$startTime = Get-Date
15
16
Write-Host " Waiting for the container '$containerName ' to be healthy..."
@@ -42,6 +43,31 @@ if ($IsLinux) {
42
43
}
43
44
44
45
if (" sqlengine" -in $Components ) {
46
+ if ($ForceEncryption ) {
47
+ Write-Output " Force encryption is set, generating self-signed certificate ..."
48
+
49
+ if ($IsLinux ) {
50
+ # SOURCE: https://learn.microsoft.com/en-us/sql/linux/sql-server-linux-docker-container-security?view=sql-server-ver16#encrypt-connections-to-sql-server-linux-containers
51
+ & mkdir - p / opt/ mssql
52
+ & openssl req - x509 - nodes - newkey rsa:2048 - subj ' /CN=sql1.contoso.com' - keyout / opt/ mssql/ mssql.key - out / opt/ mssql/ mssql.pem - days 365
53
+ $MssqlConf = @'
54
+ [network]
55
+ tlscert = /etc/ssl/certs/mssql.pem
56
+ tlskey = /etc/ssl/private/mssql.key
57
+ tlsprotocols = 1.2
58
+ forceencryption = 1
59
+ '@
60
+
61
+ Set-Content - Path / opt/ mssql/ mssql.conf - Value $MssqlConf
62
+ & sudo chmod - R 775 / opt/ mssql
63
+
64
+ Copy-Item - Path / opt/ mssql/ mssql.pem - Destination / usr/ share/ ca- certificates/ mssql.crt
65
+ & sudo dpkg- reconfigure ca- certificates
66
+
67
+ $AdditionalContainerConfiguration = " -v /opt/mssql/mssql.conf:/var/opt/mssql/mssql.conf -v /opt/mssql/mssql.pem:/etc/ssl/certs/mssql.pem -v /opt/mssql/mssql.key:/etc/ssl/private/mssql.key"
68
+ }
69
+ }
70
+
45
71
if ($IsLinux ) {
46
72
# the Ubuntu 24.04 image uses a kernel version which does not work with the current 2017 version.
47
73
# see https://github.com/microsoft/mssql-docker/issues/868
@@ -74,7 +100,7 @@ if ("sqlengine" -in $Components) {
74
100
}
75
101
76
102
Write-Output " Starting a Docker Container"
77
- Invoke-Expression " docker run --name=`" sql`" -e `" ACCEPT_EULA=Y`" -e `" SA_PASSWORD=$SaPassword `" -e `" MSSQL_PID=Express`" --health-cmd=`" /opt/mssql-tools/bin/sqlcmd -C -S localhost -U sa -P '$SaPassword ' -Q 'SELECT 1' -b -o /dev/null`" --health-start-period=`" 10s`" --health-retries=3 --health-interval=`" 10s`" -p 1433:1433 -d `" mcr.microsoft.com/mssql/server:$Version -latest`" "
103
+ Invoke-Expression " docker run --name=`" sql`" -e `" ACCEPT_EULA=Y`" -e `" SA_PASSWORD=$SaPassword `" -e `" MSSQL_PID=Express`" --health-cmd=`" /opt/mssql-tools/bin/sqlcmd -C -S localhost -U sa -P '$SaPassword ' -Q 'SELECT 1' -b -o /dev/null`" --health-start-period=`" 10s`" --health-retries=3 --health-interval=`" 10s`" -p 1433:1433 $AdditionalContainerConfiguration -d `" mcr.microsoft.com/mssql/server:$Version -latest`" "
78
104
Wait-ForContainer
79
105
}
80
106
@@ -88,9 +114,24 @@ if ("sqlengine" -in $Components) {
88
114
89
115
Write-Host " Configuring SQL Express ..."
90
116
stop-service MSSQL`$ SQLEXPRESS
91
- set-itemproperty - path ' HKLM:\software\microsoft\microsoft sql server\mssql14.SQLEXPRESS\mssqlserver\supersocketnetlib\tcp\ipall' - name tcpdynamicports - value ' '
92
- set-itemproperty - path ' HKLM:\software\microsoft\microsoft sql server\mssql14.SQLEXPRESS\mssqlserver\supersocketnetlib\tcp\ipall' - name tcpport - value 1433
93
- set-itemproperty - path ' HKLM:\software\microsoft\microsoft sql server\mssql14.SQLEXPRESS\mssqlserver\' - name LoginMode - value 2
117
+
118
+ $InstancePath = " HKLM:\software\microsoft\microsoft sql server\mssql14.SQLEXPRESS\mssqlserver"
119
+ $SuperSocketNetLibPath = " $InstancePath \supersocketnetlib"
120
+ set-itemproperty - path " $SuperSocketNetLibPath \tcp\ipall" - name tcpdynamicports - value ' '
121
+ set-itemproperty - path " $SuperSocketNetLibPath \tcp\ipall" - name tcpport - value 1433
122
+ set-itemproperty - path $InstancePath - name LoginMode - value 2
123
+
124
+ # SOURCE: https://blogs.infosupport.com/configuring-sql-server-encrypted-connections-using-powershell/
125
+ if ($ForceEncryption ) {
126
+ $params = @ {
127
+ DnsName = ' sql1.contoso.com'
128
+ CertStoreLocation = ' Cert:\LocalMachine\My'
129
+ }
130
+ $Certificate = New-SelfSignedCertificate @params
131
+
132
+ Set-ItemProperty $SuperSocketNetLibPath - Name " Certificate" - Value $Certificate.Thumbprint.ToLowerInvariant ()
133
+ Set-ItemProperty $SuperSocketNetLibPath - Name " ForceEncryption" - Value 1
134
+ }
94
135
95
136
Write-Host " Starting SQL Express ..."
96
137
start-service MSSQL`$ SQLEXPRESS
0 commit comments