Don't rely on ssh-keygen output to prevent known errors

This commit is contained in:
Nick O'Leary 2018-01-19 22:31:41 +00:00
parent c2508296a5
commit 7e27dd7678
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 51 additions and 56 deletions

View File

@ -33,14 +33,14 @@ function runSshKeygenCommand(args,cwd,env) {
stderr += data; stderr += data;
}); });
child.on('close', function(code, signal) { child.on('close', function(code, signal) {
console.log(code); // console.log(code);
console.log(stdout); // console.log(stdout);
console.log(stderr); // console.log(stderr);
if (code !== 0) { if (code !== 0) {
var err = new Error(stderr); var err = new Error(stderr);
err.stdout = stdout; err.stdout = stdout;
err.stderr = stderr; err.stderr = stderr;
if (/passphrase is too short/.test(stderr)) { if (/short/.test(stderr)) {
err.code = "key_passphrase_too_short"; err.code = "key_passphrase_too_short";
} else if(/Key must at least be 1024 bits/.test(stderr)) { } else if(/Key must at least be 1024 bits/.test(stderr)) {
err.code = "key_length_too_short"; err.code = "key_length_too_short";
@ -60,7 +60,13 @@ function init(_settings, _runtime) {
function generateKey(options) { function generateKey(options) {
var args = ['-q', '-t', 'rsa']; var args = ['-q', '-t', 'rsa'];
var err;
if (options.size) { if (options.size) {
if (options.size < 1024) {
err = new Error("key_length_too_short");
err.code = "key_length_too_short";
throw err;
}
args.push('-b', options.size); args.push('-b', options.size);
} }
if (options.location) { if (options.location) {
@ -69,7 +75,16 @@ function generateKey(options) {
if (options.comment) { if (options.comment) {
args.push('-C', options.comment); args.push('-C', options.comment);
} }
args.push('-N', options.password||''); if (options.password) {
if (options.password.length < 5) {
err = new Error("key_passphrase_too_short");
err.code = "key_passphrase_too_short";
throw err;
}
args.push('-N', options.password||'');
} else {
args.push('-N', '');
}
return runSshKeygenCommand(args,__dirname); return runSshKeygenCommand(args,__dirname);
} }

View File

@ -24,7 +24,9 @@ var keygen = require("../../../../../../../red/runtime/storage/localfilesystem/p
describe("localfilesystem/projects/ssh/keygen", function() { describe("localfilesystem/projects/ssh/keygen", function() {
afterEach(function() { afterEach(function() {
child_process.spawn.restore(); if (child_process.spawn.restore) {
child_process.spawn.restore();
}
}) })
it("invokes sshkeygen", function(done) { it("invokes sshkeygen", function(done) {
@ -47,7 +49,7 @@ describe("localfilesystem/projects/ssh/keygen", function() {
}); });
keygen.generateKey({ keygen.generateKey({
size: 123, size: 1024,
location: 'location', location: 'location',
comment: 'comment', comment: 'comment',
password: 'password' password: 'password'
@ -63,66 +65,44 @@ describe("localfilesystem/projects/ssh/keygen", function() {
var command; var command;
var args; var args;
var opts; var opts;
sinon.stub(child_process,"spawn", function(_command,_args,_opts) {
_command = command;
_args = args;
_opts = opts;
var e = new EventEmitter(); try {
e.stdout = new EventEmitter(); keygen.generateKey({
e.stderr = new EventEmitter(); size: 1024,
setTimeout(function() { location: 'location',
e.stdout.emit("data","result"); comment: 'comment',
e.stderr.emit("data","passphrase is too short"); password: '123'
e.emit("close",1); }).then(function(output) {
},5) done(new Error("Error not thrown"));
return e; }).catch(function(err) {
}); done(new Error("Error not thrown"));
})
keygen.generateKey({ } catch(err) {
size: 123,
location: 'location',
comment: 'comment',
password: 'password'
}).then(function(output) {
done(new Error("Error not thrown"));
}).catch(function(err) {
err.should.have.property("code","key_passphrase_too_short"); err.should.have.property("code","key_passphrase_too_short");
done(); done();
}) }
}); });
it("reports key length too short", function(done) { it("reports key length too short", function(done) {
var command; var command;
var args; var args;
var opts; var opts;
sinon.stub(child_process,"spawn", function(_command,_args,_opts) { try {
_command = command; keygen.generateKey({
_args = args; size: 123,
_opts = opts; location: 'location',
comment: 'comment',
var e = new EventEmitter(); password: 'password'
e.stdout = new EventEmitter(); }).then(function(output) {
e.stderr = new EventEmitter(); done(new Error("Error not thrown"));
setTimeout(function() { }).catch(function(err) {
e.stdout.emit("data","result"); done(new Error("Error not thrown"));
e.stderr.emit("data","Key must at least be 1024 bits"); })
e.emit("close",1); } catch(err) {
},50)
return e;
});
keygen.generateKey({
size: 123,
location: 'location',
comment: 'comment',
password: 'password'
}).then(function(output) {
done(new Error("Error not thrown"));
}).catch(function(err) {
err.should.have.property("code","key_length_too_short"); err.should.have.property("code","key_length_too_short");
done(); done();
})
}
}); });