Azure WebJobs can't connect to SQL using encrypted communication - java

I have a Java application running as a WebJob on Azure. This job tries to connect to my Azure SQL Server. If the encryption is on, I get a connection error:
The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "java.security.cert.CertificateException: Failed to validate the server name in a certificate during Secure Sockets Layer (SSL) initialization.".
If encrypt = false it connects fine.
Shouldn't the communication between resources in Azure always be encrypted?
Also, if I run the application locally, the database connection successfully connects in encrypted mode.

First- I should explain that Azure SQL DB is always going to default to an encrypted connection. The error you're seeing is speaking of verifying the identity of the server that you're communicating with as part of the handshake for SSL. [There's some unpacking of this idea over here.] (https://security.stackexchange.com/questions/1779/how-does-an-ssl-server-prove-its-identity)
I unpacked the encryption of communication with Azure SQL Database in answers on this MSDN thread- but I'll bring the relevant portion here:
When using Azure SQL DB, connections are encrypted even when the connection
string does not include those portions we recommend.
...
Encryption occurs only if there is a verifiable server certificate,
otherwise the connection attempt fails.
Your error, as outlined on this MSDN article, arises from the following situation:
If the encrypt property is set to true and the trustServerCertificate
property is set to false and if the server name in the connection string
does not match the server name in the SQL Server SSL certificate, the
following error will be issued: The driver could not establish a secure
connection to SQL Server by using Secure Sockets Layer (SSL) encryption.
Error: "java.security.cert.CertificateException: Failed to validate the
server name in a certificate during Secure Sockets Layer (SSL) initialization."
Apparently, the JDBC driver for SQL Server 4.0 seems to resolve this error:
Azure VM Fails to Connect to Azure SQL Service
Certificate Exception connecting to Azure SQL with JDBC with default connection string (the one the management console says to use)

Related

how to fix The server selected protocol version TLS10 is not accepted by client preferences [TLS12]

I want to run a spring boot application with a sql server data base but when I run it, the following error appears :
com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "The server selected protocol version TLS10 is not accepted by client preferences [TLS12]". ClientConnectionId:73ea4fa4-e772-4fd7-90f5-d617aec091f2
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:2924) ~[mssql-jdbc-7.4.1.jre8.jar:na]
at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1881) ~[mssql-jdbc-7.4.1.jre8.jar:na]
TLS 1.2 must be configured on SQL Server: https://support.microsoft.com/en-us/topic/kb3135244-tls-1-2-support-for-microsoft-sql-server-e4472ef8-90a9-13c1-e4d8-44aad198cdbe

Can't connect to remote MySQL (MariaDB) database in .NET but I can with Java

I am trying to migrate some code from JavaFX to use C# for Windows Forms. In Java, I was using JDBC and could connect just fine programmatically. Nos that I am trying to use C# I am getting this error:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server).
I am using the same server address and credentials. I'm using System.Data.SqlClient. I've tried a bunch of different combinations but can't get the connection string to work.
String connectionString = "server=[Server URL];database=[Database Name];persist security info=true;user id=[Username];pwd=[Password]";
Here is an example of the mysql connection string:
string connectionString = "datasource= <database ip or domain>; port=<port, usually 3306>;username=<database user>;password=<user's password>;SslMode=none<this is important if you don't use ssl>;database=<your database>";
I think the reason you can't connect is because you haven't specified the SslMode.

How to Grant a Java Web App Access to a Digital Ocean Droplet

I'm hosting mysql server on a Digital Ocean droplet, and I am trying to figure out how to grant a Spring Boot web application access to the droplet, so that it can connect to the database. I configured the droplet so that it can only be reached via an ssh tunnel (i.e.: I disabled password authentication), but the database server itself can be connected with a username and password.
I know how to connect to the database using Connector/J, configuring datasources, and so forth. However, the extra security layer of Digital Ocean is new to me, and I'm not sure how to approach this problem.
EDIT: When I run the application and try to hit an endpoint, I get the following error:
The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: null, message from server: "Host '<my network hostname>' is not allowed to connect to this MySQL server"] with root cause
So, I was mistaken in thinking that the authentication issue was with the Digital Ocean droplet. As the error message (see question above) indicates, the Spring Boot application was able to get to the remote mysql server, and the authentication issue occurred there. It turns out that the mysql user I was attempting to connect with could only be used on localhost.
So, I created a new mysql user and tied it to my local machine's public IP address, and that solved the issue. For details on how to accomplish this, please read the following answer:
Host 'xxx.xx.xxx.xxx' is not allowed to connect to this MySQL server

SQL Server secure JDBC connection without reconfiguring SQL Server for secure connections

How to connect SQL Server database instance via JDBC securely without altering the configuration of SQL Server where other connections access the server insecurely and it is not possible to change the configuration of the server?
I had some trouble connecting to SQL Server via Java JDBC using encryption. The method below supports a secure connection where the validity of the certificate is not checked. SQL Server will issue the certificate and use encryption. This method does not require other connections to the database to be secure and also does not require changes to the database server.
Just substitute your IP:port combination and your database instance in the statement below
url = "jdbc:sqlserver://x.x.x.x:1433;databaseName=yourDBname;" +
"integratedSecurity=false;encrypt=true;trustServerCertificate=true";
conn = DriverManager.getConnection(url, username, password);

How to connect my web application to SQL Server / SQL Server Express

I am working with a web application that connects to a SQL Server database with this:
jdbc.url=jdbc:jtds:sqlserver://127.0.0.1/MyDatabase
jdbc.username=sa
jdbc.password=password
I am relatively new to SQL Server, however I have successfully accessed a SQLEXPRESS instance through the SQL Server Management Studio.
I can see from the Security->Login folder there is a "sa" entry
However when I run the web application I am getting this error:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property
'dataSource' threw exception; nested exception is
org.apache.commons.dbcp.SQLNestedException: Cannot create
PoolableConnectionFactory (Network error IOException: Connection
refused: connect)
I seems that the user: "sa" is not authenticated to access the database?
If so, how can I use the SQL Server management studio to fix this?
Make sure authentication mode is set to Mixed. It's set to Windows authentication mode as default which disables sa account. See here: http://msdn.microsoft.com/en-us/library/ms143705%28v=sql.90%29.aspx
Edit: as Jacob suggested, make sure you can connect via TCP/IP (I assume it's used by the driver). To do so launch SQL Management Studio and in connection preferences set: Authentication - SQL Server authentication, login - sa, password - your password. Then click Options, connection properties and select tcp/ip as protocol. Check if you can login.
Also check these things:
http://msdn.microsoft.com/en-us/library/bb909712(v=vs.90).aspx
http://msdn.microsoft.com/en-us/library/bb909725(v=vs.90).aspx
To check whether TCP/IP is enabled and the port is not blocked you can use "telnet 1433". Until telnet doesn't connect, jTDS won't either.
e.g, c:>telnet servername 1433
to enable telnet client on windows
http://social.technet.microsoft.com/wiki/contents/articles/910.how-to-enable-telnet-client-in-windows-7.aspx

Categories