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