summaryrefslogtreecommitdiff
path: root/meta-openbmc-mods/meta-common/recipes-phosphor/interfaces/bmcweb/vm/0004-Invalid-status-code-from-InsertMedia-REST-methods.patch
blob: 3a9e672175613638b619cf85e2ecb2cdd7f152b2 (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
From 805641a2d189da9befc05168f51cef76da1ba326 Mon Sep 17 00:00:00 2001
From: Alicja Rybak <alicja.rybak@intel.com>
Date: Tue, 20 Apr 2021 16:32:37 +0200
Subject: [PATCH] Invalid status code from InsertMedia REST methods GET,
 PUT, DELETE, PATCH in proxy mode

Add handlers for GET, PUT, DELETE, PATCH method and function that
checks which mode is used and set suitable status code:
Not allowed for Legacy and Not found for Proxy.

Change-Id: Ib4c0a3e9a2a8853caa74c59239d9fcfed99c5e8b
Signed-off-by: Alicja Rybak <alicja.rybak@intel.com>
---
 redfish-core/lib/virtual_media.hpp | 155 +++++++++++++++++++++++++++++
 1 file changed, 155 insertions(+)

diff --git a/redfish-core/lib/virtual_media.hpp b/redfish-core/lib/virtual_media.hpp
index aa7c639..3e28164 100644
--- a/redfish-core/lib/virtual_media.hpp
+++ b/redfish-core/lib/virtual_media.hpp
@@ -548,6 +548,161 @@ class VirtualMediaActionInsertMedia : public Node
         return true;
     }
 
+    /**
+     * @brief Function checks if insert media request is Legacy or Proxy type
+     *        and sets suitable response code for unsupported REST method.
+     *
+     */
+    void CheckProxyMode(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
+                        const crow::Request& req,
+                        const std::vector<std::string>& params)
+    {
+        if (params.size() != 2)
+        {
+            messages::internalError(aResp->res);
+            return;
+        }
+
+        // take resource name from URL
+        const std::string& resName = params[1];
+
+        if (params[0] != "bmc")
+        {
+            messages::resourceNotFound(aResp->res, "VirtualMedia.Insert",
+                                       resName);
+
+            return;
+        }
+
+        crow::connections::systemBus->async_method_call(
+            [this, aResp{std::move(aResp)}, req,
+             resName](const boost::system::error_code ec,
+                      const GetObjectType& getObjectType) {
+                if (ec)
+                {
+                    BMCWEB_LOG_ERROR << "ObjectMapper::GetObject call failed: "
+                                     << ec;
+                    aResp->res.result(boost::beast::http::status::not_found);
+
+                    return;
+                }
+                std::string service = getObjectType.begin()->first;
+                BMCWEB_LOG_DEBUG << "GetObjectType: " << service;
+
+                crow::connections::systemBus->async_method_call(
+                    [this, service, resName, req,
+                     aResp{aResp}](const boost::system::error_code ec,
+                                   ManagedObjectType& subtree) {
+                        if (ec)
+                        {
+                            BMCWEB_LOG_DEBUG << "DBUS response error";
+
+                            return;
+                        }
+
+                        for (auto& item : subtree)
+                        {
+                            std::string thispath = item.first.filename();
+                            if (thispath.empty())
+                            {
+                                continue;
+                            }
+
+                            if (thispath != resName)
+                            {
+                                continue;
+                            }
+
+                            auto mode = item.first.parent_path();
+                            auto type = mode.parent_path();
+                            if (mode.filename().empty() ||
+                                type.filename().empty())
+                            {
+                                continue;
+                            }
+
+                            if (type.filename() != "VirtualMedia")
+                            {
+                                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")
+                            {
+                                // Not possible in proxy mode
+                                BMCWEB_LOG_DEBUG << "InsertMedia not "
+                                                    "allowed in proxy mode";
+                                aResp->res.result(
+                                    boost::beast::http::status::not_found);
+
+                                return;
+                            }
+                        }
+
+                        BMCWEB_LOG_DEBUG << "Parent item not found";
+                        aResp->res.result(
+                            boost::beast::http::status::not_found);
+                    },
+                    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>());
+    }
+
+    /**
+     * @brief Function handles GET method request.
+     */
+    void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+               const crow::Request& req,
+               const std::vector<std::string>& params) override
+    {
+        CheckProxyMode(asyncResp, req, params);
+    }
+
+    /**
+     * @brief Function handles PATCH method request.
+     */
+    void doPatch(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+                 const crow::Request& req,
+                 const std::vector<std::string>& params) override
+    {
+        CheckProxyMode(asyncResp, req, params);
+    }
+
+    /**
+     * @brief Function handles PUT method request.
+     */
+    void doPut(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+               const crow::Request& req,
+               const std::vector<std::string>& params) override
+    {
+        CheckProxyMode(asyncResp, req, params);
+    }
+
+    /**
+     * @brief Function handles DELETE method request.
+     */
+    void doDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+                  const crow::Request& req,
+                  const std::vector<std::string>& params) override
+    {
+        CheckProxyMode(asyncResp, req, params);
+    }
+
     /**
      * @brief Function handles POST method request.
      *
-- 
2.17.1