Ed Tanous | 40e9b92 | 2024-09-10 13:50:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 3 | #pragma once |
| 4 | |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 5 | #include "app.hpp" |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 6 | #include "async_resp.hpp" |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 7 | #include "error_messages.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 8 | #include "http_request.hpp" |
| 9 | #include "http_response.hpp" |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 10 | #include "logging.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 11 | #include "query.hpp" |
| 12 | #include "registries/privilege_registry.hpp" |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 13 | #include "str_utility.hpp" |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 14 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 15 | #include <boost/beast/http/field.hpp> |
| 16 | #include <boost/beast/http/status.hpp> |
| 17 | #include <boost/beast/http/verb.hpp> |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 18 | #include <boost/url/format.hpp> |
| 19 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 20 | #include <array> |
| 21 | #include <filesystem> |
| 22 | #include <format> |
| 23 | #include <functional> |
| 24 | #include <memory> |
Ed Tanous | 3544d2a | 2023-08-06 18:12:20 -0700 | [diff] [blame] | 25 | #include <ranges> |
Ed Tanous | a8c4ce9 | 2021-03-24 08:44:51 -0700 | [diff] [blame] | 26 | #include <string> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 27 | #include <system_error> |
| 28 | #include <utility> |
| 29 | #include <vector> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 30 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 31 | namespace redfish |
| 32 | { |
Ed Tanous | a8c4ce9 | 2021-03-24 08:44:51 -0700 | [diff] [blame] | 33 | |
Ed Tanous | d3355c5 | 2022-05-11 14:40:49 -0700 | [diff] [blame] | 34 | inline void redfishGet(App& app, const crow::Request& req, |
| 35 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
| 36 | { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 37 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | d3355c5 | 2022-05-11 14:40:49 -0700 | [diff] [blame] | 38 | { |
| 39 | return; |
| 40 | } |
| 41 | asyncResp->res.jsonValue["v1"] = "/redfish/v1/"; |
| 42 | } |
| 43 | |
Ed Tanous | 8c623a9 | 2022-05-24 11:54:51 -0700 | [diff] [blame] | 44 | inline void redfish404(App& app, const crow::Request& req, |
| 45 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 46 | const std::string& path) |
| 47 | { |
| 48 | asyncResp->res.addHeader(boost::beast::http::field::allow, ""); |
| 49 | |
| 50 | // If we fall to this route, we didn't have a more specific route, so return |
| 51 | // 404 |
Nan Zhou | 686b709 | 2022-06-15 20:02:27 +0000 | [diff] [blame] | 52 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 8c623a9 | 2022-05-24 11:54:51 -0700 | [diff] [blame] | 53 | { |
| 54 | return; |
| 55 | } |
| 56 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 57 | BMCWEB_LOG_WARNING("404 on path {}", path); |
Ed Tanous | 8c623a9 | 2022-05-24 11:54:51 -0700 | [diff] [blame] | 58 | |
Ed Tanous | 39662a3 | 2023-02-06 15:09:46 -0800 | [diff] [blame] | 59 | std::string name = req.url().segments().back(); |
Ed Tanous | 8c623a9 | 2022-05-24 11:54:51 -0700 | [diff] [blame] | 60 | // Note, if we hit the wildcard route, we don't know the "type" the user was |
| 61 | // actually requesting, but giving them a return with an empty string is |
| 62 | // still better than nothing. |
Ed Tanous | 079360a | 2022-06-29 10:05:19 -0700 | [diff] [blame] | 63 | messages::resourceNotFound(asyncResp->res, "", name); |
Ed Tanous | 8c623a9 | 2022-05-24 11:54:51 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Ed Tanous | 44c7041 | 2022-07-31 16:48:29 -0700 | [diff] [blame] | 66 | inline void redfish405(App& app, const crow::Request& req, |
| 67 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 68 | const std::string& path) |
| 69 | { |
| 70 | // If we fall to this route, we didn't have a more specific route, so return |
| 71 | // 405 |
| 72 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 73 | { |
| 74 | return; |
| 75 | } |
| 76 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 77 | BMCWEB_LOG_WARNING("405 on path {}", path); |
Ed Tanous | 44c7041 | 2022-07-31 16:48:29 -0700 | [diff] [blame] | 78 | asyncResp->res.result(boost::beast::http::status::method_not_allowed); |
| 79 | if (req.method() == boost::beast::http::verb::delete_) |
| 80 | { |
| 81 | messages::resourceCannotBeDeleted(asyncResp->res); |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | messages::operationNotAllowed(asyncResp->res); |
| 86 | } |
| 87 | } |
| 88 | |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame^] | 89 | inline void jsonSchemaIndexGet( |
| 90 | App& app, const crow::Request& req, |
| 91 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 92 | { |
| 93 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 94 | { |
| 95 | return; |
| 96 | } |
| 97 | nlohmann::json& json = asyncResp->res.jsonValue; |
| 98 | json["@odata.id"] = "/redfish/v1/JsonSchemas"; |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 99 | json["@odata.type"] = "#JsonSchemaFileCollection.JsonSchemaFileCollection"; |
| 100 | json["Name"] = "JsonSchemaFile Collection"; |
| 101 | json["Description"] = "Collection of JsonSchemaFiles"; |
| 102 | nlohmann::json::array_t members; |
Ed Tanous | a529a6a | 2024-05-29 16:51:37 -0700 | [diff] [blame] | 103 | |
| 104 | std::error_code ec; |
| 105 | std::filesystem::directory_iterator dirList( |
| 106 | "/usr/share/www/redfish/v1/JsonSchemas", ec); |
| 107 | if (ec) |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 108 | { |
Ed Tanous | a529a6a | 2024-05-29 16:51:37 -0700 | [diff] [blame] | 109 | messages::internalError(asyncResp->res); |
| 110 | return; |
| 111 | } |
| 112 | for (const std::filesystem::path& file : dirList) |
| 113 | { |
| 114 | std::string filename = file.filename(); |
| 115 | std::vector<std::string> split; |
| 116 | bmcweb::split(split, filename, '.'); |
| 117 | if (split.empty()) |
| 118 | { |
| 119 | continue; |
| 120 | } |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 121 | nlohmann::json::object_t member; |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 122 | member["@odata.id"] = |
| 123 | boost::urls::format("/redfish/v1/JsonSchemas/{}", split[0]); |
Patrick Williams | ad53954 | 2023-05-12 10:10:08 -0500 | [diff] [blame] | 124 | members.emplace_back(std::move(member)); |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 125 | } |
Ed Tanous | a529a6a | 2024-05-29 16:51:37 -0700 | [diff] [blame] | 126 | |
| 127 | json["Members@odata.count"] = members.size(); |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 128 | json["Members"] = std::move(members); |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | inline void jsonSchemaGet(App& app, const crow::Request& req, |
| 132 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 133 | const std::string& schema) |
| 134 | { |
| 135 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 136 | { |
| 137 | return; |
| 138 | } |
| 139 | |
Ed Tanous | a529a6a | 2024-05-29 16:51:37 -0700 | [diff] [blame] | 140 | std::error_code ec; |
| 141 | std::filesystem::directory_iterator dirList( |
| 142 | "/usr/share/www/redfish/v1/JsonSchemas", ec); |
| 143 | if (ec) |
| 144 | { |
| 145 | messages::resourceNotFound(asyncResp->res, "JsonSchemaFile", schema); |
| 146 | return; |
| 147 | } |
| 148 | for (const std::filesystem::path& file : dirList) |
| 149 | { |
| 150 | std::string filename = file.filename(); |
| 151 | std::vector<std::string> split; |
| 152 | bmcweb::split(split, filename, '.'); |
| 153 | if (split.empty()) |
| 154 | { |
| 155 | continue; |
| 156 | } |
| 157 | BMCWEB_LOG_DEBUG("Checking {}", split[0]); |
| 158 | if (split[0] != schema) |
| 159 | { |
| 160 | continue; |
| 161 | } |
| 162 | |
| 163 | nlohmann::json& json = asyncResp->res.jsonValue; |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 164 | json["@odata.id"] = |
| 165 | boost::urls::format("/redfish/v1/JsonSchemas/{}", schema); |
Ed Tanous | a529a6a | 2024-05-29 16:51:37 -0700 | [diff] [blame] | 166 | json["@odata.type"] = "#JsonSchemaFile.v1_0_2.JsonSchemaFile"; |
| 167 | json["Name"] = schema + " Schema File"; |
| 168 | json["Description"] = schema + " Schema File Location"; |
| 169 | json["Id"] = schema; |
| 170 | std::string schemaName = std::format("#{}.{}", schema, schema); |
| 171 | json["Schema"] = std::move(schemaName); |
| 172 | constexpr std::array<std::string_view, 1> languages{"en"}; |
| 173 | json["Languages"] = languages; |
| 174 | json["Languages@odata.count"] = languages.size(); |
| 175 | |
| 176 | nlohmann::json::array_t locationArray; |
| 177 | nlohmann::json::object_t locationEntry; |
| 178 | locationEntry["Language"] = "en"; |
| 179 | |
| 180 | locationEntry["PublicationUri"] = boost::urls::format( |
| 181 | "http://redfish.dmtf.org/schemas/v1/{}", filename); |
| 182 | locationEntry["Uri"] = boost::urls::format( |
| 183 | "/redfish/v1/JsonSchemas/{}/{}", schema, filename); |
| 184 | |
| 185 | locationArray.emplace_back(locationEntry); |
| 186 | |
| 187 | json["Location"] = std::move(locationArray); |
| 188 | json["Location@odata.count"] = 1; |
| 189 | return; |
| 190 | } |
| 191 | messages::resourceNotFound(asyncResp->res, "JsonSchemaFile", schema); |
| 192 | } |
| 193 | |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame^] | 194 | inline void jsonSchemaGetFile( |
| 195 | const crow::Request& /*req*/, |
| 196 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 197 | const std::string& schema, const std::string& schemaFile) |
Ed Tanous | a529a6a | 2024-05-29 16:51:37 -0700 | [diff] [blame] | 198 | { |
| 199 | // Sanity check the filename |
| 200 | if (schemaFile.find_first_not_of( |
| 201 | "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.") != |
| 202 | std::string::npos) |
| 203 | { |
| 204 | messages::resourceNotFound(asyncResp->res, "JsonSchemaFile", schema); |
| 205 | return; |
| 206 | } |
| 207 | // Schema path should look like /redfish/v1/JsonSchemas/Foo/Foo.x.json |
| 208 | // Make sure the two paths match. |
| 209 | if (!schemaFile.starts_with(schema)) |
| 210 | { |
| 211 | messages::resourceNotFound(asyncResp->res, "JsonSchemaFile", schema); |
| 212 | return; |
| 213 | } |
| 214 | std::filesystem::path filepath("/usr/share/www/redfish/v1/JsonSchemas"); |
| 215 | filepath /= schemaFile; |
| 216 | if (filepath.is_relative()) |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 217 | { |
Jiaqing Zhao | d8a5d5d | 2022-08-05 16:21:51 +0800 | [diff] [blame] | 218 | messages::resourceNotFound(asyncResp->res, "JsonSchemaFile", schema); |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 219 | return; |
| 220 | } |
| 221 | |
Myung Bae | d51c61b | 2024-09-13 10:35:34 -0500 | [diff] [blame] | 222 | crow::OpenCode ec = asyncResp->res.openFile(filepath); |
| 223 | if (ec == crow::OpenCode::FileDoesNotExist) |
| 224 | { |
| 225 | messages::resourceNotFound(asyncResp->res, "JsonSchemaFile", schema); |
| 226 | return; |
| 227 | } |
| 228 | if (ec == crow::OpenCode::InternalError) |
Ed Tanous | a529a6a | 2024-05-29 16:51:37 -0700 | [diff] [blame] | 229 | { |
| 230 | BMCWEB_LOG_DEBUG("failed to read file"); |
Myung Bae | d51c61b | 2024-09-13 10:35:34 -0500 | [diff] [blame] | 231 | messages::internalError(asyncResp->res); |
Ed Tanous | a529a6a | 2024-05-29 16:51:37 -0700 | [diff] [blame] | 232 | return; |
| 233 | } |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Ed Tanous | f65fca6 | 2022-05-24 12:49:41 -0700 | [diff] [blame] | 236 | inline void requestRoutesRedfish(App& app) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 237 | { |
| 238 | BMCWEB_ROUTE(app, "/redfish/") |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 239 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | d3355c5 | 2022-05-11 14:40:49 -0700 | [diff] [blame] | 240 | std::bind_front(redfishGet, std::ref(app))); |
Ed Tanous | 8c623a9 | 2022-05-24 11:54:51 -0700 | [diff] [blame] | 241 | |
Ed Tanous | a529a6a | 2024-05-29 16:51:37 -0700 | [diff] [blame] | 242 | BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/<str>/<str>") |
| 243 | .privileges(redfish::privileges::getJsonSchemaFile) |
| 244 | .methods(boost::beast::http::verb::get)(jsonSchemaGetFile); |
| 245 | |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 246 | BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/<str>/") |
Ed Tanous | 0ea4b4e | 2022-08-29 14:30:16 -0700 | [diff] [blame] | 247 | .privileges(redfish::privileges::getJsonSchemaFileCollection) |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 248 | .methods(boost::beast::http::verb::get)( |
| 249 | std::bind_front(jsonSchemaGet, std::ref(app))); |
| 250 | |
| 251 | BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/") |
Ed Tanous | 0ea4b4e | 2022-08-29 14:30:16 -0700 | [diff] [blame] | 252 | .privileges(redfish::privileges::getJsonSchemaFile) |
Ed Tanous | 81d523a | 2022-05-25 12:00:51 -0700 | [diff] [blame] | 253 | .methods(boost::beast::http::verb::get)( |
| 254 | std::bind_front(jsonSchemaIndexGet, std::ref(app))); |
Ed Tanous | e9dd1d3 | 2022-06-17 11:56:00 -0700 | [diff] [blame] | 255 | |
| 256 | // Note, this route must always be registered last |
| 257 | BMCWEB_ROUTE(app, "/redfish/<path>") |
Ed Tanous | 0ea4b4e | 2022-08-29 14:30:16 -0700 | [diff] [blame] | 258 | .notFound() |
| 259 | .privileges(redfish::privileges::privilegeSetLogin)( |
| 260 | std::bind_front(redfish404, std::ref(app))); |
Ed Tanous | 44c7041 | 2022-07-31 16:48:29 -0700 | [diff] [blame] | 261 | |
| 262 | BMCWEB_ROUTE(app, "/redfish/<path>") |
Ed Tanous | 0ea4b4e | 2022-08-29 14:30:16 -0700 | [diff] [blame] | 263 | .methodNotAllowed() |
| 264 | .privileges(redfish::privileges::privilegeSetLogin)( |
| 265 | std::bind_front(redfish405, std::ref(app))); |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 266 | } |
Ed Tanous | f65fca6 | 2022-05-24 12:49:41 -0700 | [diff] [blame] | 267 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 268 | } // namespace redfish |