Skip to content

Commit

Permalink
Rework to use CloudAPI v.next with fingerprints instead of key names
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Cavage committed Dec 5, 2011
1 parent 120f3f5 commit 0a98a1f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 21 deletions.
2 changes: 1 addition & 1 deletion bin/sdc-createkey
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function loadNewKey(key) {
try {
return fs.readFileSync(key, 'ascii');
} catch(e) {
common.usage(usageStr, 2, 'Unable to load key ' + identity + ': ' + e);
common.usage(usageStr, 2, 'Unable to load key ' + key + ': ' + e);
}
}

Expand Down
1 change: 0 additions & 1 deletion bin/sdc-setup
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ function dumpEnvVars(url, account, keyId) {
'easier:');
console.log('export SDC_CLI_URL=' + url);
console.log('export SDC_CLI_ACCOUNT=' + account);
console.log('export SDC_CLI_KEY_ID=' + keyId);
}


Expand Down
32 changes: 23 additions & 9 deletions lib/cli_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var fs = require('fs');
var path = require('path');
var url = require('url');

var httpSignature = require('http-signature');
var nopt = require('nopt');
var restify = require('restify');
var SSHAgentClient = require('ssh-agent');
Expand All @@ -20,7 +21,7 @@ url.name = 'url';
///--- Globals

var log = restify.log;

var getFingerprint = httpSignature.sshKeyFingerprint;

///--- Internal Functions

Expand Down Expand Up @@ -99,7 +100,25 @@ function loadSigningKey(parsed, callback) {
process.exit(2);
}
parsed.signingKey = file;
return callback(parsed);

if (parsed.keyId)
return callback(parsed);

fs.readFile(parsed.identity + '.pub', 'ascii', function(err, file) {
if (err) {
console.error(err.message);
process.exit(2);
}
try {
parsed.keyId = getFingerprint(file);
} catch (e) {
console.error('Unable to take fingerprint of public key: ' + e.stack);
process.exit(2);
}

return callback(parsed);
});

});
}

Expand Down Expand Up @@ -155,13 +174,8 @@ module.exports = {
}
}

if (!parsed.keyId) {
if (process.env.SDC_CLI_KEY_ID) {
parsed.keyId = process.env.SDC_CLI_KEY_ID;
} else {
parsed.keyId = 'id_rsa';
}
}
if (!parsed.keyId && process.env.SDC_CLI_KEY_ID)
parsed.keyId = process.env.SDC_CLI_KEY_ID;

if (!parsed.account)
parsed.account = process.env.SDC_CLI_ACCOUNT;
Expand Down
13 changes: 8 additions & 5 deletions lib/cloudapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -2045,7 +2045,7 @@ CloudAPI.prototype._request = function(path, body, callback) {
var obj = {
path: _encodeURI(path),
headers: {
Authorization: authz,
Authorization: null,
Date: now
}
};
Expand All @@ -2057,20 +2057,23 @@ CloudAPI.prototype._request = function(path, body, callback) {
obj.headers.Authorization = this.basicAuth;
} else {
if (!this.sshAgent) {
var signer = crypto.createSign('RSA-SHA256');
var alg = / DSA /.test(this.key) ? 'DSA-SHA1' : 'RSA-SHA256';
var signer = crypto.createSign(alg);
signer.update(now);
obj.headers.Authorization = sprintf(SIGNATURE,
this.keyId,
'rsa-sha256',
alg.toLowerCase(),
signer.sign(this.key, 'base64'));
} else {
var self = this;
return this.sshAgent.sign(this.key, new Buffer(now), function(err, sig) {
if (!err && sig)
if (!err && sig) {
var alg = /DSA/i.test(self.key) ? 'dsa-sha1' : 'rsa-sha1';
obj.headers.Authorization = sprintf(SIGNATURE,
self.keyId,
'rsa-sha1',
alg,
sig.signature);
}

return callback(obj);
});
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
"lib": "./lib"
},
"dependencies": {
"lru-cache": "~1.0.2",
"nopt": "~1.0.7",
"restify": "~0.4.3",
"sprintf": "~0.1.1",
"ssh-agent": "~0.1.0"
"http-signature": "0.9.6",
"lru-cache": "1.0.4",
"nopt": "1.0.10",
"restify": "0.5.4",
"sprintf": "0.1.1",
"ssh-agent": "0.1.0"
},
"devDependencies": {}
}

0 comments on commit 0a98a1f

Please sign in to comment.