Hi
I’ve prepared scripts to connect to custom database (it’s google cloud sql running mysql). It require certificates to authenticate. My login script looks like following:
function login(email, password, callback) {
var connectionConfiguration = {
host: configuration.DB_HOST,
user: configuration.DB_USER,
password: configuration.DB_PASSWORD,
database: configuration.DB_NAME,
ssl: {
rejectUnauthorized: false,
key: new Buffer(configuration.DB_CLIENT_KEY, 'base64'),
cert: new Buffer(configuration.DB_CLIENT_CERT, 'base64')
}
};
var connection = mysql(connectionConfiguration);
connection.connect();
var query = "SELECT id, email, password " +
"FROM users WHERE email = ?";
connection.query(query, [email], function (err, results) {
if (err) return callback(err);
if (results.length === 0) return callback(new WrongUsernameOrPasswordError(email));
var user = results[0];
bcrypt.compare(password, user.password, function (err, isValid) {
if (err) {
callback(err);
} else if (!isValid) {
callback(new WrongUsernameOrPasswordError(email));
} else {
callback(null, {
id: user.id.toString(),
email: user.email
});
}
});
});
}
Configuration variables are exported. When I try to run it I got: ER_ACCESS_DENIED_ERROR: Access denied for user ‘root’@‘52.28.56.226’ (using password: YES). Ip addresses are whitelisted.
When connected to debug, and modified script a bit to:
function login(email, password, callback) {
var connectionConfiguration = {
host: configuration.DB_HOST,
user: configuration.DB_USER,
password: configuration.DB_PASSWORD,
database: configuration.DB_NAME,
ssl: {
rejectUnauthorized: false,
key: new Buffer(configuration.DB_CLIENT_KEY, 'base64'),
cert: new Buffer(configuration.DB_CLIENT_CERT, 'base64')
}
};
var connection = mysql(connectionConfiguration);
connection.connect(function(err) {
if (err) {
console.log(connectionConfiguration);
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
var query = "SELECT id, email, password " +
"FROM users WHERE email = ?";
connection.query(query, [email], function (err, results) {
if (err) return callback(err);
if (results.length === 0) return callback(new WrongUsernameOrPasswordError(email));
var user = results[0];
bcrypt.compare(password, user.password, function (err, isValid) {
if (err) {
callback(err);
} else if (!isValid) {
callback(new WrongUsernameOrPasswordError(email));
} else {
callback(null, {
id: user.id.toString(),
email: user.email
});
}
});
});
}
and going to debug I can see this:
error connecting: Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'52.28.56.226' (using password: YES)
at Handshake.Sequence._packetToError (/data/sandbox/node_modules/auth0-authz-rules-api/node_modules/mysql/lib/protocol/sequences/Sequence.js:30:14)
at Handshake.ErrorPacket (/data/sandbox/node_modules/auth0-authz-rules-api/node_modules/mysql/lib/protocol/sequences/Handshake.js:67:18)
at Protocol._parsePacket (/data/sandbox/node_modules/auth0-authz-rules-api/node_modules/mysql/lib/protocol/Protocol.js:197:24)
at Parser.write (/data/sandbox/node_modules/auth0-authz-rules-api/node_modules/mysql/lib/protocol/Parser.js:62:12)
at Protocol.write (/data/sandbox/node_modules/auth0-authz-rules-api/node_modules/mysql/lib/protocol/Protocol.js:37:16)
at Socket.ondata (_stream_readable.js:536:20)
at emitOne (events.js:82:20)
at Socket.emit (events.js:169:7)
at readableAddChunk (_stream_readable.js:153:18)
at Socket.Readable.push (_stream_readable.js:111:10)
--------------------
at Protocol._enqueue (/data/sandbox/node_modules/auth0-authz-rules-api/node_modules/mysql/lib/protocol/Protocol.js:110:26)
at Protocol.handshake (/data/sandbox/node_modules/auth0-authz-rules-api/node_modules/mysql/lib/protocol/Protocol.js:42:41)
at Connection.connect (/data/sandbox/node_modules/auth0-authz-rules-api/node_modules/mysql/lib/Connection.js:81:18)
at login (/data/webtask.js:21:16)
at /data/webtask.js:54:2
at Async.series.createError.code (/data/sandbox/lib/sandbox.js:743:33)
at /data/sandbox/node_modules/async/dist/async.js:3816:26
at replenish (/data/sandbox/node_modules/async/dist/async.js:982:17)
at iterateeCallback (/data/sandbox/node_modules/async/dist/async.js:967:17)
at /data/sandbox/node_modules/async/dist/async.js:944:16
Object is configured correctly and as a test I’ve wrote the script on my local machine. It’s working. Can I ask for help in that?