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