|
| 1 | +/* |
| 2 | + * Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U |
| 3 | + * |
| 4 | + * This file is part of fiware-pep-steelskin |
| 5 | + * |
| 6 | + * fiware-pep-steelskin is free software: you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Affero General Public License as |
| 8 | + * published by the Free Software Foundation, either version 3 of the License, |
| 9 | + * or (at your option) any later version. |
| 10 | + * |
| 11 | + * fiware-pep-steelskin is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 | + * See the GNU Affero General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public |
| 17 | + * License along with fiware-pep-steelskin. |
| 18 | + * If not, seehttp://www.gnu.org/licenses/. |
| 19 | + * |
| 20 | + * For those usages not covered by the GNU Affero General Public License |
| 21 | + * please contact with::[iot_support@tid.es] |
| 22 | + */ |
| 23 | + |
| 24 | +'use strict'; |
| 25 | + |
| 26 | +var cacheUtils = require('./cacheUtils'); |
| 27 | + |
| 28 | +/** |
| 29 | + * Allowed actions by component and role type |
| 30 | + */ |
| 31 | +const ACTION_MAP = { |
| 32 | + ORION: { |
| 33 | + CUSTOMER: ['read', 'subscribe', 'discover', 'subscribe-availability'], |
| 34 | + ADMIN: ['read', 'create', 'update', 'delete', 'notify', 'register', |
| 35 | + 'discover', 'subscribe', 'subscribe-availability'] |
| 36 | + }, |
| 37 | + PERSEO: { |
| 38 | + CUSTOMER: ['readRule'], |
| 39 | + ADMIN: ['readRule', 'writeRule', 'notify'] |
| 40 | + }, |
| 41 | + IOTAGENT: { |
| 42 | + CUSTOMER: ['read'], |
| 43 | + ADMIN: ['create', 'delete', 'update', 'read'] |
| 44 | + }, |
| 45 | + STH: { |
| 46 | + CUSTOMER: ['read'], |
| 47 | + ADMIN: ['create', 'delete', 'update', 'read'] |
| 48 | + } |
| 49 | +}; |
| 50 | + |
| 51 | + |
| 52 | +/** |
| 53 | + * Helper para cacheo y respuesta |
| 54 | + */ |
| 55 | +function cacheAndReturn(cacheKey, decision, callback) { |
| 56 | + const finalDecision = decision || 'Invalid'; |
| 57 | + cacheUtils.get().data.validation.set(cacheKey, finalDecision); |
| 58 | + callback(null, finalDecision); |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +/** |
| 63 | + * validation request using local PDP implementation |
| 64 | + */ |
| 65 | +function validationRequest(logger, roles, frn, action, headers, callback) { |
| 66 | + |
| 67 | + logger.debug('pdp.validation with roles: %j, frn: %j, action: %j and headers %j', |
| 68 | + roles, frn, action, headers); |
| 69 | + const cacheKey = frn + '#' + action + '#' + roles.map(r => r.name).join('-'); |
| 70 | + |
| 71 | + try { |
| 72 | + // 1. parse FRN -> component, service and subservice |
| 73 | + // Format example: fiware:orion:smartcity:/tourism::: |
| 74 | + const frnParts = frn.split(':'); |
| 75 | + |
| 76 | + if (frnParts.length < 4) { |
| 77 | + return callback(new Error('Invalid FRN format')); |
| 78 | + } |
| 79 | + |
| 80 | + const component = frnParts[1].toUpperCase(); // ORION, PERSEO, IOTA, STH |
| 81 | + const subserviceRaw = frnParts[3]; // "/tourism", "///", etc |
| 82 | + const subservice = subserviceRaw.replace(/\//g, '') || null; // "tourism" o null |
| 83 | + |
| 84 | + const isServiceOperation = subservice === null; |
| 85 | + const isSubserviceOperation = subservice !== null; |
| 86 | + |
| 87 | + // 2. For each role: get roleType and component |
| 88 | + let matchedRole = null; |
| 89 | + |
| 90 | + for (const role of roles) { |
| 91 | + const name = role.name || ''; |
| 92 | + const parts = name.split('#'); |
| 93 | + if (parts.length !== 2) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + const roleInfo = parts[1]; |
| 98 | + |
| 99 | + // Try extrat type and component (i.e.: ServiceCustomerORION) |
| 100 | + let match = roleInfo.match( |
| 101 | + /(ServiceCustomer|ServiceAdmin|SubServiceCustomer|SubServiceAdmin)([A-Z]+)$/i |
| 102 | + ); |
| 103 | + |
| 104 | + let roleType, roleComponent; |
| 105 | + |
| 106 | + // Case 1: rol includes component |
| 107 | + if (match) { |
| 108 | + roleType = match[1]; |
| 109 | + roleComponent = match[2].toUpperCase(); |
| 110 | + } |
| 111 | + // Caso 2: rol does NOT includes component -> apply over all components |
| 112 | + else { |
| 113 | + match = roleInfo.match( |
| 114 | + /^(ServiceCustomer|ServiceAdmin|SubServiceCustomer|SubServiceAdmin)$/i |
| 115 | + ); |
| 116 | + if (!match) { |
| 117 | + continue; |
| 118 | + } |
| 119 | + |
| 120 | + roleType = match[1]; |
| 121 | + roleComponent = 'ANY'; |
| 122 | + } |
| 123 | + |
| 124 | + // 1) Component matches (or ANY) |
| 125 | + if (roleComponent !== 'ANY' && roleComponent !== component) { |
| 126 | + continue; |
| 127 | + } |
| 128 | + |
| 129 | + // 2) Matches at service/subservice level |
| 130 | + const roleIsService = roleType.startsWith('Service'); |
| 131 | + const roleIsSubservice = roleType.startsWith('SubService'); |
| 132 | + |
| 133 | + if (isServiceOperation && !roleIsService) { |
| 134 | + continue; |
| 135 | + } |
| 136 | + if (isSubserviceOperation && !roleIsSubservice) { |
| 137 | + continue; |
| 138 | + } |
| 139 | + |
| 140 | + matchedRole = { roleType, roleComponent }; |
| 141 | + break; |
| 142 | + } // end for |
| 143 | + |
| 144 | + if (!matchedRole) { |
| 145 | + cacheAndReturn(cacheKey, 'Deny', callback); |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + // 3. Final decision |
| 150 | + const { roleType } = matchedRole; |
| 151 | + const ROLE_CLASS = roleType.endsWith('Customer') ? 'CUSTOMER' : 'ADMIN'; |
| 152 | + |
| 153 | + let allowedActions = []; |
| 154 | + |
| 155 | + if (ACTION_MAP[component] && ACTION_MAP[component][ROLE_CLASS]) { |
| 156 | + allowedActions = ACTION_MAP[component][ROLE_CLASS]; |
| 157 | + } |
| 158 | + |
| 159 | + const decision = allowedActions.includes(action) ? 'Permit' : 'Deny'; |
| 160 | + |
| 161 | + cacheAndReturn(cacheKey, decision, callback); |
| 162 | + |
| 163 | + } catch (err) { |
| 164 | + logger.error('Validation exception', err); |
| 165 | + callback(err); |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | + |
| 170 | +exports.validationRequest = validationRequest; |
0 commit comments