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