summaryrefslogtreecommitdiff
path: root/redfish-core/src/utils
AgeCommit message (Collapse)AuthorFilesLines
2024-03-28Create Redfish specific setProperty callEd Tanous1-0/+76
There are currently 78 sdbusplus::asio::setProperty calls in redfish-core. The error handler for nearly all of them looks something like: ``` if (ec) { const sd_bus_error* dbusError = msg.get_error(); if ((dbusError != nullptr) && (dbusError->name == std::string_view( "xyz.openbmc_project.Common.Error.InvalidArgument"))) { BMCWEB_LOG_WARNING("DBUS response error: {}", ec); messages::propertyValueIncorrect(asyncResp->res, "<PropertyName>", <PropertyValue>); return; } messages::internalError(asyncResp->res); return; } messages::success(asyncResp->res); ``` In some cases there are more errors handled that translate to more error messages, but the vast majority only handle InvalidArgument. Many of these, like the ones in account_service.hpp, do the error handling in a lambda, which causes readability problems. This commit starts to make things more consistent, and easier for trivial property sets. This commit invents a setDbusProperty method in the redfish namespace that tries to handle all DBus errors in a consistent manner. Looking for input on whether this will work before changing over the other 73 calls. Overall this is less code, fewer inline lambdas, and defaults that should work for MOST use cases of calling an OpenBMC daemon, and fall back to more generic errors when calling a "normal" dbus daemon. As part of this, I've ported over several examples. Some things that might be up in the air: 1. Do we always return 204 no_content on property sets? Today there's a mix of 200, with a Base::Success message, and 204, with an empty body. 2. Do all DBus response codes map to the same error? A majority are covered by xyz.openbmc_project.Common.Error.InvalidArgument, but there are likely differences. If we allow any daemon to return any return code, does that cause compatibility problems later? Tested: ``` curl -k --user "root:0penBmc" -H "Content-Type: application/json" -X PATCH -d '{"HostName":"openbmc@#"}' https://192.168.7.2/redfish/v1/Managers/bmc/EthernetInterfaces/eth0 ``` Returns the appropriate error in the response Base.1.16.0.PropertyValueIncorrect Change-Id: If033a1112ba516792c9386c997d090c8f9094f3a Signed-off-by: Ed Tanous <ed@tanous.net>
2024-03-28Add misc-include-cleanerEd Tanous2-1/+3
And fix the includes that are wrong. Note, there is a very large ignore list included in the .clang-tidy configcfile. These are things that clang-tidy doesn't yet handle well, like knowing about a details include. Change-Id: Ie3744f2c8cba68a8700b406449d6c2018a736952 Signed-off-by: Ed Tanous <ed@tanous.net>
2024-03-19Call systemd SetTime directlyEd Tanous1-3/+3
Internally inside phosphor-time-manager, the elapsed(uint64) dbus call just forwards the request directly to systemd after static casting to int64_t (signed). bmcweb should just call systemd directly, for several reasons. phosphor-timesyncd might block on other calls, given it's a single threaded blocking design, due to bugs like #264. Calling systemd directly means that calls that don't require phosphor networkd won't be blocked. Calling systemd directly allows bmcweb to drop some code that parses a date as int64_t, then converts it to uint64_t to fulfill the phosphor datetime interface. We can now keep int64_t all the way through. Calling systemd directly allows bmcweb to give a more specific error code in the case there NTP is enabled, registering a PropertyValueConflict error, instead of a 500 InternalError. Tested: Patching DateTime property with NTP enabled returns 400, PropertyValueConflict ``` curl -vvvv -k --user "root:0penBmc" -H "Content-Type: application/json" -X PATCH -d '{"DateTime":"2020-12-15T15:40:52+00:00"}' https://192.168.7.2/redfish/v1/Managers/bmc ``` Disabling NTP using the following command: ``` curl -vvvv -k --user "root:0penBmc" -H "Content-Type: application/json" -X PATCH -d '{"NTP":{"ProtocolEnabled":false}}' https://192.168.7.2/redfish/v1/Managers/bmc/NetworkProtocol ``` Allows the prior command to succeed. [1] https://github.com/openbmc/phosphor-time-manager/blob/5ce9ac0e56440312997b25771507585905e8b360/bmc_epoch.cpp#L126 Change-Id: I6fbb6f63e17de8ab847ca5ed4eadc2bd313586d2 Signed-off-by: Ed Tanous <ed@tanous.net>
2023-09-08Simplify datetime parsingEd Tanous1-0/+44
This code as it stands pulls in the full datetime library from boost, including io, and a bunch of timezone code. The bmc doesn't make use of any of this, so we can rely on a much simplified version. Unfortunately for us, gcc still doesn't implement the c++20 std::chrono::parse[1]. There is a reference library available from [2] that backports the parse function to compilers that don't yet support it, and is the basis for the libc++ version. This commit opts to copy in the header as-written, under the assumption that we will never need to pull in new versions of this library, and will move to the std ersion as soon as it's available in the next gcc version. This commit simplifies things down to improve compile times and binary size. It saves ~22KB of compressed binary size, or about 3%. Tested: Unit tests pass. Pretty good coverage. [1] https://en.cppreference.com/w/cpp/chrono/parse [2] https://github.com/HowardHinnant/date/blob/master/include/date/date.h Signed-off-by: Ed Tanous <edtanous@google.com> Change-Id: I706b91cc3d9df3f32068125bc47ff0c374eb8d87
2023-06-21json utils: add getEstimatedJsonSizeNan Zhou1-0/+49
Add a utility function which estimates the size of the JSON tree. It is used in the children change to limit the reponse size of expand query. Tested: 1. unit test passed; 2. tested on hardware, the following are real sizes and estimation ``` Real payload size, Estimation, query 15.69 KB, 10.21 KB, redfish/v1?$expand=.($levels=1) 95.76 KB, 62.11 KB, redfish/v1?$expand=.($levels=2) 117.14 KB, 72.71 KB, redfish/v1?$expand=.($levels=3) 127.65 KB, 77.64 KB, redfish/v1?$expand=.($levels=4) ``` Signed-off-by: Nan Zhou <nanzhoumails@gmail.com> Change-Id: Iae26d6732a6ec63ecc59eacf657b4bf33c07c046
2023-03-11Remove body member from RequestEd Tanous1-1/+1
Per cpp core guidelines, these should be methods. Tested: on last patchset of the series. Signed-off-by: Ed Tanous <edtanous@google.com> Change-Id: Ib16479db9d2b68da68e7ad6e825c7e205c64f1de
2023-02-17Add option for validating content-type headerEd Tanous1-3/+13
For systems implementing to the OWASP security guidelines[1] (of which all should ideally) we should be checking the content-type header all times that we parse a request as JSON. This commit adds an option for parsing content-type, and sets a default of "must get content-type". Ideally this would not be a breaking change, but given the number of guides and scripts that omit the content type, it seems worthwhile to add a trapdoor, such that people can opt into their own model on how they would like to see this checking work. Tested: ``` curl --insecure -H "Content-Type: application/json" -X POST -D headers.txt https://${bmc}/redfish/v1/SessionService/Sessions -d '{"UserName":"root", "Password":"0penBmc"}' ``` Succeeds. Removing Content-Type argument causes bmc to return Base.1.13.0.UnrecognizedRequestBody. [1] cheatsheetseries.owasp.org/cheatsheets/REST_Security_Cheat_Sheet.html Change-Id: Iaa47dd563b40036ff2fc2cacb70d941fd8853038 Signed-off-by: Ed Tanous <edtanous@google.com>
2018-10-23Improve the Redfish error reporting interfaceJason M. Bills1-481/+1
Makes the Redfish error reporting interface automatically handle setting the http status and JSON content in the response object. When using an AsyncResp object, this allows for simply calling the Redfish error and returning. Change-Id: Icfdce2de763225f070e8dd61e591f296703f46bb Signed-off-by: Jason M. Bills <jason.m.bills@linux.intel.com>
2018-10-22Implement a new way of unpacking json to structsEd Tanous1-3/+0
The existing way of decoding json structures, while fast has some disadvantages. 1. it's very verbose to write. 2. It requires in depth knowlege of redfish error messages to get correct. 3. It _can_ lead to undesired behavior, like half of a patch being applied, if only some of the values have bad types. This commit implements a new interface for decoding redfish json named.... readJson. It is a templated function, that lets you decode json values based on type easily, while still handling all the correct error codes that were handled previously. Use is done similar to the example below: std::string required; boost::optional<std::string> optional; if (!json_util::readJson(req, res, "OptionalParam", optional, "RequiredParam", required)) { return; } if (optional){ // optional param was given, take action. } As part of this patchset, the systems schema is moved to the new interface, which deletes some of the code involved and shows the improvement in clarity. Change-Id: I041a97c84d294df8cd4de4c2702e5ee22c0bc120 Signed-off-by: Ed Tanous <ed.tanous@intel.com>
2018-09-05Move to clang-format-6.0Ed Tanous1-299/+371
This commit moves the codebase to the lastest clang-format file from upstream, as well as clang-format-6.0. Change-Id: Ice8313468097c0c42317fbb9e10ddf036e8cff4c Signed-off-by: Ed Tanous <ed.tanous@intel.com>
2018-07-27Move over to upstream c++ styleEd Tanous1-2/+2
This patchset moves bmcweb over to the upstream style naming conventions for variables, classes, and functions, as well as imposes the latest clang-format file. This changeset was mostly built automatically by the included .clang-tidy file, which has the ability to autoformat and auto rename variables. At some point in the future I would like to see this in greater use, but for now, we will impose it on bmcweb, and see how it goes. Tested: Code still compiles, and appears to run, although other issues are possible and likely. Change-Id: If422a2e36df924e897736b3feffa89f411d9dac1 Signed-off-by: Ed Tanous <ed.tanous@intel.com>
2018-06-29Boost beastEd Tanous1-2/+1
This commit is the beginings of attempting to transition away from crow, and toward boost::beast. Unit tests are passing, and implementation appears to be slightly faster than crow. Change-Id: Ic8d946dc7a04f514c67b1098f181eee1ced69171
2018-06-29Added JSON utilities to allow easy exception-less usage of nlohmann JSON.Kowalski, Kamil1-0/+454
These functions are not yet used, but will be required by at least two upcoming patchsets. This functionality has been cut out from Configuration patchset for easier merge without having to wait for that commit. Change-Id: Ibe5d5cefd874d4a2d896b42a2b7cfc17480f3c5a Signed-off-by: Kowalski, Kamil <kamil.kowalski@intel.com>