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