| 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 |
| Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 3 | #pragma once |
| 4 | |
| Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 5 | #include "event_service_store.hpp" |
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 6 | #include "logging.hpp" |
| Ed Tanous | 2c6ffdb | 2023-06-28 11:28:38 -0700 | [diff] [blame] | 7 | #include "ossl_random.hpp" |
| Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 8 | #include "sessions.hpp" |
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 9 | // NOLINTNEXTLINE(misc-include-cleaner) |
| 10 | #include "utility.hpp" |
| Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 11 | |
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 12 | #include <boost/beast/core/file_base.hpp> |
| Ed Tanous | c282e8b | 2024-07-01 08:56:34 -0700 | [diff] [blame] | 13 | #include <boost/beast/core/file_posix.hpp> |
| Ed Tanous | 601c71a | 2021-09-08 16:40:12 -0700 | [diff] [blame] | 14 | #include <boost/beast/http/fields.hpp> |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 15 | #include <nlohmann/json.hpp> |
| Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 16 | |
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 17 | #include <chrono> |
| Ed Tanous | a170052 | 2024-05-20 18:15:36 -0700 | [diff] [blame^] | 18 | #include <cstddef> |
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 19 | #include <cstdint> |
| Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 20 | #include <filesystem> |
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 21 | #include <memory> |
| 22 | #include <optional> |
| 23 | #include <string> |
| Ed Tanous | c282e8b | 2024-07-01 08:56:34 -0700 | [diff] [blame] | 24 | #include <system_error> |
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 25 | #include <utility> |
| Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 26 | |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 27 | namespace persistent_data |
| 28 | { |
| Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 29 | |
| Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 30 | class ConfigFile |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 31 | { |
| Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 32 | uint64_t jsonRevision = 1; |
| Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 33 | |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 34 | public: |
| Ed Tanous | 1388045 | 2025-09-17 09:23:26 -0700 | [diff] [blame] | 35 | static std::string getStateFile() |
| 36 | { |
| 37 | // NOLINTNEXTLINE(concurrency-mt-unsafe) |
| 38 | const char* stateDir = std::getenv("STATE_DIRECTORY"); |
| 39 | if (stateDir == nullptr) |
| 40 | { |
| 41 | stateDir = "."; |
| 42 | } |
| 43 | return std::string(stateDir) + "/bmcweb_persistent_data.json"; |
| 44 | } |
| 45 | |
| 46 | static const std::string& filename() |
| 47 | { |
| 48 | const static std::string fname = getStateFile(); |
| 49 | return fname; |
| 50 | } |
| Ratan Gupta | 845cb7d | 2019-07-12 00:32:25 +0530 | [diff] [blame] | 51 | |
| Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 52 | ConfigFile() |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 53 | { |
| 54 | readData(); |
| Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 55 | } |
| Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 56 | |
| Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 57 | ~ConfigFile() |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 58 | { |
| Gunnar Mills | 83cf818 | 2020-11-11 15:37:34 -0600 | [diff] [blame] | 59 | // Make sure we aren't writing stale sessions |
| 60 | persistent_data::SessionStore::getInstance().applySessionTimeouts(); |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 61 | if (persistent_data::SessionStore::getInstance().needsWrite()) |
| 62 | { |
| 63 | writeData(); |
| Kowalski, Kamil | 5cef0f7 | 2018-02-15 15:26:51 +0100 | [diff] [blame] | 64 | } |
| Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 65 | } |
| Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 66 | |
| Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 67 | ConfigFile(const ConfigFile&) = delete; |
| 68 | ConfigFile(ConfigFile&&) = delete; |
| 69 | ConfigFile& operator=(const ConfigFile&) = delete; |
| 70 | ConfigFile& operator=(ConfigFile&&) = delete; |
| 71 | |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 72 | // TODO(ed) this should really use protobuf, or some other serialization |
| 73 | // library, but adding another dependency is somewhat outside the scope of |
| 74 | // this application for the moment |
| 75 | void readData() |
| 76 | { |
| Ed Tanous | a170052 | 2024-05-20 18:15:36 -0700 | [diff] [blame^] | 77 | boost::beast::file_posix persistentFile; |
| 78 | boost::system::error_code ec; |
| 79 | const std::string& file = filename(); |
| 80 | persistentFile.open(file.c_str(), boost::beast::file_mode::read, ec); |
| Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 81 | uint64_t fileRevision = 0; |
| Ed Tanous | a170052 | 2024-05-20 18:15:36 -0700 | [diff] [blame^] | 82 | if (!ec) |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 83 | { |
| Ed Tanous | a170052 | 2024-05-20 18:15:36 -0700 | [diff] [blame^] | 84 | uint64_t size = persistentFile.size(ec); |
| 85 | if (ec) |
| 86 | { |
| 87 | BMCWEB_LOG_CRITICAL("Can't get filesize of {}", file); |
| 88 | return; |
| 89 | } |
| 90 | std::string str; |
| 91 | str.resize(static_cast<size_t>(size), '\0'); |
| 92 | persistentFile.read(str.data(), str.size(), ec); |
| 93 | if (ec) |
| 94 | { |
| 95 | BMCWEB_LOG_CRITICAL("Failed to read file {}", file); |
| 96 | return; |
| 97 | } |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 98 | // call with exceptions disabled |
| Ed Tanous | a170052 | 2024-05-20 18:15:36 -0700 | [diff] [blame^] | 99 | auto data = nlohmann::json::parse(str, nullptr, false); |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 100 | if (data.is_discarded()) |
| 101 | { |
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 102 | BMCWEB_LOG_ERROR("Error parsing persistent data in json file."); |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 103 | } |
| 104 | else |
| 105 | { |
| Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 106 | const nlohmann::json::object_t* obj = |
| 107 | data.get_ptr<nlohmann::json::object_t*>(); |
| 108 | if (obj == nullptr) |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 109 | { |
| Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 110 | return; |
| 111 | } |
| 112 | for (const auto& item : *obj) |
| 113 | { |
| 114 | if (item.first == "revision") |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 115 | { |
| 116 | fileRevision = 0; |
| 117 | |
| 118 | const uint64_t* uintPtr = |
| Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 119 | item.second.get_ptr<const uint64_t*>(); |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 120 | if (uintPtr == nullptr) |
| 121 | { |
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 122 | BMCWEB_LOG_ERROR("Failed to read revision flag"); |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 123 | } |
| 124 | else |
| 125 | { |
| 126 | fileRevision = *uintPtr; |
| 127 | } |
| 128 | } |
| Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 129 | else if (item.first == "system_uuid") |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 130 | { |
| 131 | const std::string* jSystemUuid = |
| Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 132 | item.second.get_ptr<const std::string*>(); |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 133 | if (jSystemUuid != nullptr) |
| 134 | { |
| 135 | systemUuid = *jSystemUuid; |
| 136 | } |
| 137 | } |
| Corey Ethington | e30d334 | 2025-06-24 11:25:11 -0400 | [diff] [blame] | 138 | else if (item.first == "service_identification") |
| 139 | { |
| 140 | const std::string* jServiceIdentification = |
| 141 | item.second.get_ptr<const std::string*>(); |
| 142 | if (jServiceIdentification != nullptr) |
| 143 | { |
| 144 | serviceIdentification = *jServiceIdentification; |
| 145 | } |
| 146 | } |
| Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 147 | else if (item.first == "auth_config") |
| Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 148 | { |
| Ed Tanous | 82b286f | 2025-05-06 13:29:48 -0700 | [diff] [blame] | 149 | const nlohmann::json::object_t* jObj = |
| 150 | item.second |
| 151 | .get_ptr<const nlohmann::json::object_t*>(); |
| 152 | if (jObj == nullptr) |
| 153 | { |
| 154 | continue; |
| 155 | } |
| Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 156 | SessionStore::getInstance() |
| 157 | .getAuthMethodsConfig() |
| Ed Tanous | 82b286f | 2025-05-06 13:29:48 -0700 | [diff] [blame] | 158 | .fromJson(*jObj); |
| Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 159 | } |
| Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 160 | else if (item.first == "sessions") |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 161 | { |
| Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 162 | for (const auto& elem : item.second) |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 163 | { |
| Ed Tanous | 82b286f | 2025-05-06 13:29:48 -0700 | [diff] [blame] | 164 | const nlohmann::json::object_t* jObj = |
| 165 | elem.get_ptr<const nlohmann::json::object_t*>(); |
| 166 | if (jObj == nullptr) |
| 167 | { |
| 168 | continue; |
| 169 | } |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 170 | std::shared_ptr<UserSession> newSession = |
| Ed Tanous | 82b286f | 2025-05-06 13:29:48 -0700 | [diff] [blame] | 171 | UserSession::fromJson(*jObj); |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 172 | |
| 173 | if (newSession == nullptr) |
| 174 | { |
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 175 | BMCWEB_LOG_ERROR("Problem reading session " |
| 176 | "from persistent store"); |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 177 | continue; |
| 178 | } |
| 179 | |
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 180 | BMCWEB_LOG_DEBUG("Restored session: {} {} {}", |
| 181 | newSession->csrfToken, |
| 182 | newSession->uniqueId, |
| 183 | newSession->sessionToken); |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 184 | SessionStore::getInstance().authTokens.emplace( |
| 185 | newSession->sessionToken, newSession); |
| 186 | } |
| 187 | } |
| Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 188 | else if (item.first == "timeout") |
| Manojkiran Eda | f2a4a60 | 2020-08-27 16:04:26 +0530 | [diff] [blame] | 189 | { |
| 190 | const int64_t* jTimeout = |
| Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 191 | item.second.get_ptr<const int64_t*>(); |
| Manojkiran Eda | f2a4a60 | 2020-08-27 16:04:26 +0530 | [diff] [blame] | 192 | if (jTimeout == nullptr) |
| 193 | { |
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 194 | BMCWEB_LOG_DEBUG( |
| 195 | "Problem reading session timeout value"); |
| Manojkiran Eda | f2a4a60 | 2020-08-27 16:04:26 +0530 | [diff] [blame] | 196 | continue; |
| 197 | } |
| 198 | std::chrono::seconds sessionTimeoutInseconds(*jTimeout); |
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 199 | BMCWEB_LOG_DEBUG("Restored Session Timeout: {}", |
| 200 | sessionTimeoutInseconds.count()); |
| Manojkiran Eda | f2a4a60 | 2020-08-27 16:04:26 +0530 | [diff] [blame] | 201 | SessionStore::getInstance().updateSessionTimeout( |
| 202 | sessionTimeoutInseconds); |
| 203 | } |
| Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 204 | else if (item.first == "eventservice_config") |
| JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 205 | { |
| Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 206 | const nlohmann::json::object_t* esobj = |
| 207 | item.second |
| 208 | .get_ptr<const nlohmann::json::object_t*>(); |
| 209 | if (esobj == nullptr) |
| 210 | { |
| 211 | BMCWEB_LOG_DEBUG( |
| 212 | "Problem reading EventService value"); |
| 213 | continue; |
| 214 | } |
| 215 | |
| JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 216 | EventServiceStore::getInstance() |
| 217 | .getEventServiceConfig() |
| Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 218 | .fromJson(*esobj); |
| JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 219 | } |
| Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 220 | else if (item.first == "subscriptions") |
| JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 221 | { |
| Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 222 | for (const auto& elem : item.second) |
| JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 223 | { |
| Ed Tanous | 82b286f | 2025-05-06 13:29:48 -0700 | [diff] [blame] | 224 | const nlohmann::json::object_t* subobj = |
| 225 | elem.get_ptr<const nlohmann::json::object_t*>(); |
| 226 | if (subobj == nullptr) |
| 227 | { |
| 228 | continue; |
| 229 | } |
| 230 | |
| Ed Tanous | 4b712a2 | 2023-08-02 12:56:52 -0700 | [diff] [blame] | 231 | std::optional<UserSubscription> newSub = |
| Ed Tanous | 82b286f | 2025-05-06 13:29:48 -0700 | [diff] [blame] | 232 | UserSubscription::fromJson(*subobj); |
| JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 233 | |
| Ed Tanous | 4b712a2 | 2023-08-02 12:56:52 -0700 | [diff] [blame] | 234 | if (!newSub) |
| JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 235 | { |
| Myung Bae | 6136e85 | 2025-05-14 07:53:45 -0400 | [diff] [blame] | 236 | BMCWEB_LOG_ERROR( |
| 237 | "Problem reading subscription from persistent store"); |
| JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 238 | continue; |
| 239 | } |
| 240 | |
| Myung Bae | 6136e85 | 2025-05-14 07:53:45 -0400 | [diff] [blame] | 241 | std::string id = newSub->id; |
| 242 | BMCWEB_LOG_DEBUG("Restored subscription: {} {}", id, |
| 243 | newSub->customText); |
| Ed Tanous | 4b712a2 | 2023-08-02 12:56:52 -0700 | [diff] [blame] | 244 | |
| Myung Bae | 5fe4ef3 | 2024-10-19 09:56:02 -0400 | [diff] [blame] | 245 | EventServiceStore::getInstance() |
| 246 | .subscriptionsConfigMap.emplace( |
| Myung Bae | 6136e85 | 2025-05-14 07:53:45 -0400 | [diff] [blame] | 247 | id, std::make_shared<UserSubscription>( |
| 248 | std::move(*newSub))); |
| JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 249 | } |
| 250 | } |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 251 | else |
| 252 | { |
| 253 | // Do nothing in the case of extra fields. We may have |
| 254 | // cases where fields are added in the future, and we |
| 255 | // want to at least attempt to gracefully support |
| 256 | // downgrades in that case, even if we don't officially |
| 257 | // support it |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | bool needWrite = false; |
| 263 | |
| 264 | if (systemUuid.empty()) |
| 265 | { |
| Ed Tanous | 2c6ffdb | 2023-06-28 11:28:38 -0700 | [diff] [blame] | 266 | systemUuid = bmcweb::getRandomUUID(); |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 267 | needWrite = true; |
| 268 | } |
| 269 | if (fileRevision < jsonRevision) |
| 270 | { |
| 271 | needWrite = true; |
| 272 | } |
| 273 | // write revision changes or system uuid changes immediately |
| 274 | if (needWrite) |
| 275 | { |
| 276 | writeData(); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | void writeData() |
| 281 | { |
| Ed Tanous | 1388045 | 2025-09-17 09:23:26 -0700 | [diff] [blame] | 282 | const std::string& fname = filename(); |
| 283 | std::filesystem::path path(fname); |
| Ed Tanous | c282e8b | 2024-07-01 08:56:34 -0700 | [diff] [blame] | 284 | path = path.parent_path(); |
| Myung Bae | d8f8a7d | 2024-10-23 12:55:08 -0400 | [diff] [blame] | 285 | if (!path.empty()) |
| Ed Tanous | c282e8b | 2024-07-01 08:56:34 -0700 | [diff] [blame] | 286 | { |
| Myung Bae | d8f8a7d | 2024-10-23 12:55:08 -0400 | [diff] [blame] | 287 | std::error_code ecDir; |
| 288 | std::filesystem::create_directories(path, ecDir); |
| 289 | if (ecDir) |
| 290 | { |
| 291 | BMCWEB_LOG_CRITICAL("Can't create persistent folders {}", |
| 292 | ecDir.message()); |
| 293 | return; |
| 294 | } |
| Ed Tanous | c282e8b | 2024-07-01 08:56:34 -0700 | [diff] [blame] | 295 | } |
| 296 | boost::beast::file_posix persistentFile; |
| 297 | boost::system::error_code ec; |
| Ed Tanous | 1388045 | 2025-09-17 09:23:26 -0700 | [diff] [blame] | 298 | persistentFile.open(fname.c_str(), boost::beast::file_mode::write, ec); |
| Ed Tanous | c282e8b | 2024-07-01 08:56:34 -0700 | [diff] [blame] | 299 | if (ec) |
| 300 | { |
| 301 | BMCWEB_LOG_CRITICAL("Unable to store persistent data to file {}", |
| 302 | ec.message()); |
| 303 | return; |
| 304 | } |
| Ratan Gupta | 845cb7d | 2019-07-12 00:32:25 +0530 | [diff] [blame] | 305 | |
| 306 | // set the permission of the file to 640 |
| Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 307 | std::filesystem::perms permission = |
| 308 | std::filesystem::perms::owner_read | |
| 309 | std::filesystem::perms::owner_write | |
| 310 | std::filesystem::perms::group_read; |
| Ed Tanous | 1388045 | 2025-09-17 09:23:26 -0700 | [diff] [blame] | 311 | std::filesystem::permissions(fname, permission, ec); |
| Ed Tanous | c282e8b | 2024-07-01 08:56:34 -0700 | [diff] [blame] | 312 | if (ec) |
| 313 | { |
| 314 | BMCWEB_LOG_CRITICAL("Failed to set filesystem permissions {}", |
| 315 | ec.message()); |
| 316 | return; |
| 317 | } |
| Ed Tanous | 3ce3688 | 2024-06-09 10:58:16 -0700 | [diff] [blame] | 318 | const AuthConfigMethods& c = |
| 319 | SessionStore::getInstance().getAuthMethodsConfig(); |
| JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 320 | const auto& eventServiceConfig = |
| 321 | EventServiceStore::getInstance().getEventServiceConfig(); |
| Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 322 | nlohmann::json::object_t data; |
| 323 | nlohmann::json& authConfig = data["auth_config"]; |
| Ratan Gupta | 845cb7d | 2019-07-12 00:32:25 +0530 | [diff] [blame] | 324 | |
| Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 325 | authConfig["XToken"] = c.xtoken; |
| 326 | authConfig["Cookie"] = c.cookie; |
| 327 | authConfig["SessionToken"] = c.sessionToken; |
| 328 | authConfig["BasicAuth"] = c.basic; |
| 329 | authConfig["TLS"] = c.tls; |
| Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 330 | authConfig["TLSStrict"] = c.tlsStrict; |
| Malik Akbar Hashemi Rafsanjani | 86e41a8 | 2025-05-06 11:23:32 -0700 | [diff] [blame] | 331 | authConfig["MTLSCommonNameParseMode"] = |
| Ed Tanous | 3ce3688 | 2024-06-09 10:58:16 -0700 | [diff] [blame] | 332 | static_cast<int>(c.mTLSCommonNameParsingMode); |
| JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 333 | |
| Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 334 | nlohmann::json& eventserviceConfig = data["eventservice_config"]; |
| 335 | eventserviceConfig["ServiceEnabled"] = eventServiceConfig.enabled; |
| 336 | eventserviceConfig["DeliveryRetryAttempts"] = |
| 337 | eventServiceConfig.retryAttempts; |
| 338 | eventserviceConfig["DeliveryRetryIntervalSeconds"] = |
| 339 | eventServiceConfig.retryTimeoutInterval; |
| 340 | |
| 341 | data["system_uuid"] = systemUuid; |
| Corey Ethington | e30d334 | 2025-06-24 11:25:11 -0400 | [diff] [blame] | 342 | data["service_identification"] = serviceIdentification; |
| Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 343 | data["revision"] = jsonRevision; |
| 344 | data["timeout"] = SessionStore::getInstance().getTimeoutInSeconds(); |
| Ed Tanous | 5fb91ba | 2020-09-28 15:41:28 -0700 | [diff] [blame] | 345 | |
| 346 | nlohmann::json& sessions = data["sessions"]; |
| 347 | sessions = nlohmann::json::array(); |
| 348 | for (const auto& p : SessionStore::getInstance().authTokens) |
| 349 | { |
| Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 350 | if (p.second->sessionType != persistent_data::SessionType::Basic && |
| 351 | p.second->sessionType != |
| 352 | persistent_data::SessionType::MutualTLS) |
| Ed Tanous | 5fb91ba | 2020-09-28 15:41:28 -0700 | [diff] [blame] | 353 | { |
| Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 354 | nlohmann::json::object_t session; |
| 355 | session["unique_id"] = p.second->uniqueId; |
| 356 | session["session_token"] = p.second->sessionToken; |
| 357 | session["username"] = p.second->username; |
| 358 | session["csrf_token"] = p.second->csrfToken; |
| 359 | session["client_ip"] = p.second->clientIp; |
| Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 360 | const std::optional<std::string>& clientId = p.second->clientId; |
| 361 | if (clientId) |
| Ed Tanous | bb759e3 | 2022-08-02 17:07:54 -0700 | [diff] [blame] | 362 | { |
| Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 363 | session["client_id"] = *clientId; |
| Ed Tanous | bb759e3 | 2022-08-02 17:07:54 -0700 | [diff] [blame] | 364 | } |
| Patrick Williams | b2ba307 | 2023-05-12 10:27:39 -0500 | [diff] [blame] | 365 | sessions.emplace_back(std::move(session)); |
| Ed Tanous | 5fb91ba | 2020-09-28 15:41:28 -0700 | [diff] [blame] | 366 | } |
| 367 | } |
| JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 368 | nlohmann::json& subscriptions = data["subscriptions"]; |
| 369 | subscriptions = nlohmann::json::array(); |
| 370 | for (const auto& it : |
| 371 | EventServiceStore::getInstance().subscriptionsConfigMap) |
| 372 | { |
| Myung Bae | 5fe4ef3 | 2024-10-19 09:56:02 -0400 | [diff] [blame] | 373 | if (it.second == nullptr) |
| 374 | { |
| 375 | continue; |
| 376 | } |
| 377 | const UserSubscription& subValue = *it.second; |
| wenlitao | fbfb788 | 2024-07-12 11:25:00 +0800 | [diff] [blame] | 378 | if (subValue.subscriptionType == "SSE") |
| JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 379 | { |
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 380 | BMCWEB_LOG_DEBUG("The subscription type is SSE, so skipping."); |
| JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 381 | continue; |
| 382 | } |
| Ed Tanous | 601c71a | 2021-09-08 16:40:12 -0700 | [diff] [blame] | 383 | nlohmann::json::object_t headers; |
| 384 | for (const boost::beast::http::fields::value_type& header : |
| wenlitao | fbfb788 | 2024-07-12 11:25:00 +0800 | [diff] [blame] | 385 | subValue.httpHeaders) |
| Ed Tanous | 601c71a | 2021-09-08 16:40:12 -0700 | [diff] [blame] | 386 | { |
| 387 | // Note, these are technically copies because nlohmann doesn't |
| 388 | // support key lookup by std::string_view. At least the |
| 389 | // following code can use move |
| 390 | // https://github.com/nlohmann/json/issues/1529 |
| 391 | std::string name(header.name_string()); |
| 392 | headers[std::move(name)] = header.value(); |
| 393 | } |
| 394 | |
| Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 395 | nlohmann::json::object_t subscription; |
| 396 | |
| wenlitao | fbfb788 | 2024-07-12 11:25:00 +0800 | [diff] [blame] | 397 | subscription["Id"] = subValue.id; |
| 398 | subscription["Context"] = subValue.customText; |
| 399 | subscription["DeliveryRetryPolicy"] = subValue.retryPolicy; |
| Myung Bae | 5064a25 | 2024-10-04 09:34:25 -0700 | [diff] [blame] | 400 | subscription["SendHeartbeat"] = subValue.sendHeartbeat; |
| 401 | subscription["HeartbeatIntervalMinutes"] = |
| 402 | subValue.hbIntervalMinutes; |
| wenlitao | fbfb788 | 2024-07-12 11:25:00 +0800 | [diff] [blame] | 403 | subscription["Destination"] = subValue.destinationUrl; |
| 404 | subscription["EventFormatType"] = subValue.eventFormatType; |
| Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 405 | subscription["HttpHeaders"] = std::move(headers); |
| wenlitao | fbfb788 | 2024-07-12 11:25:00 +0800 | [diff] [blame] | 406 | subscription["MessageIds"] = subValue.registryMsgIds; |
| 407 | subscription["Protocol"] = subValue.protocol; |
| 408 | subscription["RegistryPrefixes"] = subValue.registryPrefixes; |
| Ed Tanous | a14c911 | 2024-09-04 10:46:47 -0700 | [diff] [blame] | 409 | subscription["OriginResources"] = subValue.originResources; |
| wenlitao | fbfb788 | 2024-07-12 11:25:00 +0800 | [diff] [blame] | 410 | subscription["ResourceTypes"] = subValue.resourceTypes; |
| 411 | subscription["SubscriptionType"] = subValue.subscriptionType; |
| Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 412 | subscription["MetricReportDefinitions"] = |
| wenlitao | fbfb788 | 2024-07-12 11:25:00 +0800 | [diff] [blame] | 413 | subValue.metricReportDefinitions; |
| Ed Tanous | 19bb362 | 2024-07-05 10:07:40 -0500 | [diff] [blame] | 414 | subscription["VerifyCertificate"] = subValue.verifyCertificate; |
| Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 415 | |
| Patrick Williams | b2ba307 | 2023-05-12 10:27:39 -0500 | [diff] [blame] | 416 | subscriptions.emplace_back(std::move(subscription)); |
| JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 417 | } |
| Ed Tanous | c282e8b | 2024-07-01 08:56:34 -0700 | [diff] [blame] | 418 | std::string out = nlohmann::json(data).dump( |
| 419 | -1, ' ', true, nlohmann::json::error_handler_t::replace); |
| 420 | persistentFile.write(out.data(), out.size(), ec); |
| 421 | if (ec) |
| 422 | { |
| 423 | BMCWEB_LOG_ERROR("Failed to write file {}", ec.message()); |
| 424 | } |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| Ed Tanous | e05aec5 | 2022-01-25 10:28:56 -0800 | [diff] [blame] | 427 | std::string systemUuid; |
| Corey Ethington | e30d334 | 2025-06-24 11:25:11 -0400 | [diff] [blame] | 428 | std::string serviceIdentification; |
| Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 429 | }; |
| 430 | |
| Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 431 | inline ConfigFile& getConfig() |
| 432 | { |
| 433 | static ConfigFile f; |
| 434 | return f; |
| 435 | } |
| 436 | |
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 437 | } // namespace persistent_data |