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