Keys module
- Improved security for node-privkey storage - Private is stored as encrypted in file (loaded once on startup) - node private key is stored in encrypted format in memory - moved myPrivKey, myFloID to keys module
This commit is contained in:
parent
b303219d1b
commit
2dc338f90d
@ -39,7 +39,7 @@ function processIncomingData(data) {
|
|||||||
console.debug(result);
|
console.debug(result);
|
||||||
resolve(result);
|
resolve(result);
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
console.debug(error);
|
(error instanceof INVALID ? console.debug : console.error)(error);
|
||||||
reject(error);
|
reject(error);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
45
src/intra.js
45
src/intra.js
@ -1,5 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
const WebSocket = require('ws');
|
const WebSocket = require('ws');
|
||||||
|
const keys = require('./keys');
|
||||||
|
|
||||||
//CONSTANTS
|
//CONSTANTS
|
||||||
const SUPERNODE_INDICATOR = '$',
|
const SUPERNODE_INDICATOR = '$',
|
||||||
@ -128,11 +129,11 @@ _prevNode.onclose = evt => _prevNode.close();
|
|||||||
const packet_ = {};
|
const packet_ = {};
|
||||||
packet_.construct = function (message) {
|
packet_.construct = function (message) {
|
||||||
const packet = {
|
const packet = {
|
||||||
from: myFloID,
|
from: keys.node_id,
|
||||||
message: message,
|
message: message,
|
||||||
time: Date.now()
|
time: Date.now()
|
||||||
};
|
};
|
||||||
packet.sign = floCrypto.signData(this.s(packet), myPrivKey);
|
packet.sign = floCrypto.signData(this.s(packet), keys.node_priv);
|
||||||
return SUPERNODE_INDICATOR + JSON.stringify(packet);
|
return SUPERNODE_INDICATOR + JSON.stringify(packet);
|
||||||
};
|
};
|
||||||
packet_.s = d => [JSON.stringify(d.message), d.time].join("|");
|
packet_.s = d => [JSON.stringify(d.message), d.time].join("|");
|
||||||
@ -172,7 +173,7 @@ function connectToActiveNode(snID, reverse = false) {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!(snID in floGlobals.supernodes))
|
if (!(snID in floGlobals.supernodes))
|
||||||
return reject(`${snID} is not a supernode`);
|
return reject(`${snID} is not a supernode`);
|
||||||
if (snID === myFloID)
|
if (snID === keys.node_id)
|
||||||
return reject(`Reached end of circle. Next node avaiable is self`);
|
return reject(`Reached end of circle. Next node avaiable is self`);
|
||||||
connectToNode(snID)
|
connectToNode(snID)
|
||||||
.then(ws => resolve(ws))
|
.then(ws => resolve(ws))
|
||||||
@ -186,12 +187,12 @@ function connectToActiveNode(snID, reverse = false) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
//Connect to next available node
|
//Connect to next available node
|
||||||
function connectToNextNode(curNode = myFloID) {
|
function connectToNextNode(curNode = keys.node_id) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (curNode === myFloID && !(myFloID in floGlobals.supernodes))
|
if (curNode === keys.node_id && !(keys.node_id in floGlobals.supernodes))
|
||||||
return reject(`This (${myFloID}) is not a supernode`);
|
return reject(`This (${keys.node_id}) is not a supernode`);
|
||||||
let nextNodeID = cloud.nextNode(curNode);
|
let nextNodeID = cloud.nextNode(curNode);
|
||||||
if (nextNodeID === myFloID)
|
if (nextNodeID === keys.node_id)
|
||||||
return reject("No other node online");
|
return reject("No other node online");
|
||||||
connectToNode(nextNodeID).then(ws => {
|
connectToNode(nextNodeID).then(ws => {
|
||||||
_nextNode.set(nextNodeID, ws);
|
_nextNode.set(nextNodeID, ws);
|
||||||
@ -209,7 +210,7 @@ function connectToNextNode(curNode = myFloID) {
|
|||||||
|
|
||||||
function connectToAliveNodes(nodes = null) {
|
function connectToAliveNodes(nodes = null) {
|
||||||
if (!Array.isArray(nodes)) nodes = Object.keys(floGlobals.supernodes);
|
if (!Array.isArray(nodes)) nodes = Object.keys(floGlobals.supernodes);
|
||||||
nodes = nodes.filter(n => n !== myFloID);
|
nodes = nodes.filter(n => n !== keys.node_id);
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
Promise.allSettled(nodes.map(n => connectToNode(n))).then(results => {
|
Promise.allSettled(nodes.map(n => connectToNode(n))).then(results => {
|
||||||
let ws_connections = {};
|
let ws_connections = {};
|
||||||
@ -261,7 +262,7 @@ function processTaskFromNextNode(packet) {
|
|||||||
storeBackupData(task.data, from, packet);
|
storeBackupData(task.data, from, packet);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
console.log("Invalid task type:" + task.type + "from next-node");
|
console.warn("Invalid task type:" + task.type + "from next-node");
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@ -299,7 +300,7 @@ function processTaskFromPrevNode(packet) {
|
|||||||
deleteMigratedData(task.data, from, packet);
|
deleteMigratedData(task.data, from, packet);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
console.log("Invalid task type:" + task.type + "from prev-node");
|
console.warn("Invalid task type:" + task.type + "from prev-node");
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@ -325,7 +326,7 @@ function processTaskFromSupernode(packet, ws) {
|
|||||||
initiateRefresh();
|
initiateRefresh();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
console.log("Invalid task type:" + task.type + "from super-node");
|
console.warn("Invalid task type:" + task.type + "from super-node");
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@ -336,7 +337,7 @@ function processTaskFromSupernode(packet, ws) {
|
|||||||
//Acknowledge handshake
|
//Acknowledge handshake
|
||||||
function handshakeMid(id, ws) {
|
function handshakeMid(id, ws) {
|
||||||
if (_prevNode.id && _prevNode.id in floGlobals.supernodes) {
|
if (_prevNode.id && _prevNode.id in floGlobals.supernodes) {
|
||||||
if (cloud.innerNodes(_prevNode.id, myFloID).includes(id)) {
|
if (cloud.innerNodes(_prevNode.id, keys.node_id).includes(id)) {
|
||||||
//close existing prev-node connection
|
//close existing prev-node connection
|
||||||
_prevNode.send(packet_.construct({
|
_prevNode.send(packet_.construct({
|
||||||
type: RECONNECT_NEXT_NODE
|
type: RECONNECT_NEXT_NODE
|
||||||
@ -364,7 +365,7 @@ function handshakeMid(id, ws) {
|
|||||||
if (!_nextNode.id)
|
if (!_nextNode.id)
|
||||||
reconnectNextNode();
|
reconnectNextNode();
|
||||||
//Reorder storelist
|
//Reorder storelist
|
||||||
let nodes = cloud.innerNodes(_prevNode.id, myFloID).concat(myFloID),
|
let nodes = cloud.innerNodes(_prevNode.id, keys.node_id).concat(keys.node_id),
|
||||||
req_sync = [],
|
req_sync = [],
|
||||||
new_order = [];
|
new_order = [];
|
||||||
nodes.forEach(n => {
|
nodes.forEach(n => {
|
||||||
@ -438,7 +439,7 @@ function reconnectNextNode() {
|
|||||||
if (_nextNode.id)
|
if (_nextNode.id)
|
||||||
_nextNode.close();
|
_nextNode.close();
|
||||||
connectToNextNode()
|
connectToNextNode()
|
||||||
.then(result => console.log(result))
|
.then(result => console.debug(result))
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
//Case: No other node is online
|
//Case: No other node is online
|
||||||
console.info(error);
|
console.info(error);
|
||||||
@ -456,7 +457,7 @@ function reconnectNextNode() {
|
|||||||
function orderBackup(order) {
|
function orderBackup(order) {
|
||||||
let new_order = [],
|
let new_order = [],
|
||||||
req_sync = [];
|
req_sync = [];
|
||||||
let cur_serve = cloud.innerNodes(_prevNode.id, myFloID).concat(myFloID);
|
let cur_serve = cloud.innerNodes(_prevNode.id, keys.node_id).concat(keys.node_id);
|
||||||
for (let n in order) {
|
for (let n in order) {
|
||||||
if (!cur_serve.includes(n) && order[n] + 1 !== _list[n] && n in floGlobals.supernodes) {
|
if (!cur_serve.includes(n) && order[n] + 1 !== _list[n] && n in floGlobals.supernodes) {
|
||||||
if (order[n] >= floGlobals.sn_config.backupDepth)
|
if (order[n] >= floGlobals.sn_config.backupDepth)
|
||||||
@ -522,13 +523,13 @@ function sendStoredData(lastlogs, node) {
|
|||||||
id: n,
|
id: n,
|
||||||
status: true
|
status: true
|
||||||
}));
|
}));
|
||||||
console.log(`START: ${n} data sync(send) to ${node.id}`);
|
console.info(`START: ${n} data sync(send) to ${node.id}`);
|
||||||
//TODO: efficiently handle large number of data instead of loading all into memory
|
//TODO: efficiently handle large number of data instead of loading all into memory
|
||||||
result.forEach(d => node.send(packet_.construct({
|
result.forEach(d => node.send(packet_.construct({
|
||||||
type: STORE_BACKUP_DATA,
|
type: STORE_BACKUP_DATA,
|
||||||
data: d
|
data: d
|
||||||
})));
|
})));
|
||||||
console.log(`END: ${n} data sync(send) to ${node.id}`);
|
console.info(`END: ${n} data sync(send) to ${node.id}`);
|
||||||
node.send(packet_.construct({
|
node.send(packet_.construct({
|
||||||
type: DATA_SYNC,
|
type: DATA_SYNC,
|
||||||
id: n,
|
id: n,
|
||||||
@ -541,7 +542,7 @@ function sendStoredData(lastlogs, node) {
|
|||||||
|
|
||||||
//Indicate sync of data
|
//Indicate sync of data
|
||||||
function dataSyncIndication(snID, status, from) {
|
function dataSyncIndication(snID, status, from) {
|
||||||
console.log(`${status ? 'START':'END'}: ${snID} data sync(receive) form ${from}`);
|
console.info(`${status ? 'START' : 'END'}: ${snID} data sync(receive) form ${from}`);
|
||||||
};
|
};
|
||||||
|
|
||||||
//Store (backup) data
|
//Store (backup) data
|
||||||
@ -633,7 +634,7 @@ function dataMigration(node_change, flag) {
|
|||||||
if (del_nodes.includes(_nextNode.id))
|
if (del_nodes.includes(_nextNode.id))
|
||||||
reconnectNextNode();
|
reconnectNextNode();
|
||||||
else { //reconnect next node if there are newly added nodes in between self and current next node
|
else { //reconnect next node if there are newly added nodes in between self and current next node
|
||||||
let innerNodes = cloud.innerNodes(myFloID, _nextNode.id);
|
let innerNodes = cloud.innerNodes(keys.node_id, _nextNode.id);
|
||||||
if (new_nodes.filter(n => innerNodes.includes(n)).length)
|
if (new_nodes.filter(n => innerNodes.includes(n)).length)
|
||||||
reconnectNextNode();
|
reconnectNextNode();
|
||||||
};
|
};
|
||||||
@ -657,14 +658,14 @@ function dataMigration(node_change, flag) {
|
|||||||
dataMigration.process_del = async function (del_nodes, old_kb) {
|
dataMigration.process_del = async function (del_nodes, old_kb) {
|
||||||
if (!del_nodes.length)
|
if (!del_nodes.length)
|
||||||
return;
|
return;
|
||||||
let serve = _prevNode.id ? old_kb.innerNodes(_prevNode.id, myFloID) : _list.serving;
|
let serve = _prevNode.id ? old_kb.innerNodes(_prevNode.id, keys.node_id) : _list.serving;
|
||||||
let process_nodes = del_nodes.filter(n => serve.includes(n));
|
let process_nodes = del_nodes.filter(n => serve.includes(n));
|
||||||
if (process_nodes.length) {
|
if (process_nodes.length) {
|
||||||
connectToAllActiveNodes().then(ws_connections => {
|
connectToAllActiveNodes().then(ws_connections => {
|
||||||
let remaining = process_nodes.length;
|
let remaining = process_nodes.length;
|
||||||
process_nodes.forEach(n => {
|
process_nodes.forEach(n => {
|
||||||
DB.readAllData(n, 0).then(result => {
|
DB.readAllData(n, 0).then(result => {
|
||||||
console.log(`START: Data migration for ${n}`);
|
console.info(`START: Data migration for ${n}`);
|
||||||
//TODO: efficiently handle large number of data instead of loading all into memory
|
//TODO: efficiently handle large number of data instead of loading all into memory
|
||||||
result.forEach(d => {
|
result.forEach(d => {
|
||||||
let closest = cloud.closestNode(d.receiverID);
|
let closest = cloud.closestNode(d.receiverID);
|
||||||
@ -681,7 +682,7 @@ dataMigration.process_del = async function(del_nodes, old_kb) {
|
|||||||
data: d
|
data: d
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
console.log(`END: Data migration for ${n}`);
|
console.info(`END: Data migration for ${n}`);
|
||||||
_list.delete(n);
|
_list.delete(n);
|
||||||
DB.dropTable(n).then(_ => null).catch(e => console.error(e));
|
DB.dropTable(n).then(_ => null).catch(e => console.error(e));
|
||||||
remaining--;
|
remaining--;
|
||||||
|
|||||||
37
src/keys.js
Normal file
37
src/keys.js
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const PRIV_EKEY_MIN = 32,
|
||||||
|
PRIV_EKEY_MAX = 48;
|
||||||
|
|
||||||
|
var node_priv, e_key, node_id, node_pub; //containers for node-key wrapper
|
||||||
|
const _ = {
|
||||||
|
get node_priv() {
|
||||||
|
if (!node_priv || !e_key)
|
||||||
|
throw Error("keys not set");
|
||||||
|
return Crypto.AES.decrypt(node_priv, e_key);
|
||||||
|
},
|
||||||
|
set node_priv(key) {
|
||||||
|
node_pub = floCrypto.getPubKeyHex(key);
|
||||||
|
node_id = floCrypto.getFloID(node_pub);
|
||||||
|
if (!key || !node_pub || !node_id)
|
||||||
|
throw Error("Invalid Keys");
|
||||||
|
let n = floCrypto.randInt(PRIV_EKEY_MIN, PRIV_EKEY_MAX)
|
||||||
|
e_key = floCrypto.randString(n);
|
||||||
|
node_priv = Crypto.AES.encrypt(key, e_key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
set node_priv(key) {
|
||||||
|
_.node_priv = key;
|
||||||
|
},
|
||||||
|
get node_priv() {
|
||||||
|
return _.node_priv;
|
||||||
|
},
|
||||||
|
get node_id() {
|
||||||
|
return node_id;
|
||||||
|
},
|
||||||
|
get node_pub() {
|
||||||
|
return node_pub;
|
||||||
|
}
|
||||||
|
}
|
||||||
28
src/main.js
28
src/main.js
@ -1,4 +1,3 @@
|
|||||||
const config = require('../args/config.json');
|
|
||||||
global.floGlobals = require("./floGlobals");
|
global.floGlobals = require("./floGlobals");
|
||||||
require('./set_globals');
|
require('./set_globals');
|
||||||
require('./lib');
|
require('./lib');
|
||||||
@ -9,16 +8,33 @@ const Database = require("./database");
|
|||||||
const intra = require('./intra');
|
const intra = require('./intra');
|
||||||
const client = require('./client');
|
const client = require('./client');
|
||||||
const Server = require('./server');
|
const Server = require('./server');
|
||||||
|
const keys = require("./keys");
|
||||||
|
|
||||||
var DB; //Container for Database object
|
var DB; //Container for Database object
|
||||||
const INTERVAL_REFRESH_TIME = 1 * 60 * 60 * 1000; //1 hr
|
const INTERVAL_REFRESH_TIME = 1 * 60 * 60 * 1000; //1 hr
|
||||||
|
|
||||||
function startNode() {
|
function startNode() {
|
||||||
//Set myPrivKey, myPubKey, myFloID
|
|
||||||
global.myPrivKey = config["privateKey"];
|
const config = require(`../args/config.json`);
|
||||||
global.myPubKey = floCrypto.getPubKeyHex(config["privateKey"]);
|
let _pass;
|
||||||
global.myFloID = floCrypto.getFloID(config["privateKey"]);
|
for (let arg of process.argv)
|
||||||
console.info("Logged In as " + myFloID);
|
if (/^-password=/i.test(arg))
|
||||||
|
_pass = arg.split(/=(.*)/s)[1];
|
||||||
|
try {
|
||||||
|
let _tmp = require(`../args/keys.json`);
|
||||||
|
_tmp = floCrypto.retrieveShamirSecret(_tmp);
|
||||||
|
if (!_pass) {
|
||||||
|
console.error('Password not entered!');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
keys.node_priv = Crypto.AES.decrypt(_tmp, _pass);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Unable to load private key!');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.info("Logged in as", keys.node_id);
|
||||||
|
|
||||||
//DB connect
|
//DB connect
|
||||||
Database(config["sql_user"], config["sql_pwd"], config["sql_db"], config["sql_host"]).then(db => {
|
Database(config["sql_user"], config["sql_pwd"], config["sql_db"], config["sql_host"]).then(db => {
|
||||||
console.info("Connected to Database");
|
console.info("Connected to Database");
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user