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 | |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 18 | #include <bitset> |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 19 | #include <boost/container/flat_map.hpp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 20 | #include <cstdint> |
| 21 | #include <vector> |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 22 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 23 | #include "crow.h" |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 24 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 25 | namespace redfish |
| 26 | { |
| 27 | |
| 28 | enum class PrivilegeType |
| 29 | { |
| 30 | BASE, |
| 31 | OEM |
| 32 | }; |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 33 | |
Ed Tanous | a692779 | 2018-03-06 10:01:57 -0800 | [diff] [blame] | 34 | /** @brief A fixed array of compile time privileges */ |
| 35 | constexpr std::array<const char*, 5> basePrivileges{ |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 36 | "Login", "ConfigureManager", "ConfigureComponents", "ConfigureSelf", |
| 37 | "ConfigureUsers"}; |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame] | 38 | |
Ed Tanous | a692779 | 2018-03-06 10:01:57 -0800 | [diff] [blame] | 39 | constexpr const int basePrivilegeCount = basePrivileges.size(); |
| 40 | |
| 41 | /** @brief Max number of privileges per type */ |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 42 | constexpr const int maxPrivilegeCount = 32; |
Ed Tanous | a692779 | 2018-03-06 10:01:57 -0800 | [diff] [blame] | 43 | |
| 44 | /** @brief A vector of all privilege names and their indexes */ |
| 45 | static const std::vector<std::string> privilegeNames{basePrivileges.begin(), |
| 46 | basePrivileges.end()}; |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame] | 47 | |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 48 | /** |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 49 | * @brief Redfish privileges |
| 50 | * |
| 51 | * Entity privileges and user privileges are represented by this class. |
| 52 | * |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 53 | * Each incoming Connection requires a comparison between privileges held |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 54 | * by the user issuing a request and the target entity's privileges. |
| 55 | * |
| 56 | * To ensure best runtime performance of this comparison, privileges |
| 57 | * are represented as bitsets. Each bit in the bitset corresponds to a |
| 58 | * unique privilege name. |
| 59 | * |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 60 | * A bit is set if the privilege is required (entity domain) or granted |
| 61 | * (user domain) and false otherwise. |
| 62 | * |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 63 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 64 | class Privileges |
| 65 | { |
| 66 | public: |
| 67 | /** |
| 68 | * @brief Constructs object without any privileges active |
| 69 | * |
| 70 | */ |
| 71 | Privileges() = default; |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame] | 72 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 73 | /** |
| 74 | * @brief Constructs object with given privileges active |
| 75 | * |
| 76 | * @param[in] privilegeList List of privileges to be activated |
| 77 | * |
| 78 | */ |
| 79 | Privileges(std::initializer_list<const char*> privilegeList) |
| 80 | { |
| 81 | for (const char* privilege : privilegeList) |
| 82 | { |
| 83 | if (!setSinglePrivilege(privilege)) |
| 84 | { |
| 85 | BMCWEB_LOG_CRITICAL << "Unable to set privilege " << privilege |
| 86 | << "in constructor"; |
| 87 | } |
| 88 | } |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 91 | /** |
| 92 | * @brief Sets given privilege in the bitset |
| 93 | * |
| 94 | * @param[in] privilege Privilege to be set |
| 95 | * |
| 96 | * @return None |
| 97 | * |
| 98 | */ |
| 99 | bool setSinglePrivilege(const char* privilege) |
| 100 | { |
| 101 | for (int searchIndex = 0; searchIndex < privilegeNames.size(); |
| 102 | searchIndex++) |
| 103 | { |
| 104 | if (privilege == privilegeNames[searchIndex]) |
| 105 | { |
| 106 | privilegeBitset.set(searchIndex); |
| 107 | return true; |
| 108 | } |
| 109 | } |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 110 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 111 | return false; |
Ed Tanous | a692779 | 2018-03-06 10:01:57 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 114 | /** |
| 115 | * @brief Sets given privilege in the bitset |
| 116 | * |
| 117 | * @param[in] privilege Privilege to be set |
| 118 | * |
| 119 | * @return None |
| 120 | * |
| 121 | */ |
| 122 | bool setSinglePrivilege(const std::string& privilege) |
| 123 | { |
| 124 | return setSinglePrivilege(privilege.c_str()); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 125 | } |
Ed Tanous | a692779 | 2018-03-06 10:01:57 -0800 | [diff] [blame] | 126 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 127 | /** |
| 128 | * @brief Retrieves names of all active privileges for a given type |
| 129 | * |
| 130 | * @param[in] type Base or OEM |
| 131 | * |
| 132 | * @return Vector of active privileges. Pointers are valid until |
| 133 | * the setSinglePrivilege is called, or the Privilege structure is destroyed |
| 134 | * |
| 135 | */ |
| 136 | std::vector<const std::string*> |
| 137 | getActivePrivilegeNames(const PrivilegeType type) const |
| 138 | { |
| 139 | std::vector<const std::string*> activePrivileges; |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 140 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 141 | int searchIndex = 0; |
| 142 | int endIndex = basePrivilegeCount; |
| 143 | if (type == PrivilegeType::OEM) |
| 144 | { |
| 145 | searchIndex = basePrivilegeCount - 1; |
| 146 | endIndex = privilegeNames.size(); |
| 147 | } |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 148 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 149 | for (; searchIndex < endIndex; searchIndex++) |
| 150 | { |
| 151 | if (privilegeBitset.test(searchIndex)) |
| 152 | { |
| 153 | activePrivileges.emplace_back(&privilegeNames[searchIndex]); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | return activePrivileges; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * @brief Determines if this Privilege set is a superset of the given |
| 162 | * privilege set |
| 163 | * |
| 164 | * @param[in] privilege Privilege to be checked |
| 165 | * |
| 166 | * @return None |
| 167 | * |
| 168 | */ |
| 169 | bool isSupersetOf(const Privileges& p) const |
| 170 | { |
| 171 | return (privilegeBitset & p.privilegeBitset) == p.privilegeBitset; |
| 172 | } |
| 173 | |
| 174 | private: |
| 175 | std::bitset<maxPrivilegeCount> privilegeBitset = 0; |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 176 | }; |
| 177 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 178 | using OperationMap = boost::container::flat_map<boost::beast::http::verb, |
| 179 | std::vector<Privileges>>; |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame] | 180 | |
| 181 | /** |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 182 | * @brief Checks if given privileges allow to call an HTTP method |
| 183 | * |
| 184 | * @param[in] method HTTP method |
| 185 | * @param[in] user Privileges |
| 186 | * |
| 187 | * @return True if method allowed, false otherwise |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame] | 188 | * |
| 189 | */ |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 190 | inline bool isMethodAllowedWithPrivileges(const boost::beast::http::verb method, |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 191 | const OperationMap& operationMap, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 192 | const Privileges& userPrivileges) |
| 193 | { |
| 194 | const auto& it = operationMap.find(method); |
| 195 | if (it == operationMap.end()) |
| 196 | { |
| 197 | return false; |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 198 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 199 | |
| 200 | // If there are no privileges assigned, assume no privileges required |
| 201 | if (it->second.empty()) |
| 202 | { |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | for (auto& requiredPrivileges : it->second) |
| 207 | { |
| 208 | if (userPrivileges.isSupersetOf(requiredPrivileges)) |
| 209 | { |
| 210 | return true; |
| 211 | } |
| 212 | } |
| 213 | return false; |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 214 | } |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 215 | |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 216 | /** |
| 217 | * @brief Checks if a user is allowed to call an HTTP method |
| 218 | * |
| 219 | * @param[in] method HTTP method |
| 220 | * @param[in] user Username |
| 221 | * |
| 222 | * @return True if method allowed, false otherwise |
| 223 | * |
| 224 | */ |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 225 | inline bool isMethodAllowedForUser(const boost::beast::http::verb method, |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 226 | const OperationMap& operationMap, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 227 | const std::string& user) |
| 228 | { |
| 229 | // TODO: load user privileges from configuration as soon as its available |
| 230 | // now we are granting all privileges to everyone. |
| 231 | Privileges userPrivileges{"Login", "ConfigureManager", "ConfigureSelf", |
| 232 | "ConfigureUsers", "ConfigureComponents"}; |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 233 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 234 | return isMethodAllowedWithPrivileges(method, operationMap, userPrivileges); |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 235 | } |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 236 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 237 | } // namespace redfish |