Skip to content

Commit

Permalink
fix(root-user): only skip exiting inside root install
Browse files Browse the repository at this point in the history
no issue
- if outside of a root install (or a root install has been fixed),
commands will still fail as root
  • Loading branch information
acburdine authored and aileen committed Feb 7, 2018
1 parent b161432 commit 8fc693a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 49 deletions.
15 changes: 11 additions & 4 deletions lib/utils/check-root-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,27 @@ function checkRootUser(command) {
console.error(`${chalk.yellow('We discovered that you are using the Digitalocean One-Click install.')}
You need to create a user with regular account privileges and migrate your installation to use this user.
Please follow the steps here: ${chalk.underline.green('https://docs.ghost.org/docs/troubleshooting#section-fix-root-user')} to fix your setup.\n`);

// TODO: remove this 4 versions after 1.5.0
if (includes(allowedCommands, command)) {
return;
}
} else if (isRootInstall()) {
console.error(`${chalk.yellow('It seems Ghost was installed using the root user.')}
You need to create a user with regular account privileges and migrate your installation to use this user.
Please follow the steps here: ${chalk.underline.green('https://docs.ghost.org/docs/troubleshooting#section-fix-root-user')} to fix your setup.\n`);

// TODO: remove this 4 versions after 1.5.0
if (includes(allowedCommands, command)) {
return;
}
} else {
console.error(`${chalk.yellow('Can\'t run command as \'root\' user.')}
Please use the user you set up in the installation process, or create a new user with regular account privileges and use this user to run 'ghost ${command}'.
See ${chalk.underline.green('https://docs.ghost.org/docs/install#section-create-a-new-user')} for more information\n`);
}

// TODO: remove this 4 versions after 1.5.0
if (!includes(allowedCommands, command)) {
process.exit(1);
}
process.exit(1);
}

module.exports = checkRootUser;
45 changes: 0 additions & 45 deletions test/unit/utils/check-root-user-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,28 +167,6 @@ describe('Unit: Utils > checkRootUser', function () {
}
});

it('throws error command run with root for non-root installs, but doesn\'t exit on `restart`', function () {
const osStub = sandbox.stub(os, 'platform').returns('linux');
const cwdStub = sandbox.stub(process, 'cwd').returns('/var/www/ghost');
const fsStub = sandbox.stub(fs, 'existsSync');
const fsStatStub = sandbox.stub(fs, 'statSync').returns({uid: 501});
const processStub = sandbox.stub(process, 'getuid').returns(0);
const exitStub = sandbox.stub(process, 'exit').throws();
const errorStub = sandbox.stub(console, 'error');

fsStub.withArgs('/root/.digitalocean_password').returns(false);
fsStub.withArgs('/var/www/ghost/.ghost-cli').returns(true);

checkRootUser('restart');
expect(cwdStub.calledOnce).to.be.true;
expect(fsStatStub.calledWithExactly('/var/www/ghost/.ghost-cli')).to.be.true;
expect(osStub.calledOnce).to.be.true;
expect(processStub.calledOnce).to.be.true;
expect(errorStub.calledOnce).to.be.true;
expect(exitStub.calledOnce).to.be.false;
expect(errorStub.args[0][0]).to.match(/Can't run command as 'root' user/);
});

it('throws error command run with root outside of valid ghost installation', function () {
const osStub = sandbox.stub(os, 'platform').returns('linux');
const cwdStub = sandbox.stub(process, 'cwd').returns('/var/www/');
Expand Down Expand Up @@ -216,27 +194,4 @@ describe('Unit: Utils > checkRootUser', function () {
expect(errorStub.args[0][0]).to.match(/Can't run command as 'root' user/);
}
});

it('throws error command run with root outside of valid ghost installation, but doesn\'t exit on `restart`', function () {
const osStub = sandbox.stub(os, 'platform').returns('linux');
const cwdStub = sandbox.stub(process, 'cwd').returns('/var/www/');
const fsStub = sandbox.stub(fs, 'existsSync');
const fsStatStub = sandbox.stub(fs, 'statSync').returns({uid: 501});
const processStub = sandbox.stub(process, 'getuid').returns(0);
const exitStub = sandbox.stub(process, 'exit').throws();
const errorStub = sandbox.stub(console, 'error');

fsStub.withArgs('/root/.digitalocean_password').returns(false);
fsStub.withArgs('/var/www/.ghost-cli').returns(false);

checkRootUser('restart');
expect(cwdStub.calledOnce).to.be.true;
expect(fsStub.calledWithExactly('/var/www/.ghost-cli')).to.be.true;
expect(fsStatStub.calledOnce).to.be.false;
expect(osStub.calledOnce).to.be.true;
expect(processStub.calledOnce).to.be.true;
expect(errorStub.calledOnce).to.be.true;
expect(exitStub.calledOnce).to.be.false;
expect(errorStub.args[0][0]).to.match(/Can't run command as 'root' user/);
});
});

0 comments on commit 8fc693a

Please sign in to comment.