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