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 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 18 | #include "http_request.h" |
| 19 | #include "http_response.h" |
| 20 | |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 21 | #include "privileges.hpp" |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 22 | |
Ed Tanous | a0803ef | 2018-08-29 13:29:23 -0700 | [diff] [blame] | 23 | #include <error_messages.hpp> |
| 24 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 25 | #include <vector> |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 26 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 27 | namespace redfish |
| 28 | { |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 29 | |
| 30 | /** |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 31 | * AsyncResp |
| 32 | * Gathers data needed for response processing after async calls are done |
| 33 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 34 | class AsyncResp |
| 35 | { |
| 36 | public: |
| 37 | AsyncResp(crow::Response& response) : res(response) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 38 | {} |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 39 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 40 | ~AsyncResp() |
| 41 | { |
| 42 | res.end(); |
| 43 | } |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 44 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 45 | crow::Response& res; |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | /** |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 49 | * @brief Abstract class used for implementing Redfish nodes. |
| 50 | * |
| 51 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 52 | class Node |
| 53 | { |
| 54 | public: |
| 55 | template <typename... Params> |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 56 | Node(App& app, std::string&& entityUrl, [[maybe_unused]] Params... paramsIn) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 57 | { |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 58 | crow::DynamicRule& get = app.routeDynamic(entityUrl.c_str()); |
| 59 | getRule = &get; |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 60 | get.methods(boost::beast::http::verb::get)( |
| 61 | [this](const crow::Request& req, crow::Response& res, |
| 62 | Params... params) { |
| 63 | std::vector<std::string> paramVec = {params...}; |
| 64 | doGet(res, req, paramVec); |
| 65 | }); |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 66 | |
| 67 | crow::DynamicRule& patch = app.routeDynamic(entityUrl.c_str()); |
| 68 | patchRule = &patch; |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 69 | patch.methods(boost::beast::http::verb::patch)( |
| 70 | [this](const crow::Request& req, crow::Response& res, |
| 71 | Params... params) { |
| 72 | std::vector<std::string> paramVec = {params...}; |
| 73 | doPatch(res, req, paramVec); |
| 74 | }); |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 75 | |
| 76 | crow::DynamicRule& post = app.routeDynamic(entityUrl.c_str()); |
| 77 | postRule = &post; |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 78 | post.methods(boost::beast::http::verb::post)( |
| 79 | [this](const crow::Request& req, crow::Response& res, |
| 80 | Params... params) { |
| 81 | std::vector<std::string> paramVec = {params...}; |
| 82 | doPost(res, req, paramVec); |
| 83 | }); |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 84 | |
Adrian Ambrożewicz | 1530261 | 2020-07-16 11:24:47 +0200 | [diff] [blame] | 85 | crow::DynamicRule& put = app.routeDynamic(entityUrl.c_str()); |
| 86 | putRule = &put; |
| 87 | put.methods(boost::beast::http::verb::put)( |
| 88 | [this](const crow::Request& req, crow::Response& res, |
| 89 | Params... params) { |
| 90 | std::vector<std::string> paramVec = {params...}; |
| 91 | doPut(res, req, paramVec); |
| 92 | }); |
| 93 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 94 | crow::DynamicRule& deleteR = app.routeDynamic(entityUrl.c_str()); |
| 95 | deleteRule = &deleteR; |
| 96 | deleteR.methods(boost::beast::http::verb::delete_)( |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 97 | [this](const crow::Request& req, crow::Response& res, |
| 98 | Params... params) { |
| 99 | std::vector<std::string> paramVec = {params...}; |
| 100 | doDelete(res, req, paramVec); |
| 101 | }); |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void initPrivileges() |
| 105 | { |
| 106 | auto it = entityPrivileges.find(boost::beast::http::verb::get); |
| 107 | if (it != entityPrivileges.end()) |
| 108 | { |
| 109 | if (getRule != nullptr) |
| 110 | { |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 111 | getRule->privileges(it->second); |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | it = entityPrivileges.find(boost::beast::http::verb::post); |
| 115 | if (it != entityPrivileges.end()) |
| 116 | { |
| 117 | if (postRule != nullptr) |
| 118 | { |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 119 | postRule->privileges(it->second); |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | it = entityPrivileges.find(boost::beast::http::verb::patch); |
| 123 | if (it != entityPrivileges.end()) |
| 124 | { |
| 125 | if (patchRule != nullptr) |
| 126 | { |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 127 | patchRule->privileges(it->second); |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 128 | } |
| 129 | } |
Adrian Ambrożewicz | 1530261 | 2020-07-16 11:24:47 +0200 | [diff] [blame] | 130 | it = entityPrivileges.find(boost::beast::http::verb::put); |
| 131 | if (it != entityPrivileges.end()) |
| 132 | { |
| 133 | if (putRule != nullptr) |
| 134 | { |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 135 | putRule->privileges(it->second); |
Adrian Ambrożewicz | 1530261 | 2020-07-16 11:24:47 +0200 | [diff] [blame] | 136 | } |
| 137 | } |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 138 | it = entityPrivileges.find(boost::beast::http::verb::delete_); |
| 139 | if (it != entityPrivileges.end()) |
| 140 | { |
| 141 | if (deleteRule != nullptr) |
| 142 | { |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 143 | deleteRule->privileges(it->second); |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 144 | } |
| 145 | } |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 146 | } |
Ed Tanous | cbbfa96 | 2018-03-13 16:46:28 -0700 | [diff] [blame] | 147 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 148 | virtual ~Node() = default; |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 149 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 150 | OperationMap entityPrivileges; |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 151 | |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 152 | crow::DynamicRule* getRule = nullptr; |
| 153 | crow::DynamicRule* postRule = nullptr; |
| 154 | crow::DynamicRule* patchRule = nullptr; |
Adrian Ambrożewicz | 1530261 | 2020-07-16 11:24:47 +0200 | [diff] [blame] | 155 | crow::DynamicRule* putRule = nullptr; |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 156 | crow::DynamicRule* deleteRule = nullptr; |
| 157 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 158 | protected: |
| 159 | // Node is designed to be an abstract class, so doGet is pure virtual |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 160 | virtual void doGet(crow::Response& res, const crow::Request&, |
| 161 | const std::vector<std::string>&) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 162 | { |
| 163 | res.result(boost::beast::http::status::method_not_allowed); |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 164 | res.end(); |
| 165 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 166 | |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 167 | virtual void doPatch(crow::Response& res, const crow::Request&, |
| 168 | const std::vector<std::string>&) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 169 | { |
| 170 | res.result(boost::beast::http::status::method_not_allowed); |
| 171 | res.end(); |
| 172 | } |
| 173 | |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 174 | virtual void doPost(crow::Response& res, const crow::Request&, |
| 175 | const std::vector<std::string>&) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 176 | { |
| 177 | res.result(boost::beast::http::status::method_not_allowed); |
| 178 | res.end(); |
| 179 | } |
| 180 | |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 181 | virtual void doPut(crow::Response& res, const crow::Request&, |
| 182 | const std::vector<std::string>&) |
Adrian Ambrożewicz | 1530261 | 2020-07-16 11:24:47 +0200 | [diff] [blame] | 183 | { |
| 184 | res.result(boost::beast::http::status::method_not_allowed); |
| 185 | res.end(); |
| 186 | } |
| 187 | |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 188 | virtual void doDelete(crow::Response& res, const crow::Request&, |
| 189 | const std::vector<std::string>&) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 190 | { |
| 191 | res.result(boost::beast::http::status::method_not_allowed); |
| 192 | res.end(); |
| 193 | } |
Joseph Reynolds | 900f949 | 2019-11-25 15:37:29 -0600 | [diff] [blame] | 194 | |
Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 195 | /* @brief Would the operation be allowed if the user did not have the |
| 196 | * ConfigureSelf Privilege? Also honors session.isConfigureSelfOnly. |
Joseph Reynolds | 900f949 | 2019-11-25 15:37:29 -0600 | [diff] [blame] | 197 | * |
| 198 | * @param req the request |
| 199 | * |
| 200 | * @returns True if allowed, false otherwise |
| 201 | */ |
| 202 | inline bool isAllowedWithoutConfigureSelf(const crow::Request& req) |
| 203 | { |
RAJESWARAN THILLAIGOVINDAN | 61dbeef | 2019-12-13 04:26:54 -0600 | [diff] [blame] | 204 | const std::string& userRole = req.userRole; |
| 205 | BMCWEB_LOG_DEBUG << "isAllowedWithoutConfigureSelf for the role " |
| 206 | << req.userRole; |
Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 207 | Privileges effectiveUserPrivileges; |
| 208 | if (req.session && req.session->isConfigureSelfOnly) |
| 209 | { |
| 210 | // The session has no privileges because it is limited to |
| 211 | // configureSelfOnly and we are disregarding that privilege. |
| 212 | // Note that some operations do not require any privilege. |
| 213 | } |
| 214 | else |
| 215 | { |
| 216 | effectiveUserPrivileges = redfish::getUserPrivileges(userRole); |
| 217 | effectiveUserPrivileges.resetSinglePrivilege("ConfigureSelf"); |
| 218 | } |
Joseph Reynolds | 900f949 | 2019-11-25 15:37:29 -0600 | [diff] [blame] | 219 | const auto& requiredPrivilegesIt = entityPrivileges.find(req.method()); |
| 220 | return (requiredPrivilegesIt != entityPrivileges.end()) && |
| 221 | isOperationAllowedWithPrivileges(requiredPrivilegesIt->second, |
| 222 | effectiveUserPrivileges); |
| 223 | } |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 224 | }; |
| 225 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 226 | } // namespace redfish |