summaryrefslogtreecommitdiff
path: root/meta-openbmc-mods/meta-common/recipes-phosphor/interfaces/bmcweb/vm/0008-Return-404-for-POST-on-Proxy-InsertMedia.patch
blob: 2d9ab1dae12c7a6baad9f048c11a046de94a4818 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
From 793e4d6952b2a121da55d866eb510b26e179750c Mon Sep 17 00:00:00 2001
From: Przemyslaw Czarnowski <przemyslaw.hawrylewicz.czarnowski@intel.com>
Date: Fri, 4 Feb 2022 15:41:54 +0100
Subject: [PATCH] Return 404 for POST on Proxy InsertMedia

.../Actions/VirtualMedia.InsertMedia does not exist for proxy mode. But
it does for legacy, so handler needs to return 404 for proxy and run
mounting in legacy. Similar action has to be done for get, delete and
patch.

As the check shares the same codebase besides the action itself, this
patch creates generic function for parsing GetManagedObjects, finding
valid VirtualMedia endpoint and invoking specific action handler.

Tested:
Manually, with the following set of commands:
POST  /Proxy /InsertAction : returned 404
POST  /Legacy/InsertAction : returned 200 (mounted)
GET   /Proxy /InsertAction : returned 404
GET   /Legacy/InsertAction : returned 405
PATCH /Proxy /InsertAction : returned 404
PATCH /Legacy/InsertAction : returned 405
DELETE/Proxy /InsertAction : returned 404
DELETE/Legacy/InsertAction : returned 405

Signed-off-by: Przemyslaw Czarnowski <przemyslaw.hawrylewicz.czarnowski@intel.com>
---
 redfish-core/lib/virtual_media.hpp | 265 ++++++++++++-----------------
 1 file changed, 108 insertions(+), 157 deletions(-)

diff --git a/redfish-core/lib/virtual_media.hpp b/redfish-core/lib/virtual_media.hpp
index da66b4b..6dfc726 100644
--- a/redfish-core/lib/virtual_media.hpp
+++ b/redfish-core/lib/virtual_media.hpp
@@ -19,6 +19,7 @@
 #include <boost/container/flat_map.hpp>
 #include <boost/process/async_pipe.hpp>
 #include <boost/type_traits/has_dereference.hpp>
+#include <sdbusplus/message/native_types.hpp>
 #include <utils/json_utils.hpp>
 // for GetObjectType and ManagedObjectType
 
@@ -32,13 +33,14 @@ namespace redfish
 {
 
 /**
- * @brief Function checks if insert media request is Legacy or Proxy type
- *        and sets suitable response code for unsupported REST method.
+ * @brief Function parses getManagedObject response, finds item, makes generic
+ *        validation and invokes callback handler on this item.
  *
  */
-void checkProxyMode(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
-                    const crow::Request& req, const std::string& name,
-                    const std::string& resName)
+template <typename T>
+void findItemAndRunHandler(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
+                           const crow::Request& req, const std::string& name,
+                           const std::string& resName, T&& handler)
 {
     if (name != "bmc")
     {
@@ -48,8 +50,8 @@ void checkProxyMode(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
     }
 
     crow::connections::systemBus->async_method_call(
-        [aResp, req, resName](const boost::system::error_code ec,
-                              const GetObjectType& getObjectType) {
+        [aResp, req, resName, handler](const boost::system::error_code ec,
+                                       const GetObjectType& getObjectType) {
             if (ec)
             {
                 BMCWEB_LOG_ERROR << "ObjectMapper::GetObject call failed: "
@@ -70,9 +72,9 @@ void checkProxyMode(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
             BMCWEB_LOG_DEBUG << "GetObjectType: " << service;
 
             crow::connections::systemBus->async_method_call(
-                [service, resName, req,
-                 aResp](const boost::system::error_code ec,
-                        ManagedObjectType& subtree) {
+                [service, resName, req, aResp,
+                 handler](const boost::system::error_code ec,
+                          ManagedObjectType& subtree) {
                     if (ec)
                     {
                         BMCWEB_LOG_DEBUG << "DBUS response error";
@@ -105,26 +107,8 @@ void checkProxyMode(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
                             continue;
                         }
 
-                        // Check if dbus path is Legacy type
-                        if (mode.filename() == "Legacy")
-                        {
-                            BMCWEB_LOG_DEBUG << "InsertMedia only allowed "
-                                                "with POST method "
-                                                "in legacy mode";
-                            aResp->res.result(
-                                boost::beast::http::status::method_not_allowed);
-
-                            return;
-                        }
-                        // Check if dbus path is Proxy type
-                        if (mode.filename() == "Proxy")
+                        if (handler(service, aResp, item) == true)
                         {
-                            // Not possible in proxy mode
-                            BMCWEB_LOG_DEBUG << "InsertMedia not "
-                                                "allowed in proxy mode";
-                            aResp->res.result(
-                                boost::beast::http::status::not_found);
-
                             return;
                         }
                     }
@@ -289,10 +273,7 @@ static std::optional<uint64_t>
                    std::chrono::seconds(*timeoutValue + timeoutMarginSeconds))
             .count();
     }
-    else
-    {
-        return std::nullopt;
-    }
+    return std::nullopt;
 }
 
 /**
@@ -972,14 +953,43 @@ struct InsertMediaActionParams
 
 inline void requestNBDVirtualMediaRoutes(App& app)
 {
+    auto handler = []([[maybe_unused]] const std::string& service,
+                      const std::shared_ptr<bmcweb::AsyncResp>& aResp,
+                      std::pair<sdbusplus::message::object_path,
+                                DbusInterfaceType>& item) {
+        auto mode = item.first.parent_path();
+        auto type = mode.parent_path();
+        // Check if dbus path is Legacy type
+        if (mode.filename() == "Legacy")
+        {
+            BMCWEB_LOG_DEBUG << "InsertMedia only allowed "
+                                "with POST method "
+                                "in legacy mode";
+            aResp->res.result(boost::beast::http::status::method_not_allowed);
+
+            return true;
+        }
+        // Check if dbus path is Proxy type
+        if (mode.filename() == "Proxy")
+        {
+            // Not possible in proxy mode
+            BMCWEB_LOG_DEBUG << "InsertMedia not "
+                                "allowed in proxy mode";
+            aResp->res.result(boost::beast::http::status::not_found);
+
+            return true;
+        }
+        return false;
+    };
     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/VirtualMedia/<str>/Actions/"
                       "VirtualMedia.InsertMedia")
         .privileges({{"Login"}})
         .methods(boost::beast::http::verb::get)(
-            [](const crow::Request& req,
-               const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
-               const std::string& name, const std::string& resName) {
-                checkProxyMode(asyncResp, req, name, resName);
+            [handler](const crow::Request& req,
+                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+                      const std::string& name, const std::string& resName) {
+                findItemAndRunHandler(asyncResp, req, name, resName,
+                                      std::move(handler));
             });
 
     for (auto method :
@@ -991,10 +1001,12 @@ inline void requestNBDVirtualMediaRoutes(App& app)
                      "VirtualMedia.InsertMedia")
             .privileges({{"ConfigureManager"}})
             .methods(method)(
-                [](const crow::Request& req,
-                   const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
-                   const std::string& name, const std::string& resName) {
-                    checkProxyMode(asyncResp, req, name, resName);
+                [handler = std::move(handler)](
+                    const crow::Request& req,
+                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+                    const std::string& name, const std::string& resName) {
+                    findItemAndRunHandler(asyncResp, req, name, resName,
+                                          std::move(handler));
                 });
     }
 
@@ -1006,129 +1018,68 @@ inline void requestNBDVirtualMediaRoutes(App& app)
             [](const crow::Request& req,
                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
                const std::string& name, const std::string& resName) {
-                if (name != "bmc")
-                {
-                    messages::resourceNotFound(asyncResp->res,
-                                               "VirtualMedia.Insert", resName);
-
-                    return;
-                }
-                InsertMediaActionParams actionParams;
-
-                // Read obligatory parameters (url of
-                // image)
-                if (!json_util::readJson(
-                        req, asyncResp->res, "Image", actionParams.imageUrl,
-                        "WriteProtected", actionParams.writeProtected,
-                        "UserName", actionParams.userName, "Password",
-                        actionParams.password, "Inserted",
-                        actionParams.inserted, "TransferMethod",
-                        actionParams.transferMethod, "TransferProtocolType",
-                        actionParams.transferProtocolType))
-                {
-                    BMCWEB_LOG_DEBUG << "Image is not provided";
-                    return;
-                }
-
-                bool paramsValid = validateParams(
-                    asyncResp->res, actionParams.imageUrl,
-                    actionParams.inserted, actionParams.transferMethod,
-                    actionParams.transferProtocolType);
-
-                if (paramsValid == false)
-                {
-                    return;
-                }
-
-                crow::connections::systemBus->async_method_call(
-                    [asyncResp, actionParams,
-                     resName](const boost::system::error_code ec,
-                              const GetObjectType& getObjectType) mutable {
-                        if (ec)
+                // handle legacy mode (parse parameters and start action
+                // for proxy mode return 404.
+                auto handler =
+                    [req, resName](
+                        const std::string& service,
+                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+                        std::pair<sdbusplus::message::object_path,
+                                  DbusInterfaceType>& item) {
+                        auto mode = item.first.parent_path();
+                        auto type = mode.parent_path();
+                        if (mode.filename() == "Proxy")
                         {
-                            BMCWEB_LOG_ERROR
-                                << "ObjectMapper::GetObject call failed: "
-                                << ec;
-                            messages::internalError(asyncResp->res);
+                            // Not possible in proxy mode
+                            BMCWEB_LOG_DEBUG << "InsertMedia not "
+                                                "allowed in proxy mode";
+                            messages::resourceNotFound(
+                                asyncResp->res, "VirtualMedia.InsertMedia",
+                                resName);
 
-                            return;
+                            return true;
                         }
-                        std::string service = getObjectType.begin()->first;
-                        BMCWEB_LOG_DEBUG << "GetObjectType: " << service;
-
-                        crow::connections::systemBus->async_method_call(
-                            [service, resName, actionParams,
-                             asyncResp](const boost::system::error_code ec,
-                                        ManagedObjectType& subtree) mutable {
-                                if (ec)
-                                {
-                                    BMCWEB_LOG_DEBUG << "DBUS response error";
 
-                                    return;
-                                }
-
-                                for (const auto& object : subtree)
-                                {
-                                    const std::string& path =
-                                        static_cast<const std::string&>(
-                                            object.first);
-
-                                    std::size_t lastIndex = path.rfind('/');
-                                    if (lastIndex == std::string::npos)
-                                    {
-                                        continue;
-                                    }
-
-                                    lastIndex += 1;
-
-                                    if (path.substr(lastIndex) == resName)
-                                    {
-                                        lastIndex = path.rfind("Proxy");
-                                        if (lastIndex != std::string::npos)
-                                        {
-                                            // Not possible in proxy mode
-                                            BMCWEB_LOG_DEBUG
-                                                << "InsertMedia not "
-                                                   "allowed in proxy mode";
-                                            messages::resourceNotFound(
-                                                asyncResp->res,
-                                                "VirtualMedia.InsertMedia",
-                                                resName);
-
-                                            return;
-                                        }
+                        InsertMediaActionParams actionParams;
+
+                        // Read obligatory parameters (url of
+                        // image)
+                        if (!json_util::readJson(
+                                req, asyncResp->res, "Image",
+                                actionParams.imageUrl, "WriteProtected",
+                                actionParams.writeProtected, "UserName",
+                                actionParams.userName, "Password",
+                                actionParams.password, "Inserted",
+                                actionParams.inserted, "TransferMethod",
+                                actionParams.transferMethod,
+                                "TransferProtocolType",
+                                actionParams.transferProtocolType))
+                        {
+                            BMCWEB_LOG_DEBUG << "Image is not provided";
+                            return true;
+                        }
 
-                                        lastIndex = path.rfind("Legacy");
-                                        if (lastIndex == std::string::npos)
-                                        {
-                                            continue;
-                                        }
+                        bool paramsValid = validateParams(
+                            asyncResp->res, actionParams.imageUrl,
+                            actionParams.inserted, actionParams.transferMethod,
+                            actionParams.transferProtocolType);
 
-                                        // manager is irrelevant for
-                                        // VirtualMedia dbus calls
-                                        doMountVmLegacy(
-                                            asyncResp, service, resName,
-                                            actionParams.imageUrl,
-                                            !(*actionParams.writeProtected),
-                                            std::move(*actionParams.userName),
-                                            std::move(*actionParams.password));
+                        if (paramsValid == false)
+                        {
+                            return true;
+                        }
 
-                                        return;
-                                    }
-                                }
-                                BMCWEB_LOG_DEBUG << "Parent item not found";
-                                messages::resourceNotFound(
-                                    asyncResp->res, "VirtualMedia", resName);
-                            },
-                            service, "/xyz/openbmc_project/VirtualMedia",
-                            "org.freedesktop.DBus.ObjectManager",
-                            "GetManagedObjects");
-                    },
-                    "xyz.openbmc_project.ObjectMapper",
-                    "/xyz/openbmc_project/object_mapper",
-                    "xyz.openbmc_project.ObjectMapper", "GetObject",
-                    "/xyz/openbmc_project/VirtualMedia",
-                    std::array<const char*, 0>());
+                        // manager is irrelevant for
+                        // VirtualMedia dbus calls
+                        doMountVmLegacy(asyncResp, service, resName,
+                                        actionParams.imageUrl,
+                                        !(*actionParams.writeProtected),
+                                        std::move(*actionParams.userName),
+                                        std::move(*actionParams.password));
+
+                        return true;
+                    };
+                findItemAndRunHandler(asyncResp, req, name, resName, handler);
             });
 
     BMCWEB_ROUTE(
-- 
2.31.1