Permalink
Browse files

Added key reading to callback, added forcing, opts with default location

  • Loading branch information...
1 parent 2252a2d commit b175d739051a2d64954290d208a50e33202fa32c @ericvicenti committed Mar 24, 2013
Showing with 107 additions and 26 deletions.
  1. +19 −9 README.md
  2. +1 −1 package.json
  3. +78 −13 src/ssh-keygen.js
  4. +9 −3 test.js
View
@@ -24,15 +24,16 @@ var location = __dirname + '/foo_rsa';
var comment = 'joe@foobar.com';
var password = 'keypassword'; // false and undefined will convert to an empty pw
-keygen(location, comment, {
- password: password
-}, function(err){
- if(err) return console.log('Something went wrong!');
- console.log('Keys created!')
- var private = fs.readFileSync( location );
- var public = fs.readFileSync( location + '.pub');
- console.log('private key: '+private);
- console.log('public key: '+public);
+keygen({
+ location: location,
+ comment: comment,
+ password: password,
+ read: true
+}, function(err, out){
+ if(err) return console.log('Something went wrong: '+err);
+ console.log('Keys created!');
+ console.log('private key: '+out.key);
+ console.log('public key: '+out.pubKey);
});
```
@@ -60,6 +61,15 @@ The key's randomart image is:
+-----------------+
```
+### Parameters
+
+* location, desired location for the key. The public key will be at the location + `.pub`
+* read, should the callback have the key files read into it, defaults true
+* force, destroy pre-existing files with the location name and the public key name, defaults true
+* destroy, destroy the key files once they have been read
+* comment, the comment that should be embedded into the key
+* password, the password for the key, defaults empty
+
### Note
It is advisable to generate your keys on a machine with a significant random source like one with a mouse/trackpad.
View
@@ -1,6 +1,6 @@
{
"name": "ssh-keygen",
- "version": "0.1.0",
+ "version": "0.2.0",
"author": {
"name": "Eric Vicenti",
"url": "http://github.com/ericvicenti"
View
@@ -1,19 +1,53 @@
var spawn = require('child_process').spawn;
+var _ = require('underscore');
var fs = require('fs');
+var os = require('os');
var log = function(a){
if(process.env.VERBOSE) console.log('ssh-keygen: '+a);
}
-
-function ssh_keygen(location, comment, opts, callback){
+function checkAvailability(location, force, callback){
+ var pubLocation = location+'.pub';
+ log('checking availability: '+location);
+ fs.exists(location, function(keyExists){
+ log('checking availability: '+pubLocation);
+ fs.exists(pubLocation, function(pubKeyExists){
+ doForce(keyExists, pubKeyExists);
+ })
+ });
+ function doForce(keyExists, pubKeyExists){
+ if(!force && keyExists) return callback(location+' already exists');
+ if(!force && pubKeyExists) return callback(pubLocation+' already exists');
+ if(!keyExists && !pubKeyExists) return callback();
+ if(keyExists){
+ log('removing '+location);
+ fs.unlink(location, function(err){
+ if(err) return callback(err);
+ keyExists = false;
+ if(!keyExists && !pubKeyExists) callback();
+ });
+ }
+ if(pubKeyExists) {
+ log('removing '+pubLocation);
+ fs.unlink(pubLocation, function(err){
+ if(err) return callback(err);
+ pubKeyExists = false;
+ if(!keyExists && !pubKeyExists) callback();
+ });
+ }
+ }
+}
+function ssh_keygen(location, opts, callback){
opts || (opts={});
- if(!comment) comment = '';
+
+ var pubLocation = location+'.pub';
+ if(!opts.comment) opts.comment = '';
if(!opts.password) opts.password = '';
var keygen = spawn('ssh-keygen', [
'-t','rsa',
'-b','2048',
- '-C', comment,
+ '-C', opts.comment,
'-N', opts.password,
'-f', location
]);
@@ -22,23 +56,54 @@ function ssh_keygen(location, comment, opts, callback){
log('stdout:'+a);
});
+ var read = opts.read;
+ var destroy = opts.destroy;
+
keygen.on('exit',function(){
log('exited');
- if(callback) callback();
+ if(read){
+ log('reading key '+location);
+ fs.readFile(location, {encoding: 'utf8'}, function(err, key){
+ if(destroy){
+ log('destroying key '+location);
+ fs.unlink(location, function(err){
+ if(err) return callback(err);
+ readPubKey();
+ });
+ } else readPubKey();
+ function readPubKey(){
+ log('reading pub key '+pubLocation);
+ fs.readFile(pubLocation, {encoding: 'utf8'}, function(err, pubKey){
+ if(destroy){
+ log('destroying pub key '+pubLocation);
+ fs.unlink(pubLocation, function(err){
+ if(err) return callback(err);
+ return callback(undefined, { key: key, pubKey: pubKey });
+ });
+ } else callback(undefined, { key: key, pubKey: pubKey });
+ });
+ }
+ });
+ } else if(callback) callback();
});
keygen.stderr.on('data',function(a){
log('stderr:'+a);
});
};
-module.exports = function(location, comment, opts, callback){
- // Make sure the keys dont already exist
- fs.exists(location, function(exists){
- if(exists) return callback(true);
- fs.exists(location+'.pub', function(exists){
- if(exists) return callback(true);
- ssh_keygen(location, comment, opts, callback);
- })
+module.exports = function(opts, callback){
+ var location = opts.location;
+ if(!location) location = os.tmpDir()+'id_rsa';
+
+ if(_.isUndefined(opts.read)) opts.read = true;
+ if(_.isUndefined(opts.force)) opts.force = true;
+
+ checkAvailability(location, opts.force, function(err){
+ if(err){
+ log('availability err '+err);
+ return callback(err);
+ }
+ ssh_keygen(location, opts, callback);
});
};
View
12 test.js
@@ -1,7 +1,13 @@
var keygen = require('./src/ssh-keygen');
console.log('Generating key pair')
-keygen(__dirname + '/foobar_rsa', 'john@doe.com', 'keyPassword', function(err){
- if(err) console.log('There was a problem');
- else console.log('Done generating key pairs in '+__dirname);
+
+keygen({
+ comment: 'john@doe.com',
+ read: true
+}, function(err, out){
+ if(err) return console.log('There was a problem : '+err);
+ console.log('Done generating key pairs');
+ console.log(out.key)
+ console.log(out.pubKey)
});

0 comments on commit b175d73

Please sign in to comment.