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