Added Intel AMT device import to Intel AMT only device groups.

Ylian Saint-Hilaire committed Jul 29, 2022 at 10:21 UTC fce2914f7a06c06c25c70dcceab3a7b4b5ee0d91
2 files changed +132 -1
meshuser.js
+102
@@ -4960,6 +4960,108 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
4960 }
4961 break;
4962 }
4963 + case 'importamtdevices': {
4964 + if ((command.amtdevices == null) || (command.meshid == null) || (typeof command.meshid != 'string') || (command.meshid.startsWith('mesh/' + domain.id + '/') == false)) return;
4965 + const mesh = parent.meshes[command.meshid];
4966 + if ((mesh == null) || (mesh.mtype != 1) || (parent.GetMeshRights(user, command.meshid) & MESHRIGHT_EDITMESH) == 0) return null; // This user must have mesh rights to edit the device group
4967 + var amtDevices = [];
4968 +
4969 + // Decode a JSON file from the Intel EMA migration tool
4970 + if ((typeof command.amtdevices == 'object') && (typeof command.amtdevices.ApplicationData == 'object') && (command.amtdevices.ApplicationData.Application == 'Intel EMA Migration Tool') && (Array.isArray(command.amtdevices['Managed Systems']))) {
4971 + for (var i in command.amtdevices['Managed Systems']) {
4972 + const importDev = command.amtdevices['Managed Systems'][i];
4973 + var host = null;
4974 + if ((typeof importDev.curr_AMTFqdn == 'string') && (importDev.curr_AMTFqdn != '')) { host = importDev.curr_AMTFqdn; }
4975 + if ((host == null) && (typeof importDev.curr_AMTIPv4 == 'string') && (importDev.curr_AMTIPv4 != '')) { host = importDev.curr_AMTIPv4; }
4976 + if (host != null) {
4977 + // Create a new Intel AMT device
4978 + const nodeid = 'node/' + domain.id + '/' + parent.crypto.randomBytes(48).toString('base64').replace(/\+/g, '@').replace(/\//g, '$');
4979 + const device = { type: 'node', _id: nodeid, meshid: mesh._id, mtype: 1, icon: 1, name: host, host: host, domain: domain.id, intelamt: { user: 'admin', state: 2 } };
4980 +
4981 + // Add optional fields
4982 + if (typeof importDev.AMTVersion == 'string') { device.intelamt.ver = importDev.AMTVersion; }
4983 + if (typeof importDev.ConfiguredPassword == 'string') { device.intelamt.pass = importDev.ConfiguredPassword; }
4984 + if (typeof importDev.uuid == 'string') { device.intelamt.uuid = importDev.uuid; }
4985 +
4986 + // Check if we are already adding a device with the same hostname, if so, skip it.
4987 + var skip = false;
4988 + for (var i in amtDevices) { if (amtDevices[i].host.toLowerCase() == device.host.toLowerCase()) { skip = true; } }
4989 + if (skip == false) { amtDevices.push(device); }
4990 + }
4991 + }
4992 + }
4993 +
4994 + // Decode a JSON file from MeshCommander
4995 + if ((typeof command.amtdevices == 'object') && (typeof command.amtdevices.webappversion == 'string') && (Array.isArray(command.amtdevices.computers))) {
4996 + for (var i in command.amtdevices.computers) {
4997 + const importDev = command.amtdevices.computers[i];
4998 + if ((typeof importDev.host == 'string') && (importDev.host != '') && (importDev.host != '127.0.0.1') && (importDev.host.toLowerCase() != 'localhost')) {
4999 + // Create a new Intel AMT device
5000 + const nodeid = 'node/' + domain.id + '/' + parent.crypto.randomBytes(48).toString('base64').replace(/\+/g, '@').replace(/\//g, '$');
5001 + const device = { type: 'node', _id: nodeid, meshid: mesh._id, mtype: 1, icon: 1, host: importDev.host, domain: domain.id, intelamt: { user: 'admin', state: 2 } };
5002 + if (typeof importDev.name == 'string') { device.name = importDev.name; } else { device.name = importDev.host; }
5003 +
5004 + // Add optional fields
5005 + if (typeof importDev.user == 'string') { device.intelamt.user = importDev.user; }
5006 + if (typeof importDev.pass == 'string') { device.intelamt.pass = importDev.pass; }
5007 + if ((importDev.tls === true) || (importDev.tls === 1)) { device.intelamt.tls = 1; }
5008 + if (typeof importDev.digestrealm == 'string') { device.intelamt.realm = importDev.digestrealm; }
5009 + if (typeof importDev.ver == 'string') { device.intelamt.ver = importDev.ver; }
5010 + if (typeof importDev.uuid == 'string') { device.intelamt.uuid = importDev.uuid; }
5011 + if (typeof importDev.pstate == 'number') { device.intelamt.state = importDev.pstate; }
5012 + if (typeof importDev.tlscerthash == 'string') { device.intelamt.hash = importDev.tlscerthash; }
5013 + if (typeof importDev.icon == 'number') { device.icon = importDev.icon; }
5014 + if (typeof importDev.desc == 'string') { device.desc = importDev.desc; }
5015 +
5016 + // Check if we are already adding a device with the same hostname, if so, skip it.
5017 + var skip = false;
5018 + for (var i in amtDevices) { if (amtDevices[i].host.toLowerCase() == device.host.toLowerCase()) { skip = true; } }
5019 + if (skip == false) { amtDevices.push(device); }
5020 + }
5021 + }
5022 + }
5023 +
5024 + // Decode a JSON file in simple format
5025 + if (Array.isArray(command.amtdevices)) {
5026 + for (var i in command.amtdevices) {
5027 + const importDev = command.amtdevices[i];
5028 + if ((typeof importDev.fqdn == 'string') && (importDev.fqdn != '') && (importDev.fqdn != '127.0.0.1') && (importDev.fqdn.toLowerCase() != 'localhost')) {
5029 + // Create a new Intel AMT device
5030 + const nodeid = 'node/' + domain.id + '/' + parent.crypto.randomBytes(48).toString('base64').replace(/\+/g, '@').replace(/\//g, '$');
5031 + const device = { type: 'node', _id: nodeid, meshid: mesh._id, mtype: 1, icon: 1, host: importDev.fqdn, domain: domain.id, intelamt: { user: 'admin', state: 2 } };
5032 + if (typeof importDev.name == 'string') { device.name = importDev.name; } else { device.name = importDev.host; }
5033 +
5034 + // Add optional fields
5035 + if (typeof importDev.username == 'string') { device.intelamt.user = importDev.username; }
5036 + if (typeof importDev.password == 'string') { device.intelamt.pass = importDev.password; }
5037 + if ((importDev.tls === true) || (importDev.tls === 1)) { device.intelamt.tls = 1; }
5038 + if (typeof importDev.version == 'string') { device.intelamt.ver = importDev.version; }
5039 + if (typeof importDev.digestrealm == 'string') { device.intelamt.realm = importDev.digestrealm; }
5040 + if (typeof importDev.uuid == 'string') { device.intelamt.uuid = importDev.uuid; }
5041 + if (typeof importDev.pstate == 'number') { device.intelamt.state = importDev.pstate; }
5042 + if (typeof importDev.tlscerthash == 'string') { device.intelamt.hash = importDev.tlscerthash; }
5043 + if (typeof importDev.icon == 'number') { device.icon = importDev.icon; }
5044 + if (typeof importDev.desc == 'string') { device.desc = importDev.desc; }
5045 +
5046 + // Check if we are already adding a device with the same hostname, if so, skip it.
5047 + var skip = false;
5048 + for (var i in amtDevices) { if (amtDevices[i].host.toLowerCase() == device.host.toLowerCase()) { skip = true; } }
5049 + if (skip == false) { amtDevices.push(device); }
5050 + }
5051 + }
5052 + }
5053 +
5054 + // Add all the correctly parsed devices to the database and event them
5055 + // TODO: We may want to remove any devices with duplicate hostnames
5056 + if (amtDevices.length == 0) return;
5057 + for (var i in amtDevices) {
5058 + // Save the device to the database
5059 + db.Set(amtDevices[i]);
5060 + // Event the new node
5061 + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(command.meshid, [nodeid]), obj, { etype: 'node', userid: user._id, username: user.name, action: 'addnode', node: parent.CloneSafeNode(amtDevices[i]), msgid: 84, msgArgs: [amtDevices[i].name, mesh.name], msg: 'Added device ' + amtDevices[i].name + ' to device group ' + mesh.name, domain: domain.id });
5062 + }
5063 + break;
5064 + }
5065 default: {
5066 // Unknown user action
5067 console.log('Unknown action from user ' + user.name + ': ' + command.action + '.');
views/default.handlebars
+30 -1
@@ -4969,6 +4969,35 @@
4969 return false;
4970 }
4971
4972 + // Intel AMT device import for a device group
4973 + function showAmtImport(meshid) {
4974 + if (xxdialogMode) return false;
4975 + var x = '<form method=post enctype=multipart/form-data action=amtimport.ashx target=fileUploadFrame><input type=text name=link style=display:none id=amtImportDevGroup value="' + meshid + '" /><input type=file name=files id=amtImportInput style=width:100% onchange="p5updateUploadDialogOk(\'p5uploadinput\')" /><input type=hidden name=authCookie value=' + authCookie + ' /><input type=submit id=p5loginSubmit style=display:none /></form>';
4976 + setDialogMode(2, "Import Intel AMT devices", 3, p5uploadFileEx, x);
4977 + p5updateUploadDialogOk('amtImportInput');
4978 + }
4979 +
4980 + function showAmtImport(meshid) {
4981 + if (xxdialogMode) return;
4982 + var x = "Create many Intel&reg; AMT device at once by importing a JSON file with the following format:" + '<br /><pre>[\r\n {\r\n "fqdn":"mycomputer.com",\r\n "uuid":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",\r\n "version":"12.0.1",\r\n "username":"admin",\r\n "password":"password",\r\n "tls":true\r\n }\r\n]</pre><input style=width:370px type=file id=d4importFile accept=".json" onchange=showAmtImportValidate() />';
4983 + setDialogMode(2, "Intel&reg; AMT Device Import", 3, showAmtImportEx, x, meshid);
4984 + QE('idx_dlgOkButton', false);
4985 + }
4986 +
4987 + function showAmtImportValidate() {
4988 + QE('idx_dlgOkButton', Q('d4importFile').value != null);
4989 + }
4990 +
4991 + function showAmtImportEx(b, meshid) {
4992 + var fr = new FileReader();
4993 + fr.onload = function (r) {
4994 + var j = null;
4995 + try { j = JSON.parse(r.target.result); } catch (ex) { setDialogMode(2, "Intel&reg; AMT Device Import", 1, null, format("Invalid JSON file: {0}.", ex)); return; }
4996 + if (j != null) { meshserver.send({ action: 'importamtdevices', meshid: meshid, amtdevices: j }); } else { setDialogMode(2, "Intel&reg; AMT Device Import", 1, null, "Invalid JSON file format."); }
4997 + };
4998 + fr.readAsText(Q('d4importFile').files[0]);
4999 + }
5000 +
5001 // Intel AMT activation and configuration for agentless device groups
5002 function showAmtSetup(meshid) {
5003 if (xxdialogMode) return false;
@@ -12396,7 +12425,7 @@
12425
12426 //if (meshrights & 4) { }
12427 if (currentMesh.mtype == 1) {
12399 - //x += '<a href=# style=cursor:pointer;margin-right:10px title="' + "Import Intel&reg; AMT devices." + '" onclick=\'return showAmtInput("' + currentMesh._id + '")\'><img src=images/icon-installmesh.png border=0 height=12 width=12> ' + "Import" + '</a>';
12428 + if (meshrights & 1) { x += '<a href=# style=cursor:pointer;margin-right:10px title="' + "Import Intel&reg; AMT devices." + '" onclick=\'return showAmtImport("' + currentMesh._id + '")\'><img src=images/icon-installmesh.png border=0 height=12 width=12> ' + "Import" + '</a>'; }
12429 /*
12430 if ((features & 1) == 0) { // If not WAN-Only
12431 x += '<a href=# onclick=\'return addDeviceToMesh("' + currentMesh._id + '")\' style=cursor:pointer;margin-right:10px title="' + "Add a new Intel&reg; AMT computer that is located on the local network." + '"><img src=images/icon-installmesh.png border=0 height=12 width=12> ' + "Install local" + '</a>';