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 | |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 18 | #include "logging.hpp" |
| 19 | |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 20 | #include <boost/beast/http/verb.hpp> |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 21 | #include <boost/container/flat_map.hpp> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 22 | #include <boost/container/vector.hpp> |
| 23 | #include <boost/move/algo/move.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 24 | |
| 25 | #include <array> |
| 26 | #include <bitset> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 27 | #include <cstddef> |
| 28 | #include <functional> |
| 29 | #include <initializer_list> |
| 30 | #include <string> |
| 31 | #include <string_view> |
| 32 | #include <utility> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 33 | #include <vector> |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 34 | |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 35 | // IWYU pragma: no_include <stddef.h> |
| 36 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 37 | namespace redfish |
| 38 | { |
| 39 | |
| 40 | enum class PrivilegeType |
| 41 | { |
| 42 | BASE, |
| 43 | OEM |
| 44 | }; |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 45 | |
Ed Tanous | a692779 | 2018-03-06 10:01:57 -0800 | [diff] [blame] | 46 | /** @brief A fixed array of compile time privileges */ |
| 47 | constexpr std::array<const char*, 5> basePrivileges{ |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 48 | "Login", "ConfigureManager", "ConfigureComponents", "ConfigureSelf", |
| 49 | "ConfigureUsers"}; |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame] | 50 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 51 | constexpr const size_t basePrivilegeCount = basePrivileges.size(); |
Ed Tanous | a692779 | 2018-03-06 10:01:57 -0800 | [diff] [blame] | 52 | |
| 53 | /** @brief Max number of privileges per type */ |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 54 | constexpr const size_t maxPrivilegeCount = 32; |
Ed Tanous | a692779 | 2018-03-06 10:01:57 -0800 | [diff] [blame] | 55 | |
| 56 | /** @brief A vector of all privilege names and their indexes */ |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 57 | static const std::array<std::string, maxPrivilegeCount> privilegeNames{ |
| 58 | "Login", "ConfigureManager", "ConfigureComponents", "ConfigureSelf", |
| 59 | "ConfigureUsers"}; |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame] | 60 | |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 61 | /** |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 62 | * @brief Redfish privileges |
| 63 | * |
Joseph Reynolds | 900f949 | 2019-11-25 15:37:29 -0600 | [diff] [blame] | 64 | * This implements a set of Redfish privileges. These directly represent |
| 65 | * user privileges and help represent entity privileges. |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 66 | * |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 67 | * Each incoming Connection requires a comparison between privileges held |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 68 | * by the user issuing a request and the target entity's privileges. |
| 69 | * |
| 70 | * To ensure best runtime performance of this comparison, privileges |
| 71 | * are represented as bitsets. Each bit in the bitset corresponds to a |
| 72 | * unique privilege name. |
| 73 | * |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 74 | * A bit is set if the privilege is required (entity domain) or granted |
| 75 | * (user domain) and false otherwise. |
| 76 | * |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 77 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 78 | class Privileges |
| 79 | { |
| 80 | public: |
| 81 | /** |
| 82 | * @brief Constructs object without any privileges active |
| 83 | * |
| 84 | */ |
| 85 | Privileges() = default; |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame] | 86 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 87 | /** |
| 88 | * @brief Constructs object with given privileges active |
| 89 | * |
| 90 | * @param[in] privilegeList List of privileges to be activated |
| 91 | * |
| 92 | */ |
| 93 | Privileges(std::initializer_list<const char*> privilegeList) |
| 94 | { |
| 95 | for (const char* privilege : privilegeList) |
| 96 | { |
| 97 | if (!setSinglePrivilege(privilege)) |
| 98 | { |
| 99 | BMCWEB_LOG_CRITICAL << "Unable to set privilege " << privilege |
| 100 | << "in constructor"; |
| 101 | } |
| 102 | } |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 105 | /** |
| 106 | * @brief Sets given privilege in the bitset |
| 107 | * |
| 108 | * @param[in] privilege Privilege to be set |
| 109 | * |
| 110 | * @return None |
| 111 | * |
| 112 | */ |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 113 | bool setSinglePrivilege(std::string_view privilege) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 114 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 115 | for (size_t searchIndex = 0; searchIndex < privilegeNames.size(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 116 | searchIndex++) |
| 117 | { |
| 118 | if (privilege == privilegeNames[searchIndex]) |
| 119 | { |
| 120 | privilegeBitset.set(searchIndex); |
| 121 | return true; |
| 122 | } |
| 123 | } |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 124 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 125 | return false; |
Ed Tanous | a692779 | 2018-03-06 10:01:57 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 128 | /** |
Joseph Reynolds | 900f949 | 2019-11-25 15:37:29 -0600 | [diff] [blame] | 129 | * @brief Resets the given privilege in the bitset |
| 130 | * |
| 131 | * @param[in] privilege Privilege to be reset |
| 132 | * |
| 133 | * @return None |
| 134 | * |
| 135 | */ |
| 136 | bool resetSinglePrivilege(const char* privilege) |
| 137 | { |
| 138 | for (size_t searchIndex = 0; searchIndex < privilegeNames.size(); |
| 139 | searchIndex++) |
| 140 | { |
| 141 | if (privilege == privilegeNames[searchIndex]) |
| 142 | { |
| 143 | privilegeBitset.reset(searchIndex); |
| 144 | return true; |
| 145 | } |
| 146 | } |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | /** |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 151 | * @brief Retrieves names of all active privileges for a given type |
| 152 | * |
| 153 | * @param[in] type Base or OEM |
| 154 | * |
| 155 | * @return Vector of active privileges. Pointers are valid until |
| 156 | * the setSinglePrivilege is called, or the Privilege structure is destroyed |
| 157 | * |
| 158 | */ |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 159 | std::vector<std::string> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 160 | getActivePrivilegeNames(const PrivilegeType type) const |
| 161 | { |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 162 | std::vector<std::string> activePrivileges; |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 163 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 164 | size_t searchIndex = 0; |
| 165 | size_t endIndex = basePrivilegeCount; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 166 | if (type == PrivilegeType::OEM) |
| 167 | { |
Joseph Reynolds | 8770446 | 2021-08-24 14:42:39 -0500 | [diff] [blame] | 168 | searchIndex = basePrivilegeCount; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 169 | endIndex = privilegeNames.size(); |
| 170 | } |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 171 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 172 | for (; searchIndex < endIndex; searchIndex++) |
| 173 | { |
| 174 | if (privilegeBitset.test(searchIndex)) |
| 175 | { |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 176 | activePrivileges.emplace_back(privilegeNames[searchIndex]); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | return activePrivileges; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * @brief Determines if this Privilege set is a superset of the given |
| 185 | * privilege set |
| 186 | * |
| 187 | * @param[in] privilege Privilege to be checked |
| 188 | * |
| 189 | * @return None |
| 190 | * |
| 191 | */ |
| 192 | bool isSupersetOf(const Privileges& p) const |
| 193 | { |
| 194 | return (privilegeBitset & p.privilegeBitset) == p.privilegeBitset; |
| 195 | } |
| 196 | |
Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 197 | /** |
| 198 | * @brief Returns the intersection of two Privilege sets. |
| 199 | * |
| 200 | * @param[in] privilege Privilege set to intersect with. |
| 201 | * |
| 202 | * @return The new Privilege set. |
| 203 | * |
| 204 | */ |
| 205 | Privileges intersection(const Privileges& p) const |
| 206 | { |
| 207 | return Privileges{privilegeBitset & p.privilegeBitset}; |
| 208 | } |
| 209 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 210 | private: |
Ed Tanous | 4e23a44 | 2022-06-06 09:57:26 -0700 | [diff] [blame] | 211 | explicit Privileges(const std::bitset<maxPrivilegeCount>& p) : |
| 212 | privilegeBitset{p} |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 213 | {} |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 214 | std::bitset<maxPrivilegeCount> privilegeBitset = 0; |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 215 | }; |
| 216 | |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 217 | inline const Privileges& getUserPrivileges(const std::string& userRole) |
| 218 | { |
| 219 | // Redfish privilege : Administrator |
| 220 | if (userRole == "priv-admin") |
| 221 | { |
| 222 | static Privileges admin{"Login", "ConfigureManager", "ConfigureSelf", |
| 223 | "ConfigureUsers", "ConfigureComponents"}; |
| 224 | return admin; |
| 225 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 226 | if (userRole == "priv-operator") |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 227 | { |
| 228 | // Redfish privilege : Operator |
| 229 | static Privileges op{"Login", "ConfigureSelf", "ConfigureComponents"}; |
| 230 | return op; |
| 231 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 232 | if (userRole == "priv-user") |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 233 | { |
| 234 | // Redfish privilege : Readonly |
| 235 | static Privileges readOnly{"Login", "ConfigureSelf"}; |
| 236 | return readOnly; |
| 237 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 238 | // Redfish privilege : NoAccess |
| 239 | static Privileges noaccess; |
| 240 | return noaccess; |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 241 | } |
| 242 | |
Joseph Reynolds | 900f949 | 2019-11-25 15:37:29 -0600 | [diff] [blame] | 243 | /** |
| 244 | * @brief The OperationMap represents the privileges required for a |
| 245 | * single entity (URI). It maps from the allowable verbs to the |
| 246 | * privileges required to use that operation. |
| 247 | * |
| 248 | * This represents the Redfish "Privilege AND and OR syntax" as given |
| 249 | * in the spec and shown in the Privilege Registry. This does not |
| 250 | * implement any Redfish property overrides, subordinate overrides, or |
| 251 | * resource URI overrides. This does not implement the limitation of |
| 252 | * the ConfigureSelf privilege to operate only on your own account or |
| 253 | * session. |
| 254 | **/ |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 255 | using OperationMap = boost::container::flat_map<boost::beast::http::verb, |
| 256 | std::vector<Privileges>>; |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame] | 257 | |
Joseph Reynolds | 900f949 | 2019-11-25 15:37:29 -0600 | [diff] [blame] | 258 | /* @brief Checks if user is allowed to call an operation |
| 259 | * |
| 260 | * @param[in] operationPrivilegesRequired Privileges required |
| 261 | * @param[in] userPrivileges Privileges the user has |
| 262 | * |
| 263 | * @return True if operation is allowed, false otherwise |
| 264 | */ |
| 265 | inline bool isOperationAllowedWithPrivileges( |
| 266 | const std::vector<Privileges>& operationPrivilegesRequired, |
| 267 | const Privileges& userPrivileges) |
| 268 | { |
| 269 | // If there are no privileges assigned, there are no privileges required |
| 270 | if (operationPrivilegesRequired.empty()) |
| 271 | { |
| 272 | return true; |
| 273 | } |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 274 | for (const auto& requiredPrivileges : operationPrivilegesRequired) |
Joseph Reynolds | 900f949 | 2019-11-25 15:37:29 -0600 | [diff] [blame] | 275 | { |
Andrew Geissler | 54fbf17 | 2021-11-11 15:59:30 -0600 | [diff] [blame] | 276 | BMCWEB_LOG_DEBUG << "Checking operation privileges..."; |
Joseph Reynolds | 900f949 | 2019-11-25 15:37:29 -0600 | [diff] [blame] | 277 | if (userPrivileges.isSupersetOf(requiredPrivileges)) |
| 278 | { |
Andrew Geissler | 54fbf17 | 2021-11-11 15:59:30 -0600 | [diff] [blame] | 279 | BMCWEB_LOG_DEBUG << "...success"; |
Joseph Reynolds | 900f949 | 2019-11-25 15:37:29 -0600 | [diff] [blame] | 280 | return true; |
| 281 | } |
| 282 | } |
| 283 | return false; |
| 284 | } |
| 285 | |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame] | 286 | /** |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 287 | * @brief Checks if given privileges allow to call an HTTP method |
| 288 | * |
| 289 | * @param[in] method HTTP method |
| 290 | * @param[in] user Privileges |
| 291 | * |
| 292 | * @return True if method allowed, false otherwise |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame] | 293 | * |
| 294 | */ |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 295 | inline bool isMethodAllowedWithPrivileges(const boost::beast::http::verb method, |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 296 | const OperationMap& operationMap, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 297 | const Privileges& userPrivileges) |
| 298 | { |
| 299 | const auto& it = operationMap.find(method); |
| 300 | if (it == operationMap.end()) |
| 301 | { |
| 302 | return false; |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 303 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 304 | |
Joseph Reynolds | 900f949 | 2019-11-25 15:37:29 -0600 | [diff] [blame] | 305 | return isOperationAllowedWithPrivileges(it->second, userPrivileges); |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 306 | } |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 307 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 308 | } // namespace redfish |