SSL Routines error when connecting to local DB - node.js

So I have this piece of code that is responsible for checking if a connection is valid:
const sequelize = new Sequelize(req.body.database, req.body.user, req.body.password, {
host: req.body.host,
dialect: 'mssql',
port: req.body.port,
dialectOptions: {
options: {
encrypt: true,
}
}
});
sequelize.authenticate().then((err) => {
res.send('Connected');
})
.catch((err) => {
console.log(err.message)
res.send(err.message);
});
This worked perfectly when I was connecting to a local DB on my network. However, when I was trying to connect to a DB through a VPN (simulating a local network) I got the following error:
Failed to connect to 10.1.90.20:1433 - 4C300000:error:0A000102:SSL
routines:ssl_choose_client_version:unsupported
protocol:c:\ws\deps\openssl\openssl\ssl\statem\statem_lib.c:1983:
I read somewhere that adding
cryptoCredentialsDetails: {
minVersion: 'TLSv1'
}
in the options will resolve this but it just gave me a new error:
Failed to connect to 10.1.90.20:1433 - 70290000:error:0A0C0103:SSL
routines:tls_process_key_exchange:internal
error:c:\ws\deps\openssl\openssl\ssl\statem\statem_clnt.c:2255:
Any idea how to fix this?
Keep in mind that both Databases are local however one of them is on a remote network accessible through SSL VPN.

Related

Why can't my node js script connect to my mssql database running in docker, but Microsoft SQL Server Management can

I'm trying to connect node js script to MsSQL database server, but fails.
My Microsoft SQL Server Management is however able to connect to the database.
node.js code
var Connection = require('tedious').Connection;
var config = {
server: "172.168.200.35",
dialect: "mssql",
port:51433,
authentication: {
type: 'default',
options: {
userName: 'testuser',
password: 'abc123'
}
},
options: {
database: 'IoTDb'
}
};
var connection = new Connection(config);
// Attempt to connect and execute queries if connection goes through
connection.on("connect", err => {
if (err) {
console.error(err.message);
} else {
executeStatement();
}
});
When running the code, I get this error
Failed to connect to 172.168.200.35:51433:1433 - getaddrinfo ENOTFOUND 172.168.200.35:51433
I don't understand why its fails to connect port 1433, as the port is defined as 51433
However, when I try to connect to the database via this login info (same as node config variable)
It connects and I get this view over the database
For info about the database, it is running via docker on another pc, but is reachable
I moved the port to options and now the error is this
Failed to connect to 172.168.200.35:51433:51433 - getaddrinfo ENOTFOUND 172.168.200.35:51433
As this issue shows the port should be specified in options:
var config = {
server: "172.168.200.35",
dialect: "mssql",
...
options: {
database: 'IoTDb',
port:51433,
}
};

SQL Server database access issue from AWS Lambda

I'm getting this error while accessing SQL Server database from aws-lambda. Everything works fine from local machine.Only having access issue when executing the code from lambda.
ConnectionError: Failed to connect to 10.2.3.44\SQLSRVR code: ETIMEOUT
This is my code snippet, any help would be appreciated!
const { Sequelize } = require('sequelize');
const sequelize = new Sequelize('DBname', null, null, {
dialect: 'mssql',
host: '10.2.3.44', //MSSQL Server IP sample
dialectOptions: {
authentication: {
type: 'ntlm',
options: {
domain: 'addidas',
userName: "uname",
password: "pwd"
}
},
options: {
instanceName: 'SQLSRVR'
}
}
})
async function connect() {
try {
await sequelize.authenticate();
console.log('Connection has been established successfully.');
} catch (error) {
console.error('Unable to connect to the database:', error);
}
}
connect();
It was because of the network restrictions only, as assumed. So had to assign lambdas in the same network which SQL server is hosted.
Thanks for all the suggestions.

Error connecting NodeJS and SQL Server - Could not connect (sequence)

I'm trying to create a connection with SQL Server and NodeJS as shown below (SQL Server installed locally -> localhost):
It's throwing the following error: ERROR: Failed to connect to MY_SERVER:1433 - Could not connect (sequence)
This didn't work either.
I thought it was a problem with the SQL Server installation, but via Python It works well on the same database.
I'm using SQL Server 2019 and latest NodeJS version.
REFERENCE:
https://learn.microsoft.com/en-us/sql/connect/node-js/step-3-proof-of-concept-connecting-to-sql-using-node-js?view=sql-server-ver15
How to fix this error?
var config = {
server: 'MY_SERVER',
authentication: {
type: 'default',
options: {
userName: 'sa',
password: 'admin'
}
},
options: {
encrypt: false, // I've already tried with "true" also.
database: 'MyDataBase'
}
};
var connection = new Connection(config);
connection.on('connect', function(err) {
if (err) {
console.log("Error: ", err.message);
} else {
console.log("Connected");
}
});
connection.connect();
I got it!! Basically, for NodeJS get connection to SQLServer, the TCP/IP Protocol must be enabled for the MSSQLSERVER Instance, in the SQL Server network settings.
Ref.:
Nodejs connection with mssql showing error
https://store.oceansystems.com/knowledgebase/quickdme-faqs/sql-server-sql-express/configure-sql-express-server-host-enable-tcp-ip-firewall-settings/

Connection fail to SQL Server using NodeJS mssql msnodesqlv8 from the server

I wrote a little service in NodeJS which connects to SQL Server.
In my local machine, the connection is established successfully.
After putting my files in a remote machine, I get this error:
ConnectionError: [Microsoft][ODBC Driver Manager] Data source name not
found and no default driver specified
I use windows authentication but when the username didn't have permissions, I got a different error.
const mssql = require("mssql/msnodesqlv8");
const mypool = new mssql.ConnectionPool({
driver: 'msnodesqlv8',
server: myhost,
database: mydb,
port: 1443,
options: {
trustedConnection: true,
enableArithAbort: true
}
});
mypool.connect().then(pool => {
console.log('Connected to MSSQL (DB: mydb)');
return pool;
}).catch(err => console.log('Database Connection Failed! Bad Config: ', err))
Try modifying the connection pool as below,
const pool=new sql.ConnectionPool (config, function(err){
if(err){
console.log('Error while creating connection pool \n'+err);
}
});

Unable to use tedious with Azure database

I've a node application which do have a connection to SQL Server.
Also, I'm using database as a service from Azure.
Code Snippet :
import { Connection } from 'tedious';
import { Request } from 'tedious';
var config = {
userName: 'dbuser',
password: 'dbpassword',
server: 'mydatabase.database.windows.net',
options: {
instanceName: 'SQLEXPRESS', // Removed this line while deploying it on server as it has no instance.
database: 'dbname'
}
};
connection = new Connection(config);
connection.on('connect', function(err) {
if (err) {
console.log('error : '+err);
} else {
console.log("Connected to Database");
}
});
It has a successful connection, if done, locally.
Console Output => Connected to Database.
Deep dive done using console log :
-> Connection object is being created, but, the event ".on" is not being able to establish.
-> The connection gets established when deployed locally, while, when deployed on server, it doesn't works.
Based on the documentation here, you need to provide an additional option for encrypted connection.
Please try the following for config:
var config = {
userName: 'dbuser',
password: 'dbpassword',
server: 'mydatabase.database.windows.net',
options: {
database: 'dbname',
encrypt: true //Need to add this for connecting to Azure DB
}
};
Using this configuration, I was able to connect to my database hosted in Azure.

Resources