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