Skip to content

Commit 068b90a

Browse files
Connect to Azure SQL with certificate validation instead of disabling it (#122)
* Connect to SQL with the driver's certificate validation instead of disabling it The connection string no longer carries TrustServerCertificate. Its default is already "no" in ODBC Driver 18, so the driver validates the server certificate, which is what Microsoft recommends: "Install a certificate on the server that the client trusts. This option is the recommended one, and it's the only one that keeps both encryption and validation" (https://learn.microsoft.com/sql/connect/odbc/major-version-differences#encryption-changes). Keeping the keyword at its default value only obscured that the sample was opting out of validation. This works against LocalStack for Azure because the emulator now serves a publicly trusted certificate for the host name it returns in fullyQualifiedDomainName, so the sample no longer needs a local exception to run. The same keyword is gone from the connection string that deploy.sh stores in Key Vault, since the app reads that value. Verified end to end on both targets, with the sample otherwise unchanged: - Real Azure: deploy.sh and validate.sh exit 0, and https://<app>.azurewebsites.net returns the app's own page with 9 cells rendered from the Azure SQL database. - LocalStack for Azure: deploy.sh and validate.sh exit 0, and the app answers on the emulator endpoint with the same page and the same 9 cells. If LocalStack cannot download its public certificate (for example with SKIP_SSL_CERT_DOWNLOAD=1) the server presents a certificate issued by the LocalStack root certificate authority, and a client then needs that authority installed or TrustServerCertificate=yes. The code comment says so. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * Keep the stored connection string free of TrustServerCertificate too deploy.sh writes the connection string into Key Vault and the app reads it from there, so leaving the keyword in that copy would keep suggesting that validation has to be disabled, even though the app no longer sets it. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 06a6c35 commit 068b90a

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

‎samples/web-app-sql-database/python/scripts/deploy.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ else
360360
fi
361361

362362
# Build connection string
363-
SQL_CONNECTION_STRING="Server=tcp:${SQL_SERVER_FQDN},1433;Database=${SQL_DATABASE_NAME};User ID=${DATABASE_USER_NAME};Password=${DATABASE_USER_PASSWORD};Encrypt=yes;TrustServerCertificate=yes;Connection Timeout=30;"
363+
SQL_CONNECTION_STRING="Server=tcp:${SQL_SERVER_FQDN},1433;Database=${SQL_DATABASE_NAME};User ID=${DATABASE_USER_NAME};Password=${DATABASE_USER_PASSWORD};Encrypt=yes;Connection Timeout=30;"
364364

365365
# Create secret
366366
echo "Creating secret [$SECRET_NAME] in Key Vault..."

‎samples/web-app-sql-database/python/src/database.py‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,17 @@ def _build_connection_string(self) -> str:
166166
f"Server=tcp:{self.server},1433;"
167167
f"Database={self.database};"
168168
f"Encrypt=yes;"
169-
f"TrustServerCertificate=yes;"
170169
f"Connection Timeout={self.connection_timeout};"
171170
)
172171

173-
# TrustServerCertificate=yes tells the ODBC driver to accept self-signed certificates without verification
174-
# This is appropriate for:
175-
# - Local development with Docker containers
176-
# - Testing environments with self-signed certificates
177-
# - Internal networks where you control the SQL Server
172+
# TrustServerCertificate is deliberately absent: its default is already "no" in ODBC Driver 18,
173+
# so the driver validates the server certificate, which is what Microsoft recommends
174+
# (https://learn.microsoft.com/sql/connect/odbc/major-version-differences#encryption-changes).
175+
# That works against Azure SQL Database and against LocalStack for Azure alike, because the
176+
# emulator serves a publicly trusted certificate for the host name it returns in
177+
# fullyQualifiedDomainName. Add TrustServerCertificate=yes only if LocalStack could not
178+
# download that certificate (for example with SKIP_SSL_CERT_DOWNLOAD=1), in which case the
179+
# server presents a certificate issued by the LocalStack root certificate authority.
178180

179181
if not self.use_azure_credential:
180182
# Traditional SQL authentication

0 commit comments

Comments
 (0)