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 | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 18 | #include "privileges.hpp" |
| 19 | #include "token_authorization_middleware.hpp" |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 20 | #include "webserver_common.hpp" |
Borawski.Lukasz | b6df6dc | 2018-01-24 10:20:45 +0100 | [diff] [blame] | 21 | #include "crow.h" |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 22 | |
| 23 | namespace redfish { |
| 24 | |
| 25 | /** |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 26 | * AsyncResp |
| 27 | * Gathers data needed for response processing after async calls are done |
| 28 | */ |
| 29 | class AsyncResp { |
| 30 | public: |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 31 | AsyncResp(crow::Response& response) : res(response) {} |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 32 | |
| 33 | ~AsyncResp() { res.end(); } |
| 34 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 35 | crow::Response& res; |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | /** |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 39 | * @brief Abstract class used for implementing Redfish nodes. |
| 40 | * |
| 41 | */ |
| 42 | class Node { |
| 43 | public: |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 44 | template <typename... Params> |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 45 | Node(CrowApp& app, std::string&& entityUrl, Params... params) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 46 | app.routeDynamic(entityUrl.c_str()) |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 47 | .methods("GET"_method, "PATCH"_method, "POST"_method, |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 48 | "DELETE"_method)([&](const crow::Request& req, |
| 49 | crow::Response& res, Params... params) { |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 50 | std::vector<std::string> paramVec = {params...}; |
| 51 | dispatchRequest(app, req, res, paramVec); |
| 52 | }); |
| 53 | } |
| 54 | |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 55 | virtual ~Node() = default; |
| 56 | |
Ed Tanous | cbbfa96 | 2018-03-13 16:46:28 -0700 | [diff] [blame] | 57 | const std::string* getUrl() const { |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 58 | auto odataId = json.find("@odata.id"); |
Ed Tanous | cbbfa96 | 2018-03-13 16:46:28 -0700 | [diff] [blame] | 59 | if (odataId == json.end()) { |
| 60 | return nullptr; |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 61 | } |
Ed Tanous | cbbfa96 | 2018-03-13 16:46:28 -0700 | [diff] [blame] | 62 | |
| 63 | return odataId->get_ptr<const std::string*>(); |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @brief Inserts subroute fields into for the node's json in the form: |
| 68 | * "subroute_name" : { "odata.id": "node_url/subroute_name/" } |
| 69 | * Excludes metadata urls starting with "$" and child urls having |
| 70 | * more than one level. |
| 71 | * |
| 72 | * @return None |
| 73 | */ |
| 74 | void getSubRoutes(const std::vector<std::unique_ptr<Node>>& allNodes) { |
Ed Tanous | cbbfa96 | 2018-03-13 16:46:28 -0700 | [diff] [blame] | 75 | const std::string* url = getUrl(); |
| 76 | if (url == nullptr) { |
Ed Tanous | 8ceb2ec | 2018-08-13 11:11:56 -0700 | [diff] [blame] | 77 | // BMCWEB_LOG_CRITICAL << "Unable to get url for route"; |
Ed Tanous | cbbfa96 | 2018-03-13 16:46:28 -0700 | [diff] [blame] | 78 | return; |
| 79 | } |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 80 | |
| 81 | for (const auto& node : allNodes) { |
Ed Tanous | cbbfa96 | 2018-03-13 16:46:28 -0700 | [diff] [blame] | 82 | const std::string* route = node->getUrl(); |
| 83 | if (route == nullptr) { |
Ed Tanous | 8ceb2ec | 2018-08-13 11:11:56 -0700 | [diff] [blame] | 84 | // BMCWEB_LOG_CRITICAL << "Unable to get url for route"; |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 85 | continue; |
Ed Tanous | cbbfa96 | 2018-03-13 16:46:28 -0700 | [diff] [blame] | 86 | } |
| 87 | if (boost::starts_with(*route, *url)) { |
| 88 | std::string subRoute = route->substr(url->size()); |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 89 | if (subRoute.empty()) { |
| 90 | continue; |
| 91 | } |
| 92 | |
Ed Tanous | cbbfa96 | 2018-03-13 16:46:28 -0700 | [diff] [blame] | 93 | if (boost::starts_with(subRoute, "/")) { |
| 94 | subRoute.erase(0, 1); |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Ed Tanous | cbbfa96 | 2018-03-13 16:46:28 -0700 | [diff] [blame] | 97 | if (boost::ends_with(subRoute, "/")) { |
| 98 | subRoute.pop_back(); |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Ed Tanous | cbbfa96 | 2018-03-13 16:46:28 -0700 | [diff] [blame] | 101 | if (!boost::starts_with(subRoute, "$") && |
| 102 | subRoute.find('/') == std::string::npos) { |
| 103 | json[subRoute] = nlohmann::json{{"@odata.id", *route}}; |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 109 | OperationMap entityPrivileges; |
| 110 | |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 111 | protected: |
| 112 | // Node is designed to be an abstract class, so doGet is pure virtual |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 113 | virtual void doGet(crow::Response& res, const crow::Request& req, |
Ed Tanous | 74b3f92 | 2018-07-25 11:08:10 -0700 | [diff] [blame] | 114 | const std::vector<std::string>& params) { |
| 115 | res.result(boost::beast::http::status::method_not_allowed); |
| 116 | res.end(); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 117 | } |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 118 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 119 | virtual void doPatch(crow::Response& res, const crow::Request& req, |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 120 | const std::vector<std::string>& params) { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 121 | res.result(boost::beast::http::status::method_not_allowed); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 122 | res.end(); |
| 123 | } |
| 124 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 125 | virtual void doPost(crow::Response& res, const crow::Request& req, |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 126 | const std::vector<std::string>& params) { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 127 | res.result(boost::beast::http::status::method_not_allowed); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 128 | res.end(); |
| 129 | } |
| 130 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 131 | virtual void doDelete(crow::Response& res, const crow::Request& req, |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 132 | const std::vector<std::string>& params) { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 133 | res.result(boost::beast::http::status::method_not_allowed); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 134 | res.end(); |
| 135 | } |
| 136 | |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 137 | nlohmann::json json; |
| 138 | |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 139 | private: |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 140 | void dispatchRequest(CrowApp& app, const crow::Request& req, |
| 141 | crow::Response& res, |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 142 | const std::vector<std::string>& params) { |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 143 | auto ctx = |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 144 | app.template getContext<crow::token_authorization::Middleware>(req); |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 145 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 146 | if (!isMethodAllowedForUser(req.method(), entityPrivileges, |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 147 | ctx.session->username)) { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 148 | res.result(boost::beast::http::status::method_not_allowed); |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 149 | res.end(); |
| 150 | return; |
| 151 | } |
| 152 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 153 | switch (req.method()) { |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 154 | case "GET"_method: |
| 155 | doGet(res, req, params); |
| 156 | break; |
| 157 | |
| 158 | case "PATCH"_method: |
| 159 | doPatch(res, req, params); |
| 160 | break; |
| 161 | |
| 162 | case "POST"_method: |
| 163 | doPost(res, req, params); |
| 164 | break; |
| 165 | |
| 166 | case "DELETE"_method: |
| 167 | doDelete(res, req, params); |
| 168 | break; |
| 169 | |
| 170 | default: |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 171 | res.result(boost::beast::http::status::not_found); |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 172 | res.end(); |
| 173 | } |
| 174 | return; |
| 175 | } |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 176 | }; |
| 177 | |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 178 | } // namespace redfish |