From 6c1287c3a2ca27b1819d1ceb995cac17b5c7c7d2 Mon Sep 17 00:00:00 2001 From: Jason Williams Date: Wed, 31 Dec 2014 15:23:46 +0000 Subject: [PATCH] Allow socket permissions to be set from config Closes #4478 --- core/server/config/index.js | 24 +++++++++++++++++++++--- core/server/ghost-server.js | 12 ++++++------ core/test/unit/config_spec.js | 30 +++++++++++++++++++++++++++--- 3 files changed, 54 insertions(+), 12 deletions(-) diff --git a/core/server/config/index.js b/core/server/config/index.js index 54399821a5..a6be1155e0 100644 --- a/core/server/config/index.js +++ b/core/server/config/index.js @@ -41,11 +41,29 @@ function ConfigManager(config) { // Are we using sockets? Custom socket or the default? ConfigManager.prototype.getSocket = function () { + var socketConfig, + values = { + path: path.join(this._config.paths.contentPath, process.env.NODE_ENV + '.socket'), + permissions: '660' + }; + if (this._config.server.hasOwnProperty('socket')) { - return _.isString(this._config.server.socket) ? - this._config.server.socket : - path.join(this._config.paths.contentPath, process.env.NODE_ENV + '.socket'); + socketConfig = this._config.server.socket; + + if (_.isString(socketConfig)) { + values.path = socketConfig; + + return values; + } + + if (_.isObject(socketConfig)) { + values.path = socketConfig.path || values.path; + values.permissions = socketConfig.permissions || values.permissions; + + return values; + } } + return false; }; diff --git a/core/server/ghost-server.js b/core/server/ghost-server.js index 5e1e456148..42e150094e 100644 --- a/core/server/ghost-server.js +++ b/core/server/ghost-server.js @@ -124,19 +124,19 @@ GhostServer.prototype.start = function (externalApp) { // ## Start Ghost App return new Promise(function (resolve) { - if (config.getSocket()) { + var socketConfig = config.getSocket(); + + if (socketConfig) { // Make sure the socket is gone before trying to create another try { - fs.unlinkSync(config.getSocket()); + fs.unlinkSync(socketConfig.path); } catch (e) { // We can ignore this. } - self.httpServer = rootApp.listen( - config.getSocket() - ); + self.httpServer = rootApp.listen(socketConfig.path); - fs.chmod(config.getSocket(), '0660'); + fs.chmod(socketConfig.path, socketConfig.permissions); } else { self.httpServer = rootApp.listen( config.server.port, diff --git a/core/test/unit/config_spec.js b/core/test/unit/config_spec.js index b3c4e82db7..98545c1a26 100644 --- a/core/test/unit/config_spec.js +++ b/core/test/unit/config_spec.js @@ -519,9 +519,33 @@ describe('Config', function () { it('allows server to use a socket', function (done) { overrideConfig({server: {socket: 'test'}}); - config.load().then(function (localConfig) { - should.exist(localConfig); - localConfig.server.socket.should.equal('test'); + config.load().then(function () { + var socketConfig = config.getSocket(); + + socketConfig.should.be.an.Object; + socketConfig.path.should.equal('test'); + socketConfig.permissions.should.equal('660'); + + done(); + }).catch(done); + }); + + it('allows server to use a socket and user-defined permissions', function (done) { + overrideConfig({ + server: { + socket: { + path: 'test', + permissions: '666' + } + } + }); + + config.load().then(function () { + var socketConfig = config.getSocket(); + + socketConfig.should.be.an.Object; + socketConfig.path.should.equal('test'); + socketConfig.permissions.should.equal('666'); done(); }).catch(done);