blob: 4b08b6960ec0ae976d851f1f42b52429826a1efb [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Ed Tanous911ac312017-08-15 09:37:42 -07003#pragma once
4
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08005#include "app.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -08006#include "async_resp.hpp"
Ed Tanous81d523a2022-05-25 12:00:51 -07007#include "error_messages.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08008#include "http_request.hpp"
9#include "http_response.hpp"
Ed Tanous3577e442025-08-19 19:34:00 -070010#include "human_sort.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -080011#include "logging.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080012#include "query.hpp"
13#include "registries/privilege_registry.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -080014#include "str_utility.hpp"
Ed Tanous81d523a2022-05-25 12:00:51 -070015
Ed Tanousd7857202025-01-28 15:32:26 -080016#include <boost/beast/http/field.hpp>
17#include <boost/beast/http/status.hpp>
18#include <boost/beast/http/verb.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070019#include <boost/url/format.hpp>
20
Ed Tanous3577e442025-08-19 19:34:00 -070021#include <algorithm>
Ed Tanousd7857202025-01-28 15:32:26 -080022#include <array>
23#include <filesystem>
24#include <format>
25#include <functional>
26#include <memory>
Ed Tanous3544d2a2023-08-06 18:12:20 -070027#include <ranges>
Ed Tanousa8c4ce92021-03-24 08:44:51 -070028#include <string>
Ed Tanousd7857202025-01-28 15:32:26 -080029#include <system_error>
30#include <utility>
31#include <vector>
Ed Tanous1abe55e2018-09-05 08:30:59 -070032
Ed Tanous1abe55e2018-09-05 08:30:59 -070033namespace redfish
34{
Ed Tanousa8c4ce92021-03-24 08:44:51 -070035
Ed Tanousd3355c52022-05-11 14:40:49 -070036inline void redfishGet(App& app, const crow::Request& req,
37 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
38{
Carson Labrado3ba00072022-06-06 19:40:56 +000039 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanousd3355c52022-05-11 14:40:49 -070040 {
41 return;
42 }
43 asyncResp->res.jsonValue["v1"] = "/redfish/v1/";
44}
45
Ed Tanous8c623a92022-05-24 11:54:51 -070046inline 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 Zhou686b7092022-06-15 20:02:27 +000054 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous8c623a92022-05-24 11:54:51 -070055 {
56 return;
57 }
58
Ed Tanous62598e32023-07-17 17:06:25 -070059 BMCWEB_LOG_WARNING("404 on path {}", path);
Ed Tanous8c623a92022-05-24 11:54:51 -070060
Ed Tanous39662a32023-02-06 15:09:46 -080061 std::string name = req.url().segments().back();
Ed Tanous8c623a92022-05-24 11:54:51 -070062 // 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 Tanous079360a2022-06-29 10:05:19 -070065 messages::resourceNotFound(asyncResp->res, "", name);
Ed Tanous8c623a92022-05-24 11:54:51 -070066}
67
Ed Tanous44c70412022-07-31 16:48:29 -070068inline 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 Tanous62598e32023-07-17 17:06:25 -070079 BMCWEB_LOG_WARNING("405 on path {}", path);
Ed Tanous44c70412022-07-31 16:48:29 -070080 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 Williams504af5a2025-02-03 14:29:03 -050091inline void jsonSchemaIndexGet(
92 App& app, const crow::Request& req,
93 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous81d523a2022-05-25 12:00:51 -070094{
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 Tanous81d523a2022-05-25 12:00:51 -0700101 json["@odata.type"] = "#JsonSchemaFileCollection.JsonSchemaFileCollection";
102 json["Name"] = "JsonSchemaFile Collection";
103 json["Description"] = "Collection of JsonSchemaFiles";
Ed Tanousa529a6a2024-05-29 16:51:37 -0700104
105 std::error_code ec;
106 std::filesystem::directory_iterator dirList(
107 "/usr/share/www/redfish/v1/JsonSchemas", ec);
108 if (ec)
Ed Tanous81d523a2022-05-25 12:00:51 -0700109 {
Ed Tanousa529a6a2024-05-29 16:51:37 -0700110 messages::internalError(asyncResp->res);
111 return;
112 }
Myung Bae04c39932025-06-21 12:42:39 -0500113
114 std::vector<std::string> schemaNames;
Ed Tanousa529a6a2024-05-29 16:51:37 -0700115 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 Bae04c39932025-06-21 12:42:39 -0500124 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 Tanous81d523a2022-05-25 12:00:51 -0700131 nlohmann::json::object_t member;
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400132 member["@odata.id"] =
Myung Bae04c39932025-06-21 12:42:39 -0500133 boost::urls::format("/redfish/v1/JsonSchemas/{}", schema);
Patrick Williamsad539542023-05-12 10:10:08 -0500134 members.emplace_back(std::move(member));
Ed Tanous81d523a2022-05-25 12:00:51 -0700135 }
Ed Tanousa529a6a2024-05-29 16:51:37 -0700136 json["Members@odata.count"] = members.size();
Ed Tanous81d523a2022-05-25 12:00:51 -0700137 json["Members"] = std::move(members);
Ed Tanous81d523a2022-05-25 12:00:51 -0700138}
139
140inline 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 Tanousa529a6a2024-05-29 16:51:37 -0700149 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 Williamsbd79bce2024-08-16 15:22:20 -0400173 json["@odata.id"] =
174 boost::urls::format("/redfish/v1/JsonSchemas/{}", schema);
Ed Tanousa529a6a2024-05-29 16:51:37 -0700175 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 Williams504af5a2025-02-03 14:29:03 -0500203inline 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 Tanousa529a6a2024-05-29 16:51:37 -0700207{
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 Tanous81d523a2022-05-25 12:00:51 -0700226 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800227 messages::resourceNotFound(asyncResp->res, "JsonSchemaFile", schema);
Ed Tanous81d523a2022-05-25 12:00:51 -0700228 return;
229 }
230
Myung Baed51c61b2024-09-13 10:35:34 -0500231 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 Tanousa529a6a2024-05-29 16:51:37 -0700238 {
239 BMCWEB_LOG_DEBUG("failed to read file");
Myung Baed51c61b2024-09-13 10:35:34 -0500240 messages::internalError(asyncResp->res);
Ed Tanousa529a6a2024-05-29 16:51:37 -0700241 return;
242 }
Ed Tanous81d523a2022-05-25 12:00:51 -0700243}
244
Ed Tanousf65fca62022-05-24 12:49:41 -0700245inline void requestRoutesRedfish(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700246{
247 BMCWEB_ROUTE(app, "/redfish/")
Ed Tanousb41187f2019-10-24 16:30:02 -0700248 .methods(boost::beast::http::verb::get)(
Ed Tanousd3355c52022-05-11 14:40:49 -0700249 std::bind_front(redfishGet, std::ref(app)));
Ed Tanous8c623a92022-05-24 11:54:51 -0700250
Ed Tanousa529a6a2024-05-29 16:51:37 -0700251 BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/<str>/<str>")
252 .privileges(redfish::privileges::getJsonSchemaFile)
253 .methods(boost::beast::http::verb::get)(jsonSchemaGetFile);
254
Ed Tanous81d523a2022-05-25 12:00:51 -0700255 BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/<str>/")
Ed Tanous0ea4b4e2022-08-29 14:30:16 -0700256 .privileges(redfish::privileges::getJsonSchemaFileCollection)
Ed Tanous81d523a2022-05-25 12:00:51 -0700257 .methods(boost::beast::http::verb::get)(
258 std::bind_front(jsonSchemaGet, std::ref(app)));
259
260 BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/")
Ed Tanous0ea4b4e2022-08-29 14:30:16 -0700261 .privileges(redfish::privileges::getJsonSchemaFile)
Ed Tanous81d523a2022-05-25 12:00:51 -0700262 .methods(boost::beast::http::verb::get)(
263 std::bind_front(jsonSchemaIndexGet, std::ref(app)));
Ed Tanouse9dd1d32022-06-17 11:56:00 -0700264
265 // Note, this route must always be registered last
266 BMCWEB_ROUTE(app, "/redfish/<path>")
Ed Tanous0ea4b4e2022-08-29 14:30:16 -0700267 .notFound()
268 .privileges(redfish::privileges::privilegeSetLogin)(
269 std::bind_front(redfish404, std::ref(app)));
Ed Tanous44c70412022-07-31 16:48:29 -0700270
271 BMCWEB_ROUTE(app, "/redfish/<path>")
Ed Tanous0ea4b4e2022-08-29 14:30:16 -0700272 .methodNotAllowed()
273 .privileges(redfish::privileges::privilegeSetLogin)(
274 std::bind_front(redfish405, std::ref(app)));
Ed Tanous3dac7492017-08-02 13:46:20 -0700275}
Ed Tanousf65fca62022-05-24 12:49:41 -0700276
Ed Tanous1abe55e2018-09-05 08:30:59 -0700277} // namespace redfish