Skip to content

Commit c4bc2a1

Browse files
author
imzhengfei
committed
fix(mysql): improve random password for mysql user
1 parent 53a3425 commit c4bc2a1

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

extensions/mysql/index.js

+53-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const Promise = require('bluebird');
44
const mysql = require('mysql');
5-
const crypto = require('crypto');
65
const omit = require('lodash/omit');
76
const cli = require('../../lib');
87

@@ -77,7 +76,59 @@ class MySQLExtension extends cli.Extension {
7776
}
7877

7978
createUser(ctx, dbconfig) {
80-
const randomPassword = crypto.randomBytes(10).toString('hex');
79+
// Generate random MySQL password
80+
const generateRandomPassword = () => {
81+
/**
82+
* Random Sort
83+
* @param {Array} array
84+
* @return {Array}
85+
*/
86+
const randomSort = (array) => array.sort(() => Math.random() > .5 ? -1 : 1);
87+
88+
/**
89+
* Random Number
90+
* @param {Number} length
91+
* @return {String}
92+
*/
93+
const randomNumber = (length) => {
94+
let _str = '';
95+
for (let i = 0; i < length; i = i + 1) {
96+
_str += String.fromCharCode(Math.round(Math.random() * 9) + 48);
97+
}
98+
return _str;
99+
}
100+
101+
/**
102+
* Random Upper Case
103+
* @param {Number} length
104+
* @return {String}
105+
*/
106+
const randomUpperCase = (length) => {
107+
let _str = '';
108+
for (let i = 0; i < length; i = i + 1) {
109+
_str += String.fromCharCode(Math.round(Math.random() * 25) + 65);
110+
}
111+
return _str;
112+
}
113+
114+
/**
115+
* Random Character
116+
* @param {Number} length
117+
* @return {String}
118+
*/
119+
const randomChar = (length) => {
120+
const chars = ['~', '!', '@', '#', '$'];
121+
return randomSort(chars).join('').substr(0, length);
122+
}
123+
124+
const allChars = randomNumber(4)
125+
+ randomUpperCase(4)
126+
+ randomUpperCase(4).toLowerCase()
127+
+ randomChar(4);
128+
129+
return randomSort(allChars.split('')).join('');
130+
}
131+
const randomPassword = generateRandomPassword();
81132

82133
// IMPORTANT: we generate random MySQL usernames
83134
// e.g. you delete all your Ghost instances from your droplet and start from scratch, the MySQL users would remain and the CLI has to generate a random user name to work

extensions/mysql/test/extension-spec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,14 @@ describe('Unit: Mysql extension', function () {
192192
expect(queryStub.calledThrice).to.be.true;
193193
expect(queryStub.args[0][0]).to.match(/^CREATE USER 'ghost-[0-9]{1,4}'@'localhost' IDENTIFIED WITH mysql_native_password;$/);
194194
expect(queryStub.args[1][0]).to.equal('SET old_passwords = 0;');
195-
expect(queryStub.args[2][0]).to.match(/^SET PASSWORD FOR 'ghost-[0-9]{1,4}'@'localhost' = PASSWORD\('[0-9A-Fa-f]*'\);$/);
195+
expect(queryStub.args[2][0]).to.match(/^SET PASSWORD FOR 'ghost-[0-9]{1,4}'@'localhost' = PASSWORD\('[0-9A-Za-z~!@#$%]*'\);$/);
196196
expect(logStub.calledThrice).to.be.true;
197197
expect(logStub.args[0][0]).to.match(/created new user/);
198198
expect(logStub.args[1][0]).to.match(/disabled old_password/);
199199
expect(logStub.args[2][0]).to.match(/successfully created password for user/);
200200
expect(ctx.mysql).to.exist;
201201
expect(ctx.mysql.username).to.match(/^ghost-[0-9]{1,4}$/);
202-
expect(ctx.mysql.password).to.match(/^[0-9A-Fa-f]*$/);
202+
expect(ctx.mysql.password).to.match(/^[0-9A-Za-z~!@#$%]*$/);
203203
});
204204
});
205205

@@ -219,15 +219,15 @@ describe('Unit: Mysql extension', function () {
219219
expect(queryStub.args[0][0]).to.match(/^CREATE USER 'ghost-[0-9]{1,4}'@'localhost' IDENTIFIED WITH mysql_native_password;$/);
220220
expect(queryStub.args[1][0]).to.match(/^CREATE USER 'ghost-[0-9]{1,4}'@'localhost' IDENTIFIED WITH mysql_native_password;$/);
221221
expect(queryStub.args[2][0]).to.equal('SET old_passwords = 0;');
222-
expect(queryStub.args[3][0]).to.match(/^SET PASSWORD FOR 'ghost-[0-9]{1,4}'@'localhost' = PASSWORD\('[0-9A-Fa-f]*'\);$/);
222+
expect(queryStub.args[3][0]).to.match(/^SET PASSWORD FOR 'ghost-[0-9]{1,4}'@'localhost' = PASSWORD\('[0-9A-Za-z~!@#$%]*'\);$/);
223223
expect(logStub.callCount).to.equal(4);
224224
expect(logStub.args[0][0]).to.match(/user exists, re-trying user creation/);
225225
expect(logStub.args[1][0]).to.match(/created new user/);
226226
expect(logStub.args[2][0]).to.match(/disabled old_password/);
227227
expect(logStub.args[3][0]).to.match(/successfully created password for user/);
228228
expect(ctx.mysql).to.exist;
229229
expect(ctx.mysql.username).to.match(/^ghost-[0-9]{1,4}$/);
230-
expect(ctx.mysql.password).to.match(/^[0-9A-Fa-f]*$/);
230+
expect(ctx.mysql.password).to.match(/^[0-9A-Za-z~!@#$%]*$/);
231231
});
232232
});
233233

0 commit comments

Comments
 (0)