Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 3 | #include <nlohmann/json.hpp> |
| 4 | #include <pam_authenticate.hpp> |
Borawski.Lukasz | 4b1b868 | 2018-04-04 12:50:16 +0200 | [diff] [blame] | 5 | #include <sessions.hpp> |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 6 | #include <webassets.hpp> |
Borawski.Lukasz | 1623897 | 2018-01-17 15:36:53 +0100 | [diff] [blame] | 7 | #include <random> |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 8 | #include <crow/app.h> |
| 9 | #include <crow/http_request.h> |
| 10 | #include <crow/http_response.h> |
| 11 | #include <boost/container/flat_map.hpp> |
| 12 | #include <boost/uuid/uuid.hpp> |
| 13 | #include <boost/uuid/uuid_generators.hpp> |
| 14 | #include <boost/uuid/uuid_io.hpp> |
| 15 | |
| 16 | namespace crow { |
| 17 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 18 | namespace persistent_data { |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 19 | |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 20 | class Middleware { |
| 21 | // todo(ed) should read this from a fixed location somewhere, not CWD |
| 22 | static constexpr const char* filename = "bmcweb_persistent_data.json"; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 23 | int jsonRevision = 1; |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 24 | |
| 25 | public: |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 26 | struct Context {}; |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 27 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 28 | Middleware() { readData(); } |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 29 | |
| 30 | ~Middleware() { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 31 | if (persistent_data::SessionStore::getInstance().needsWrite()) { |
| 32 | writeData(); |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 33 | } |
| 34 | } |
| 35 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 36 | void beforeHandle(crow::Request& req, Response& res, Context& ctx) {} |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 37 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 38 | void afterHandle(Request& req, Response& res, Context& ctx) {} |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 39 | |
| 40 | // TODO(ed) this should really use protobuf, or some other serialization |
| 41 | // library, but adding another dependency is somewhat outside the scope of |
| 42 | // this application for the moment |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 43 | void readData() { |
| 44 | std::ifstream persistentFile(filename); |
| 45 | int fileRevision = 0; |
| 46 | if (persistentFile.is_open()) { |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 47 | // call with exceptions disabled |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 48 | auto data = nlohmann::json::parse(persistentFile, nullptr, false); |
Ed Tanous | e1ae533 | 2018-07-09 15:21:27 -0700 | [diff] [blame] | 49 | if (data.is_discarded()) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 50 | BMCWEB_LOG_ERROR << "Error parsing persistent data in json file."; |
Ed Tanous | e1ae533 | 2018-07-09 15:21:27 -0700 | [diff] [blame] | 51 | } else { |
| 52 | for (const auto& item : data.items()) { |
| 53 | if (item.key() == "revision") { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 54 | fileRevision = 0; |
Kowalski, Kamil | 5cef0f7 | 2018-02-15 15:26:51 +0100 | [diff] [blame] | 55 | |
Ed Tanous | e1ae533 | 2018-07-09 15:21:27 -0700 | [diff] [blame] | 56 | const uint64_t* uintPtr = item.value().get_ptr<const uint64_t*>(); |
| 57 | if (uintPtr == nullptr) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 58 | BMCWEB_LOG_ERROR << "Failed to read revision flag"; |
Ed Tanous | e1ae533 | 2018-07-09 15:21:27 -0700 | [diff] [blame] | 59 | } else { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 60 | fileRevision = *uintPtr; |
Kowalski, Kamil | 5cef0f7 | 2018-02-15 15:26:51 +0100 | [diff] [blame] | 61 | } |
Ed Tanous | e1ae533 | 2018-07-09 15:21:27 -0700 | [diff] [blame] | 62 | } else if (item.key() == "system_uuid") { |
| 63 | const std::string* jSystemUuid = |
| 64 | item.value().get_ptr<const std::string*>(); |
| 65 | if (jSystemUuid != nullptr) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 66 | systemUuid = *jSystemUuid; |
Ed Tanous | e1ae533 | 2018-07-09 15:21:27 -0700 | [diff] [blame] | 67 | } |
| 68 | } else if (item.key() == "sessions") { |
| 69 | for (const auto& elem : item.value()) { |
| 70 | std::shared_ptr<UserSession> newSession = |
| 71 | UserSession::fromJson(elem); |
| 72 | |
| 73 | if (newSession == nullptr) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 74 | BMCWEB_LOG_ERROR |
Ed Tanous | e1ae533 | 2018-07-09 15:21:27 -0700 | [diff] [blame] | 75 | << "Problem reading session from persistent store"; |
| 76 | continue; |
| 77 | } |
| 78 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 79 | BMCWEB_LOG_DEBUG << "Restored session: " << newSession->csrfToken |
Ed Tanous | a434f2b | 2018-07-27 13:04:22 -0700 | [diff] [blame] | 80 | << " " << newSession->uniqueId << " " |
| 81 | << newSession->sessionToken; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 82 | SessionStore::getInstance().authTokens.emplace( |
| 83 | newSession->sessionToken, newSession); |
Ed Tanous | e1ae533 | 2018-07-09 15:21:27 -0700 | [diff] [blame] | 84 | } |
| 85 | } else { |
| 86 | // Do nothing in the case of extra fields. We may have cases where |
| 87 | // fields are added in the future, and we want to at least attempt |
| 88 | // to gracefully support downgrades in that case, even if we don't |
| 89 | // officially support it |
Kowalski, Kamil | 5cef0f7 | 2018-02-15 15:26:51 +0100 | [diff] [blame] | 90 | } |
| 91 | } |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 94 | bool needWrite = false; |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 95 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 96 | if (systemUuid.empty()) { |
| 97 | systemUuid = boost::uuids::to_string(boost::uuids::random_generator()()); |
| 98 | needWrite = true; |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 99 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 100 | if (fileRevision < jsonRevision) { |
| 101 | needWrite = true; |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 102 | } |
| 103 | // write revision changes or system uuid changes immediately |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 104 | if (needWrite) { |
| 105 | writeData(); |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 109 | void writeData() { |
| 110 | std::ofstream persistentFile(filename); |
Ed Tanous | a434f2b | 2018-07-27 13:04:22 -0700 | [diff] [blame] | 111 | nlohmann::json data{{"sessions", SessionStore::getInstance().authTokens}, |
| 112 | {"system_uuid", systemUuid}, |
| 113 | {"revision", jsonRevision}}; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 114 | persistentFile << data; |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 117 | std::string systemUuid{""}; |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 118 | }; |
| 119 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 120 | } // namespace persistent_data |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 121 | } // namespace crow |