Fixed CIRA connections for devices that changed device group.

Ylian Saint-Hilaire committed Jul 31, 2021 at 10:05 UTC dbf0dd446f2d9858ca698894c9254fa193110dad
2 files changed +30 -24
db.js
+3 -3
@@ -1012,7 +1012,7 @@ module.exports.CreateDB = function (parent, func) {
1012 obj.SetUser = function (user) { if (user.subscriptions != null) { var u = Clone(user); if (u.subscriptions) { delete u.subscriptions; } obj.Set(u); } else { obj.Set(user); } };
1013 obj.dispose = function () { for (var x in obj) { if (obj[x].close) { obj[x].close(); } delete obj[x]; } };
1014 obj.getLocalAmtNodes = function (func) { sqlDbQuery('SELECT doc FROM main WHERE (type = "node") AND (extraex IS NOT NULL)', null, function (err, docs) { var r = []; if (err == null) { for (var i in docs) { if (docs[i].host != null) { r.push(docs[i]); } } } func(err, r); }); };
1015 - obj.getAmtUuidMeshNode = function (meshid, uuid, func) { sqlDbQuery('SELECT doc FROM main WHERE meshid = ? AND extraex = ?', [meshid, 'uuid/' + uuid], func); };
1015 + obj.getAmtUuidMeshNode = function (domainid, mtype, uuid, func) { sqlDbQuery('SELECT doc FROM main WHERE domain = ? AND extraex = ?', [domainid, 'uuid/' + uuid], func); };
1016 obj.isMaxType = function (max, type, domainid, func) { if (max == null) { func(false); } else { sqlDbExec('SELECT COUNT(id) FROM main WHERE domain = ? AND type = ?', [domainid, type], function (err, response) { func((response['COUNT(id)'] == null) || (response['COUNT(id)'] > max), response['COUNT(id)']) }); } }
1017
1018 // Database actions on the events collection
@@ -1248,7 +1248,7 @@ module.exports.CreateDB = function (parent, func) {
1248 obj.SetUser = function (user) { if (user.subscriptions != null) { var u = Clone(user); if (u.subscriptions) { delete u.subscriptions; } obj.Set(u); } else { obj.Set(user); } };
1249 obj.dispose = function () { for (var x in obj) { if (obj[x].close) { obj[x].close(); } delete obj[x]; } };
1250 obj.getLocalAmtNodes = function (func) { obj.file.find({ type: 'node', host: { $exists: true, $ne: null }, intelamt: { $exists: true } }).toArray(func); };
1251 - obj.getAmtUuidMeshNode = function (meshid, uuid, func) { obj.file.find({ type: 'node', meshid: meshid, 'intelamt.uuid': uuid }).toArray(func); };
1251 + obj.getAmtUuidMeshNode = function (domainid, mtype, uuid, func) { obj.file.find({ type: 'node', domain: domainid, mtype: mtype, 'intelamt.uuid': uuid }).toArray(func); };
1252
1253 // TODO: Starting in MongoDB 4.0.3, you should use countDocuments() instead of count() that is deprecated. We should detect MongoDB version and switch.
1254 // https://docs.mongodb.com/manual/reference/method/db.collection.countDocuments/
@@ -1453,7 +1453,7 @@ module.exports.CreateDB = function (parent, func) {
1453 obj.SetUser = function (user) { if (user.subscriptions != null) { var u = Clone(user); if (u.subscriptions) { delete u.subscriptions; } obj.Set(u); } else { obj.Set(user); } };
1454 obj.dispose = function () { for (var x in obj) { if (obj[x].close) { obj[x].close(); } delete obj[x]; } };
1455 obj.getLocalAmtNodes = function (func) { obj.file.find({ type: 'node', host: { $exists: true, $ne: null }, intelamt: { $exists: true } }, func); };
1456 - obj.getAmtUuidMeshNode = function (meshid, uuid, func) { obj.file.find({ type: 'node', meshid: meshid, 'intelamt.uuid': uuid }, func); };
1456 + obj.getAmtUuidMeshNode = function (domainid, mtype, uuid, func) { obj.file.find({ type: 'node', domain: domainid, mtype: mtype, 'intelamt.uuid': uuid }, func); };
1457 obj.isMaxType = function (max, type, domainid, func) { if (max == null) { func(false); } else { obj.file.count({ type: type, domain: domainid }, function (err, count) { func((err != null) || (count > max), count); }); } }
1458
1459 // Database actions on the events collection
mpsserver.js
+27 -21
@@ -633,18 +633,19 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
633 SendUserAuthFail(socket);
634 return -1;
635 }
636 - // Find the device group for this CIRA connection. Since Intel AMT does not allow @ or $ in the username, we escape these.
636 + // Find the initial device group for this CIRA connection. Since Intel AMT does not allow @ or $ in the username, we escape these.
637 // For possible for CIRA-LMS connections to still send @ or $, so we need to escape both sides.
638 - var mesh = null;
638 + // The initial device group will tell us what device group type and domain this connection is for
639 + var initialMesh = null;
640 const meshIdStart = ('/' + username).replace(/\@/g, 'X').replace(/\$/g, 'X');
641 if (obj.parent.webserver.meshes) {
642 for (var i in obj.parent.webserver.meshes) {
643 if (obj.parent.webserver.meshes[i]._id.replace(/\@/g, 'X').replace(/\$/g, 'X').indexOf(meshIdStart) > 0) {
643 - mesh = obj.parent.webserver.meshes[i]; break;
644 + initialMesh = obj.parent.webserver.meshes[i]; break;
645 }
646 }
647 }
647 - if (mesh == null) {
648 + if (initialMesh == null) {
649 meshNotFoundCount++;
650 socket.ControlMsg({ action: 'console', msg: 'Device group not found (2): ' + meshIdStart + ', u: ' + username + ', p: ' + password });
651 parent.debug('mps', 'Device group not found (2)', meshIdStart, username, password);
@@ -654,23 +655,22 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
655 }
656
657 // If this is a agent-less mesh, use the device guid 3 times as ID.
657 - if (mesh.mtype == 1) {
658 + if (initialMesh.mtype == 1) {
659 // Intel AMT GUID (socket.tag.SystemId) will be used as NodeID
660 var systemid = socket.tag.SystemId.split('-').join('');
661 var nodeid = Buffer.from(systemid + systemid + systemid, 'hex').toString('base64').replace(/\+/g, '@').replace(/\//g, '$');
661 - var domain = obj.parent.config.domains[mesh.domain];
662 + var domain = obj.parent.config.domains[initialMesh.domain];
663 socket.tag.domain = domain;
663 - socket.tag.domainid = mesh.domain;
664 + socket.tag.domainid = initialMesh.domain;
665 if (socket.tag.name == null) { socket.tag.name = ''; }
665 - socket.tag.nodeid = 'node/' + mesh.domain + '/' + nodeid; // Turn 16bit systemid guid into 48bit nodeid that is base64 encoded
666 - socket.tag.meshid = mesh._id;
666 + socket.tag.nodeid = 'node/' + initialMesh.domain + '/' + nodeid; // Turn 16bit systemid guid into 48bit nodeid that is base64 encoded
667 socket.tag.connectTime = Date.now();
668
669 obj.db.Get(socket.tag.nodeid, function (err, nodes) {
670 if ((nodes == null) || (nodes.length !== 1)) {
671 // Check if we already have too many devices for this domain
672 if (domain.limits && (typeof domain.limits.maxdevices == 'number')) {
673 - db.isMaxType(domain.limits.maxdevices, 'node', mesh.domain, function (ismax, count) {
673 + db.isMaxType(domain.limits.maxdevices, 'node', initialMesh.domain, function (ismax, count) {
674 if (ismax == true) {
675 // Too many devices in this domain.
676 maxDomainDevicesReached++;
@@ -682,16 +682,19 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
682 var hostname = socket.remoteAddr;
683 if ((err == null) && (hostnames != null) && (hostnames.length > 0)) { hostname = hostnames[0]; }
684
685 + // Set the device group
686 + socket.tag.meshid = initialMesh._id;
687 +
688 // We are under the limit, create the new device.
689 // Node is not in the database, add it. Credentials will be empty until added by the user.
687 - var device = { type: 'node', mtype: 1, _id: socket.tag.nodeid, meshid: socket.tag.meshid, name: socket.tag.name, icon: (socket.tag.meiState.isBatteryPowered) ? 2 : 1, host: hostname, domain: mesh.domain, intelamt: { user: (typeof socket.tag.meiState.amtuser == 'string') ? socket.tag.meiState.amtuser : '', pass: (typeof socket.tag.meiState.amtpass == 'string') ? socket.tag.meiState.amtpass : '', tls: 0, state: 2 } };
690 + var device = { type: 'node', mtype: 1, _id: socket.tag.nodeid, meshid: socket.tag.meshid, name: socket.tag.name, icon: (socket.tag.meiState.isBatteryPowered) ? 2 : 1, host: hostname, domain: initialMesh.domain, intelamt: { user: (typeof socket.tag.meiState.amtuser == 'string') ? socket.tag.meiState.amtuser : '', pass: (typeof socket.tag.meiState.amtpass == 'string') ? socket.tag.meiState.amtpass : '', tls: 0, state: 2 } };
691 if ((typeof socket.tag.meiState.desc == 'string') && (socket.tag.meiState.desc.length > 0) && (socket.tag.meiState.desc.length < 1024)) { device.desc = socket.tag.meiState.desc; }
692 obj.db.Set(device);
693
694 // Event the new node
695 addedDeviceCount++;
693 - var change = 'CIRA added device ' + socket.tag.name + ' to group ' + mesh.name;
694 - obj.parent.DispatchEvent(['*', socket.tag.meshid], obj, { etype: 'node', action: 'addnode', node: parent.webserver.CloneSafeNode(device), msg: change, domain: mesh.domain });
696 + var change = 'CIRA added device ' + socket.tag.name + ' to group ' + initialMesh.name;
697 + obj.parent.DispatchEvent(['*', socket.tag.meshid], obj, { etype: 'node', action: 'addnode', node: parent.webserver.CloneSafeNode(device), msg: change, domain: initialMesh.domain });
698
699 // Add the connection to the MPS connection list
700 addCiraConnection(socket);
@@ -706,8 +709,11 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
709 var hostname = socket.remoteAddr;
710 if ((err == null) && (hostnames != null) && (hostnames.length > 0)) { hostname = hostnames[0]; }
711
712 + // Set the device group
713 + socket.tag.meshid = initialMesh._id;
714 +
715 // Node is not in the database, add it. Credentials will be empty until added by the user.
710 - var device = { type: 'node', mtype: 1, _id: socket.tag.nodeid, meshid: socket.tag.meshid, name: socket.tag.name, icon: (socket.tag.meiState && socket.tag.meiState.isBatteryPowered) ? 2 : 1, host: hostname, domain: mesh.domain, intelamt: { user: ((socket.tag.meiState) && (typeof socket.tag.meiState.amtuser == 'string')) ? socket.tag.meiState.amtuser : '', pass: ((socket.tag.meiState) && (typeof socket.tag.meiState.amtpass == 'string')) ? socket.tag.meiState.amtpass : '', tls: 0, state: 2 } };
716 + var device = { type: 'node', mtype: 1, _id: socket.tag.nodeid, meshid: socket.tag.meshid, name: socket.tag.name, icon: (socket.tag.meiState && socket.tag.meiState.isBatteryPowered) ? 2 : 1, host: hostname, domain: initialMesh.domain, intelamt: { user: ((socket.tag.meiState) && (typeof socket.tag.meiState.amtuser == 'string')) ? socket.tag.meiState.amtuser : '', pass: ((socket.tag.meiState) && (typeof socket.tag.meiState.amtpass == 'string')) ? socket.tag.meiState.amtpass : '', tls: 0, state: 2 } };
717 if ((socket.tag.meiState != null) && (typeof socket.tag.meiState.desc == 'string') && (socket.tag.meiState.desc.length > 0) && (socket.tag.meiState.desc.length < 1024)) { device.desc = socket.tag.meiState.desc; }
718 obj.db.Set(device);
719
@@ -720,7 +726,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
726 } else {
727 // Node is already present
728 var node = nodes[0];
723 - socket.tag.meshid = node.meshid; // Correct the MeshID if the node has moved.
729 + socket.tag.meshid = node.meshid;
730 if ((node.intelamt != null) && (node.intelamt.state == 2)) { socket.tag.host = node.intelamt.host; }
731 }
732
@@ -728,13 +734,13 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
734 addCiraConnection(socket);
735 SendUserAuthSuccess(socket); // Notify the auth success on the CIRA connection
736 });
731 - } else if (mesh.mtype == 2) { // If this is a agent mesh, search the mesh for this device UUID
737 + } else if (initialMesh.mtype == 2) { // If this is a agent mesh, search the mesh for this device UUID
738 // Intel AMT GUID (socket.tag.SystemId) will be used to search the node
733 - obj.db.getAmtUuidMeshNode(mesh._id, socket.tag.SystemId, function (err, nodes) { // TODO: Need to optimize this request with indexes
739 + obj.db.getAmtUuidMeshNode(initialMesh.domain, initialMesh.mtype, socket.tag.SystemId, function (err, nodes) { // TODO: Need to optimize this request with indexes
740 if ((nodes == null) || (nodes.length === 0) || (obj.parent.webserver.meshes == null)) {
741 // New CIRA connection for unknown node, disconnect.
742 unknownNodeCount++;
737 - console.log('CIRA connection for unknown node. groupid: ' + mesh._id + ', uuid: ' + socket.tag.SystemId);
743 + console.log('CIRA connection for unknown node. groupid: ' + initialMesh._id + ', uuid: ' + socket.tag.SystemId);
744 socket.end();
745 return;
746 }
@@ -742,7 +748,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
748 // Looking at nodes that match this UUID, select one in the same domain and mesh type.
749 var node = null;
750 for (var i in nodes) {
745 - if (mesh.domain == nodes[i].domain) {
751 + if (initialMesh.domain == nodes[i].domain) {
752 var nodemesh = obj.parent.webserver.meshes[nodes[i].meshid];
753 if ((nodemesh != null) && (nodemesh.mtype == 2)) { node = nodes[i]; }
754 }
@@ -751,7 +757,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
757 if (node == null) {
758 // New CIRA connection for unknown node, disconnect.
759 unknownNodeCount++;
754 - console.log('CIRA connection for unknown node. candidate(s): ' + nodes.length + ', groupid: ' + mesh._id + ', uuid: ' + socket.tag.SystemId);
760 + console.log('CIRA connection for unknown node. candidate(s): ' + nodes.length + ', groupid: ' + initialMesh._id + ', uuid: ' + socket.tag.SystemId);
761 socket.end();
762 return;
763 }
@@ -759,7 +765,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
765 // Node is present
766 if ((node.intelamt != null) && (node.intelamt.state == 2)) { socket.tag.host = node.intelamt.host; }
767 socket.tag.nodeid = node._id;
762 - socket.tag.meshid = node.meshid; // Correct the MeshID if the node has moved.
768 + socket.tag.meshid = node.meshid;
769 socket.tag.connectTime = Date.now();
770
771 // Add the connection to the MPS connection list