Skip to content

Commit 6d5ce7d

Browse files
gompaacburdine
authored andcommitted
fix(mysql): improve password compatibility of mysql user
closes #511 - improve password compatibility with mysql password requirements
1 parent a112533 commit 6d5ce7d

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

extensions/mysql/index.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
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');
7+
const generator = require('generate-password');
88

99
class MySQLExtension extends cli.Extension {
1010
setup(cmd, argv) {
@@ -77,7 +77,12 @@ class MySQLExtension extends cli.Extension {
7777
}
7878

7979
createUser(ctx, dbconfig) {
80-
const randomPassword = crypto.randomBytes(10).toString('hex');
80+
const randomPassword = generator.generate({
81+
length: 10,
82+
numbers: true,
83+
symbols: true,
84+
strict: true
85+
});
8186

8287
let username;
8388

extensions/mysql/test/extension-spec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,15 @@ describe('Unit: Mysql extension', function () {
192192
return instance.createUser(ctx, {host: 'localhost'}).then(() => {
193193
expect(queryStub.calledThrice).to.be.true;
194194
expect(queryStub.args[0][0]).to.equal('SET old_passwords = 0;');
195-
expect(queryStub.args[1][0]).to.match(/^SELECT PASSWORD\('[0-9A-Fa-f]*'\) AS password;$/);
195+
expect(queryStub.args[1][0]).to.match(/^SELECT PASSWORD\('[a-zA-Z0-9!@#$%^&*()+_\-=}{[\]|:;"/?.><,`~]*'\) AS password;$/);
196196
expect(queryStub.args[2][0]).to.match(/^CREATE USER 'ghost-[0-9]{1,4}'@'localhost' IDENTIFIED WITH mysql_native_password AS '\*[0-9A-F]*';$/);
197197
expect(logStub.calledThrice).to.be.true;
198198
expect(logStub.args[0][0]).to.match(/disabled old_password/);
199199
expect(logStub.args[1][0]).to.match(/created password hash/);
200200
expect(logStub.args[2][0]).to.match(/successfully created new user/);
201201
expect(ctx.mysql).to.exist;
202202
expect(ctx.mysql.username).to.match(/^ghost-[0-9]{1,4}$/);
203-
expect(ctx.mysql.password).to.match(/^[0-9A-Fa-f]*$/);
203+
expect(ctx.mysql.password).to.match(/^[a-zA-Z0-9!@#$%^&*()+_\-=}{[\]|:;"/?.><,`~]*$/);
204204
});
205205
});
206206

@@ -219,7 +219,7 @@ describe('Unit: Mysql extension', function () {
219219
return instance.createUser(ctx, {host: 'localhost'}).then(() => {
220220
expect(queryStub.callCount).to.equal(4);
221221
expect(queryStub.args[0][0]).to.equal('SET old_passwords = 0;');
222-
expect(queryStub.args[1][0]).to.match(/^SELECT PASSWORD\('[0-9A-Fa-f]*'\) AS password;$/);
222+
expect(queryStub.args[1][0]).to.match(/^SELECT PASSWORD\('[a-zA-Z0-9!@#$%^&*()+_\-=}{[\]|:;"/?.><,`~]*'\) AS password;$/);
223223
expect(queryStub.args[2][0]).to.match(/^CREATE USER 'ghost-[0-9]{1,4}'@'localhost' IDENTIFIED WITH mysql_native_password AS '\*[0-9A-F]*';$/);
224224
expect(queryStub.args[3][0]).to.match(/^CREATE USER 'ghost-[0-9]{1,4}'@'localhost' IDENTIFIED WITH mysql_native_password AS '\*[0-9A-F]*';$/);
225225
expect(logStub.callCount).to.equal(4);
@@ -229,7 +229,7 @@ describe('Unit: Mysql extension', function () {
229229
expect(logStub.args[3][0]).to.match(/successfully created new user/);
230230
expect(ctx.mysql).to.exist;
231231
expect(ctx.mysql.username).to.match(/^ghost-[0-9]{1,4}$/);
232-
expect(ctx.mysql.password).to.match(/^[0-9A-Fa-f]*$/);
232+
expect(ctx.mysql.password).to.match(/^[a-zA-Z0-9!@#$%^&*()+_\-=}{[\]|:;"/?.><,`~]*$/);
233233
});
234234
});
235235

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"find-plugins": "1.1.3",
5858
"fkill": "5.1.0",
5959
"fs-extra": "4.0.2",
60+
"generate-password": "1.3.0",
6061
"ghost-ignition": "2.8.16",
6162
"got": "7.1.0",
6263
"inquirer": "3.3.0",

yarn.lock

+4
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,10 @@ functional-red-black-tree@^1.0.1:
14641464
version "1.0.1"
14651465
resolved "https://registry.yarnpkg.com/functional-red-black-tree/-/functional-red-black-tree-1.0.1.tgz#1b0ab3bd553b2a0d6399d29c0e3ea0b252078327"
14661466

1467+
1468+
version "1.3.0"
1469+
resolved "https://registry.yarnpkg.com/generate-password/-/generate-password-1.3.0.tgz#4da4c154530d21c1995a77aac5a3ea04882fc8ad"
1470+
14671471
get-caller-file@^1.0.1:
14681472
version "1.0.2"
14691473
resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-1.0.2.tgz#f702e63127e7e231c160a80c1554acb70d5047e5"

0 commit comments

Comments
 (0)