Name

sn_agent.Base64CheckParamsUtil

Description

A utility class for managing check params which should be passed Base64 encoded

Script

var Base64CheckParamsUtil = Class.create();
Base64CheckParamsUtil.prototype = {
  initialize: function() {
      this.initBase64EncCheckParams(); // initialize base64 encoded check params
  },

  // as defined in PolicyClientsGenerator
  SERVICE_INFO_PREFIX: "rel_service_",

  // a mapping of check def ids to names of check params (grouped by types defined in BASE64_ENC_CHECK_PARAM_TYPES) to pass base64 encoded, which is 
  // initialized in the constructor/initializtion method. The mapping is from a check def id to a set (a JSON
  // in which the keys are parameter names)
  checkDefIdToBase64CheckParams: {},

  // names of records in sn_agent_flags which provide info about which param values should be passed base64 encoded
  // and for which check defs should this be done. The hash field of the records should be in the following format:
  // "<check_def_id>:<param_name>"
  BASE64_ENC_CHECK_PARAM_TYPES: {
      BASE64_ENC_CHECK_PARAM: "base64_enc_check_param",
      BASE64_ENC_CHECK_CI_PARAM: "base64_enc_check_ci_param"
  },

  // intialize this.checkDefIdToBase64CheckParams
  initBase64EncCheckParams: function() {
      var hashGr = new GlideRecord("sn_agent_flags");
      hashGr.addQuery("name", [this.BASE64_ENC_CHECK_PARAM_TYPES.BASE64_ENC_CHECK_PARAM, this.BASE64_ENC_CHECK_PARAM_TYPES.BASE64_ENC_CHECK_CI_PARAM]);
      hashGr.query();
      while (hashGr.next()) {
          var flagValue = hashGr.getValue("hash");
          if (!gs.nil(flagValue)) {
              var flagName = hashGr.getValue("name");
              var i = flagValue.indexOf(":"); // get the first index where ":" is present
              if (i > 0 && i + 1 < flagValue.length) { // present and not first and not last
                  var checkDefId = flagValue.substring(0, i);
                  var paramName = flagValue.substring(i + 1);
                  if (flagName == this.BASE64_ENC_CHECK_PARAM_TYPES.BASE64_ENC_CHECK_CI_PARAM && paramName.startsWith(this.SERVICE_INFO_PREFIX)) {
                      gs.warn(gs.getMessage("{0}: {1} flag (sys_id = {2}) is prefixed with '{3}' which is forbidden as passing base64 encoded service params is not supported", [Base64CheckParamsUtil.prototype.type, flagName, hashGr.getUniqueValue(), this.SERVICE_INFO_PREFIX]));
                      continue;
                  }
                  var checkDefGr = new GlideRecord("sn_agent_check_def");
                  if (checkDefGr.get(checkDefId)) { // only if the check def actually exists
                      if (!this.checkDefIdToBase64CheckParams.hasOwnProperty(checkDefId)) { // if no values yet for this check def, intialize a set for it
                          this.checkDefIdToBase64CheckParams[checkDefId] = {};
                          this.checkDefIdToBase64CheckParams[checkDefId][this.BASE64_ENC_CHECK_PARAM_TYPES.BASE64_ENC_CHECK_PARAM] = {};
                          this.checkDefIdToBase64CheckParams[checkDefId][this.BASE64_ENC_CHECK_PARAM_TYPES.BASE64_ENC_CHECK_CI_PARAM] = {};
                      }
                      this.checkDefIdToBase64CheckParams[checkDefId][flagName][paramName] = true; // add param to the relevant set of current check def
                  } else
                      gs.warn(gs.getMessage("{0}: {1} flag (sys_id = {2}) refers to a non existent check def sys_id ({3})", [Base64CheckParamsUtil.prototype.type, flagName, hashGr.getUniqueValue(), checkDefId]));
              } else
                  gs.warn(gs.getMessage("{0}: {1} flag (sys_id = {2}) doesn't contain a ':' character which is not in the beginning or at the end of the hash value", [Base64CheckParamsUtil.prototype.type, flagName, hashGr.getUniqueValue()]));
          } else
              gs.warn(gs.getMessage("{0}: {1} flag (sys_id = {2}) has a nil/empty hash value", [Base64CheckParamsUtil.prototype.type, flagName, hashGr.getUniqueValue()]));
      }
  },

  // given a checkId, a parameter type and a parameter name, return if this parameter should be passed base64 encoded or not
  shouldBase64EncodeCheckParam: function(checkId, paramType, paramName, skipCheckDefCheck) {
      var checkDefId = checkId;
      if (!skipCheckDefCheck)
          checkDefId = this.resolveCheckDefId(checkId);
      if (this.checkDefIdToBase64CheckParams.hasOwnProperty(checkDefId))
          if (this.checkDefIdToBase64CheckParams[checkDefId].hasOwnProperty(paramType))
              return this.checkDefIdToBase64CheckParams[checkDefId][paramType].hasOwnProperty(paramName);
      return false;
  },

  // given a check definition sys idand a parameter name, return if this parameter should be passed base64 encoded or not
  shouldEncodeCheckParam: function(checkDefId, paramName) {
      return this.shouldBase64EncodeCheckParam(checkDefId, this.BASE64_ENC_CHECK_PARAM_TYPES.BASE64_ENC_CHECK_CI_PARAM, paramName, true);
  },

  // resolve the check def id of the given check id. If the given id is an id of a check instance, then
  // the id of the check def it is derrived from will be returned. Otherwise, it is assumed that the given id is
  // an id of a check definition and it is returned
  resolveCheckDefId: function(checkId) {
      var checkInstGr = new GlideRecord("sn_agent_check");
      return checkInstGr.get(checkId) ? checkInstGr.getValue("check_def") : checkId;
  },

  type: 'Base64CheckParamsUtil'
};

Sys ID

4dcf09c077cd011098d6c9822b5a99c8

Offical Documentation

Official Docs: