Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
| 17 | |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 18 | #include <crow/logging.h> |
| 19 | |
| 20 | #include <array> |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 21 | #include <bitset> |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 22 | #include <boost/beast/http/verb.hpp> |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 23 | #include <boost/container/flat_map.hpp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 24 | #include <cstdint> |
| 25 | #include <vector> |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 26 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 27 | namespace redfish |
| 28 | { |
| 29 | |
| 30 | enum class PrivilegeType |
| 31 | { |
| 32 | BASE, |
| 33 | OEM |
| 34 | }; |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 35 | |
Ed Tanous | a692779 | 2018-03-06 10:01:57 -0800 | [diff] [blame] | 36 | /** @brief A fixed array of compile time privileges */ |
| 37 | constexpr std::array<const char*, 5> basePrivileges{ |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 38 | "Login", "ConfigureManager", "ConfigureComponents", "ConfigureSelf", |
| 39 | "ConfigureUsers"}; |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame] | 40 | |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 41 | constexpr const int basePrivilegeCount = basePrivileges.size(); |
Ed Tanous | a692779 | 2018-03-06 10:01:57 -0800 | [diff] [blame] | 42 | |
| 43 | /** @brief Max number of privileges per type */ |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 44 | constexpr const int maxPrivilegeCount = 32; |
Ed Tanous | a692779 | 2018-03-06 10:01:57 -0800 | [diff] [blame] | 45 | |
| 46 | /** @brief A vector of all privilege names and their indexes */ |
| 47 | static const std::vector<std::string> privilegeNames{basePrivileges.begin(), |
| 48 | basePrivileges.end()}; |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame] | 49 | |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 50 | /** |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 51 | * @brief Redfish privileges |
| 52 | * |
| 53 | * Entity privileges and user privileges are represented by this class. |
| 54 | * |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 55 | * Each incoming Connection requires a comparison between privileges held |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 56 | * by the user issuing a request and the target entity's privileges. |
| 57 | * |
| 58 | * To ensure best runtime performance of this comparison, privileges |
| 59 | * are represented as bitsets. Each bit in the bitset corresponds to a |
| 60 | * unique privilege name. |
| 61 | * |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 62 | * A bit is set if the privilege is required (entity domain) or granted |
| 63 | * (user domain) and false otherwise. |
| 64 | * |
Joseph Reynolds | 8fd315a | 2019-09-12 12:02:33 -0500 | [diff] [blame^] | 65 | * This does not implement any Redfish property overrides, |
| 66 | * subordinate overrides, or resource URI overrides. This does |
| 67 | * not implement the limitation of the ConfigureSelf privilege |
| 68 | * to operate only on your own account or session. |
| 69 | * |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 70 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 71 | class Privileges |
| 72 | { |
| 73 | public: |
| 74 | /** |
| 75 | * @brief Constructs object without any privileges active |
| 76 | * |
| 77 | */ |
| 78 | Privileges() = default; |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame] | 79 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 80 | /** |
| 81 | * @brief Constructs object with given privileges active |
| 82 | * |
| 83 | * @param[in] privilegeList List of privileges to be activated |
| 84 | * |
| 85 | */ |
| 86 | Privileges(std::initializer_list<const char*> privilegeList) |
| 87 | { |
| 88 | for (const char* privilege : privilegeList) |
| 89 | { |
| 90 | if (!setSinglePrivilege(privilege)) |
| 91 | { |
| 92 | BMCWEB_LOG_CRITICAL << "Unable to set privilege " << privilege |
| 93 | << "in constructor"; |
| 94 | } |
| 95 | } |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 98 | /** |
Joseph Reynolds | 8fd315a | 2019-09-12 12:02:33 -0500 | [diff] [blame^] | 99 | * @brief Resets the given privilege in the bitset |
| 100 | * |
| 101 | * @param[in] privilege Privilege to be reset |
| 102 | * |
| 103 | * @return None |
| 104 | * |
| 105 | */ |
| 106 | bool resetSinglePrivilege(const char* privilege) |
| 107 | { |
| 108 | for (int searchIndex = 0; searchIndex < privilegeNames.size(); |
| 109 | searchIndex++) |
| 110 | { |
| 111 | if (privilege == privilegeNames[searchIndex]) |
| 112 | { |
| 113 | privilegeBitset.reset(searchIndex); |
| 114 | return true; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | /** |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 122 | * @brief Sets given privilege in the bitset |
| 123 | * |
| 124 | * @param[in] privilege Privilege to be set |
| 125 | * |
| 126 | * @return None |
| 127 | * |
| 128 | */ |
| 129 | bool setSinglePrivilege(const char* privilege) |
| 130 | { |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 131 | for (int searchIndex = 0; searchIndex < privilegeNames.size(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 132 | searchIndex++) |
| 133 | { |
| 134 | if (privilege == privilegeNames[searchIndex]) |
| 135 | { |
| 136 | privilegeBitset.set(searchIndex); |
| 137 | return true; |
| 138 | } |
| 139 | } |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 140 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 141 | return false; |
Ed Tanous | a692779 | 2018-03-06 10:01:57 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 144 | /** |
| 145 | * @brief Sets given privilege in the bitset |
| 146 | * |
| 147 | * @param[in] privilege Privilege to be set |
| 148 | * |
| 149 | * @return None |
| 150 | * |
| 151 | */ |
| 152 | bool setSinglePrivilege(const std::string& privilege) |
| 153 | { |
| 154 | return setSinglePrivilege(privilege.c_str()); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 155 | } |
Ed Tanous | a692779 | 2018-03-06 10:01:57 -0800 | [diff] [blame] | 156 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 157 | /** |
| 158 | * @brief Retrieves names of all active privileges for a given type |
| 159 | * |
| 160 | * @param[in] type Base or OEM |
| 161 | * |
| 162 | * @return Vector of active privileges. Pointers are valid until |
| 163 | * the setSinglePrivilege is called, or the Privilege structure is destroyed |
| 164 | * |
| 165 | */ |
| 166 | std::vector<const std::string*> |
| 167 | getActivePrivilegeNames(const PrivilegeType type) const |
| 168 | { |
| 169 | std::vector<const std::string*> activePrivileges; |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 170 | |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 171 | int searchIndex = 0; |
| 172 | int endIndex = basePrivilegeCount; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 173 | if (type == PrivilegeType::OEM) |
| 174 | { |
| 175 | searchIndex = basePrivilegeCount - 1; |
| 176 | endIndex = privilegeNames.size(); |
| 177 | } |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 178 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 179 | for (; searchIndex < endIndex; searchIndex++) |
| 180 | { |
| 181 | if (privilegeBitset.test(searchIndex)) |
| 182 | { |
| 183 | activePrivileges.emplace_back(&privilegeNames[searchIndex]); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | return activePrivileges; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * @brief Determines if this Privilege set is a superset of the given |
| 192 | * privilege set |
| 193 | * |
| 194 | * @param[in] privilege Privilege to be checked |
| 195 | * |
| 196 | * @return None |
| 197 | * |
| 198 | */ |
| 199 | bool isSupersetOf(const Privileges& p) const |
| 200 | { |
| 201 | return (privilegeBitset & p.privilegeBitset) == p.privilegeBitset; |
| 202 | } |
| 203 | |
Joseph Reynolds | 8fd315a | 2019-09-12 12:02:33 -0500 | [diff] [blame^] | 204 | std::string to_string() const |
| 205 | { |
| 206 | return privilegeBitset.to_string(); |
| 207 | } |
| 208 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 209 | private: |
| 210 | std::bitset<maxPrivilegeCount> privilegeBitset = 0; |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 211 | }; |
| 212 | |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 213 | inline const Privileges& getUserPrivileges(const std::string& userRole) |
| 214 | { |
| 215 | // Redfish privilege : Administrator |
| 216 | if (userRole == "priv-admin") |
| 217 | { |
| 218 | static Privileges admin{"Login", "ConfigureManager", "ConfigureSelf", |
| 219 | "ConfigureUsers", "ConfigureComponents"}; |
| 220 | return admin; |
| 221 | } |
| 222 | else if (userRole == "priv-operator") |
| 223 | { |
| 224 | // Redfish privilege : Operator |
| 225 | static Privileges op{"Login", "ConfigureSelf", "ConfigureComponents"}; |
| 226 | return op; |
| 227 | } |
Joseph Reynolds | 8fd315a | 2019-09-12 12:02:33 -0500 | [diff] [blame^] | 228 | else if (userRole == "special-priv-configure-self") |
| 229 | { |
| 230 | // Redfish privilege : N/A - internal within BMCWeb |
| 231 | static Privileges configSelf{"ConfigureSelf"}; |
| 232 | return configSelf; |
| 233 | } |
Ed Tanous | 88e097c | 2019-09-18 16:03:55 +0000 | [diff] [blame] | 234 | else |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 235 | { |
| 236 | // Redfish privilege : Readonly |
| 237 | static Privileges readOnly{"Login", "ConfigureSelf"}; |
| 238 | return readOnly; |
| 239 | } |
| 240 | } |
| 241 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 242 | using OperationMap = boost::container::flat_map<boost::beast::http::verb, |
| 243 | std::vector<Privileges>>; |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame] | 244 | |
Joseph Reynolds | 8fd315a | 2019-09-12 12:02:33 -0500 | [diff] [blame^] | 245 | /* @brief Checks if user is allowed to call an operation |
| 246 | * |
| 247 | * @param[in] operationPrivilegesRequired Privileges required |
| 248 | * @param[in] userPrivileges Privileges the user has |
| 249 | * |
| 250 | * @return True if operation is allowed, false otherwise |
| 251 | */ |
| 252 | inline bool isOperationAllowedWithPrivileges( |
| 253 | const std::vector<Privileges>& operationPrivilegesRequired, |
| 254 | const Privileges& userPrivileges) |
| 255 | { |
| 256 | // If there are no privileges assigned, there are no privileges required |
| 257 | if (operationPrivilegesRequired.empty()) |
| 258 | { |
| 259 | return true; |
| 260 | } |
| 261 | for (auto& requiredPrivileges : operationPrivilegesRequired) |
| 262 | { |
| 263 | BMCWEB_LOG_ERROR << "Checking operation privileges..."; |
| 264 | if (userPrivileges.isSupersetOf(requiredPrivileges)) |
| 265 | { |
| 266 | BMCWEB_LOG_ERROR << "...success"; |
| 267 | return true; |
| 268 | } |
| 269 | } |
| 270 | return false; |
| 271 | } |
| 272 | |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame] | 273 | /** |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 274 | * @brief Checks if given privileges allow to call an HTTP method |
| 275 | * |
| 276 | * @param[in] method HTTP method |
| 277 | * @param[in] user Privileges |
| 278 | * |
| 279 | * @return True if method allowed, false otherwise |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame] | 280 | * |
| 281 | */ |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 282 | inline bool isMethodAllowedWithPrivileges(const boost::beast::http::verb method, |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 283 | const OperationMap& operationMap, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 284 | const Privileges& userPrivileges) |
| 285 | { |
| 286 | const auto& it = operationMap.find(method); |
| 287 | if (it == operationMap.end()) |
| 288 | { |
| 289 | return false; |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 290 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 291 | |
Joseph Reynolds | 8fd315a | 2019-09-12 12:02:33 -0500 | [diff] [blame^] | 292 | return isOperationAllowedWithPrivileges(it->second, userPrivileges); |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 293 | } |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 294 | |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 295 | /** |
| 296 | * @brief Checks if a user is allowed to call an HTTP method |
| 297 | * |
| 298 | * @param[in] method HTTP method |
| 299 | * @param[in] user Username |
| 300 | * |
| 301 | * @return True if method allowed, false otherwise |
| 302 | * |
| 303 | */ |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 304 | inline bool isMethodAllowedForUser(const boost::beast::http::verb method, |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 305 | const OperationMap& operationMap, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 306 | const std::string& user) |
| 307 | { |
| 308 | // TODO: load user privileges from configuration as soon as its available |
| 309 | // now we are granting all privileges to everyone. |
| 310 | Privileges userPrivileges{"Login", "ConfigureManager", "ConfigureSelf", |
| 311 | "ConfigureUsers", "ConfigureComponents"}; |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 312 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 313 | return isMethodAllowedWithPrivileges(method, operationMap, userPrivileges); |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 314 | } |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 315 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 316 | } // namespace redfish |