blob: efdfcfa18c6536f619edcccc43f866e91c531800 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Ed Tanousba9f9a62017-10-11 16:40:35 -07003#pragma once
4
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08005#include "event_service_store.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -08006#include "logging.hpp"
Ed Tanous2c6ffdb2023-06-28 11:28:38 -07007#include "ossl_random.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08008#include "sessions.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -08009// NOLINTNEXTLINE(misc-include-cleaner)
10#include "utility.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080011
Ed Tanousd7857202025-01-28 15:32:26 -080012#include <boost/beast/core/file_base.hpp>
Ed Tanousc282e8b2024-07-01 08:56:34 -070013#include <boost/beast/core/file_posix.hpp>
Ed Tanous601c71a2021-09-08 16:40:12 -070014#include <boost/beast/http/fields.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -070015#include <nlohmann/json.hpp>
Ed Tanousba9f9a62017-10-11 16:40:35 -070016
Ed Tanousd7857202025-01-28 15:32:26 -080017#include <chrono>
18#include <cstdint>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050019#include <filesystem>
James Feist3909dc82020-04-03 10:58:55 -070020#include <fstream>
Ed Tanousd7857202025-01-28 15:32:26 -080021#include <memory>
22#include <optional>
23#include <string>
Ed Tanousc282e8b2024-07-01 08:56:34 -070024#include <system_error>
Ed Tanousd7857202025-01-28 15:32:26 -080025#include <utility>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050026
Ed Tanous1abe55e2018-09-05 08:30:59 -070027namespace persistent_data
28{
Borawski.Lukasz9d8fd302018-01-05 14:56:09 +010029
Ed Tanous52cc1122020-07-18 13:51:21 -070030class ConfigFile
Ed Tanous1abe55e2018-09-05 08:30:59 -070031{
Ed Tanous271584a2019-07-09 16:24:22 -070032 uint64_t jsonRevision = 1;
Ed Tanousc963aa42017-10-27 16:00:19 -070033
Ed Tanous1abe55e2018-09-05 08:30:59 -070034 public:
Ratan Gupta845cb7d2019-07-12 00:32:25 +053035 // todo(ed) should read this from a fixed location somewhere, not CWD
36 static constexpr const char* filename = "bmcweb_persistent_data.json";
37
Ed Tanous52cc1122020-07-18 13:51:21 -070038 ConfigFile()
Ed Tanous1abe55e2018-09-05 08:30:59 -070039 {
40 readData();
Ed Tanousc963aa42017-10-27 16:00:19 -070041 }
Ed Tanousc963aa42017-10-27 16:00:19 -070042
Ed Tanous52cc1122020-07-18 13:51:21 -070043 ~ConfigFile()
Ed Tanous1abe55e2018-09-05 08:30:59 -070044 {
Gunnar Mills83cf8182020-11-11 15:37:34 -060045 // Make sure we aren't writing stale sessions
46 persistent_data::SessionStore::getInstance().applySessionTimeouts();
Ed Tanous1abe55e2018-09-05 08:30:59 -070047 if (persistent_data::SessionStore::getInstance().needsWrite())
48 {
49 writeData();
Kowalski, Kamil5cef0f72018-02-15 15:26:51 +010050 }
Ed Tanousc963aa42017-10-27 16:00:19 -070051 }
Ed Tanousc963aa42017-10-27 16:00:19 -070052
Ed Tanousecd6a3a2022-01-07 09:18:40 -080053 ConfigFile(const ConfigFile&) = delete;
54 ConfigFile(ConfigFile&&) = delete;
55 ConfigFile& operator=(const ConfigFile&) = delete;
56 ConfigFile& operator=(ConfigFile&&) = delete;
57
Ed Tanous1abe55e2018-09-05 08:30:59 -070058 // 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 Tanous271584a2019-07-09 16:24:22 -070064 uint64_t fileRevision = 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -070065 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 Tanous62598e32023-07-17 17:06:25 -070071 BMCWEB_LOG_ERROR("Error parsing persistent data in json file.");
Ed Tanous1abe55e2018-09-05 08:30:59 -070072 }
73 else
74 {
Ed Tanous0bdda662023-08-03 17:27:34 -070075 const nlohmann::json::object_t* obj =
76 data.get_ptr<nlohmann::json::object_t*>();
77 if (obj == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -070078 {
Ed Tanous0bdda662023-08-03 17:27:34 -070079 return;
80 }
81 for (const auto& item : *obj)
82 {
83 if (item.first == "revision")
Ed Tanous1abe55e2018-09-05 08:30:59 -070084 {
85 fileRevision = 0;
86
87 const uint64_t* uintPtr =
Ed Tanous0bdda662023-08-03 17:27:34 -070088 item.second.get_ptr<const uint64_t*>();
Ed Tanous1abe55e2018-09-05 08:30:59 -070089 if (uintPtr == nullptr)
90 {
Ed Tanous62598e32023-07-17 17:06:25 -070091 BMCWEB_LOG_ERROR("Failed to read revision flag");
Ed Tanous1abe55e2018-09-05 08:30:59 -070092 }
93 else
94 {
95 fileRevision = *uintPtr;
96 }
97 }
Ed Tanous0bdda662023-08-03 17:27:34 -070098 else if (item.first == "system_uuid")
Ed Tanous1abe55e2018-09-05 08:30:59 -070099 {
100 const std::string* jSystemUuid =
Ed Tanous0bdda662023-08-03 17:27:34 -0700101 item.second.get_ptr<const std::string*>();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700102 if (jSystemUuid != nullptr)
103 {
104 systemUuid = *jSystemUuid;
105 }
106 }
Ed Tanous0bdda662023-08-03 17:27:34 -0700107 else if (item.first == "auth_config")
Zbigniew Kurzynski78158632019-11-05 12:57:37 +0100108 {
Ed Tanous82b286f2025-05-06 13:29:48 -0700109 const nlohmann::json::object_t* jObj =
110 item.second
111 .get_ptr<const nlohmann::json::object_t*>();
112 if (jObj == nullptr)
113 {
114 continue;
115 }
Zbigniew Kurzynski78158632019-11-05 12:57:37 +0100116 SessionStore::getInstance()
117 .getAuthMethodsConfig()
Ed Tanous82b286f2025-05-06 13:29:48 -0700118 .fromJson(*jObj);
Zbigniew Kurzynski78158632019-11-05 12:57:37 +0100119 }
Ed Tanous0bdda662023-08-03 17:27:34 -0700120 else if (item.first == "sessions")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700121 {
Ed Tanous0bdda662023-08-03 17:27:34 -0700122 for (const auto& elem : item.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700123 {
Ed Tanous82b286f2025-05-06 13:29:48 -0700124 const nlohmann::json::object_t* jObj =
125 elem.get_ptr<const nlohmann::json::object_t*>();
126 if (jObj == nullptr)
127 {
128 continue;
129 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700130 std::shared_ptr<UserSession> newSession =
Ed Tanous82b286f2025-05-06 13:29:48 -0700131 UserSession::fromJson(*jObj);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700132
133 if (newSession == nullptr)
134 {
Ed Tanous62598e32023-07-17 17:06:25 -0700135 BMCWEB_LOG_ERROR("Problem reading session "
136 "from persistent store");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700137 continue;
138 }
139
Ed Tanous62598e32023-07-17 17:06:25 -0700140 BMCWEB_LOG_DEBUG("Restored session: {} {} {}",
141 newSession->csrfToken,
142 newSession->uniqueId,
143 newSession->sessionToken);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700144 SessionStore::getInstance().authTokens.emplace(
145 newSession->sessionToken, newSession);
146 }
147 }
Ed Tanous0bdda662023-08-03 17:27:34 -0700148 else if (item.first == "timeout")
Manojkiran Edaf2a4a602020-08-27 16:04:26 +0530149 {
150 const int64_t* jTimeout =
Ed Tanous0bdda662023-08-03 17:27:34 -0700151 item.second.get_ptr<const int64_t*>();
Manojkiran Edaf2a4a602020-08-27 16:04:26 +0530152 if (jTimeout == nullptr)
153 {
Ed Tanous62598e32023-07-17 17:06:25 -0700154 BMCWEB_LOG_DEBUG(
155 "Problem reading session timeout value");
Manojkiran Edaf2a4a602020-08-27 16:04:26 +0530156 continue;
157 }
158 std::chrono::seconds sessionTimeoutInseconds(*jTimeout);
Ed Tanous62598e32023-07-17 17:06:25 -0700159 BMCWEB_LOG_DEBUG("Restored Session Timeout: {}",
160 sessionTimeoutInseconds.count());
Manojkiran Edaf2a4a602020-08-27 16:04:26 +0530161 SessionStore::getInstance().updateSessionTimeout(
162 sessionTimeoutInseconds);
163 }
Ed Tanous0bdda662023-08-03 17:27:34 -0700164 else if (item.first == "eventservice_config")
JunLin Chen28afb492021-02-24 17:13:29 +0800165 {
Ed Tanous0bdda662023-08-03 17:27:34 -0700166 const nlohmann::json::object_t* esobj =
167 item.second
168 .get_ptr<const nlohmann::json::object_t*>();
169 if (esobj == nullptr)
170 {
171 BMCWEB_LOG_DEBUG(
172 "Problem reading EventService value");
173 continue;
174 }
175
JunLin Chen28afb492021-02-24 17:13:29 +0800176 EventServiceStore::getInstance()
177 .getEventServiceConfig()
Ed Tanous0bdda662023-08-03 17:27:34 -0700178 .fromJson(*esobj);
JunLin Chen28afb492021-02-24 17:13:29 +0800179 }
Ed Tanous0bdda662023-08-03 17:27:34 -0700180 else if (item.first == "subscriptions")
JunLin Chen28afb492021-02-24 17:13:29 +0800181 {
Ed Tanous0bdda662023-08-03 17:27:34 -0700182 for (const auto& elem : item.second)
JunLin Chen28afb492021-02-24 17:13:29 +0800183 {
Ed Tanous82b286f2025-05-06 13:29:48 -0700184 const nlohmann::json::object_t* subobj =
185 elem.get_ptr<const nlohmann::json::object_t*>();
186 if (subobj == nullptr)
187 {
188 continue;
189 }
190
Ed Tanous4b712a22023-08-02 12:56:52 -0700191 std::optional<UserSubscription> newSub =
Ed Tanous82b286f2025-05-06 13:29:48 -0700192 UserSubscription::fromJson(*subobj);
JunLin Chen28afb492021-02-24 17:13:29 +0800193
Ed Tanous4b712a22023-08-02 12:56:52 -0700194 if (!newSub)
JunLin Chen28afb492021-02-24 17:13:29 +0800195 {
Myung Bae6136e852025-05-14 07:53:45 -0400196 BMCWEB_LOG_ERROR(
197 "Problem reading subscription from persistent store");
JunLin Chen28afb492021-02-24 17:13:29 +0800198 continue;
199 }
200
Myung Bae6136e852025-05-14 07:53:45 -0400201 std::string id = newSub->id;
202 BMCWEB_LOG_DEBUG("Restored subscription: {} {}", id,
203 newSub->customText);
Ed Tanous4b712a22023-08-02 12:56:52 -0700204
Myung Bae5fe4ef32024-10-19 09:56:02 -0400205 EventServiceStore::getInstance()
206 .subscriptionsConfigMap.emplace(
Myung Bae6136e852025-05-14 07:53:45 -0400207 id, std::make_shared<UserSubscription>(
208 std::move(*newSub)));
JunLin Chen28afb492021-02-24 17:13:29 +0800209 }
210 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700211 else
212 {
213 // Do nothing in the case of extra fields. We may have
214 // cases where fields are added in the future, and we
215 // want to at least attempt to gracefully support
216 // downgrades in that case, even if we don't officially
217 // support it
218 }
219 }
220 }
221 }
222 bool needWrite = false;
223
224 if (systemUuid.empty())
225 {
Ed Tanous2c6ffdb2023-06-28 11:28:38 -0700226 systemUuid = bmcweb::getRandomUUID();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700227 needWrite = true;
228 }
229 if (fileRevision < jsonRevision)
230 {
231 needWrite = true;
232 }
233 // write revision changes or system uuid changes immediately
234 if (needWrite)
235 {
236 writeData();
237 }
238 }
239
240 void writeData()
241 {
Ed Tanousc282e8b2024-07-01 08:56:34 -0700242 std::filesystem::path path(filename);
243 path = path.parent_path();
Myung Baed8f8a7d2024-10-23 12:55:08 -0400244 if (!path.empty())
Ed Tanousc282e8b2024-07-01 08:56:34 -0700245 {
Myung Baed8f8a7d2024-10-23 12:55:08 -0400246 std::error_code ecDir;
247 std::filesystem::create_directories(path, ecDir);
248 if (ecDir)
249 {
250 BMCWEB_LOG_CRITICAL("Can't create persistent folders {}",
251 ecDir.message());
252 return;
253 }
Ed Tanousc282e8b2024-07-01 08:56:34 -0700254 }
255 boost::beast::file_posix persistentFile;
256 boost::system::error_code ec;
257 persistentFile.open(filename, boost::beast::file_mode::write, ec);
258 if (ec)
259 {
260 BMCWEB_LOG_CRITICAL("Unable to store persistent data to file {}",
261 ec.message());
262 return;
263 }
Ratan Gupta845cb7d2019-07-12 00:32:25 +0530264
265 // set the permission of the file to 640
Ed Tanous52cc1122020-07-18 13:51:21 -0700266 std::filesystem::perms permission =
267 std::filesystem::perms::owner_read |
268 std::filesystem::perms::owner_write |
269 std::filesystem::perms::group_read;
Ed Tanousc282e8b2024-07-01 08:56:34 -0700270 std::filesystem::permissions(filename, permission, ec);
271 if (ec)
272 {
273 BMCWEB_LOG_CRITICAL("Failed to set filesystem permissions {}",
274 ec.message());
275 return;
276 }
Ed Tanous3ce36882024-06-09 10:58:16 -0700277 const AuthConfigMethods& c =
278 SessionStore::getInstance().getAuthMethodsConfig();
JunLin Chen28afb492021-02-24 17:13:29 +0800279 const auto& eventServiceConfig =
280 EventServiceStore::getInstance().getEventServiceConfig();
Ed Tanous14766872022-03-15 10:44:42 -0700281 nlohmann::json::object_t data;
282 nlohmann::json& authConfig = data["auth_config"];
Ratan Gupta845cb7d2019-07-12 00:32:25 +0530283
Ed Tanous14766872022-03-15 10:44:42 -0700284 authConfig["XToken"] = c.xtoken;
285 authConfig["Cookie"] = c.cookie;
286 authConfig["SessionToken"] = c.sessionToken;
287 authConfig["BasicAuth"] = c.basic;
288 authConfig["TLS"] = c.tls;
Ed Tanous3281bcf2024-06-25 16:02:05 -0700289 authConfig["TLSStrict"] = c.tlsStrict;
Malik Akbar Hashemi Rafsanjani86e41a82025-05-06 11:23:32 -0700290 authConfig["MTLSCommonNameParseMode"] =
Ed Tanous3ce36882024-06-09 10:58:16 -0700291 static_cast<int>(c.mTLSCommonNameParsingMode);
JunLin Chen28afb492021-02-24 17:13:29 +0800292
Ed Tanous14766872022-03-15 10:44:42 -0700293 nlohmann::json& eventserviceConfig = data["eventservice_config"];
294 eventserviceConfig["ServiceEnabled"] = eventServiceConfig.enabled;
295 eventserviceConfig["DeliveryRetryAttempts"] =
296 eventServiceConfig.retryAttempts;
297 eventserviceConfig["DeliveryRetryIntervalSeconds"] =
298 eventServiceConfig.retryTimeoutInterval;
299
300 data["system_uuid"] = systemUuid;
301 data["revision"] = jsonRevision;
302 data["timeout"] = SessionStore::getInstance().getTimeoutInSeconds();
Ed Tanous5fb91ba2020-09-28 15:41:28 -0700303
304 nlohmann::json& sessions = data["sessions"];
305 sessions = nlohmann::json::array();
306 for (const auto& p : SessionStore::getInstance().authTokens)
307 {
Ed Tanous89cda632024-04-16 08:45:54 -0700308 if (p.second->sessionType != persistent_data::SessionType::Basic &&
309 p.second->sessionType !=
310 persistent_data::SessionType::MutualTLS)
Ed Tanous5fb91ba2020-09-28 15:41:28 -0700311 {
Ed Tanous14766872022-03-15 10:44:42 -0700312 nlohmann::json::object_t session;
313 session["unique_id"] = p.second->uniqueId;
314 session["session_token"] = p.second->sessionToken;
315 session["username"] = p.second->username;
316 session["csrf_token"] = p.second->csrfToken;
317 session["client_ip"] = p.second->clientIp;
Ed Tanouse01d0c32023-06-30 13:21:32 -0700318 const std::optional<std::string>& clientId = p.second->clientId;
319 if (clientId)
Ed Tanousbb759e32022-08-02 17:07:54 -0700320 {
Ed Tanouse01d0c32023-06-30 13:21:32 -0700321 session["client_id"] = *clientId;
Ed Tanousbb759e32022-08-02 17:07:54 -0700322 }
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500323 sessions.emplace_back(std::move(session));
Ed Tanous5fb91ba2020-09-28 15:41:28 -0700324 }
325 }
JunLin Chen28afb492021-02-24 17:13:29 +0800326 nlohmann::json& subscriptions = data["subscriptions"];
327 subscriptions = nlohmann::json::array();
328 for (const auto& it :
329 EventServiceStore::getInstance().subscriptionsConfigMap)
330 {
Myung Bae5fe4ef32024-10-19 09:56:02 -0400331 if (it.second == nullptr)
332 {
333 continue;
334 }
335 const UserSubscription& subValue = *it.second;
wenlitaofbfb7882024-07-12 11:25:00 +0800336 if (subValue.subscriptionType == "SSE")
JunLin Chen28afb492021-02-24 17:13:29 +0800337 {
Ed Tanous62598e32023-07-17 17:06:25 -0700338 BMCWEB_LOG_DEBUG("The subscription type is SSE, so skipping.");
JunLin Chen28afb492021-02-24 17:13:29 +0800339 continue;
340 }
Ed Tanous601c71a2021-09-08 16:40:12 -0700341 nlohmann::json::object_t headers;
342 for (const boost::beast::http::fields::value_type& header :
wenlitaofbfb7882024-07-12 11:25:00 +0800343 subValue.httpHeaders)
Ed Tanous601c71a2021-09-08 16:40:12 -0700344 {
345 // Note, these are technically copies because nlohmann doesn't
346 // support key lookup by std::string_view. At least the
347 // following code can use move
348 // https://github.com/nlohmann/json/issues/1529
349 std::string name(header.name_string());
350 headers[std::move(name)] = header.value();
351 }
352
Ed Tanous14766872022-03-15 10:44:42 -0700353 nlohmann::json::object_t subscription;
354
wenlitaofbfb7882024-07-12 11:25:00 +0800355 subscription["Id"] = subValue.id;
356 subscription["Context"] = subValue.customText;
357 subscription["DeliveryRetryPolicy"] = subValue.retryPolicy;
Myung Bae5064a252024-10-04 09:34:25 -0700358 subscription["SendHeartbeat"] = subValue.sendHeartbeat;
359 subscription["HeartbeatIntervalMinutes"] =
360 subValue.hbIntervalMinutes;
wenlitaofbfb7882024-07-12 11:25:00 +0800361 subscription["Destination"] = subValue.destinationUrl;
362 subscription["EventFormatType"] = subValue.eventFormatType;
Ed Tanous14766872022-03-15 10:44:42 -0700363 subscription["HttpHeaders"] = std::move(headers);
wenlitaofbfb7882024-07-12 11:25:00 +0800364 subscription["MessageIds"] = subValue.registryMsgIds;
365 subscription["Protocol"] = subValue.protocol;
366 subscription["RegistryPrefixes"] = subValue.registryPrefixes;
Ed Tanousa14c9112024-09-04 10:46:47 -0700367 subscription["OriginResources"] = subValue.originResources;
wenlitaofbfb7882024-07-12 11:25:00 +0800368 subscription["ResourceTypes"] = subValue.resourceTypes;
369 subscription["SubscriptionType"] = subValue.subscriptionType;
Ed Tanous14766872022-03-15 10:44:42 -0700370 subscription["MetricReportDefinitions"] =
wenlitaofbfb7882024-07-12 11:25:00 +0800371 subValue.metricReportDefinitions;
Ed Tanous19bb3622024-07-05 10:07:40 -0500372 subscription["VerifyCertificate"] = subValue.verifyCertificate;
Ed Tanous14766872022-03-15 10:44:42 -0700373
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500374 subscriptions.emplace_back(std::move(subscription));
JunLin Chen28afb492021-02-24 17:13:29 +0800375 }
Ed Tanousc282e8b2024-07-01 08:56:34 -0700376 std::string out = nlohmann::json(data).dump(
377 -1, ' ', true, nlohmann::json::error_handler_t::replace);
378 persistentFile.write(out.data(), out.size(), ec);
379 if (ec)
380 {
381 BMCWEB_LOG_ERROR("Failed to write file {}", ec.message());
382 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700383 }
384
Ed Tanouse05aec52022-01-25 10:28:56 -0800385 std::string systemUuid;
Ed Tanousba9f9a62017-10-11 16:40:35 -0700386};
387
Ed Tanous52cc1122020-07-18 13:51:21 -0700388inline ConfigFile& getConfig()
389{
390 static ConfigFile f;
391 return f;
392}
393
Ed Tanous1abe55e2018-09-05 08:30:59 -0700394} // namespace persistent_data