We are currently experiencing intermittent login issues on Community.  The team is actively working on a fix.

How retrieve password stored as credentials into script include

BartFrelian1337
Tera Contributor

Hello Folks!

 

I am running into bit of a problem.

I have a script include that execute Outbound REST API call.

Trying to build this script include as safetely as possible therefore i have stored the API Key as a password in the Credentials table.

The regular glide record failed to retrieve value from that table so i found GlideCredentialUtility but i expect i am doing something wrong to get the value.

 

Here is my snip-set:

 

-------------------


// Retrieve API Key from Credentials table using GlideCredentialUtility
var apiKey = new GlideCredentialUtility().getPassword(credentialSysId);
 
var apiUrl = baseUrl + functionName + "?code=" + apiKey;
 
gs.info(apiUrl);
 
This gives me following exception:
 
com.glide.script.RhinoEcmaError: "GlideCredentialUtility" is not defined.
sys_script_include.b04412b7475cae109371eee3716d43bc.script : Line(23) column(0)

Stack trace:
at sys_script_include.b04412b7475cae109371eee3716d43bc.script:23 (callAzureFunction)
at sys_script.81e7dee347986e109371eee3716d439d.script:5 (executeRule)
at sys_script.81e7dee347986e109371eee3716d439d.script:8
 
Do you have experience in such a usecase how to securely retrieve value stored in the Credentials table into Script Include?
 
Any help will be appreciated!
 
Thanks
Bart.
 
 
6 REPLIES 6

Hey, have you explored the following API?
StandardCredentialsProvider - Scoped, Global

For example, in my case I needed to store an API Key in the sys_alias & api_key_credentials tables.
The way I got the API key extracted so I can use it in scripts (i.e: Triggering a REST Call with RestMessageV2) was running a code like this:

function getAPIKey(){
	var credentialAliasId = "<ALIAS SYS ID>";
	var provider = new sn_cc.StandardCredentialsProvider();
	var credentials = provider.getCredentialByAliasID(credentialAliasId);

	return credentials.getAttribute("api_key");
}

 
However, I noticed that, when storing the value in with a system property of type password2, the extraction is a bit faster (+-10ms).

To be honest, I have not been able to find any recommendations on where to store API Keys or Credential related information and how to use them from scripts.

Hope this helps!


TejasSN_LogicX
Tera Contributor

hi ,

this throws "GlideCredentialUtility" is not defined error.
You should not query the Credentials (sys_credentials) table directly, because passwords there are encrypted.
Instead, you use the sn_auth.Credential API that ServiceNow provides.

 

 You should not query the Credentials (sys_credentials) table directly, because passwords there are encrypted.
Instead, you use the sn_auth.Credential API that ServiceNow provides.