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