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