blob: d6a9d82846e6342706ce365d9b01ac2c04257b0e [file] [log] [blame]
Ed Tanous911ac312017-08-15 09:37:42 -07001#pragma once
2
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08003#include "app.hpp"
Ed Tanous81d523a2022-05-25 12:00:51 -07004#include "error_messages.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08005#include "http_request.hpp"
6#include "http_response.hpp"
7#include "query.hpp"
8#include "registries/privilege_registry.hpp"
Ed Tanous81d523a2022-05-25 12:00:51 -07009#include "utility.hpp"
10
Ed Tanousef4c65b2023-04-24 15:28:50 -070011#include <boost/url/format.hpp>
12
Ed Tanous3544d2a2023-08-06 18:12:20 -070013#include <ranges>
Ed Tanousa8c4ce92021-03-24 08:44:51 -070014#include <string>
Ed Tanous1abe55e2018-09-05 08:30:59 -070015
Ed Tanous1abe55e2018-09-05 08:30:59 -070016namespace redfish
17{
Ed Tanousa8c4ce92021-03-24 08:44:51 -070018
Ed Tanousd3355c52022-05-11 14:40:49 -070019inline void redfishGet(App& app, const crow::Request& req,
20 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
21{
Carson Labrado3ba00072022-06-06 19:40:56 +000022 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanousd3355c52022-05-11 14:40:49 -070023 {
24 return;
25 }
26 asyncResp->res.jsonValue["v1"] = "/redfish/v1/";
27}
28
Ed Tanous8c623a92022-05-24 11:54:51 -070029inline 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 Zhou686b7092022-06-15 20:02:27 +000037 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous8c623a92022-05-24 11:54:51 -070038 {
39 return;
40 }
41
Ed Tanous62598e32023-07-17 17:06:25 -070042 BMCWEB_LOG_WARNING("404 on path {}", path);
Ed Tanous8c623a92022-05-24 11:54:51 -070043
Ed Tanous39662a32023-02-06 15:09:46 -080044 std::string name = req.url().segments().back();
Ed Tanous8c623a92022-05-24 11:54:51 -070045 // 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 Tanous079360a2022-06-29 10:05:19 -070048 messages::resourceNotFound(asyncResp->res, "", name);
Ed Tanous8c623a92022-05-24 11:54:51 -070049}
50
Ed Tanous44c70412022-07-31 16:48:29 -070051inline 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 Tanous62598e32023-07-17 17:06:25 -070062 BMCWEB_LOG_WARNING("405 on path {}", path);
Ed Tanous44c70412022-07-31 16:48:29 -070063 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 Tanous81d523a2022-05-25 12:00:51 -070074inline 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 Tanous81d523a2022-05-25 12:00:51 -070084 json["@odata.type"] = "#JsonSchemaFileCollection.JsonSchemaFileCollection";
85 json["Name"] = "JsonSchemaFile Collection";
86 json["Description"] = "Collection of JsonSchemaFiles";
87 nlohmann::json::array_t members;
Ed Tanousa529a6a2024-05-29 16:51:37 -070088
89 std::error_code ec;
90 std::filesystem::directory_iterator dirList(
91 "/usr/share/www/redfish/v1/JsonSchemas", ec);
92 if (ec)
Ed Tanous81d523a2022-05-25 12:00:51 -070093 {
Ed Tanousa529a6a2024-05-29 16:51:37 -070094 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 Tanous81d523a2022-05-25 12:00:51 -0700106 nlohmann::json::object_t member;
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400107 member["@odata.id"] =
108 boost::urls::format("/redfish/v1/JsonSchemas/{}", split[0]);
Patrick Williamsad539542023-05-12 10:10:08 -0500109 members.emplace_back(std::move(member));
Ed Tanous81d523a2022-05-25 12:00:51 -0700110 }
Ed Tanousa529a6a2024-05-29 16:51:37 -0700111
112 json["Members@odata.count"] = members.size();
Ed Tanous81d523a2022-05-25 12:00:51 -0700113 json["Members"] = std::move(members);
Ed Tanous81d523a2022-05-25 12:00:51 -0700114}
115
116inline 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 Tanousa529a6a2024-05-29 16:51:37 -0700125 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 Williamsbd79bce2024-08-16 15:22:20 -0400149 json["@odata.id"] =
150 boost::urls::format("/redfish/v1/JsonSchemas/{}", schema);
Ed Tanousa529a6a2024-05-29 16:51:37 -0700151 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
179inline 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 Tanous81d523a2022-05-25 12:00:51 -0700202 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800203 messages::resourceNotFound(asyncResp->res, "JsonSchemaFile", schema);
Ed Tanous81d523a2022-05-25 12:00:51 -0700204 return;
205 }
206
Ed Tanousa529a6a2024-05-29 16:51:37 -0700207 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 Tanous81d523a2022-05-25 12:00:51 -0700214
Ed Tanousa529a6a2024-05-29 16:51:37 -0700215 messages::resourceNotFound(asyncResp->res, "JsonSchemaFile", schema);
Ed Tanous81d523a2022-05-25 12:00:51 -0700216}
217
Ed Tanousf65fca62022-05-24 12:49:41 -0700218inline void requestRoutesRedfish(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700219{
220 BMCWEB_ROUTE(app, "/redfish/")
Ed Tanousb41187f2019-10-24 16:30:02 -0700221 .methods(boost::beast::http::verb::get)(
Ed Tanousd3355c52022-05-11 14:40:49 -0700222 std::bind_front(redfishGet, std::ref(app)));
Ed Tanous8c623a92022-05-24 11:54:51 -0700223
Ed Tanousa529a6a2024-05-29 16:51:37 -0700224 BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/<str>/<str>")
225 .privileges(redfish::privileges::getJsonSchemaFile)
226 .methods(boost::beast::http::verb::get)(jsonSchemaGetFile);
227
Ed Tanous81d523a2022-05-25 12:00:51 -0700228 BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/<str>/")
Ed Tanous0ea4b4e2022-08-29 14:30:16 -0700229 .privileges(redfish::privileges::getJsonSchemaFileCollection)
Ed Tanous81d523a2022-05-25 12:00:51 -0700230 .methods(boost::beast::http::verb::get)(
231 std::bind_front(jsonSchemaGet, std::ref(app)));
232
233 BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/")
Ed Tanous0ea4b4e2022-08-29 14:30:16 -0700234 .privileges(redfish::privileges::getJsonSchemaFile)
Ed Tanous81d523a2022-05-25 12:00:51 -0700235 .methods(boost::beast::http::verb::get)(
236 std::bind_front(jsonSchemaIndexGet, std::ref(app)));
Ed Tanouse9dd1d32022-06-17 11:56:00 -0700237
238 // Note, this route must always be registered last
239 BMCWEB_ROUTE(app, "/redfish/<path>")
Ed Tanous0ea4b4e2022-08-29 14:30:16 -0700240 .notFound()
241 .privileges(redfish::privileges::privilegeSetLogin)(
242 std::bind_front(redfish404, std::ref(app)));
Ed Tanous44c70412022-07-31 16:48:29 -0700243
244 BMCWEB_ROUTE(app, "/redfish/<path>")
Ed Tanous0ea4b4e2022-08-29 14:30:16 -0700245 .methodNotAllowed()
246 .privileges(redfish::privileges::privilegeSetLogin)(
247 std::bind_front(redfish405, std::ref(app)));
Ed Tanous3dac7492017-08-02 13:46:20 -0700248}
Ed Tanousf65fca62022-05-24 12:49:41 -0700249
Ed Tanous1abe55e2018-09-05 08:30:59 -0700250} // namespace redfish