blob: e9dd422eaa446b362362e3cf885a824a469d7b86 [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"
Myung Bae04c39932025-06-21 12:42:39 -050014#include "utils/json_utils.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 Tanousd7857202025-01-28 15:32:26 -080021#include <array>
22#include <filesystem>
23#include <format>
24#include <functional>
25#include <memory>
Ed Tanous3544d2a2023-08-06 18:12:20 -070026#include <ranges>
Ed Tanousa8c4ce92021-03-24 08:44:51 -070027#include <string>
Ed Tanousd7857202025-01-28 15:32:26 -080028#include <system_error>
29#include <utility>
30#include <vector>
Ed Tanous1abe55e2018-09-05 08:30:59 -070031
Ed Tanous1abe55e2018-09-05 08:30:59 -070032namespace redfish
33{
Ed Tanousa8c4ce92021-03-24 08:44:51 -070034
Ed Tanousd3355c52022-05-11 14:40:49 -070035inline void redfishGet(App& app, const crow::Request& req,
36 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
37{
Carson Labrado3ba00072022-06-06 19:40:56 +000038 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanousd3355c52022-05-11 14:40:49 -070039 {
40 return;
41 }
42 asyncResp->res.jsonValue["v1"] = "/redfish/v1/";
43}
44
Ed Tanous8c623a92022-05-24 11:54:51 -070045inline 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 Zhou686b7092022-06-15 20:02:27 +000053 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous8c623a92022-05-24 11:54:51 -070054 {
55 return;
56 }
57
Ed Tanous62598e32023-07-17 17:06:25 -070058 BMCWEB_LOG_WARNING("404 on path {}", path);
Ed Tanous8c623a92022-05-24 11:54:51 -070059
Ed Tanous39662a32023-02-06 15:09:46 -080060 std::string name = req.url().segments().back();
Ed Tanous8c623a92022-05-24 11:54:51 -070061 // 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 Tanous079360a2022-06-29 10:05:19 -070064 messages::resourceNotFound(asyncResp->res, "", name);
Ed Tanous8c623a92022-05-24 11:54:51 -070065}
66
Ed Tanous44c70412022-07-31 16:48:29 -070067inline 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 Tanous62598e32023-07-17 17:06:25 -070078 BMCWEB_LOG_WARNING("405 on path {}", path);
Ed Tanous44c70412022-07-31 16:48:29 -070079 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 Williams504af5a2025-02-03 14:29:03 -050090inline void jsonSchemaIndexGet(
91 App& app, const crow::Request& req,
92 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous81d523a2022-05-25 12:00:51 -070093{
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 Tanous81d523a2022-05-25 12:00:51 -0700100 json["@odata.type"] = "#JsonSchemaFileCollection.JsonSchemaFileCollection";
101 json["Name"] = "JsonSchemaFile Collection";
102 json["Description"] = "Collection of JsonSchemaFiles";
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 }
Myung Bae04c39932025-06-21 12:42:39 -0500112
113 std::vector<std::string> schemaNames;
Ed Tanousa529a6a2024-05-29 16:51:37 -0700114 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 Bae04c39932025-06-21 12:42:39 -0500123 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 Tanous81d523a2022-05-25 12:00:51 -0700130 nlohmann::json::object_t member;
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400131 member["@odata.id"] =
Myung Bae04c39932025-06-21 12:42:39 -0500132 boost::urls::format("/redfish/v1/JsonSchemas/{}", schema);
Patrick Williamsad539542023-05-12 10:10:08 -0500133 members.emplace_back(std::move(member));
Ed Tanous81d523a2022-05-25 12:00:51 -0700134 }
Ed Tanousa529a6a2024-05-29 16:51:37 -0700135 json["Members@odata.count"] = members.size();
Ed Tanous81d523a2022-05-25 12:00:51 -0700136 json["Members"] = std::move(members);
Ed Tanous81d523a2022-05-25 12:00:51 -0700137}
138
139inline 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 Tanousa529a6a2024-05-29 16:51:37 -0700148 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 Williamsbd79bce2024-08-16 15:22:20 -0400172 json["@odata.id"] =
173 boost::urls::format("/redfish/v1/JsonSchemas/{}", schema);
Ed Tanousa529a6a2024-05-29 16:51:37 -0700174 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 Williams504af5a2025-02-03 14:29:03 -0500202inline 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 Tanousa529a6a2024-05-29 16:51:37 -0700206{
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 Tanous81d523a2022-05-25 12:00:51 -0700225 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800226 messages::resourceNotFound(asyncResp->res, "JsonSchemaFile", schema);
Ed Tanous81d523a2022-05-25 12:00:51 -0700227 return;
228 }
229
Myung Baed51c61b2024-09-13 10:35:34 -0500230 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 Tanousa529a6a2024-05-29 16:51:37 -0700237 {
238 BMCWEB_LOG_DEBUG("failed to read file");
Myung Baed51c61b2024-09-13 10:35:34 -0500239 messages::internalError(asyncResp->res);
Ed Tanousa529a6a2024-05-29 16:51:37 -0700240 return;
241 }
Ed Tanous81d523a2022-05-25 12:00:51 -0700242}
243
Ed Tanousf65fca62022-05-24 12:49:41 -0700244inline void requestRoutesRedfish(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700245{
246 BMCWEB_ROUTE(app, "/redfish/")
Ed Tanousb41187f2019-10-24 16:30:02 -0700247 .methods(boost::beast::http::verb::get)(
Ed Tanousd3355c52022-05-11 14:40:49 -0700248 std::bind_front(redfishGet, std::ref(app)));
Ed Tanous8c623a92022-05-24 11:54:51 -0700249
Ed Tanousa529a6a2024-05-29 16:51:37 -0700250 BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/<str>/<str>")
251 .privileges(redfish::privileges::getJsonSchemaFile)
252 .methods(boost::beast::http::verb::get)(jsonSchemaGetFile);
253
Ed Tanous81d523a2022-05-25 12:00:51 -0700254 BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/<str>/")
Ed Tanous0ea4b4e2022-08-29 14:30:16 -0700255 .privileges(redfish::privileges::getJsonSchemaFileCollection)
Ed Tanous81d523a2022-05-25 12:00:51 -0700256 .methods(boost::beast::http::verb::get)(
257 std::bind_front(jsonSchemaGet, std::ref(app)));
258
259 BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/")
Ed Tanous0ea4b4e2022-08-29 14:30:16 -0700260 .privileges(redfish::privileges::getJsonSchemaFile)
Ed Tanous81d523a2022-05-25 12:00:51 -0700261 .methods(boost::beast::http::verb::get)(
262 std::bind_front(jsonSchemaIndexGet, std::ref(app)));
Ed Tanouse9dd1d32022-06-17 11:56:00 -0700263
264 // Note, this route must always be registered last
265 BMCWEB_ROUTE(app, "/redfish/<path>")
Ed Tanous0ea4b4e2022-08-29 14:30:16 -0700266 .notFound()
267 .privileges(redfish::privileges::privilegeSetLogin)(
268 std::bind_front(redfish404, std::ref(app)));
Ed Tanous44c70412022-07-31 16:48:29 -0700269
270 BMCWEB_ROUTE(app, "/redfish/<path>")
Ed Tanous0ea4b4e2022-08-29 14:30:16 -0700271 .methodNotAllowed()
272 .privileges(redfish::privileges::privilegeSetLogin)(
273 std::bind_front(redfish405, std::ref(app)));
Ed Tanous3dac7492017-08-02 13:46:20 -0700274}
Ed Tanousf65fca62022-05-24 12:49:41 -0700275
Ed Tanous1abe55e2018-09-05 08:30:59 -0700276} // namespace redfish