From 71287bdcb8054b90c417718cd216a83820bdbc10 Mon Sep 17 00:00:00 2001 From: PV Date: Wed, 19 Jul 2017 12:28:52 -0700 Subject: [PATCH] Feature: Add authentication with CLI params (#12) Feature: Add CLI params for authentication --- .travis.yml | 12 +++ README.md | 50 ++++++++++- bin/dpd | 10 ++- lib/cli/start.js | 214 +++++++++++++++++++++++++---------------------- lib/cli/stop.js | 3 + 5 files changed, 188 insertions(+), 101 deletions(-) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..022ef45 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,12 @@ +language: node_js +cache: + directories: + - node_modules +sudo: false +services: mongodb +node_js: + - "7" + - "6" + - "5" + - "4" + - "0.12" diff --git a/README.md b/README.md index dd1d1d8..c787d10 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,55 @@ Deployd requires a running MongoDB to start sucessfully. Check the [Deployd Requ ```bash $ dpd create hello $ cd hello -$ dpd -d +$ dpd +``` +To start dpd with Database authentication: + +``` +dpd --host "127.0.0.1" -P '27017' -n "mymongodb" -u "myusername" -s "mypassword" + +or + +dpd --host "127.0.0.1" -P '27017' -n "mymongodb" -a "myusername:mypassword" + +``` + +## dpd Command Options + +``` + Usage: dpd [options] [command] + + + Options: + + -V, --version output the version number + -m, --mongod [path] path to mongod executable (defaults to `mongod`) + -p, --port [port] port to host server (defaults to 2403) + -w, --wait wait for input before exiting + -d, --dashboard start the dashboard immediately + -o, --open open in a browser + -e, --environment [env] defaults to development + -H, --host [host] specify host for mongo server + -P, --mongoPort [mongoPort] mongodb port to connect to + -n, --dbname [dbname] name of the mongo database + -a, --auth usesrname:password mongo server credentials + -u, --username The user to authenticate as + -s, --password The user's password + -c, --dbconn The MongoDB Connection String + --deploydPath [deploydPath] allow overriding the path to deployd main script + -h, --help output usage information + + + Commands: + + create [project-name] create a project in a new directory + eg. `dpd create my-app` + keygen generate a key for remote access (./.dpd/keys.json) + showkey shows current key for connecting to remote dashboard (./.dpd/keys.json) + * [default] start the server in the current project in development mode + with an interactive shell/repl for interacting with the running server + e.g. dpd (starts server in current directory), + dpd my-app/app.dpd (starts app from file) ``` ## License diff --git a/bin/dpd b/bin/dpd index 55e3ba7..8d35149 100755 --- a/bin/dpd +++ b/bin/dpd @@ -18,9 +18,14 @@ cli.program .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') + .option('-a, --auth ', ' usesrname:password mongo server credentials') + .option('-u, --username ', 'The user to authenticate as') + .option('-s, --password ', 'The user\'s password') + .option('-c, --dbconn ', 'The MongoDB Connection String') .option(' --deploydPath [deploydPath]', 'allow overriding the path to deployd main script'); + + cli.program .command('create [project-name]') .description('\tcreate a project in a new directory\n\teg. `dpd create my-app`') @@ -52,4 +57,5 @@ cli.program cli.program.parse(process.argv); -if (cli.program.args.length === 0) cli.start(); \ No newline at end of file +if (cli.program.args.length === 0) cli.start(); + \ No newline at end of file diff --git a/lib/cli/start.js b/lib/cli/start.js index 426b4d0..942538d 100644 --- a/lib/cli/start.js +++ b/lib/cli/start.js @@ -2,35 +2,36 @@ * External dependencies */ var http = require('http'), - program = require('commander'), - fs = require('fs'), - semver = require('semver'), - Step = require('step'), - shelljs = require('shelljs/global'), - path = require('path'); + program = require('commander'), + fs = require('fs'), + semver = require('semver'), + Step = require('step'), + shelljs = require('shelljs/global'), + path = require('path'); /** - * Local modules + * Local modules */ var stop = require('./stop'), - mongod = require('../util/mongod'), - repl = require('../client/repl'), - packageInfo = require('../../package'), - latestversionFile = path.join(__dirname, '../../.latestversion'), - createServer = require('./createserver'); + mongod = require('../util/mongod'), + repl = require('../client/repl'), + packageInfo = require('../../package'), + latestversionFile = path.join(__dirname, '../../.latestversion'), + createServer = require('./createserver'); /** * Start the server */ -var start = function(file) { +var start = function (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, - credentials; + host = program.host || '127.0.0.1', + dbname = program.dbname || '-deployd', + mongoPort = generatePort(), + env = program.environment || process.env.DPD_ENV || 'development', + retries = 0, + credentials = {}; + if (!port) { port = 2403; @@ -38,7 +39,7 @@ var start = function(file) { } if (program.mongoPort) { - mongoPort = Number(program.mongoPort); + mongoPort = Number(program.mongoPort); } if (file) { @@ -62,46 +63,62 @@ var start = function(file) { if (!test('-d', './data')) mkdir('-p', './data'); if (program.auth && program.host === undefined) { - console.error("Authentication requires the '-h' host flag... exiting."); - process.exit(); + 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 - ); + + + + + function setCredentials(username, password) { + Step(function () { + var next = this; + if (username && username !== '') { + credentials.username = username; + next(); } else { - startup(); + console.error('Username cannot be blank.'); + process.exit(); } - } else { - mongod.restart(program.mongod || 'mongod', env, mongoPort, startup); + }, + function () { + var next = this; + if (password && password !== '') { + credentials.password = password; + next(); + } else { + console.error('Password cannot be blank.'); + process.exit(); + } + }, + startup + ); } + + + if (program.host) { + if (program.auth) { + var auth = program.auth.split(":"); + username = auth[0]; + password = auth[1]; + setCredentials(username, password); + } else if (program.username || program.password) { + + setCredentials(program.username, program.password); + } + 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 \" to create a new app"); @@ -109,68 +126,69 @@ var start = function(file) { stop(1); } - function startup (err) { - if (err) { - console.log("Failed to start MongoDB (Make sure 'mongod' are in your $PATH or use dpd --mongod option. Ref: http://docs.deployd.com/docs/basics/cli.html)"); - return stop(1); - } + function startup(err) { + if (err) { + console.log("Failed to start MongoDB (Make sure 'mongod' are in your $PATH or use dpd --mongod option. Ref: http://docs.deployd.com/docs/basics/cli.html)"); + return stop(1); + } - 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 = createServer(options); - dpd.on('listening', onListening); - dpd.on('error', onError); - dpd.listen(); - dpd.deploydPath = program.deploydPath; - - function onListening () { - console.info('listening on port', options.port); - var commands = repl(dpd); - if (program.dashboard) { - commands.dashboard(); - } else if (program.open) { - commands.open(); - } + 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 = createServer(options); + dpd.on('listening', onListening); + dpd.on('error', onError); + dpd.listen(); + dpd.deploydPath = program.deploydPath; + + function onListening() { + console.info('listening on port', options.port); + var commands = repl(dpd); + if (program.dashboard) { + commands.dashboard(); + } else if (program.open) { + commands.open(); } + } - function onError (err) { - if (err.code === "EADDRINUSE") { - console.error(); - console.error("ERROR: port " + options.port + " is already in use"); - if (retries > 0) { - options.port++; - console.log("Trying again on port " + options.port + "..."); - console.log(); - retries--; - dpd = createServer(options); - dpd.on('listening', onListening); - dpd.on('error', onError); - dpd.listen(); - } else { - process.exit(); - } + function onError(err) { + if (err.code === "EADDRINUSE") { + console.error(); + console.error("ERROR: port " + options.port + " is already in use"); + if (retries > 0) { + options.port++; + console.log("Trying again on port " + options.port + "..."); + console.log(); + retries--; + dpd = createServer(options); + dpd.on('listening', onListening); + dpd.on('error', onError); + dpd.listen(); } else { - console.error(err); process.exit(); } + } else { + console.error(err); + process.exit(); } } + } } /** * Port generation */ function generatePort() { - var portRange = [ 3000, 9000 ]; + var portRange = [3000, 9000]; return Math.floor(Math.random() * (portRange[1] - portRange[0])) + portRange[0]; } function checkForUpdates() { - http.get('http://registry.npmjs.org/deployd-cli', function(err, res, body) { + http.get('http://registry.npmjs.org/deployd-cli', function (err, res, body) { if (!err) { var json; try { diff --git a/lib/cli/stop.js b/lib/cli/stop.js index 8110110..4cc0410 100644 --- a/lib/cli/stop.js +++ b/lib/cli/stop.js @@ -1,6 +1,9 @@ /** * Start the server */ +var program = require('commander'); + + var stop = function(code) { var fn = function() { exit(code);