| 1 | /* |
| 2 | Copyright 2018-2021 Intel Corporation |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /** |
| 18 | * @description Intel(r) AMT WSMAN Stack |
| 19 | * @author Ylian Saint-Hilaire |
| 20 | * @version v0.2.0 |
| 21 | */ |
| 22 | |
| 23 | // Construct a MeshServer object |
| 24 | function WsmanStackCreateService(/*CreateWsmanComm, host, port, user, pass, tls, extra*/) |
| 25 | { |
| 26 | var obj = {_ObjectID: 'WSMAN'}; |
| 27 | //obj.onDebugMessage = null; // Set to a function if you want to get debug messages. |
| 28 | obj.NextMessageId = 1; // Next message number, used to label WSMAN calls. |
| 29 | obj.Address = '/wsman'; |
| 30 | obj.xmlParser = require('amt-xml'); |
| 31 | |
| 32 | if (arguments.length == 1 && typeof (arguments[0] == 'object')) |
| 33 | { |
| 34 | var CreateWsmanComm = arguments[0].transport; |
| 35 | if (CreateWsmanComm) { obj.comm = new CreateWsmanComm(arguments[0]); } |
| 36 | } |
| 37 | else |
| 38 | { |
| 39 | var CreateWsmanComm = arguments[0]; |
| 40 | if (CreateWsmanComm) { obj.comm = new CreateWsmanComm(arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6]); } |
| 41 | } |
| 42 | |
| 43 | obj.PerformAjax = function PerformAjax(postdata, callback, tag, pri, namespaces) { |
| 44 | if (namespaces == null) namespaces = ''; |
| 45 | obj.comm.PerformAjax('<?xml version=\"1.0\" encoding=\"utf-8\"?><Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd" xmlns=\"http://www.w3.org/2003/05/soap-envelope\" ' + namespaces + '><Header><a:Action>' + postdata, function (data, status, tag) { |
| 46 | if (status != 200) { callback(obj, null, (data != null) ? data : { Header: { HttpError: status } }, status, tag); return; } |
| 47 | var wsresponse = obj.xmlParser.ParseWsman(data); |
| 48 | if (!wsresponse || wsresponse == null) { callback(obj, null, { Header: { HttpError: status } }, 601, tag); } else { callback(obj, wsresponse.Header["ResourceURI"], wsresponse, 200, tag); } |
| 49 | }, tag, pri); |
| 50 | } |
| 51 | |
| 52 | // Private method |
| 53 | //obj.Debug = function (msg) { /*console.log(msg);*/ } |
| 54 | |
| 55 | // Cancel all pending queries with given status |
| 56 | obj.CancelAllQueries = function CancelAllQueries(s) { obj.comm.CancelAllQueries(s); } |
| 57 | |
| 58 | // Get the last element of a URI string |
| 59 | obj.GetNameFromUrl = function (resuri) { |
| 60 | var x = resuri.lastIndexOf("/"); |
| 61 | return (x == -1)?resuri:resuri.substring(x + 1); |
| 62 | } |
| 63 | |
| 64 | // Perform a WSMAN Subscribe operation |
| 65 | obj.ExecSubscribe = function ExecSubscribe(resuri, delivery, url, callback, tag, pri, selectors, opaque, user, pass) { |
| 66 | var digest = "", digest2 = "", opaque = ""; |
| 67 | if (user != null && pass != null) { digest = '<t:IssuedTokens xmlns:t="http://schemas.xmlsoap.org/ws/2005/02/trust" xmlns:se="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><t:RequestSecurityTokenResponse><t:TokenType>http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#UsernameToken</t:TokenType><t:RequestedSecurityToken><se:UsernameToken><se:Username>' + user + '</se:Username><se:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd#PasswordText">' + pass + '</se:Password></se:UsernameToken></t:RequestedSecurityToken></t:RequestSecurityTokenResponse></t:IssuedTokens>'; digest2 = '<w:Auth Profile="http://schemas.dmtf.org/wbem/wsman/1/wsman/secprofile/http/digest"/>'; } |
| 68 | if (opaque != null) { opaque = '<a:ReferenceParameters><m:arg>' + opaque + '</m:arg></a:ReferenceParameters>'; } |
| 69 | if (delivery == 'PushWithAck') { delivery = 'dmtf.org/wbem/wsman/1/wsman/PushWithAck'; } else if (delivery == 'Push') { delivery = 'xmlsoap.org/ws/2004/08/eventing/DeliveryModes/Push'; } |
| 70 | var data = "http://schemas.xmlsoap.org/ws/2004/08/eventing/Subscribe</a:Action><a:To>" + obj.Address + "</a:To><w:ResourceURI>" + resuri + "</w:ResourceURI><a:MessageID>" + (obj.NextMessageId++) + "</a:MessageID><a:ReplyTo><a:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address></a:ReplyTo>" + _PutObjToSelectorsXml(selectors) + digest + '</Header><Body><e:Subscribe><e:Delivery Mode="http://schemas.' + delivery + '"><e:NotifyTo><a:Address>' + url + '</a:Address>' + opaque + '</e:NotifyTo>' + digest2 + '</e:Delivery></e:Subscribe>'; |
| 71 | obj.PerformAjax(data + "</Body></Envelope>", callback, tag, pri, 'xmlns:e="http://schemas.xmlsoap.org/ws/2004/08/eventing" xmlns:m="http://x.com"'); |
| 72 | } |
| 73 | |
| 74 | // Perform a WSMAN UnSubscribe operation |
| 75 | obj.ExecUnSubscribe = function ExecUnSubscribe(resuri, callback, tag, pri, selectors) { |
| 76 | var data = "http://schemas.xmlsoap.org/ws/2004/08/eventing/Unsubscribe</a:Action><a:To>" + obj.Address + "</a:To><w:ResourceURI>" + resuri + "</w:ResourceURI><a:MessageID>" + (obj.NextMessageId++) + "</a:MessageID><a:ReplyTo><a:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address></a:ReplyTo>" + _PutObjToSelectorsXml(selectors) + '</Header><Body><e:Unsubscribe/>'; |
| 77 | obj.PerformAjax(data + "</Body></Envelope>", callback, tag, pri, 'xmlns:e="http://schemas.xmlsoap.org/ws/2004/08/eventing"'); |
| 78 | } |
| 79 | |
| 80 | // Perform a WSMAN PUT operation |
| 81 | obj.ExecPut = function ExecPut(resuri, putobj, callback, tag, pri, selectors) { |
| 82 | var data = "http://schemas.xmlsoap.org/ws/2004/09/transfer/Put</a:Action><a:To>" + obj.Address + "</a:To><w:ResourceURI>" + resuri + "</w:ResourceURI><a:MessageID>" + (obj.NextMessageId++) + "</a:MessageID><a:ReplyTo><a:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address></a:ReplyTo><w:OperationTimeout>PT60.000S</w:OperationTimeout>" + _PutObjToSelectorsXml(selectors) + '</Header><Body>' + _PutObjToBodyXml(resuri, putobj); |
| 83 | obj.PerformAjax(data + "</Body></Envelope>", callback, tag, pri); |
| 84 | } |
| 85 | |
| 86 | // Perform a WSMAN CREATE operation |
| 87 | obj.ExecCreate = function ExecCreate(resuri, putobj, callback, tag, pri, selectors) { |
| 88 | var objname = obj.GetNameFromUrl(resuri); |
| 89 | var data = "http://schemas.xmlsoap.org/ws/2004/09/transfer/Create</a:Action><a:To>" + obj.Address + "</a:To><w:ResourceURI>" + resuri + "</w:ResourceURI><a:MessageID>" + (obj.NextMessageId++) + "</a:MessageID><a:ReplyTo><a:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address></a:ReplyTo><w:OperationTimeout>PT60S</w:OperationTimeout>" + _PutObjToSelectorsXml(selectors) + "</Header><Body><g:" + objname + " xmlns:g=\"" + resuri + "\">"; |
| 90 | for (var n in putobj) { data += "<g:" + n + ">" + putobj[n] + "</g:" + n + ">" } |
| 91 | obj.PerformAjax(data + "</g:" + objname + "></Body></Envelope>", callback, tag, pri); |
| 92 | } |
| 93 | |
| 94 | // Perform a WSMAN DELETE operation |
| 95 | obj.ExecDelete = function ExecDelete(resuri, putobj, callback, tag, pri) { |
| 96 | var data = "http://schemas.xmlsoap.org/ws/2004/09/transfer/Delete</a:Action><a:To>" + obj.Address + "</a:To><w:ResourceURI>" + resuri + "</w:ResourceURI><a:MessageID>" + (obj.NextMessageId++) + "</a:MessageID><a:ReplyTo><a:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address></a:ReplyTo><w:OperationTimeout>PT60S</w:OperationTimeout>" + _PutObjToSelectorsXml(putobj) + "</Header><Body /></Envelope>"; |
| 97 | obj.PerformAjax(data, callback, tag, pri); |
| 98 | } |
| 99 | |
| 100 | // Perform a WSMAN GET operation |
| 101 | obj.ExecGet = function ExecGet(resuri, callback, tag, pri) { |
| 102 | obj.PerformAjax("http://schemas.xmlsoap.org/ws/2004/09/transfer/Get</a:Action><a:To>" + obj.Address + "</a:To><w:ResourceURI>" + resuri + "</w:ResourceURI><a:MessageID>" + (obj.NextMessageId++) + "</a:MessageID><a:ReplyTo><a:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address></a:ReplyTo><w:OperationTimeout>PT60S</w:OperationTimeout></Header><Body /></Envelope>", callback, tag, pri); |
| 103 | } |
| 104 | |
| 105 | // Perform a WSMAN method call operation |
| 106 | obj.ExecMethod = function ExecMethod(resuri, method, args, callback, tag, pri, selectors) { |
| 107 | var argsxml = ""; |
| 108 | for (var i in args) { if (args[i] != null) { if (Array.isArray(args[i])) { for (var x in args[i]) { argsxml += "<r:" + i + ">" + args[i][x] + "</r:" + i + ">"; } } else { argsxml += "<r:" + i + ">" + args[i] + "</r:" + i + ">"; } } } |
| 109 | obj.ExecMethodXml(resuri, method, argsxml, callback, tag, pri, selectors); |
| 110 | } |
| 111 | |
| 112 | // Perform a WSMAN method call operation. The arguments are already formatted in XML. |
| 113 | obj.ExecMethodXml = function ExecMethodXml(resuri, method, argsxml, callback, tag, pri, selectors) { |
| 114 | obj.PerformAjax(resuri + "/" + method + "</a:Action><a:To>" + obj.Address + "</a:To><w:ResourceURI>" + resuri + "</w:ResourceURI><a:MessageID>" + (obj.NextMessageId++) + "</a:MessageID><a:ReplyTo><a:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address></a:ReplyTo><w:OperationTimeout>PT60S</w:OperationTimeout>" + _PutObjToSelectorsXml(selectors) + "</Header><Body><r:" + method + '_INPUT' + " xmlns:r=\"" + resuri + "\">" + argsxml + "</r:" + method + "_INPUT></Body></Envelope>", callback, tag, pri); |
| 115 | } |
| 116 | |
| 117 | // Perform a WSMAN ENUM operation |
| 118 | obj.ExecEnum = function ExecEnum(resuri, callback, tag, pri) { |
| 119 | obj.PerformAjax("http://schemas.xmlsoap.org/ws/2004/09/enumeration/Enumerate</a:Action><a:To>" + obj.Address + "</a:To><w:ResourceURI>" + resuri + "</w:ResourceURI><a:MessageID>" + (obj.NextMessageId++) + "</a:MessageID><a:ReplyTo><a:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address></a:ReplyTo><w:OperationTimeout>PT60S</w:OperationTimeout></Header><Body><Enumerate xmlns=\"http://schemas.xmlsoap.org/ws/2004/09/enumeration\" /></Body></Envelope>", callback, tag, pri); |
| 120 | } |
| 121 | |
| 122 | // Perform a WSMAN PULL operation |
| 123 | obj.ExecPull = function ExecPull(resuri, enumctx, callback, tag, pri) { |
| 124 | obj.PerformAjax("http://schemas.xmlsoap.org/ws/2004/09/enumeration/Pull</a:Action><a:To>" + obj.Address + "</a:To><w:ResourceURI>" + resuri + "</w:ResourceURI><a:MessageID>" + (obj.NextMessageId++) + "</a:MessageID><a:ReplyTo><a:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address></a:ReplyTo><w:OperationTimeout>PT60S</w:OperationTimeout></Header><Body><Pull xmlns=\"http://schemas.xmlsoap.org/ws/2004/09/enumeration\"><EnumerationContext>" + enumctx + "</EnumerationContext><MaxElements>999</MaxElements><MaxCharacters>99999</MaxCharacters></Pull></Body></Envelope>", callback, tag, pri); |
| 125 | } |
| 126 | |
| 127 | function _PutObjToBodyXml(resuri, putObj) { |
| 128 | if (!resuri || putObj == null) return ''; |
| 129 | var objname = obj.GetNameFromUrl(resuri); |
| 130 | var result = '<r:' + objname + ' xmlns:r="' + resuri + '">'; |
| 131 | |
| 132 | for (var prop in putObj) { |
| 133 | if (!putObj.hasOwnProperty(prop) || prop.indexOf('__') === 0 || prop.indexOf('@') === 0) continue; |
| 134 | if (putObj[prop] == null || typeof putObj[prop] === 'function') continue; |
| 135 | if (typeof putObj[prop] === 'object' && putObj[prop]['ReferenceParameters']) { |
| 136 | result += '<r:' + prop + '><a:Address>' + putObj[prop].Address + '</a:Address><a:ReferenceParameters><w:ResourceURI>' + putObj[prop]['ReferenceParameters']["ResourceURI"] + '</w:ResourceURI><w:SelectorSet>'; |
| 137 | var selectorArray = putObj[prop]['ReferenceParameters']['SelectorSet']['Selector']; |
| 138 | if (Array.isArray(selectorArray)) { |
| 139 | for (var i=0; i< selectorArray.length; i++) { |
| 140 | result += '<w:Selector' + _ObjectToXmlAttributes(selectorArray[i]) + '>' + selectorArray[i]['Value'] + '</w:Selector>'; |
| 141 | } |
| 142 | } |
| 143 | else { |
| 144 | result += '<w:Selector' + _ObjectToXmlAttributes(selectorArray) + '>' + selectorArray['Value'] + '</w:Selector>'; |
| 145 | } |
| 146 | result += '</w:SelectorSet></a:ReferenceParameters></r:' + prop + '>'; |
| 147 | } |
| 148 | else { |
| 149 | if (Array.isArray(putObj[prop])) { |
| 150 | for (var i = 0; i < putObj[prop].length; i++) { |
| 151 | result += '<r:' + prop + '>' + putObj[prop][i].toString() + '</r:' + prop + '>'; |
| 152 | } |
| 153 | } else { |
| 154 | result += '<r:' + prop + '>' + putObj[prop].toString() + '</r:' + prop + '>'; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | result += '</r:' + objname + '>'; |
| 160 | return result; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | convert |
| 165 | { @Name: 'InstanceID', @AttrName: 'Attribute Value'} |
| 166 | into |
| 167 | ' Name="InstanceID" AttrName="Attribute Value" ' |
| 168 | */ |
| 169 | function _ObjectToXmlAttributes(objWithAttributes) { |
| 170 | if(!objWithAttributes) return ''; |
| 171 | var result = ' '; |
| 172 | for (var propName in objWithAttributes) { |
| 173 | if (!objWithAttributes.hasOwnProperty(propName) || propName.indexOf('@') !== 0) continue; |
| 174 | result += propName.substring(1) + '="' + objWithAttributes[propName] + '" '; |
| 175 | } |
| 176 | return result; |
| 177 | } |
| 178 | |
| 179 | function _PutObjToSelectorsXml(selectorSet) { |
| 180 | if ((selectorSet == null) || (selectorSet == 'null')) return ''; |
| 181 | if (typeof selectorSet == 'string') return selectorSet; |
| 182 | if (selectorSet['InstanceID']) return "<w:SelectorSet><w:Selector Name=\"InstanceID\">" + selectorSet['InstanceID'] + "</w:Selector></w:SelectorSet>"; |
| 183 | var result = '<w:SelectorSet>'; |
| 184 | for(var propName in selectorSet) { |
| 185 | if (!selectorSet.hasOwnProperty(propName)) continue; |
| 186 | result += '<w:Selector Name="' + propName + '">'; |
| 187 | if (selectorSet[propName]['ReferenceParameters']) { |
| 188 | result += '<a:EndpointReference>'; |
| 189 | result += '<a:Address>' + selectorSet[propName]['Address'] + '</a:Address><a:ReferenceParameters><w:ResourceURI>' + selectorSet[propName]['ReferenceParameters']['ResourceURI'] + '</w:ResourceURI><w:SelectorSet>'; |
| 190 | var selectorArray = selectorSet[propName]['ReferenceParameters']['SelectorSet']['Selector']; |
| 191 | if (Array.isArray(selectorArray)) { |
| 192 | for (var i = 0; i < selectorArray.length; i++) { |
| 193 | result += '<w:Selector' + _ObjectToXmlAttributes(selectorArray[i]) + '>' + selectorArray[i]['Value'] + '</w:Selector>'; |
| 194 | } |
| 195 | } else { |
| 196 | result += '<w:Selector' + _ObjectToXmlAttributes(selectorArray) + '>' + selectorArray['Value'] + '</w:Selector>'; |
| 197 | } |
| 198 | result += '</w:SelectorSet></a:ReferenceParameters></a:EndpointReference>'; |
| 199 | } else { |
| 200 | result += selectorSet[propName]; |
| 201 | } |
| 202 | result += '</w:Selector>'; |
| 203 | } |
| 204 | result += '</w:SelectorSet>'; |
| 205 | return result; |
| 206 | } |
| 207 | |
| 208 | return obj; |
| 209 | } |
| 210 | |
| 211 | module.exports = WsmanStackCreateService; |