blob: de205f021ef42930c36935af2756baaaf89ca41b [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 Tanousd7857202025-01-28 15:32:26 -080010#include "logging.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080011#include "query.hpp"
12#include "registries/privilege_registry.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -080013#include "str_utility.hpp"
Ed Tanous81d523a2022-05-25 12:00:51 -070014
Ed Tanousd7857202025-01-28 15:32:26 -080015#include <boost/beast/http/field.hpp>
16#include <boost/beast/http/status.hpp>
17#include <boost/beast/http/verb.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070018#include <boost/url/format.hpp>
19
Ed Tanousd7857202025-01-28 15:32:26 -080020#include <array>
21#include <filesystem>
22#include <format>
23#include <functional>
24#include <memory>
Ed Tanous3544d2a2023-08-06 18:12:20 -070025#include <ranges>
Ed Tanousa8c4ce92021-03-24 08:44:51 -070026#include <string>
Ed Tanousd7857202025-01-28 15:32:26 -080027#include <system_error>
28#include <utility>
29#include <vector>
Ed Tanous1abe55e2018-09-05 08:30:59 -070030
Ed Tanous1abe55e2018-09-05 08:30:59 -070031namespace redfish
32{
Ed Tanousa8c4ce92021-03-24 08:44:51 -070033
Ed Tanousd3355c52022-05-11 14:40:49 -070034inline void redfishGet(App& app, const crow::Request& req,
35 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
36{
Carson Labrado3ba00072022-06-06 19:40:56 +000037 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanousd3355c52022-05-11 14:40:49 -070038 {
39 return;
40 }
41 asyncResp->res.jsonValue["v1"] = "/redfish/v1/";
42}
43
Ed Tanous8c623a92022-05-24 11:54:51 -070044inline 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 Zhou686b7092022-06-15 20:02:27 +000052 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous8c623a92022-05-24 11:54:51 -070053 {
54 return;
55 }
56
Ed Tanous62598e32023-07-17 17:06:25 -070057 BMCWEB_LOG_WARNING("404 on path {}", path);
Ed Tanous8c623a92022-05-24 11:54:51 -070058
Ed Tanous39662a32023-02-06 15:09:46 -080059 std::string name = req.url().segments().back();
Ed Tanous8c623a92022-05-24 11:54:51 -070060 // 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 Tanous079360a2022-06-29 10:05:19 -070063 messages::resourceNotFound(asyncResp->res, "", name);
Ed Tanous8c623a92022-05-24 11:54:51 -070064}
65
Ed Tanous44c70412022-07-31 16:48:29 -070066inline 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 Tanous62598e32023-07-17 17:06:25 -070077 BMCWEB_LOG_WARNING("405 on path {}", path);
Ed Tanous44c70412022-07-31 16:48:29 -070078 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 Williams504af5a2025-02-03 14:29:03 -050089inline void jsonSchemaIndexGet(
90 App& app, const crow::Request& req,
91 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous81d523a2022-05-25 12:00:51 -070092{
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 Tanous81d523a2022-05-25 12:00:51 -070099 json["@odata.type"] = "#JsonSchemaFileCollection.JsonSchemaFileCollection";
100 json["Name"] = "JsonSchemaFile Collection";
101 json["Description"] = "Collection of JsonSchemaFiles";
102 nlohmann::json::array_t members;
Ed Tanousa529a6a2024-05-29 16:51:37 -0700103
104 std::error_code ec;
105 std::filesystem::directory_iterator dirList(
106 "/usr/share/www/redfish/v1/JsonSchemas", ec);
107 if (ec)
Ed Tanous81d523a2022-05-25 12:00:51 -0700108 {
Ed Tanousa529a6a2024-05-29 16:51:37 -0700109 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 Tanous81d523a2022-05-25 12:00:51 -0700121 nlohmann::json::object_t member;
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400122 member["@odata.id"] =
123 boost::urls::format("/redfish/v1/JsonSchemas/{}", split[0]);
Patrick Williamsad539542023-05-12 10:10:08 -0500124 members.emplace_back(std::move(member));
Ed Tanous81d523a2022-05-25 12:00:51 -0700125 }
Ed Tanousa529a6a2024-05-29 16:51:37 -0700126
127 json["Members@odata.count"] = members.size();
Ed Tanous81d523a2022-05-25 12:00:51 -0700128 json["Members"] = std::move(members);
Ed Tanous81d523a2022-05-25 12:00:51 -0700129}
130
131inline 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 Tanousa529a6a2024-05-29 16:51:37 -0700140 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 Williamsbd79bce2024-08-16 15:22:20 -0400164 json["@odata.id"] =
165 boost::urls::format("/redfish/v1/JsonSchemas/{}", schema);
Ed Tanousa529a6a2024-05-29 16:51:37 -0700166 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 Williams504af5a2025-02-03 14:29:03 -0500194inline 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 Tanousa529a6a2024-05-29 16:51:37 -0700198{
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 Tanous81d523a2022-05-25 12:00:51 -0700217 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800218 messages::resourceNotFound(asyncResp->res, "JsonSchemaFile", schema);
Ed Tanous81d523a2022-05-25 12:00:51 -0700219 return;
220 }
221
Myung Baed51c61b2024-09-13 10:35:34 -0500222 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 Tanousa529a6a2024-05-29 16:51:37 -0700229 {
230 BMCWEB_LOG_DEBUG("failed to read file");
Myung Baed51c61b2024-09-13 10:35:34 -0500231 messages::internalError(asyncResp->res);
Ed Tanousa529a6a2024-05-29 16:51:37 -0700232 return;
233 }
Ed Tanous81d523a2022-05-25 12:00:51 -0700234}
235
Ed Tanousf65fca62022-05-24 12:49:41 -0700236inline void requestRoutesRedfish(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700237{
238 BMCWEB_ROUTE(app, "/redfish/")
Ed Tanousb41187f2019-10-24 16:30:02 -0700239 .methods(boost::beast::http::verb::get)(
Ed Tanousd3355c52022-05-11 14:40:49 -0700240 std::bind_front(redfishGet, std::ref(app)));
Ed Tanous8c623a92022-05-24 11:54:51 -0700241
Ed Tanousa529a6a2024-05-29 16:51:37 -0700242 BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/<str>/<str>")
243 .privileges(redfish::privileges::getJsonSchemaFile)
244 .methods(boost::beast::http::verb::get)(jsonSchemaGetFile);
245
Ed Tanous81d523a2022-05-25 12:00:51 -0700246 BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/<str>/")
Ed Tanous0ea4b4e2022-08-29 14:30:16 -0700247 .privileges(redfish::privileges::getJsonSchemaFileCollection)
Ed Tanous81d523a2022-05-25 12:00:51 -0700248 .methods(boost::beast::http::verb::get)(
249 std::bind_front(jsonSchemaGet, std::ref(app)));
250
251 BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/")
Ed Tanous0ea4b4e2022-08-29 14:30:16 -0700252 .privileges(redfish::privileges::getJsonSchemaFile)
Ed Tanous81d523a2022-05-25 12:00:51 -0700253 .methods(boost::beast::http::verb::get)(
254 std::bind_front(jsonSchemaIndexGet, std::ref(app)));
Ed Tanouse9dd1d32022-06-17 11:56:00 -0700255
256 // Note, this route must always be registered last
257 BMCWEB_ROUTE(app, "/redfish/<path>")
Ed Tanous0ea4b4e2022-08-29 14:30:16 -0700258 .notFound()
259 .privileges(redfish::privileges::privilegeSetLogin)(
260 std::bind_front(redfish404, std::ref(app)));
Ed Tanous44c70412022-07-31 16:48:29 -0700261
262 BMCWEB_ROUTE(app, "/redfish/<path>")
Ed Tanous0ea4b4e2022-08-29 14:30:16 -0700263 .methodNotAllowed()
264 .privileges(redfish::privileges::privilegeSetLogin)(
265 std::bind_front(redfish405, std::ref(app)));
Ed Tanous3dac7492017-08-02 13:46:20 -0700266}
Ed Tanousf65fca62022-05-24 12:49:41 -0700267
Ed Tanous1abe55e2018-09-05 08:30:59 -0700268} // namespace redfish