Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper ssh key file modes and callback usage #154

Merged
merged 2 commits into from
Jun 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/tessel-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ parser.command('key')
.callback(function(opts) {
key(opts)
.then(function() {
logs.info('Key successfully generated.');
process.exit(0);
})
.catch(function(err) {
Expand Down
6 changes: 3 additions & 3 deletions lib/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ var provision = require('./tessel/provision'),

module.exports = function() {
return new Promise(function(resolve, reject) {
provision.setupLocal(function(err, created) {
if (!created) {
reject(new Error('Key already exists. No new key created.'));
provision.setupLocal(function(err) {
if (err) {
reject(err);
} else {
resolve();
}
Expand Down
32 changes: 19 additions & 13 deletions lib/tessel/provision.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ Tessel.prototype.provisionTessel = function() {
var self = this;
if (self.connection.connectionType === 'USB') {
// Check if local .tessel file has keypair, if not, put it there
setupLocal(function() {
// Authorize Tessel for SSH
authTessel(self);
setupLocal(function(err) {
if (err) {
logs.err(err);
return;
} else {
// Authorize Tessel for SSH
authTessel(self);
}
});
} else {
logs.warn('Tessel must be connected with USB to use this command.');
Expand Down Expand Up @@ -50,16 +55,17 @@ function setupLocal(callback) {

function createKeyFile(filepath, key, successString, callback) {
// Put SSH keys for Tessel in that folder
fs.writeFile(filepath, key, function(err) {
fs.writeFile(filepath, key, {
mode: '0600'
}, function(err) {
// Handle any errors that may have occurred
if (Tessel._commonErrorHandler(err)) {
if (Tessel._commonErrorHandler(err, callback)) {
return;
} else {
if (typeof callback === 'function') {
callback();
}
}

fs.chmod(filepath + '.pub', '600', function() {
logs.info(successString);
callback();
});
});
}

Expand All @@ -70,19 +76,19 @@ function setupLocal(callback) {
],
function(err) {
// Handle any errors that may have occurred
if (Tessel._commonErrorHandler(err)) {
if (Tessel._commonErrorHandler(err, callback)) {
return;
} else {
if (typeof callback === 'function') {
callback(null, true);
callback();
}
}
});
});
});
} else {
if (typeof callback === 'function') {
callback(null, false);
callback(new Error('Keys already exist. Refusing to overwrite them.'));
}
}
}
Expand Down