Skip to content

Instantly share code, notes, and snippets.

@ChecksumFailed
Last active October 29, 2021 12:31
Show Gist options
  • Save ChecksumFailed/0477eb42801c508b5917c026828e163e to your computer and use it in GitHub Desktop.
Save ChecksumFailed/0477eb42801c508b5917c026828e163e to your computer and use it in GitHub Desktop.
Script include used as part of secret server discovery integration. Inspired by https://joscor.com/blog/servicenow-thycotic-secret-server/
var SecretServer = Class.create();
SecretServer.prototype = {
initialize: function() {
this.BASE_URL = 'https://mySecretServer/SecretServer'; //temp change due to load balanced URL being down
this.AUTH_PROFILE = 'SYSID'; //sys_id of basic auth profile
this.TOKEN_CRED = this._getCredential(this.AUTH_PROFILE); //Glide Record of auth Profile
this.ACCESS_TOKEN = ''; //Holder for Access Token
this.GRANTJSON = 'SYSID'; //sys_id of oauth2_grant.json script file
this.MID = this._getMid(); // Get mid server to use for request
},
/*** Description: return access Token.
* Parameters: encrPass - encrypted password
* Returns: string */
getAccessToken: function() {
try {
var client = new sn_ws.RESTMessageV2();
client.setHttpMethod('post');
client.setEndpoint(this.BASE_URL + '/oauth2/token');
client.setEccParameter("skip_sensor", "true"); // prevent Discovery sensors running for the ECC input
client.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
client.setRequestBody('username=' + this.TOKEN_CRED.username.toString() +
'&password=' + this._getDecryptedPassword(this.TOKEN_CRED.password.toString()) +
'&grant_type=password');
client.setHttpTimeout(10000);
client.setMIDServer(this.MID);
// Make request, decode JSON response, and return the access token
this.ACCESS_TOKEN = (JSON.parse(client.execute().getBody())).access_token;
} catch (ex) {
gs.error('Exception: (' + ex + '), ' + ex.getMessage());
}
return this;
},
updateScriptFile: function() {
if (JSUtil.nil(this.ACCESS_TOKEN)) {
return;
}
var grSF = new GlideRecord('ecc_agent_script_file');
grSF.get(this.GRANTJSON);
grSF.setValue('script', this.ACCESS_TOKEN);
grSF.update();
return this;
},
/*** Description: Get credential record
* Parameters: credID - sys_id of credential
* Returns: Credential GlideRecord */
_getCredential: function(credID) {
if (JSUtil.nil(credID)) {
return;
}
var grCred = new GlideRecord('sys_auth_profile_basic');
return grCred.get(credID) ? grCred : null;
},
/*** Description: return deccrypted password
* Parameters: encrPass - encrypted password
* Returns: string */
_getDecryptedPassword: function(encrPass) {
if (JSUtil.nil(encrPass)) {
return;
}
return new GlideEncrypter().decrypt(encrPass);
},
/*** Description: find available mid server to use for request
* Parameters:
* Returns: sys_id of mid */
_getMid: function() {
var grMid = new GlideRecord('ecc_agent_application_m2m');
grMid.addEncodedQuery('application=35aa573fd7802200bdbaee5b5e610375^ORapplication=b5f91a57d7002200bdbaee5b5e6103ec^agent.status=Up');
grMid.setLimit(1);
grMid.query();
grMid.next();
return grMid.agent.name.toString();
},
type: 'SecretServer'
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment