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