Feras Aldahlawi | 735ef6d | 2021-03-19 14:01:46 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 3 | #include "app.hpp" |
| 4 | #include "async_resp.hpp" |
| 5 | #include "dbus_utility.hpp" |
| 6 | #include "error_messages.hpp" |
| 7 | #include "utils/collection.hpp" |
| 8 | #include "utils/hex_utils.hpp" |
| 9 | #include "utils/json_utils.hpp" |
| 10 | |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 11 | #include <boost/system/error_code.hpp> |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame^] | 12 | #include <boost/url/format.hpp> |
Feras Aldahlawi | 735ef6d | 2021-03-19 14:01:46 -0700 | [diff] [blame] | 13 | #include <nlohmann/json.hpp> |
Vidya Satyamsetti | 4cee35e | 2022-04-21 14:53:44 -0700 | [diff] [blame] | 14 | |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 15 | #include <array> |
| 16 | #include <string_view> |
Vidya Satyamsetti | 4cee35e | 2022-04-21 14:53:44 -0700 | [diff] [blame] | 17 | #include <vector> |
Feras Aldahlawi | 735ef6d | 2021-03-19 14:01:46 -0700 | [diff] [blame] | 18 | |
| 19 | namespace crow |
| 20 | { |
| 21 | namespace google_api |
| 22 | { |
Vidya Satyamsetti | 4cee35e | 2022-04-21 14:53:44 -0700 | [diff] [blame] | 23 | |
Nan Zhou | 30aacdd | 2022-07-03 05:02:36 +0000 | [diff] [blame] | 24 | inline void |
| 25 | handleGoogleV1Get(const crow::Request& /*req*/, |
| 26 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Vidya Satyamsetti | 4cee35e | 2022-04-21 14:53:44 -0700 | [diff] [blame] | 27 | { |
| 28 | asyncResp->res.jsonValue["@odata.type"] = |
| 29 | "#GoogleServiceRoot.v1_0_0.GoogleServiceRoot"; |
| 30 | asyncResp->res.jsonValue["@odata.id"] = "/google/v1"; |
| 31 | asyncResp->res.jsonValue["Id"] = "Google Rest RootService"; |
| 32 | asyncResp->res.jsonValue["Name"] = "Google Service Root"; |
| 33 | asyncResp->res.jsonValue["Version"] = "1.0.0"; |
| 34 | asyncResp->res.jsonValue["RootOfTrustCollection"]["@odata.id"] = |
Nan Zhou | cd02759 | 2022-07-03 05:18:29 +0000 | [diff] [blame] | 35 | "/google/v1/RootOfTrustCollection"; |
Vidya Satyamsetti | 4cee35e | 2022-04-21 14:53:44 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Nan Zhou | 30aacdd | 2022-07-03 05:02:36 +0000 | [diff] [blame] | 38 | inline void handleRootOfTrustCollectionGet( |
Vidya Satyamsetti | 4cee35e | 2022-04-21 14:53:44 -0700 | [diff] [blame] | 39 | const crow::Request& /*req*/, |
| 40 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
| 41 | { |
Nan Zhou | cd02759 | 2022-07-03 05:18:29 +0000 | [diff] [blame] | 42 | asyncResp->res.jsonValue["@odata.id"] = "/google/v1/RootOfTrustCollection"; |
Vidya Satyamsetti | 4cee35e | 2022-04-21 14:53:44 -0700 | [diff] [blame] | 43 | asyncResp->res.jsonValue["@odata.type"] = |
| 44 | "#RootOfTrustCollection.RootOfTrustCollection"; |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 45 | const std::array<std::string_view, 1> interfaces{ |
| 46 | "xyz.openbmc_project.Control.Hoth"}; |
Vidya Satyamsetti | 4cee35e | 2022-04-21 14:53:44 -0700 | [diff] [blame] | 47 | redfish::collection_util::getCollectionMembers( |
Willy Tu | ae9031f | 2022-09-27 05:48:07 +0000 | [diff] [blame] | 48 | asyncResp, boost::urls::url("/google/v1/RootOfTrustCollection"), |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 49 | interfaces, "/xyz/openbmc_project"); |
Vidya Satyamsetti | 4cee35e | 2022-04-21 14:53:44 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | // Helper struct to identify a resolved D-Bus object interface |
| 53 | struct ResolvedEntity |
| 54 | { |
| 55 | std::string id; |
| 56 | std::string service; |
| 57 | std::string object; |
Nan Zhou | 16a5535 | 2022-07-03 05:06:08 +0000 | [diff] [blame] | 58 | std::string interface; |
Vidya Satyamsetti | 4cee35e | 2022-04-21 14:53:44 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | using ResolvedEntityHandler = std::function<void( |
| 62 | const std::string&, const std::shared_ptr<bmcweb::AsyncResp>&, |
| 63 | const ResolvedEntity&)>; |
| 64 | |
Nan Zhou | 30aacdd | 2022-07-03 05:02:36 +0000 | [diff] [blame] | 65 | inline void hothGetSubtreeCallback( |
| 66 | const std::string& command, |
| 67 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 68 | const std::string& rotId, const ResolvedEntityHandler& entityHandler, |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 69 | const boost::system::error_code& ec, |
Nan Zhou | 30aacdd | 2022-07-03 05:02:36 +0000 | [diff] [blame] | 70 | const dbus::utility::MapperGetSubTreeResponse& subtree) |
| 71 | { |
| 72 | if (ec) |
| 73 | { |
| 74 | redfish::messages::internalError(asyncResp->res); |
| 75 | return; |
| 76 | } |
Nan Zhou | 5600f02 | 2022-07-03 16:22:38 +0000 | [diff] [blame] | 77 | for (const auto& [path, services] : subtree) |
Nan Zhou | 30aacdd | 2022-07-03 05:02:36 +0000 | [diff] [blame] | 78 | { |
Nan Zhou | 5600f02 | 2022-07-03 16:22:38 +0000 | [diff] [blame] | 79 | sdbusplus::message::object_path objPath(path); |
| 80 | if (objPath.filename() != rotId || services.empty()) |
Nan Zhou | 30aacdd | 2022-07-03 05:02:36 +0000 | [diff] [blame] | 81 | { |
| 82 | continue; |
| 83 | } |
| 84 | |
Nan Zhou | cd02759 | 2022-07-03 05:18:29 +0000 | [diff] [blame] | 85 | ResolvedEntity resolvedEntity = { |
| 86 | .id = rotId, |
Nan Zhou | 5600f02 | 2022-07-03 16:22:38 +0000 | [diff] [blame] | 87 | .service = services[0].first, |
| 88 | .object = path, |
Nan Zhou | cd02759 | 2022-07-03 05:18:29 +0000 | [diff] [blame] | 89 | .interface = "xyz.openbmc_project.Control.Hoth"}; |
Nan Zhou | 30aacdd | 2022-07-03 05:02:36 +0000 | [diff] [blame] | 90 | entityHandler(command, asyncResp, resolvedEntity); |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | // Couldn't find an object with that name. return an error |
Jiaqing Zhao | d8a5d5d | 2022-08-05 16:21:51 +0800 | [diff] [blame] | 95 | redfish::messages::resourceNotFound(asyncResp->res, "RootOfTrust", rotId); |
Nan Zhou | 30aacdd | 2022-07-03 05:02:36 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Vidya Satyamsetti | 4cee35e | 2022-04-21 14:53:44 -0700 | [diff] [blame] | 98 | inline void resolveRoT(const std::string& command, |
| 99 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 100 | const std::string& rotId, |
| 101 | ResolvedEntityHandler&& entityHandler) |
| 102 | { |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 103 | constexpr std::array<std::string_view, 1> hothIfaces = { |
Nan Zhou | cd02759 | 2022-07-03 05:18:29 +0000 | [diff] [blame] | 104 | "xyz.openbmc_project.Control.Hoth"}; |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 105 | dbus::utility::getSubTree( |
| 106 | "/xyz/openbmc_project", 0, hothIfaces, |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 107 | [command, asyncResp, rotId, |
| 108 | entityHandler{std::forward<ResolvedEntityHandler>(entityHandler)}]( |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 109 | const boost::system::error_code& ec, |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 110 | const dbus::utility::MapperGetSubTreeResponse& subtree) { |
Nan Zhou | 30aacdd | 2022-07-03 05:02:36 +0000 | [diff] [blame] | 111 | hothGetSubtreeCallback(command, asyncResp, rotId, entityHandler, ec, |
| 112 | subtree); |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 113 | }); |
Vidya Satyamsetti | 4cee35e | 2022-04-21 14:53:44 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | inline void populateRootOfTrustEntity( |
| 117 | const std::string& /*unused*/, |
| 118 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 119 | const ResolvedEntity& resolvedEntity) |
| 120 | { |
| 121 | asyncResp->res.jsonValue["@odata.type"] = "#RootOfTrust.v1_0_0.RootOfTrust"; |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame^] | 122 | asyncResp->res.jsonValue["@odata.id"] = boost::urls::format( |
| 123 | "/google/v1/RootOfTrustCollection/{}", resolvedEntity.id); |
Vidya Satyamsetti | 4cee35e | 2022-04-21 14:53:44 -0700 | [diff] [blame] | 124 | |
| 125 | asyncResp->res.jsonValue["Status"]["State"] = "Enabled"; |
| 126 | asyncResp->res.jsonValue["Id"] = resolvedEntity.id; |
| 127 | // Need to fix this later to a stabler property. |
| 128 | asyncResp->res.jsonValue["Name"] = resolvedEntity.id; |
| 129 | asyncResp->res.jsonValue["Description"] = "Google Root Of Trust"; |
| 130 | asyncResp->res.jsonValue["Actions"]["#RootOfTrust.SendCommand"]["target"] = |
| 131 | "/google/v1/RootOfTrustCollection/" + resolvedEntity.id + |
| 132 | "/Actions/RootOfTrust.SendCommand"; |
| 133 | |
| 134 | asyncResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] = |
| 135 | resolvedEntity.id; |
| 136 | asyncResp->res.jsonValue["Location"]["PartLocation"]["LocationType"] = |
| 137 | "Embedded"; |
| 138 | } |
| 139 | |
Nan Zhou | 30aacdd | 2022-07-03 05:02:36 +0000 | [diff] [blame] | 140 | inline void |
| 141 | handleRootOfTrustGet(const crow::Request& /*req*/, |
| 142 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 143 | const std::string& param) |
Vidya Satyamsetti | 4cee35e | 2022-04-21 14:53:44 -0700 | [diff] [blame] | 144 | { |
Nan Zhou | 30aacdd | 2022-07-03 05:02:36 +0000 | [diff] [blame] | 145 | std::string emptyCommand; |
| 146 | resolveRoT(emptyCommand, asyncResp, param, populateRootOfTrustEntity); |
| 147 | } |
| 148 | |
| 149 | inline void |
| 150 | invocationCallback(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 151 | const boost::system::error_code& ec, |
Nan Zhou | 30aacdd | 2022-07-03 05:02:36 +0000 | [diff] [blame] | 152 | const std::vector<uint8_t>& responseBytes) |
| 153 | { |
| 154 | if (ec) |
| 155 | { |
| 156 | BMCWEB_LOG_ERROR << "RootOfTrust.Actions.SendCommand failed: " |
| 157 | << ec.message(); |
| 158 | redfish::messages::internalError(asyncResp->res); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | asyncResp->res.jsonValue["CommandResponse"] = |
| 163 | bytesToHexString(responseBytes); |
Vidya Satyamsetti | 4cee35e | 2022-04-21 14:53:44 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | inline void |
| 167 | invokeRoTCommand(const std::string& command, |
| 168 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 169 | const ResolvedEntity& resolvedEntity) |
| 170 | { |
Vidya Satyamsetti | 4cee35e | 2022-04-21 14:53:44 -0700 | [diff] [blame] | 171 | std::vector<uint8_t> bytes = hexStringToBytes(command); |
| 172 | if (bytes.empty()) |
| 173 | { |
| 174 | BMCWEB_LOG_DEBUG << "Invalid command: " << command; |
| 175 | redfish::messages::actionParameterValueTypeError(command, "Command", |
| 176 | "SendCommand"); |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 181 | [asyncResp{asyncResp}](const boost::system::error_code& ec, |
Nan Zhou | 30aacdd | 2022-07-03 05:02:36 +0000 | [diff] [blame] | 182 | const std::vector<uint8_t>& responseBytes) { |
| 183 | invocationCallback(asyncResp, ec, responseBytes); |
| 184 | }, |
| 185 | resolvedEntity.service, resolvedEntity.object, resolvedEntity.interface, |
| 186 | "SendHostCommand", bytes); |
Vidya Satyamsetti | 4cee35e | 2022-04-21 14:53:44 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Nan Zhou | 30aacdd | 2022-07-03 05:02:36 +0000 | [diff] [blame] | 189 | inline void handleRoTSendCommandPost( |
| 190 | const crow::Request& request, |
| 191 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 192 | const std::string& rotId) |
Vidya Satyamsetti | 4cee35e | 2022-04-21 14:53:44 -0700 | [diff] [blame] | 193 | { |
| 194 | std::string command; |
| 195 | if (!redfish::json_util::readJsonAction(request, asyncResp->res, "Command", |
| 196 | command)) |
| 197 | { |
| 198 | BMCWEB_LOG_DEBUG << "Missing property Command."; |
| 199 | redfish::messages::actionParameterMissing(asyncResp->res, "SendCommand", |
| 200 | "Command"); |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | resolveRoT(command, asyncResp, rotId, invokeRoTCommand); |
| 205 | } |
Feras Aldahlawi | 735ef6d | 2021-03-19 14:01:46 -0700 | [diff] [blame] | 206 | |
| 207 | inline void requestRoutes(App& app) |
| 208 | { |
| 209 | BMCWEB_ROUTE(app, "/google/v1/") |
Nan Zhou | 30aacdd | 2022-07-03 05:02:36 +0000 | [diff] [blame] | 210 | .methods(boost::beast::http::verb::get)(handleGoogleV1Get); |
Vidya Satyamsetti | 4cee35e | 2022-04-21 14:53:44 -0700 | [diff] [blame] | 211 | |
| 212 | BMCWEB_ROUTE(app, "/google/v1/RootOfTrustCollection") |
| 213 | .privileges({{"ConfigureManager"}}) |
Nan Zhou | 30aacdd | 2022-07-03 05:02:36 +0000 | [diff] [blame] | 214 | .methods(boost::beast::http::verb::get)(handleRootOfTrustCollectionGet); |
Vidya Satyamsetti | 4cee35e | 2022-04-21 14:53:44 -0700 | [diff] [blame] | 215 | |
| 216 | BMCWEB_ROUTE(app, "/google/v1/RootOfTrustCollection/<str>") |
| 217 | .privileges({{"ConfigureManager"}}) |
Nan Zhou | 30aacdd | 2022-07-03 05:02:36 +0000 | [diff] [blame] | 218 | .methods(boost::beast::http::verb::get)(handleRootOfTrustGet); |
Vidya Satyamsetti | 4cee35e | 2022-04-21 14:53:44 -0700 | [diff] [blame] | 219 | |
| 220 | BMCWEB_ROUTE( |
| 221 | app, |
| 222 | "/google/v1/RootOfTrustCollection/<str>/Actions/RootOfTrust.SendCommand") |
| 223 | .privileges({{"ConfigureManager"}}) |
Nan Zhou | 30aacdd | 2022-07-03 05:02:36 +0000 | [diff] [blame] | 224 | .methods(boost::beast::http::verb::post)(handleRoTSendCommandPost); |
Feras Aldahlawi | 735ef6d | 2021-03-19 14:01:46 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | } // namespace google_api |
| 228 | } // namespace crow |