Skip to content

Commit

Permalink
Merge pull request deployd#54 from aaronfay/master
Browse files Browse the repository at this point in the history
Fixes deployd#52 - authenticate with remote mongo server
  • Loading branch information
ritch committed Sep 21, 2012
2 parents 96bf90f + 659976d commit 7683ba4
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 8 deletions.
68 changes: 63 additions & 5 deletions bin/dpd
Expand Up @@ -17,6 +17,7 @@ var program = require('commander')
, packageInfo = require('../package')
, latestversionFile = path.join(__dirname, '../.latestversion')
, Deployment = require('../lib/client/deploy').Deployment
, Step = require('step')
, open = require('../lib/util/open');

/**
Expand All @@ -30,7 +31,12 @@ program
.option('-w, --wait', 'wait for input before exiting')
.option('-d, --dashboard', 'start the dashboard immediately')
.option('-o, --open', 'open in a browser')
.option('-e, --environment [env]', 'defaults to development');
.option('-e, --environment [env]', 'defaults to development')
.option('-h, --host [host]', 'specify host for mongo server')
.option('-P, --mongoPort [mongoPort]', 'mongodb port to connect to')
.option('-n, --dbname [dbname]', 'name of the mongo database')
.option('-a, --auth', 'prompts for mongo server credentials')
;

/**
* Commands
Expand Down Expand Up @@ -65,14 +71,22 @@ program

function start(file) {
var port = program.port
, host = program.host || '127.0.0.1'
, dbname = program.dbname || '-deployd'
, mongoPort = generatePort()
, env = program.environment || process.env.DPD_ENV || 'development'
, retries = 0;
, retries = 0
, credentials
;

if (!port) {
port = 2403;
retries = env === 'development' && 5;
}

if (program.mongoPort) {
mongoPort = Number(program.mongoPort);
}

if (file) {
process.chdir(path.dirname(file));
Expand All @@ -92,16 +106,18 @@ function start(file) {
if (!test('-d', './.dpd/pids')) mkdir('-p', './.dpd/pids');
if (!test('-d', './data')) mkdir('-p', './data');

mongod.restart(program.mongod || 'mongod', env, mongoPort, function(err) {
function startup (err) {
if (err) {
console.log("Failed to start MongoDB");
return stop(1);
}
var options = {port: port, env: 'development', db: {host: '127.0.0.1', port: mongoPort, name: '-deployd'}};

var options = {port: port, env: 'development', db: {host: host, port: mongoPort, name: dbname}};

options.env = program.environment || process.env.DPD_ENV || options.env;
if(options.env !== 'development') console.log('starting in %s mode', options.env);

if(credentials !== undefined) options.db.credentials = credentials;
var dpd = deployd(options);
dpd.listen();

Expand Down Expand Up @@ -140,7 +156,49 @@ function start(file) {

dpd.on('listening', onListening);
dpd.on('error', onError);
});
}

if (program.auth && program.host === undefined) {
console.error("Authentication requires the '-h' host flag... exiting.");
process.exit();
}

if (program.host) {
if (program.auth) {
Step(function () {
var next = this;
credentials = {};
program.prompt('username: ', function(username){
if (username && username != '') {
credentials.username = username;
next();
} else {
console.error('Username cannot be blank.')
process.exit();
}
});
},
function () {
var next = this;
program.password('Password: ', function(pass){
if (pass && pass != '') {
credentials.password = pass;
next();
} else {
console.error('Password cannot be blank.')
process.exit();
}
});
},
startup
);
} else {
startup();
}
} else {
mongod.restart(program.mongod || 'mongod', env, mongoPort, startup);
}

} else {
console.log("This directory does not contain a Deployd app!");
console.log("Use \"dpd create <appname>\" to create a new app");
Expand Down
18 changes: 16 additions & 2 deletions lib/db.js
Expand Up @@ -125,8 +125,22 @@ function getConnection(db, fn) {
db.connected = false;
throw err;
} else {
db.connected = true;
fn(null, db._mdb);

// check for credentials
var credentials = db.options.credentials;
if (credentials && credentials.username && credentials.password) {
db._mdb.authenticate(credentials.username, credentials.password, function (err) {
if (err) {
db.connected = false;
throw err;
}
db.connected = true;
fn(null, db._mdb);
});
} else {
db.connected = true;
fn(null, db._mdb);
}
}
});
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -31,7 +31,8 @@
"underscore": "1.x.x",
"ejs": "0.7.x",
"async": "0.1.x",
"doh": "0.x.x",
"doh": ">=0.0.4",
"step": ">=0.0.5",
"forever-monitor": "1.1.x",
"keypress": "~0.1.0"
},
Expand Down
11 changes: 11 additions & 0 deletions test/db-remote.config.json
@@ -0,0 +1,11 @@
{
"warning": "This test is going to wipe your db, don't use a production machine",
"you've": "been warned...",
"host": "foo.com",
"port": 27017,
"name": "test-collection",
"credentials": {
"username": "foo",
"password": "secret"
}
}
61 changes: 61 additions & 0 deletions test/db-remote.unit.js
@@ -0,0 +1,61 @@
var fs = require('fs')
, db = require('../lib/db')
, configContents = fs.readFileSync('db-remote.config.json','utf8')
, config = JSON.parse(configContents)
, tester = db.create(config)
, store = tester.createStore('test-store')
, Store = require('../lib/db').Store
, assert = require('assert')
;

if (config.host == 'foo.com') {
console.warn('Before you run db-remote.unit.js tests, set up the configuration in "db-remote.config.json".')
return;
}

beforeEach(function(done){
store.remove(function () {
store.find(function (err, result) {
assert.equal(err, null);
assert.equal(result.length, 0);
done(err);
});
});
});

describe('db', function(){
describe('.create(options)', function(){
it('should connect to a remote database', function(done) {
store.find(function (err, empty) {
assert.equal(empty.length, 0)
done(err);
});
});
});
});

describe('store', function(){

describe('.find(query, fn)', function(){
it('should not find anything when the store is empty', function(done) {
store.find(function (err, empty) {
assert.equal(empty.length, 0);
done(err);
});
});

it('should pass the query to the underlying database', function(done) {
store.insert([{i:1},{i:2},{i:3}], function () {
store.find({i: {$lt: 3}}, function (err, result) {
assert.equal(result.length, 2);
result.forEach(function (obj) {
assert.equal(typeof obj.id, 'string')
});
done(err);
});
});
});

// TODO: convert the rest of the tests
});
});

0 comments on commit 7683ba4

Please sign in to comment.