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 | 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 1× 1× 1× 1× 1× 2× 464× 464× 464× 464× 464× 462× 462× 2× 2× 2× 1× 1× 1× 2× 114× 114× 114× 114× | 'use strict'; const _ = require('lodash'); const child_process = require('child_process'); const Q = require('q'); const config = require(__dirname + '/../config'); const logger = require(__dirname + '/../loggers/logger'); const redisHelper = require(__dirname + '/../utils/redis-helper'); //export namespace as `sslbumpPolicyEvaluation` const sslbumpPolicyEvaluation = exports; let ip2num = function (ip) { let d = ip.split('.'); return ((((((+d[0])*256)+(+d[1]))*256)+(+d[2]))*256)+(+d[3]); }; let getRuleId = function (rule) { let str = rule.uuid + ':' + rule.type + ':' + rule.value; if (rule.end_value) { str += ':' + rule.end_value; } return str; }; let getNodeInUceData = function (uceData, domain, nodeName) { if (!uceData || !uceData[domain]) { logger.info('msg="sslbump_error:Invalid uce response"'); return null; } let node = uceData[domain][nodeName]; if (node) { return node; } let msg = 'sslbump_error:Invalid uce response. ' + nodeName + ' not found'; let data = JSON.stringify(uceData[domain]); logger.info('msg="' + msg + '" uce="' + data + '"'); return null; }; let getClientIp = function (analyticsData) { if (!analyticsData || !analyticsData['x-client-ip']) { logger.info('msg="sslbump_category_rule:No client ip"'); return null; } return analyticsData['x-client-ip']; }; let endValueExists = function (rule) { if (rule.end_value) { return true; } logger.warn('msg=sslbump_error:Invalid sslbump_bypass rule" ' + 'rule="' + JSON.stringify(rule) + '"'); return false; }; let ipInRange = function (rangeStart, rangeEnd, ipToTest) { let ip = ip2num(ipToTest); return ((rangeStart <= ip) && (ip <= rangeEnd)); }; let sslbumpCategoryRuleEvaluator = function ( rule, domain, analyticsData, uceJsonResponse) { let categories = getNodeInUceData(uceJsonResponse, domain, 'cats'); if (!categories) { return false; } let catNames = _.pluck(categories, 'name'); return _.contains(catNames, rule.value); }; let sslbumpSourceIpRuleEvaluator = function ( rule, domain, analyticsData, uceJsonResponse) { // jshint ignore:line let clientIp = getClientIp(analyticsData); if (!clientIp) { return false; } return (rule.value === clientIp); }; let sslbumpSourceIpRangeRuleEvaluator = function ( rule, domain, analyticsData, uceJsonResponse) { // jshint ignore:line let clientIp = getClientIp(analyticsData); if (!clientIp || !endValueExists(rule)) { return false; } return ipInRange(rule.rangeStart, rule.rangeEnd, clientIp); }; let sslbumpDestinationIpRuleEvaluator = function ( rule, domain, analyticsData, uceJsonResponse) { let ips = getNodeInUceData(uceJsonResponse, domain, 'ips'); if (!ips) { return false; } return _.contains(ips, rule.value); }; let sslbumpDestinationIpRangeRuleEvaluator = function ( rule, domain, analyticsData, uceJsonResponse) { let ips = getNodeInUceData(uceJsonResponse, domain, 'ips'); if (!ips || !endValueExists(rule)) { return false; } let isMatch = false; _.forEach(ips, function (ip) { if (ipInRange(rule.rangeStart, rule.rangeEnd, ip)) { isMatch = true; return false; //Stop iteration } }); return isMatch; }; let sslbumpDestinationDomainRuleEvaluator = function ( rule, domain, analyticsData, uceJsonResponse) { // jshint ignore:line let ruleParts = rule.value.split('.').reverse(); let domainParts = domain.split('.').reverse(); domainParts = _.first(domainParts, ruleParts.length); return _.isEqual(ruleParts, domainParts); }; let sslbumpRuleEvaluators = { 'category' : sslbumpCategoryRuleEvaluator, 'source_ip' : sslbumpSourceIpRuleEvaluator, 'source_ip_range' : sslbumpSourceIpRangeRuleEvaluator, 'dest_ip' : sslbumpDestinationIpRuleEvaluator, 'dest_ip_range' : sslbumpDestinationIpRangeRuleEvaluator, 'dest_domain' : sslbumpDestinationDomainRuleEvaluator }; let squidLastUpdatedKey = 'pe_app:squidLastUpdatedAt'; let squidLastUpdatedKeyLock = 'pe_app:squidLastUpdatedAtLock'; let reloadSquidConfig = function () { return Q.when(redisHelper.getDataFromRedis(squidLastUpdatedKey)) .then(function (lastUpdated) { Iif (lastUpdated && (_.now() - lastUpdated < 60 * 1000)) { // There are multiple instances of pnr-enforcement module running // on a single machine. Only 1 instance needs to flush the squid // acl cache. Other instances can skip flushing the acl cache. logger.info('msg="sslbump_info:squid acl cache was flushed ' + 'less a minute ago. Do not flush squid acl cache again."'); return; } logger.info('msg="sslbump_info:flushing squid acl cache"'); // if you use exec = require('child_process').exec, // then exec cannot be mocked; the tests rewire the functions after // the module has been loaded, so the functions reference real exec child_process.exec(config.pnr_enforcement.squid_reload_config_script); return Q.when(redisHelper.setDataInRedis( squidLastUpdatedKey, _.now())); }); }; /** * @function compilePolicy * @desc Optimizes policy for faster evaluation. * @param {object} policy * @param {object} old policy */ sslbumpPolicyEvaluation.compilePolicy = function (policy, oldPolicy) { var rules = policy.sslbump_bypass; Eif (rules) { _.forEach(rules, function (rule) { if (_.contains(['source_ip_range', 'dest_ip_range'], rule.type)) { rule.rangeStart = ip2num(rule.value); rule.rangeEnd = ip2num(rule.end_value); } }); } Iif (policy.tid !== -1) { // Currently, we are applying sslbump policy only to tenant id = -1. return; } if (oldPolicy && _.isEqual(rules, oldPolicy.sslbump_bypass)) { logger.info('msg="sslbump_info:sslbump policy has not changed. ' + 'Do not flush squid acl cache"'); return; } let ttl = 5; //5 sec return Q.when(redisHelper.acquireLock(squidLastUpdatedKeyLock, ttl)) .then(function (lockAcquired) { if (!lockAcquired) { logger.info('msg="sslbump_info:lock could not be acquired.'); return; } return reloadSquidConfig(); }) .fail(function (error) { logger.warn('msg="sslbump_error:Unable to get data from redis" ' + 'stack="' + error.stack + '"'); }); }; /** * @function getPolicyAction * @desc Return policy based decision on whether a ssl * domain needs to bumped by squid or not. * @param {string} domain * @param {object} analyticsData * @param {object} uceResponseData * @param {object} userInfo * @param {object} rules * @param {object} result */ sslbumpPolicyEvaluation.getPolicyAction = function ( domain, analyticsData, uceJsonResponse, userInfo, rules, result) { let tid = userInfo.tenantId; _.forEach(rules, function (rule) { if (!rule || !rule.type || !rule.value || !rule.action || !rule.uuid) { logger.warn('msg=sslbump_error:Invalid sslbump_bypass rule" ' + 'tid=' + tid); return true; //continue iteration } let fn = sslbumpRuleEvaluators[rule.type]; if (!fn) { logger.warn('msg=sslbump_error:Invalid sslbump_bypass rule type" ' + ' tid=' + tid + ' type=' + rule.type); return true; //continue iteration } let isMatch = fn(rule, domain, analyticsData, uceJsonResponse); if (isMatch) { result.action = rule.action; result.reason = getRuleId(rule); analyticsData.pe_action = result.action; analyticsData.pe_reason = result.reason; return false; //Stop iteration } }); logger.info('pe_type=sslbump_bypass match=' + Boolean(result.reason) + ' tid=' + tid + ' client_ip=' + userInfo.clientIp + ' domain=' + domain + ' action=' + result.action + ' reason="' + result.reason + '"'); return result; }; |