Connecting to MariaDB with nodejs over ssl with clear text password - mysql

I am trying to connect to mariadb instance over ssl,
var mysql = require('mysql');
var conn = mysql.createConnection({
user: 'user',
password: 'password',
debug: true,
port: '3306',
host: "host",
ssl: {
"ca": ca
}
});
conn.connect();
the ca is an array of cerificates. I am getting the following error.
Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MariaDB client
I am able to connect to the db using python with mysql.connector.
After setting it on debug mode, I can see the client is trying to use mysql_native_password authentication. I need to use mysql_clear_password.

if your server supports plugin based auth and auth switch request, you can try following code:
var mysql = require('mysql2');
var conn = mysql.createConnection({
user: 'user',
password: 'password',
debug: true,
port: '3306',
host: "host",
ssl: {
"ca": ca
},
authSwitchHandler: (data, cb) => {
if (data.pluginName === 'mysql_clear_password') {
// https://dev.mysql.com/doc/internals/en/clear-text-authentication.html
var password = 'password\0';
var buffer = Buffer.from(password);
cb(null, buffer);
}
});
unfortunately initial auth type is always mysql_native with node-mysql2 ( I hope to fix this soon ), so you need both auth_plugin and auth_switch support enabled on server
Edit: Syntax

Related

NodeJS / MariaDB Connector -- createConnection() does not return proper connection object

Node.js is installed in the following version on my Ubuntu 18/04 machine:
node -v
v14.16.0
The following modules are installed:
sudo npm list -g --depth=0
/usr/lib
├── mariadb#2.5.3
└── npm#6.14.11
The required package
The relevant code in my app file maria.js looks like this:
const mariadb = require('mariadb');
let conn = mariadb.createConnection({
host: 'localhost',
database: 'db1',
user:'dbuser1',
password: 'dbpwd',
port: 3306
});
conn.connect(function(err){
if(err){
console.log('Database connection error');
}else{
console.log('Database connection successful');
}
});
The required package 'mariadb' is satisfied and createConnection() does not return an error.
However, getting to the connect() statement, NodeJS returns the following:
conn.connect(function(err){
^
TypeError: conn.connect is not a function
at Object.<anonymous> (/home/juerg/bin/node/maria.js:12:6)
So looks like that createConnection() returns an empty JS object. Any other Connection class method won't work either, and I can't discover an error in this short code fragment.
mariadb.createConnection returns a promise so you would have to await for it to return the connection, like:
let conn = await mariadb.createConnection({
host: 'localhost',
database: 'db1',
user:'dbuser1',
password: 'dbpwd',
port: 3306
});
Also there might not be a need to call conn.connect() separately since it is already done in createConnection: Source Code
Or if you do not want to make your function async then you can do something like:
mariadb.createConnection({
host: 'localhost',
database: 'db1',
user:'dbuser1',
password: 'dbpwd',
port: 3306
}).then((conn) => {
// Do here what you want to do with MySQL connection...
});
As per the MariaDB connector documentation there are two available API's, Promise and Callback, Promise is the default API which you require and use in the following manner.
const mariadb = require("mariadb");
mariadb.createConnection({
host: 'localhost',
database: 'database',
user:'username',
password: 'password',
port: 3306
}).then(conn => {
// Execute a query/do whatever you need with the connection object here.
});
Alternatively, you can use the Callback API (which to note, is for compatibility with the mysql and mysql2 modules) by modifiyig the require statement and then connecting as follows:
const mariadb = require("mariadb/callback");
const conn = mariadb.createConnection({
host: 'localhost',
database: 'database',
user:'username',
password: 'password',
port: 3306
});
conn.connect(err => {
if (err) return console.log("Failed to connect");
console.log(`Successfully connected to mariadb server: ${conn.serverVersion()}`);
});
// Execute a query/do whatever you need with the connection object here.

MySQL error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client

Could someone tell what error "ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client" means?
My code:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "user",
password: "password",
database: 'database'
});
con.connect(function(err) {
if (err) throw 'THE ERROR IS: ' + err;
console.log("Connected!");
});
In the link that I sent for you, was added this property: insecureAuth : true.
var con = mysql.createConnection({
host: "localhost",
user: "user",
password: "password",
database: 'database',
insecureAuth : true
});
I also faced the same issue but the error got resolved by using mysql2 instead of mysql.
so type
npm install mysql2
in your terminal.
and update your code with
const mysql = require("mysql2");

How to configure sequalize & node mysql to use new aws rds root cert rds-ca-2019

As AWS changes their root ssl cert for rds services 2019, the old certificate from 2015 looses its validity 03/2020. see https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.SSL-certificate-rotation.html
How to I configure sequalize to use the new rds-ca-2019 certificate?
// current sequalize aws rds configuration as of working with 2015 cert
var sequelizeConfig = {
...
host: "xyz.rds.amazonaws.com",
dialectOptions": {
ssl: 'Amazon RDS'
}
}
I could not figure any option for adding a certificate manually using sequalize 3.x
Ensure you update to the latest node mysql package, this may resolve the issue in future.
As of now the new aws rds-ca-2019 ca certificate seems not yet to be merged. https://github.com/mysqljs/mysql/pull/2280
A temporary fix without ca validation:
var sequelizeConfig = {
...
host: "xyz.rds.amazonaws.com",
dialectOptions: {
ssl: true
}
}
and with certificate validation:
// get rds-ca-2019 certificate directly from aws https://s3.amazonaws.com/rds-downloads/rds-ca-2019-root.pem to ensure validity!!!
const fs = require('fs');
const rdsCa = fs.readFileSync(__dirname + '/rds-ca-2019-root.pem');
var sequelizeConfig = {
...
host: "xyz.rds.amazonaws.com",
dialectOptions: {
ssl: {
rejectUnauthorized: true,
ca: [rdsCa]
}
}
}
thx #jarmod for linking and the post "Best Security Practices for Amazon RDS with Sequelize" by soluto-nashville showing the solution
I think the dialectOptions parameter should not be defined inside the pool but outside.
like this:
const sequelize = new Sequelize(dbname, username, password, {
host: 'host name',
dialect: 'mysql',
dialectOptions: {
ssl: 'Amazon RDS'
},
pool: {
...
}
});
or like this:
const sequelize = new Sequelize(dbname, username, password, {
host: 'host name',
dialect: 'mysql',
ssl: 'Amazon RDS'
dialectOptions: {
...
},
pool: {
...
}
});

bug with angular2-meteor app :failed connection to mysql via meteor

i'm trying to connect to mysql database with meteor using nodets:mysql and i'm facing this error :
Unhandled rejection Error: No infromation can be fetched by your database, please check your permissions
this is my part of code :
Meteor.startup(function() {
//Start of changes
var connectionSettings = {
host: '127.0.0.1',
user: 'root',
password: '',
database: 'test'
};
var db = Mysql.connect(connectionSettings);
})
i need to add the port of mysql
Meteor.startup(function() {
//Start of changes
var connectionSettings = {
host: '127.0.0.1',
port : (mysqlport),
user: 'root',
password: '',
database: 'test'
};
var db = Mysql.connect(connectionSettings);
})

How can I use SSL with a node-mysql pooled connection?

I'm using node-mysql by felixge to make database connections via Node.JS. I'm making lots of connections, so I thought it would be a good idea to use a connection pool:
var mysql = require('mysql');
function createPool() {
var pool = mysql.createPool({
dateStrings : true,
host : '***',
user : '***',
password : '***',
database : '***'
});
return pool;
}
However, while node-mysql makes it very simple to use SSL for a normal connection:
var connection = mysql.createConnection({
host : 'localhost',
ssl : {
ca : fs.readFileSync(__dirname + '/mysql-ca.crt')
}
});
It doesn't seem to show in the documentation that the SSL options are available when utilizing a connection pool.
How can I use SSL with a node-mysql pooled connection? or rather, can it be done at all?
I re-read the docs and noticed this line:
Pools accept all the same options as a connection.
Sorry about that. Anyway, the answer is that it accepts the same options as a normal connection.
var mysql = require('mysql');
function createPool() {
var pool = mysql.createPool({
dateStrings : true,
host : '***',
user : '***',
password : '***',
database : '***',
ssl : {
ca : fs.readFileSync(__dirname + '/mysql-ca.crt')
}
});
return pool;
}