Skip to content

Commit

Permalink
clear kvs if msp ids do not match
Browse files Browse the repository at this point in the history
  • Loading branch information
dshuffma-ibm committed Sep 22, 2017
1 parent d64f778 commit dc7b6da
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
21 changes: 21 additions & 0 deletions utils/fc_wrangler/common.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//-------------------------------------------------------------------
// Common/Helper Library for Fabric Client Wrangler
//-------------------------------------------------------------------
var fs = require('fs');
var path = require('path');

module.exports = function (logger) {
var common = {};
Expand Down Expand Up @@ -87,5 +89,24 @@ module.exports = function (logger) {
return (Buffer.from(b64string, 'base64')).toString();
};


//------------------------------------------------------------
// Delete a folder - synchronous
//------------------------------------------------------------
common.rmdir = function (dir_path) {
if (fs.existsSync(dir_path)) {
fs.readdirSync(dir_path).forEach(function (entry) {
var entry_path = path.join(dir_path, entry);
if (fs.lstatSync(entry_path).isDirectory()) {
common.rmdir(entry_path);
}
else {
fs.unlinkSync(entry_path);
}
});
fs.rmdirSync(dir_path);
}
};

return common;
};
15 changes: 11 additions & 4 deletions utils/fc_wrangler/enrollment.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,23 @@ module.exports = function (logger) {
var member;
return client.getUserContext(options.enroll_id, true).then((user) => {
if (user && user.isEnrolled()) {
logger.info('[fcw] Successfully loaded enrollment from persistence'); //load from KVS if we can
return user;
if (user._mspId !== options.msp_id) { //if they don't match, this isn't our user! can't use it
logger.warn('[fcw] The msp id in KVS does not match the msp id passed to enroll. Need to clear the KVS.', user._mspId, options.msp_id);
common.rmdir(options.kvs_path); //delete it
logger.error('[fcw] MSP in KVS mismatch. KVS has been deleted. Restart the app to try again.');
process.exit(); //this is terrible, but can't seem to reset client._userContext
} else {
logger.info('[fcw] Successfully loaded enrollment from persistence'); //load from KVS if we can
return user;
}
} else {

// Need to enroll it with the CA
var tlsOptions = {
trustedRoots: [options.ca_tls_opts.pem], //pem cert required
trustedRoots: [options.ca_tls_opts.pem], //pem cert required
verify: false
};
var ca_client = new CaService(options.ca_url, tlsOptions, options.ca_name); //ca_name is important for the bluemix service
var ca_client = new CaService(options.ca_url, tlsOptions, options.ca_name); //ca_name is important for the bluemix service
member = new User(options.enroll_id);

logger.debug('enroll id: "' + options.enroll_id + '", secret: "' + options.enroll_secret + '"');
Expand Down

0 comments on commit dc7b6da

Please sign in to comment.