Code coverage report for app/utils/validator.js

Statements: 96.19% (101 / 105)      Branches: 100% (49 / 49)      Functions: 95% (38 / 40)      Lines: 96.08% (98 / 102)      Ignored: none     

All files » app/utils/ » validator.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373    1                                     1     19 19       175 175       12 12       472 1   471 471               9 9 9 9 9 12   1   8 11 3     5                                 4                   20 20 20 20 20 22   3   17 19 8     9                 9 9 9 4     5                 11 11                 15 15       4 4       21 21                       510                 5 5                 14 14                                         12 12                 18 18                 3 3                 1 1       16               6   10                         56 98 196   98 98 8 8 7 14   7   95   56 51               5 5   20         8   8                   9 9                 26   26                 4 4                 4 4       4 4                 9 9                 7 7       21 21        
'use strict';
 
;require.register("utils/validator", function (exports, require, module) {
  /**
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
 
  module.exports = {
 
    isValidEmail: function isValidEmail(value) {
      var emailRegex = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i;
      return emailRegex.test(value);
    },
 
    isValidInt: function isValidInt(value) {
      var intRegex = /^-?\d+$/;
      return intRegex.test(value);
    },
 
    isValidUNIXUser: function isValidUNIXUser(value) {
      var regex = /^[a-z_][a-z0-9_-]{0,31}$/;
      return regex.test(value);
    },
 
    isValidFloat: function isValidFloat(value) {
      if (typeof value === 'string' && value.trim() === '') {
        return false;
      }
      var floatRegex = /^-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/;
      return floatRegex.test(value);
    },
    /**
     * validate directory with slash or drive at the start
     * @param value
     * @return {Boolean}
     */
    isValidDir: function isValidDir(value) {
      var floatRegex = /^\/[0-9a-z]*/;
      var winRegex = /^[a-z]:\\[0-9a-zA-Z]*/;
      var winUrlRegex = /^file:\/\/\/[a-zA-Z]:\/[0-9a-zA-Z]*/;
      var dirs = value.split(',');
      if (dirs.some(function (i) {
        return i.startsWith(' ');
      })) {
        return false;
      }
      for (var i = 0; i < dirs.length; i++) {
        if (!floatRegex.test(dirs[i]) && !winRegex.test(dirs[i]) && !winUrlRegex.test(dirs[i])) {
          return false;
        }
      }
      return true;
    },
 
    /**
     * validate filename
     */
    isValidFileName: function isValidFileName(value) {
      var filenameRegex = /^[0-9a-zA-Z_-]+\.[a-zA-Z]+$/;
      return filenameRegex.test(value);
    },
 
    /**
     * defines if config value looks like link to other config
     * @param value
     * @returns {boolean}
     */
    isConfigValueLink: function isConfigValueLink(value) {
      return (/^\${.+}$/.test(value)
      );
    },
 
    /**
     * validate directory with slash at the start
     * @param value
     * @returns {boolean}
     */
    isValidDataNodeDir: function isValidDataNodeDir(value) {
      var dirRegex = /^(\[[0-9a-zA-Z]+_?[0-9a-zA-Z]+\])?(file:\/\/)?(\/[0-9a-z]*)/;
      var winRegex = /^(\[[0-9a-zA-Z]+_?[0-9a-zA-Z]+\])?[a-zA-Z]:\\[0-9a-zA-Z]*/;
      var winUrlRegex = /^(\[[0-9a-zA-Z]+_?[0-9a-zA-Z]+\])?file:\/\/\/[a-zA-Z]:\/[0-9a-zA-Z]*/;
      var dirs = value.split(',');
      if (dirs.some(function (i) {
        return i.startsWith(' ');
      })) {
        return false;
      }
      for (var i = 0; i < dirs.length; i++) {
        if (!dirRegex.test(dirs[i]) && !winRegex.test(dirs[i]) && !winUrlRegex.test(dirs[i])) {
          return false;
        }
      }
      return true;
    },
 
    /**
     * validate directory doesn't start "home" or "homes"
     * @param value
     * @returns {boolean}
     */
    isAllowedDir: function isAllowedDir(value) {
      var dirs = value.replace(/,/g, ' ').trim().split(new RegExp("\\s+", "g"));
      for (var i = 0; i < dirs.length; i++) {
        if (dirs[i].startsWith('/home') || dirs[i].startsWith('/homes')) {
          return false;
        }
      }
      return true;
    },
 
    /**
     * validate ip address with port
     * @param value
     * @return {Boolean}
     */
    isIpAddress: function isIpAddress(value) {
      var ipRegex = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)($|\:[0-9]{1,5})$/;
      return ipRegex.test(value);
    },
 
    /**
     * validate hostname
     * @param value
     * @return {Boolean}
     */
    isHostname: function isHostname(value) {
      var regex = /(?=^.{3,254}$)(^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*(\.[a-zA-Z]{1,62})$)/;
      return value === 'localhost' || regex.test(value);
    },
 
    hasSpaces: function hasSpaces(value) {
      var regex = /(\s+)/;
      return regex.test(value);
    },
 
    isNotTrimmed: function isNotTrimmed(value) {
      var regex = /(^\s+|\s+$)/;
      return regex.test(value);
    },
 
    /**
     * Check if string ends with spaces.
     * For multiline content only last line will be checked.
     *
     * @method isNotTrimmedLeft
     * @param {String} value
     * @returns {Boolean} - <code>true</code> if ends with spaces
     */
    isNotTrimmedRight: function isNotTrimmedRight(value) {
      return value !== ' ' && /\s+$/.test(("" + value).split(/\n/).slice(-1)[0]);
    },
 
    /**
     * validate domain name with port
     * @param value
     * @return {Boolean}
     */
    isDomainName: function isDomainName(value) {
      var domainRegex = /^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$/;
      return domainRegex.test(value);
    },
 
    /**
     * validate username
     * @param value
     * @return {Boolean}
     */
    isValidUserName: function isValidUserName(value) {
      var usernameRegex = /^[a-z]([-a-z0-9]{0,30})$/;
      return usernameRegex.test(value);
    },
 
    /**
     * validate db name
     * @param value
     * @returns {boolean}
     */
    isValidDbName: function isValidDbName(value) {
      var dbPattern = /^\S+$/;
      return dbPattern.test(value);
    },
 
    /**
     * validate key of configurations
     * allow spaces as prefix and suffix
     *
     * @param value
     * @return {Boolean}
     */
    isValidConfigKey: function isValidConfigKey(value) {
      var configKeyRegex = /^\s*[0-9a-z_\-\.\/\*]+\s*$/i;
      return configKeyRegex.test(value);
    },
 
    /**
     * validate configuration group name
     * @param value
     * @return {Boolean}
     */
    isValidConfigGroupName: function isValidConfigGroupName(value) {
      var configKeyRegex = /^[\s0-9a-z_\-]+$/i;
      return configKeyRegex.test(value);
    },
 
    /**
     * validate alert notification name
     * @param value
     * @return {Boolean}
     */
    isValidAlertNotificationName: function isValidAlertNotificationName(value) {
      var configKeyRegex = /^[\s0-9a-z_\-]+$/i;
      return configKeyRegex.test(value);
    },
 
    /**
     * validate alert group name
     * @param value
     * @return {Boolean}
     */
    isValidAlertGroupName: function isValidAlertGroupName(value) {
      var configKeyRegex = /^[\s0-9a-z_\-]+$/i;
      return configKeyRegex.test(value);
    },
 
    empty: function empty(e) {
      switch (e) {
        case "":
        case 0:
        case "0":
        case null:
        case false:
        case undefined:
        case typeof this == "undefined":
          return true;
        default:
          return false;
      }
    },
    /**
     * Validate string that will pass as parameter to .matches() url param.
     * Try to prevent invalid regexp.
     * For example: /api/v1/clusters/c1/hosts?Hosts/host_name.matches(.*localhost.)
     *
     * @param {String} value - string to validate
     * @return {Boolean}
     * @method isValidMatchesRegexp
     */
    isValidMatchesRegexp: function isValidMatchesRegexp(value) {
      var checkPair = function checkPair(chars) {
        chars = chars.map(function (c) {
          return '\\' + c;
        });
        var charsReg = new RegExp(chars.join('|'), 'g');
        if (charsReg.test(value)) {
          var pairContentReg = new RegExp(chars.join('.*'), 'g');
          if (!pairContentReg.test(value)) return false;
          var pairCounts = chars.map(function (c) {
            return value.match(new RegExp(c, 'g')).length;
          });
          if (pairCounts[0] != pairCounts[1]) return false;
        }
        return true;
      };
      if (/^[\?\|\*\!,]/.test(value)) return false;
      return (/^((\.\*?)?([\w\s\[\]\/\?\-_,\|\*\!\{\}\(\)]*)?)+(\.\*?)?$/g.test(value) && checkPair(['[', ']']) && checkPair(['{', '}'])
      );
    },
 
    /**
    * Remove validation messages for components which are already installed
    */
    filterNotInstalledComponents: function filterNotInstalledComponents(validationData) {
      var hostComponents = App.HostComponent.find();
      return validationData.resources[0].items.filter(function (item) {
        // true is there is no host with this component
        return hostComponents.filterProperty("componentName", item["component-name"]).filterProperty("hostName", item.host).length === 0;
      });
    },
 
    isValidRackId: function isValidRackId(path) {
      var _path = '' + path;
      // See app/message.js:hostPopup.setRackId.invalid
      return (/^\/[/.\w-]+$/.test(_path) && _path.length < 255
      );
    },
 
    /**
     * Validate url
     * @param value
     * @return {Boolean}
     */
    isValidURL: function isValidURL(value) {
      var urlRegex = /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|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$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i;
      return urlRegex.test(value);
    },
 
    /**
     * Validate base URL
     * @param {string} value
     * @returns {boolean}
     */
    isValidBaseUrl: function isValidBaseUrl(value) {
      var remotePattern = /^$|^(?:(?:https?|ftp):\/{2})(?:\S+(?::\S*)?@)?(?:(?:(?:[\w\-.]))*)(?::[0-9]+)?(?:\/\S*)?$/,
          localPattern = /^$|^file:\/{2,3}([a-zA-Z][:|]\/){0,1}[\w~!*'();@&=\/\\\-+$,?%#.\[\]]+$/;
      return remotePattern.test(value) || localPattern.test(value);
    },
 
    /**
     * Validate widget name
     * @param {string} value
     * @returns {boolean}
     */
    isValidWidgetName: function isValidWidgetName(value) {
      var widgetNameRegex = /^[\s0-9a-z_\-%]+$/i;
      return widgetNameRegex.test(value);
    },
 
    /**
     * Validate widget description
     * @param {string} value
     * @returns {boolean}
     */
    isValidWidgetDescription: function isValidWidgetDescription(value) {
      var widgetDescriptionRegex = /^[\s0-9a-z_\-%]+$/i;
      return widgetDescriptionRegex.test(value);
    },
 
    isValidChartWidgetDatasetLabel: function isValidChartWidgetDatasetLabel(value) {
      var widgetDescriptionRegex = /^[\s0-9a-z_\-%]+$/i;
      return widgetDescriptionRegex.test(value);
    },
 
    /**
     * Validate alert name
     * @param {string} value
     * @returns {boolean}
     */
    isValidAlertName: function isValidAlertName(value) {
      var alertNameRegex = /^[\s0-9a-z_\-%\(\)]+$/i;
      return alertNameRegex.test(value);
    },
 
    /**
     * Validate ldaps URL
     * @param {string} value
     * @returns {boolean}
     */
    isValidLdapsURL: function isValidLdapsURL(value) {
      var ldapsUrlRegex = /^(ldaps):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i;
      return ldapsUrlRegex.test(value);
    },
 
    isValidNameServiceId: function isValidNameServiceId(value) {
      var nameSarviceIdRegex = /^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])$/;
      return nameSarviceIdRegex.test(value);
    }
 
  };
});