Using REST API from ServiceNow Scripting
I would like to use the REST API from ServiceNow but I'm not able to generate the HMAC using the ServiceNow Glide System native class GlideCertificateEncryption(), the resulting values don't match with what I would expect from Python or PowerShell. Are there any ServiceNow developers out there who have managed to call the LogicMonitor REST API from a ServiceNow script? If you have, could anyone share an example script please.
var JavaString = Packages.java.lang.String;
var key = new JavaString("key");
var message = new JavaString("message");
var keyUTF8 = new JavaString(key.getBytes("UTF8"));
var messageUTF8 = new JavaString(message.getBytes("UTF8"));
var util = new GlideStringUtil();
var encryption = new GlideCertificateEncryption();
//var mac = encryption.generateMac(util.base64Encode(keyUTF8), "HMACSHA256", messageUTF8);
var mac = encryption.generateMac(HexUtil.convertToBase64(keyUTF8 + ""), "HMACSHA256", messageUTF8 + "");
gs.log(mac);
gs.log(HexUtil.convertToBase64(mac));
var decoded = util.base64Decode(mac);
var utf8Bytes = decoded.getBytes("UTF8");
gs.log(utf8Bytes.length);
gs.log(JSON.stringify(utf8Bytes));
See this thread for additional information.
https://community.logicmonitor.com/product-discussions-22/trouble-authenticating-to-logicmonitor-rest-api-from-servicenow-3537Mosh
OP5 years agoHi @sirisha,
Can you paste more of your code? Need to see what you are doing before you call this function.
sirisha
·5 years agoMosh
OP5 years agoAnswer above.
Mosh
OP5 years agoSolved it. For anyone who needs it here is how to generate the MAC part of the signature using only native ServiceNow script and classes. (I had to write my own convertByteArrayToHex() utility as there was nothing I could find native in ServiceNow.) The problem with using CryptoJS is that in ServiceNow it would only be allowed in a scoped app and I wanted to do this in the global scope.
Mosh
OP5 years agoThanks @Mike Moniz that's a good idea.
Mike Moniz
·5 years agoWhen I lasted looked at it SN didn't have the built in hash function to implement the auth needed for LM. If you install the "LogicMonitor Incident Management Integration" add-on from the ServiceNow store, It provides some supporting script includes functions you may be able to use.