Skip to content
Browse files

CLI for using the SSH agent

  • Loading branch information...
1 parent d18122f commit fc858a6bae9941a8d1d919334126bc88740e6e6a Mark Cavage committed Aug 31, 2012
Showing with 158 additions and 13 deletions.
  1. +61 −0 bin/ssha-list
  2. +79 −0 bin/ssha-sign
  3. +8 −7 lib/ssh_agent_client.js
  4. +10 −6 package.json
View
61 bin/ssha-list
@@ -0,0 +1,61 @@
+#!/usr/bin/env node
+// -*- mode: js -*-
+
+var getopt = require('posix-getopt');
+var path = require('path');
+var SSHAgentClient = require(path.resolve(__dirname, '../lib/ssh_agent_client.js'));
+
+
+
+///--- Functions
+
+function usage(msg, code) {
+ if (msg)
+ console.error(msg);
+
+ var str = 'usage: ' + path.basename(process.argv[1]);
+ console.error(str);
+ process.exit(code || 0);
+}
+
+
+function parseOptions() {
+ var option;
+ var parser = new getopt.BasicParser('h', process.argv);
+ var tmp;
+
+ while ((option = parser.getopt()) !== undefined && !option.error) {
+ switch (option.option) {
+ case 'h':
+ usage();
+ break;
+
+ default:
+ usage('invalid option', 1);
+ break;
+ }
+ }
+}
+
+
+
+///--- Mainline
+
+parseOptions();
+
+(new SSHAgentClient()).requestIdentities(function(err, keys) {
+ if (err) {
+ console.error(err.toString());
+ process.exit(1);
+ }
+
+ console.log(JSON.stringify(keys.map(function (k) {
+ return ({
+ key: k.ssh_key,
+ type: k.type,
+ name: k.comment
+ });
+ }), null, 2));
+
+ process.exit(0);
+});
View
79 bin/ssha-sign
@@ -0,0 +1,79 @@
+#!/usr/bin/env node
+// -*- mode: js -*-
+
+var getopt = require('posix-getopt');
+var path = require('path');
+var SSHAgentClient = require(path.resolve(__dirname, '../lib/ssh_agent_client.js'));
+
+
+
+///--- Functions
+
+function usage(msg, code) {
+ if (msg)
+ console.error(msg);
+
+ var str = 'usage: ' + path.basename(process.argv[1]);
+ str += ' key_name data';
+ console.error(str);
+ process.exit(code || 0);
+}
+
+
+function parseOptions() {
+ var option;
+ var parser = new getopt.BasicParser('h', process.argv);
+ var tmp;
+
+ while ((option = parser.getopt()) !== undefined && !option.error) {
+ switch (option.option) {
+ case 'h':
+ usage();
+ break;
+
+ default:
+ usage('invalid option', 1);
+ break;
+ }
+ }
+
+ if (parser.optind() >= process.argv.length)
+ usage('missing required argument: "name"');
+ if ((parser.optind() + 1) >= process.argv.length)
+ usage('missing required arguments: "data"');
+
+ var tmp = process.argv.slice(parser.optind());
+ return ({
+ name: tmp[0],
+ data: new Buffer(tmp[1])
+ });
+}
+
+
+
+///--- Mainline
+
+var client = new SSHAgentClient();
+var opts = parseOptions();
+
+client.requestIdentities(function(err, keys) {
+ if (err) {
+ console.error(err.toString());
+ process.exit(1);
+ }
+
+ var key = keys.filter(function (k) {
+ return (k.comment === opts.name);
+ }).pop();
+
+ if (!key)
+ usage(opts.name + ' invalid', 1);
+
+ client.sign(key, opts.data, function(err2, signature) {
+ if (err) {
+ console.error(err.toString());
+ process.exit(1);
+ }
+ console.log(signature.signature);
+ });
+});
View
15 lib/ssh_agent_client.js
@@ -1,6 +1,7 @@
// Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
var assert = require('assert');
var net = require('net');
+var util = require('util');
var ctype = require('ctype');
@@ -23,27 +24,27 @@ var PROTOCOL = {
function MissingEnvironmentVariableError(variable) {
this.name = 'MissingEnvironmentVariableError';
- this.variable = variable;
this.message = variable + ' was not found in your environment';
- this.stack = (new Error()).stack;
+ this.variable = variable;
+ Error.captureStackTrace(this, MissingEnvironmentVariableError);
}
-MissingEnvironmentVariableError.prototype = new Error();
+util.inherits(MissingEnvironmentVariableError, Error);
function TimeoutError(message) {
this.name = 'TimeoutError';
this.message = message;
- this.stack = (new Error()).stack;
+ Error.captureStackTrace(this, TimeoutError);
}
-TimeoutError.prototype = new Error();
+util.inherits(TimeoutError, Error);
function InvalidProtocolError(message) {
this.name = 'InvalidProtocolError';
this.message = message;
- this.stack = (new Error()).stack;
+ Error.captureStackTrace(this, InvalidProtocolError);
}
-InvalidProtocolError.prototype = new Error();
+util.inherits(InvalidProtocolError, Error);
View
16 package.json
@@ -2,23 +2,27 @@
"author": "Mark Cavage <mcavage@gmail.com>",
"name": "ssh-agent",
"description": "An API for interacting with the SSH Agent.",
- "version": "0.1.0",
+ "version": "0.2.0",
"repository": {
"type": "git",
"url": "git://github.com/mcavage/node-ssh-agent.git"
},
+ "bin": {
+ "ssha-list": "./bin/ssha-list",
+ "ssha-sign": "./bin/ssha-sign"
+ },
"main": "lib/ssh_agent_client",
"engines": {
- "node": ">= 0.4"
+ "node": ">= 0.8"
},
"dependencies": {
- "ctype": ">=0.0.2"
+ "ctype": "0.5.0",
+ "posix-getopt": "1.0.0"
},
"devDependencies": {
- "whiskey": ">=0.4.0"
+ "whiskey": "0.6.10"
},
"scripts": {
- "pretest": "which gjslint; if [[ \"$?\" = 0 ]] ; then gjslint --nojsdoc -r lib; else echo \"Missing gjslint. Skipping lint\"; fi",
- "test": "SSH_PRIVATE_KEY=$HOME/.ssh/id_rsa ./node_modules/.bin/whiskey -t \"`find tst -name *.test.js | xargs`\""
+ "test": "./node_modules/.bin/whiskey -t \"`find tst -name *.test.js | xargs`\""
}
}

0 comments on commit fc858a6

Please sign in to comment.
Something went wrong with that request. Please try again.