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