| 1 | /* |
| 2 | Copyright 2020-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 | @description Intel AMT WSMAN Stack |
| 17 | @author Ylian Saint-Hilaire |
| 18 | @version v0.3.0 |
| 19 | */ |
| 20 | |
| 21 | /*jslint node: true */ |
| 22 | /*jshint node: true */ |
| 23 | /*jshint strict:false */ |
| 24 | /*jshint -W097 */ |
| 25 | /*jshint esversion: 6 */ |
| 26 | "use strict"; |
| 27 | |
| 28 | // Construct a WSMAN stack object |
| 29 | function WsmanStackCreateService(comm) |
| 30 | { |
| 31 | var obj = {_ObjectID: 'WSMAN'}; |
| 32 | //obj.onDebugMessage = null; // Set to a function if you want to get debug messages. |
| 33 | obj.NextMessageId = 1; // Next message number, used to label WSMAN calls. |
| 34 | obj.Address = '/wsman'; |
| 35 | obj.xmlParser = require('./amt-xml.js'); |
| 36 | obj.comm = comm; |
| 37 | |
| 38 | obj.PerformAjax = function PerformAjax(postdata, callback, tag, pri, namespaces) { |
| 39 | if (namespaces == null) namespaces = ''; |
| 40 | 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) { |
| 41 | var wsresponse = null; |
| 42 | if (data != null) { try { wsresponse = obj.xmlParser.ParseWsman(data); } catch (ex) { } } |
| 43 | if ((data != null) && (!wsresponse || wsresponse == null) && (status == 200)) { |
| 44 | callback(obj, null, { Header: { HttpError: status } }, 601, tag); |
| 45 | } else { |
| 46 | if (status != 200) { |
| 47 | if (wsresponse == null) { wsresponse = { Header: {} }; } |
| 48 | wsresponse.Header.HttpError = status; |
| 49 | try { wsresponse.Header.WsmanError = wsresponse.Body['Reason']['Text']['Value']; } catch (ex) { } |
| 50 | } |
| 51 | callback(obj, wsresponse.Header['ResourceURI'], wsresponse, status, tag); |
| 52 | } |
| 53 | }, tag, pri); |
| 54 | } |
| 55 | |
| 56 | // Private method |
| 57 | //obj.Debug = function (msg) { /*console.log(msg);*/ } |
| 58 | |
| 59 | // Cancel all pending queries with given status |
| 60 | obj.CancelAllQueries = function CancelAllQueries(s) { obj.comm.CancelAllQueries(s); } |
| 61 | |
| 62 | // Get the last element of a URI string |
| 63 | obj.GetNameFromUrl = function (resuri) { |
| 64 | var x = resuri.lastIndexOf("/"); |
| 65 | return (x == -1)?resuri:resuri.substring(x + 1); |
| 66 | } |
| 67 | |
| 68 | // Perform a WSMAN Subscribe operation |
| 69 | obj.ExecSubscribe = function ExecSubscribe(resuri, delivery, url, callback, tag, pri, selectors, opaque, user, pass) { |
| 70 | var digest = "", digest2 = "", opaque = ""; |
| 71 | 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"/>'; } |
| 72 | if (opaque != null) { opaque = '<a:ReferenceParameters><m:arg>' + opaque + '</m:arg></a:ReferenceParameters>'; } |
| 73 | if (delivery == 'PushWithAck') { delivery = 'dmtf.org/wbem/wsman/1/wsman/PushWithAck'; } else if (delivery == 'Push') { delivery = 'xmlsoap.org/ws/2004/08/eventing/DeliveryModes/Push'; } |
| 74 | 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>'; |
| 75 | obj.PerformAjax(data + "</Body></Envelope>", callback, tag, pri, 'xmlns:e="http://schemas.xmlsoap.org/ws/2004/08/eventing" xmlns:m="http://x.com"'); |
| 76 | } |
| 77 | |
| 78 | // Perform a WSMAN UnSubscribe operation |
| 79 | obj.ExecUnSubscribe = function ExecUnSubscribe(resuri, callback, tag, pri, selectors) { |
| 80 | 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/>'; |
| 81 | obj.PerformAjax(data + "</Body></Envelope>", callback, tag, pri, 'xmlns:e="http://schemas.xmlsoap.org/ws/2004/08/eventing"'); |
| 82 | } |
| 83 | |
| 84 | // Perform a WSMAN PUT operation |
| 85 | obj.ExecPut = function ExecPut(resuri, putobj, callback, tag, pri, selectors) { |
| 86 | 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); |
| 87 | obj.PerformAjax(data + "</Body></Envelope>", callback, tag, pri); |
| 88 | } |
| 89 | |
| 90 | // Perform a WSMAN CREATE operation |
| 91 | obj.ExecCreate = function ExecCreate(resuri, putobj, callback, tag, pri, selectors) { |
| 92 | var objname = obj.GetNameFromUrl(resuri); |
| 93 | 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 + "\">"; |
| 94 | for (var n in putobj) { data += "<g:" + n + ">" + putobj[n] + "</g:" + n + ">" } |
| 95 | obj.PerformAjax(data + "</g:" + objname + "></Body></Envelope>", callback, tag, pri); |
| 96 | } |
| 97 | |
| 98 | // Perform a WSMAN DELETE operation |
| 99 | obj.ExecDelete = function ExecDelete(resuri, putobj, callback, tag, pri) { |
| 100 | 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>"; |
| 101 | obj.PerformAjax(data, callback, tag, pri); |
| 102 | } |
| 103 | |
| 104 | // Perform a WSMAN GET operation |
| 105 | obj.ExecGet = function ExecGet(resuri, callback, tag, pri) { |
| 106 | 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); |
| 107 | } |
| 108 | |
| 109 | // Perform a WSMAN method call operation |
| 110 | obj.ExecMethod = function ExecMethod(resuri, method, args, callback, tag, pri, selectors) { |
| 111 | var argsxml = ""; |
| 112 | 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 + ">"; } } } |
| 113 | obj.ExecMethodXml(resuri, method, argsxml, callback, tag, pri, selectors); |
| 114 | } |
| 115 | |
| 116 | // Perform a WSMAN method call operation. The arguments are already formatted in XML. |
| 117 | obj.ExecMethodXml = function ExecMethodXml(resuri, method, argsxml, callback, tag, pri, selectors) { |
| 118 | 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); |
| 119 | } |
| 120 | |
| 121 | // Perform a WSMAN ENUM operation |
| 122 | obj.ExecEnum = function ExecEnum(resuri, callback, tag, pri) { |
| 123 | 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); |
| 124 | } |
| 125 | |
| 126 | // Perform a WSMAN PULL operation |
| 127 | obj.ExecPull = function ExecPull(resuri, enumctx, callback, tag, pri) { |
| 128 | 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); |
| 129 | } |
| 130 | |
| 131 | function _PutObjToBodyXml(resuri, putObj) { |
| 132 | if (!resuri || putObj == null) return ''; |
| 133 | var objname = obj.GetNameFromUrl(resuri); |
| 134 | var result = '<r:' + objname + ' xmlns:r="' + resuri + '">'; |
| 135 | |
| 136 | for (var prop in putObj) { |
| 137 | if (!putObj.hasOwnProperty(prop) || prop.indexOf('__') === 0 || prop.indexOf('@') === 0) continue; |
| 138 | if (putObj[prop] == null || typeof putObj[prop] === 'function') continue; |
| 139 | if (typeof putObj[prop] === 'object' && putObj[prop]['ReferenceParameters']) { |
| 140 | result += '<r:' + prop + '><a:Address>' + putObj[prop].Address + '</a:Address><a:ReferenceParameters><w:ResourceURI>' + putObj[prop]['ReferenceParameters']["ResourceURI"] + '</w:ResourceURI><w:SelectorSet>'; |
| 141 | var selectorArray = putObj[prop]['ReferenceParameters']['SelectorSet']['Selector']; |
| 142 | if (Array.isArray(selectorArray)) { |
| 143 | for (var i=0; i< selectorArray.length; i++) { |
| 144 | result += '<w:Selector' + _ObjectToXmlAttributes(selectorArray[i]) + '>' + selectorArray[i]['Value'] + '</w:Selector>'; |
| 145 | } |
| 146 | } |
| 147 | else { |
| 148 | result += '<w:Selector' + _ObjectToXmlAttributes(selectorArray) + '>' + selectorArray['Value'] + '</w:Selector>'; |
| 149 | } |
| 150 | result += '</w:SelectorSet></a:ReferenceParameters></r:' + prop + '>'; |
| 151 | } |
| 152 | else { |
| 153 | if (Array.isArray(putObj[prop])) { |
| 154 | for (var i = 0; i < putObj[prop].length; i++) { |
| 155 | result += '<r:' + prop + '>' + putObj[prop][i].toString() + '</r:' + prop + '>'; |
| 156 | } |
| 157 | } else { |
| 158 | result += '<r:' + prop + '>' + putObj[prop].toString() + '</r:' + prop + '>'; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | result += '</r:' + objname + '>'; |
| 164 | return result; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | convert |
| 169 | { @Name: 'InstanceID', @AttrName: 'Attribute Value'} |
| 170 | into |
| 171 | ' Name="InstanceID" AttrName="Attribute Value" ' |
| 172 | */ |
| 173 | function _ObjectToXmlAttributes(objWithAttributes) { |
| 174 | if(!objWithAttributes) return ''; |
| 175 | var result = ' '; |
| 176 | for (var propName in objWithAttributes) { |
| 177 | if (!objWithAttributes.hasOwnProperty(propName) || propName.indexOf('@') !== 0) continue; |
| 178 | result += propName.substring(1) + '="' + objWithAttributes[propName] + '" '; |
| 179 | } |
| 180 | return result; |
| 181 | } |
| 182 | |
| 183 | function _PutObjToSelectorsXml(selectorSet) { |
| 184 | if (!selectorSet) return ''; |
| 185 | if (typeof selectorSet == 'string') return selectorSet; |
| 186 | if (selectorSet['InstanceID']) return "<w:SelectorSet><w:Selector Name=\"InstanceID\">" + selectorSet['InstanceID'] + "</w:Selector></w:SelectorSet>"; |
| 187 | var result = '<w:SelectorSet>'; |
| 188 | for(var propName in selectorSet) { |
| 189 | if (!selectorSet.hasOwnProperty(propName)) continue; |
| 190 | result += '<w:Selector Name="' + propName + '">'; |
| 191 | if (selectorSet[propName]['ReferenceParameters']) { |
| 192 | result += '<a:EndpointReference>'; |
| 193 | result += '<a:Address>' + selectorSet[propName]['Address'] + '</a:Address><a:ReferenceParameters><w:ResourceURI>' + selectorSet[propName]['ReferenceParameters']['ResourceURI'] + '</w:ResourceURI><w:SelectorSet>'; |
| 194 | var selectorArray = selectorSet[propName]['ReferenceParameters']['SelectorSet']['Selector']; |
| 195 | if (Array.isArray(selectorArray)) { |
| 196 | for (var i = 0; i < selectorArray.length; i++) { |
| 197 | result += '<w:Selector' + _ObjectToXmlAttributes(selectorArray[i]) + '>' + selectorArray[i]['Value'] + '</w:Selector>'; |
| 198 | } |
| 199 | } else { |
| 200 | result += '<w:Selector' + _ObjectToXmlAttributes(selectorArray) + '>' + selectorArray['Value'] + '</w:Selector>'; |
| 201 | } |
| 202 | result += '</w:SelectorSet></a:ReferenceParameters></a:EndpointReference>'; |
| 203 | } else { |
| 204 | result += selectorSet[propName]; |
| 205 | } |
| 206 | result += '</w:Selector>'; |
| 207 | } |
| 208 | result += '</w:SelectorSet>'; |
| 209 | return result; |
| 210 | } |
| 211 | |
| 212 | return obj; |
| 213 | } |
| 214 | |
| 215 | module.exports = WsmanStackCreateService; |