blob: 0282f35ea3101f37c0c81899d9797f2f86177708 [file] [log] [blame]
Borawski.Lukasz86e1b662018-01-19 14:22:14 +01001/*
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
Ed Tanousc94ad492019-10-10 15:39:33 -070018#include <logging.h>
Tanousf00032d2018-11-05 01:18:10 -030019
Tanousf00032d2018-11-05 01:18:10 -030020#include <boost/beast/http/verb.hpp>
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010021#include <boost/container/flat_map.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050022
23#include <array>
24#include <bitset>
Ed Tanous1abe55e2018-09-05 08:30:59 -070025#include <cstdint>
26#include <vector>
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010027
Ed Tanous1abe55e2018-09-05 08:30:59 -070028namespace redfish
29{
30
31enum class PrivilegeType
32{
33 BASE,
34 OEM
35};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010036
Ed Tanousa6927792018-03-06 10:01:57 -080037/** @brief A fixed array of compile time privileges */
38constexpr std::array<const char*, 5> basePrivileges{
Ed Tanous3ebd75f2018-03-05 18:20:01 -080039 "Login", "ConfigureManager", "ConfigureComponents", "ConfigureSelf",
40 "ConfigureUsers"};
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010041
Ed Tanous271584a2019-07-09 16:24:22 -070042constexpr const size_t basePrivilegeCount = basePrivileges.size();
Ed Tanousa6927792018-03-06 10:01:57 -080043
44/** @brief Max number of privileges per type */
Ed Tanous271584a2019-07-09 16:24:22 -070045constexpr const size_t maxPrivilegeCount = 32;
Ed Tanousa6927792018-03-06 10:01:57 -080046
47/** @brief A vector of all privilege names and their indexes */
48static const std::vector<std::string> privilegeNames{basePrivileges.begin(),
49 basePrivileges.end()};
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010050
Borawski.Lukasz86e1b662018-01-19 14:22:14 +010051/**
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010052 * @brief Redfish privileges
53 *
Joseph Reynolds900f9492019-11-25 15:37:29 -060054 * This implements a set of Redfish privileges. These directly represent
55 * user privileges and help represent entity privileges.
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010056 *
Ed Tanous55c7b7a2018-05-22 15:27:24 -070057 * Each incoming Connection requires a comparison between privileges held
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010058 * by the user issuing a request and the target entity's privileges.
59 *
60 * To ensure best runtime performance of this comparison, privileges
61 * are represented as bitsets. Each bit in the bitset corresponds to a
62 * unique privilege name.
63 *
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010064 * A bit is set if the privilege is required (entity domain) or granted
65 * (user domain) and false otherwise.
66 *
Borawski.Lukasz86e1b662018-01-19 14:22:14 +010067 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070068class Privileges
69{
70 public:
71 /**
72 * @brief Constructs object without any privileges active
73 *
74 */
75 Privileges() = default;
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010076
Ed Tanous1abe55e2018-09-05 08:30:59 -070077 /**
78 * @brief Constructs object with given privileges active
79 *
80 * @param[in] privilegeList List of privileges to be activated
81 *
82 */
83 Privileges(std::initializer_list<const char*> privilegeList)
84 {
85 for (const char* privilege : privilegeList)
86 {
87 if (!setSinglePrivilege(privilege))
88 {
89 BMCWEB_LOG_CRITICAL << "Unable to set privilege " << privilege
90 << "in constructor";
91 }
92 }
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010093 }
94
Ed Tanous1abe55e2018-09-05 08:30:59 -070095 /**
96 * @brief Sets given privilege in the bitset
97 *
98 * @param[in] privilege Privilege to be set
99 *
100 * @return None
101 *
102 */
103 bool setSinglePrivilege(const char* privilege)
104 {
Ed Tanous271584a2019-07-09 16:24:22 -0700105 for (size_t searchIndex = 0; searchIndex < privilegeNames.size();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700106 searchIndex++)
107 {
108 if (privilege == privilegeNames[searchIndex])
109 {
110 privilegeBitset.set(searchIndex);
111 return true;
112 }
113 }
Ed Tanous3ebd75f2018-03-05 18:20:01 -0800114
Ed Tanous1abe55e2018-09-05 08:30:59 -0700115 return false;
Ed Tanousa6927792018-03-06 10:01:57 -0800116 }
117
Ed Tanous1abe55e2018-09-05 08:30:59 -0700118 /**
119 * @brief Sets given privilege in the bitset
120 *
121 * @param[in] privilege Privilege to be set
122 *
123 * @return None
124 *
125 */
126 bool setSinglePrivilege(const std::string& privilege)
127 {
128 return setSinglePrivilege(privilege.c_str());
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100129 }
Ed Tanousa6927792018-03-06 10:01:57 -0800130
Ed Tanous1abe55e2018-09-05 08:30:59 -0700131 /**
Joseph Reynolds900f9492019-11-25 15:37:29 -0600132 * @brief Resets the given privilege in the bitset
133 *
134 * @param[in] privilege Privilege to be reset
135 *
136 * @return None
137 *
138 */
139 bool resetSinglePrivilege(const char* privilege)
140 {
141 for (size_t searchIndex = 0; searchIndex < privilegeNames.size();
142 searchIndex++)
143 {
144 if (privilege == privilegeNames[searchIndex])
145 {
146 privilegeBitset.reset(searchIndex);
147 return true;
148 }
149 }
150 return false;
151 }
152
153 /**
Ed Tanous1abe55e2018-09-05 08:30:59 -0700154 * @brief Retrieves names of all active privileges for a given type
155 *
156 * @param[in] type Base or OEM
157 *
158 * @return Vector of active privileges. Pointers are valid until
159 * the setSinglePrivilege is called, or the Privilege structure is destroyed
160 *
161 */
162 std::vector<const std::string*>
163 getActivePrivilegeNames(const PrivilegeType type) const
164 {
165 std::vector<const std::string*> activePrivileges;
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100166
Ed Tanous271584a2019-07-09 16:24:22 -0700167 size_t searchIndex = 0;
168 size_t endIndex = basePrivilegeCount;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700169 if (type == PrivilegeType::OEM)
170 {
171 searchIndex = basePrivilegeCount - 1;
172 endIndex = privilegeNames.size();
173 }
Ed Tanous3ebd75f2018-03-05 18:20:01 -0800174
Ed Tanous1abe55e2018-09-05 08:30:59 -0700175 for (; searchIndex < endIndex; searchIndex++)
176 {
177 if (privilegeBitset.test(searchIndex))
178 {
179 activePrivileges.emplace_back(&privilegeNames[searchIndex]);
180 }
181 }
182
183 return activePrivileges;
184 }
185
186 /**
187 * @brief Determines if this Privilege set is a superset of the given
188 * privilege set
189 *
190 * @param[in] privilege Privilege to be checked
191 *
192 * @return None
193 *
194 */
195 bool isSupersetOf(const Privileges& p) const
196 {
197 return (privilegeBitset & p.privilegeBitset) == p.privilegeBitset;
198 }
199
Joseph Reynolds3bf4e632020-02-06 14:44:32 -0600200 /**
201 * @brief Returns the intersection of two Privilege sets.
202 *
203 * @param[in] privilege Privilege set to intersect with.
204 *
205 * @return The new Privilege set.
206 *
207 */
208 Privileges intersection(const Privileges& p) const
209 {
210 return Privileges{privilegeBitset & p.privilegeBitset};
211 }
212
Ed Tanous1abe55e2018-09-05 08:30:59 -0700213 private:
Joseph Reynolds3bf4e632020-02-06 14:44:32 -0600214 Privileges(const std::bitset<maxPrivilegeCount>& p) : privilegeBitset{p}
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500215 {}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700216 std::bitset<maxPrivilegeCount> privilegeBitset = 0;
Borawski.Lukasz86e1b662018-01-19 14:22:14 +0100217};
218
Ratan Gupta6f359562019-04-03 10:39:08 +0530219inline const Privileges& getUserPrivileges(const std::string& userRole)
220{
221 // Redfish privilege : Administrator
222 if (userRole == "priv-admin")
223 {
224 static Privileges admin{"Login", "ConfigureManager", "ConfigureSelf",
225 "ConfigureUsers", "ConfigureComponents"};
226 return admin;
227 }
228 else if (userRole == "priv-operator")
229 {
230 // Redfish privilege : Operator
231 static Privileges op{"Login", "ConfigureSelf", "ConfigureComponents"};
232 return op;
233 }
jayaprakash Mutyalad7e08022019-12-05 23:29:13 +0000234 else if (userRole == "priv-user")
Ratan Gupta6f359562019-04-03 10:39:08 +0530235 {
236 // Redfish privilege : Readonly
237 static Privileges readOnly{"Login", "ConfigureSelf"};
238 return readOnly;
239 }
jayaprakash Mutyalad7e08022019-12-05 23:29:13 +0000240 else
241 {
242 // Redfish privilege : NoAccess
243 static Privileges noaccess;
244 return noaccess;
245 }
Ratan Gupta6f359562019-04-03 10:39:08 +0530246}
247
Joseph Reynolds900f9492019-11-25 15:37:29 -0600248/**
249 * @brief The OperationMap represents the privileges required for a
250 * single entity (URI). It maps from the allowable verbs to the
251 * privileges required to use that operation.
252 *
253 * This represents the Redfish "Privilege AND and OR syntax" as given
254 * in the spec and shown in the Privilege Registry. This does not
255 * implement any Redfish property overrides, subordinate overrides, or
256 * resource URI overrides. This does not implement the limitation of
257 * the ConfigureSelf privilege to operate only on your own account or
258 * session.
259 **/
Ed Tanouse0d918b2018-03-27 17:41:04 -0700260using OperationMap = boost::container::flat_map<boost::beast::http::verb,
261 std::vector<Privileges>>;
Borawski.Lukasz43a095a2018-02-19 15:39:01 +0100262
Joseph Reynolds900f9492019-11-25 15:37:29 -0600263/* @brief Checks if user is allowed to call an operation
264 *
265 * @param[in] operationPrivilegesRequired Privileges required
266 * @param[in] userPrivileges Privileges the user has
267 *
268 * @return True if operation is allowed, false otherwise
269 */
270inline bool isOperationAllowedWithPrivileges(
271 const std::vector<Privileges>& operationPrivilegesRequired,
272 const Privileges& userPrivileges)
273{
274 // If there are no privileges assigned, there are no privileges required
275 if (operationPrivilegesRequired.empty())
276 {
277 return true;
278 }
279 for (auto& requiredPrivileges : operationPrivilegesRequired)
280 {
281 BMCWEB_LOG_ERROR << "Checking operation privileges...";
282 if (userPrivileges.isSupersetOf(requiredPrivileges))
283 {
284 BMCWEB_LOG_ERROR << "...success";
285 return true;
286 }
287 }
288 return false;
289}
290
Borawski.Lukasz43a095a2018-02-19 15:39:01 +0100291/**
Ed Tanous3ebd75f2018-03-05 18:20:01 -0800292 * @brief Checks if given privileges allow to call an HTTP method
293 *
294 * @param[in] method HTTP method
295 * @param[in] user Privileges
296 *
297 * @return True if method allowed, false otherwise
Borawski.Lukasz43a095a2018-02-19 15:39:01 +0100298 *
299 */
Ed Tanouse0d918b2018-03-27 17:41:04 -0700300inline bool isMethodAllowedWithPrivileges(const boost::beast::http::verb method,
Ed Tanous3ebd75f2018-03-05 18:20:01 -0800301 const OperationMap& operationMap,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700302 const Privileges& userPrivileges)
303{
304 const auto& it = operationMap.find(method);
305 if (it == operationMap.end())
306 {
307 return false;
Ed Tanous3ebd75f2018-03-05 18:20:01 -0800308 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700309
Joseph Reynolds900f9492019-11-25 15:37:29 -0600310 return isOperationAllowedWithPrivileges(it->second, userPrivileges);
Ed Tanous3ebd75f2018-03-05 18:20:01 -0800311}
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100312
Ed Tanous3ebd75f2018-03-05 18:20:01 -0800313/**
314 * @brief Checks if a user is allowed to call an HTTP method
315 *
316 * @param[in] method HTTP method
317 * @param[in] user Username
318 *
319 * @return True if method allowed, false otherwise
320 *
321 */
Ed Tanouse0d918b2018-03-27 17:41:04 -0700322inline bool isMethodAllowedForUser(const boost::beast::http::verb method,
Ed Tanous3ebd75f2018-03-05 18:20:01 -0800323 const OperationMap& operationMap,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700324 const std::string& user)
325{
326 // TODO: load user privileges from configuration as soon as its available
327 // now we are granting all privileges to everyone.
328 Privileges userPrivileges{"Login", "ConfigureManager", "ConfigureSelf",
329 "ConfigureUsers", "ConfigureComponents"};
Ed Tanous3ebd75f2018-03-05 18:20:01 -0800330
Ed Tanous1abe55e2018-09-05 08:30:59 -0700331 return isMethodAllowedWithPrivileges(method, operationMap, userPrivileges);
Ed Tanous3ebd75f2018-03-05 18:20:01 -0800332}
Borawski.Lukasz86e1b662018-01-19 14:22:14 +0100333
Ed Tanous1abe55e2018-09-05 08:30:59 -0700334} // namespace redfish