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