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 | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 4 | #include <boost/container/flat_map.hpp> |
| 5 | #include <boost/uuid/uuid.hpp> |
| 6 | #include <boost/uuid/uuid_generators.hpp> |
| 7 | #include <boost/uuid/uuid_io.hpp> |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame^] | 8 | #include <http_request.hpp> |
| 9 | #include <http_response.hpp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 10 | #include <nlohmann/json.hpp> |
| 11 | #include <pam_authenticate.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 | { |
| 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 | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 42 | // TODO(ed) this should really use protobuf, or some other serialization |
| 43 | // library, but adding another dependency is somewhat outside the scope of |
| 44 | // this application for the moment |
| 45 | void readData() |
| 46 | { |
| 47 | std::ifstream persistentFile(filename); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 48 | uint64_t fileRevision = 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 49 | if (persistentFile.is_open()) |
| 50 | { |
| 51 | // call with exceptions disabled |
| 52 | auto data = nlohmann::json::parse(persistentFile, nullptr, false); |
| 53 | if (data.is_discarded()) |
| 54 | { |
| 55 | BMCWEB_LOG_ERROR |
| 56 | << "Error parsing persistent data in json file."; |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | for (const auto& item : data.items()) |
| 61 | { |
| 62 | if (item.key() == "revision") |
| 63 | { |
| 64 | fileRevision = 0; |
| 65 | |
| 66 | const uint64_t* uintPtr = |
| 67 | item.value().get_ptr<const uint64_t*>(); |
| 68 | if (uintPtr == nullptr) |
| 69 | { |
| 70 | BMCWEB_LOG_ERROR << "Failed to read revision flag"; |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | fileRevision = *uintPtr; |
| 75 | } |
| 76 | } |
| 77 | else if (item.key() == "system_uuid") |
| 78 | { |
| 79 | const std::string* jSystemUuid = |
| 80 | item.value().get_ptr<const std::string*>(); |
| 81 | if (jSystemUuid != nullptr) |
| 82 | { |
| 83 | systemUuid = *jSystemUuid; |
| 84 | } |
| 85 | } |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 86 | else if (item.key() == "auth_config") |
| 87 | { |
| 88 | SessionStore::getInstance() |
| 89 | .getAuthMethodsConfig() |
| 90 | .fromJson(item.value()); |
| 91 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 92 | else if (item.key() == "sessions") |
| 93 | { |
| 94 | for (const auto& elem : item.value()) |
| 95 | { |
| 96 | std::shared_ptr<UserSession> newSession = |
| 97 | UserSession::fromJson(elem); |
| 98 | |
| 99 | if (newSession == nullptr) |
| 100 | { |
| 101 | BMCWEB_LOG_ERROR << "Problem reading session " |
| 102 | "from persistent store"; |
| 103 | continue; |
| 104 | } |
| 105 | |
| 106 | BMCWEB_LOG_DEBUG |
| 107 | << "Restored session: " << newSession->csrfToken |
| 108 | << " " << newSession->uniqueId << " " |
| 109 | << newSession->sessionToken; |
| 110 | SessionStore::getInstance().authTokens.emplace( |
| 111 | newSession->sessionToken, newSession); |
| 112 | } |
| 113 | } |
Manojkiran Eda | f2a4a60 | 2020-08-27 16:04:26 +0530 | [diff] [blame] | 114 | else if (item.key() == "timeout") |
| 115 | { |
| 116 | const int64_t* jTimeout = |
| 117 | item.value().get_ptr<int64_t*>(); |
| 118 | if (jTimeout == nullptr) |
| 119 | { |
| 120 | BMCWEB_LOG_DEBUG |
| 121 | << "Problem reading session timeout value"; |
| 122 | continue; |
| 123 | } |
| 124 | std::chrono::seconds sessionTimeoutInseconds(*jTimeout); |
| 125 | BMCWEB_LOG_DEBUG << "Restored Session Timeout: " |
| 126 | << sessionTimeoutInseconds.count(); |
| 127 | SessionStore::getInstance().updateSessionTimeout( |
| 128 | sessionTimeoutInseconds); |
| 129 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 130 | else |
| 131 | { |
| 132 | // Do nothing in the case of extra fields. We may have |
| 133 | // cases where fields are added in the future, and we |
| 134 | // want to at least attempt to gracefully support |
| 135 | // downgrades in that case, even if we don't officially |
| 136 | // support it |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | bool needWrite = false; |
| 142 | |
| 143 | if (systemUuid.empty()) |
| 144 | { |
| 145 | systemUuid = |
| 146 | boost::uuids::to_string(boost::uuids::random_generator()()); |
| 147 | needWrite = true; |
| 148 | } |
| 149 | if (fileRevision < jsonRevision) |
| 150 | { |
| 151 | needWrite = true; |
| 152 | } |
| 153 | // write revision changes or system uuid changes immediately |
| 154 | if (needWrite) |
| 155 | { |
| 156 | writeData(); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void writeData() |
| 161 | { |
| 162 | std::ofstream persistentFile(filename); |
Ratan Gupta | 845cb7d | 2019-07-12 00:32:25 +0530 | [diff] [blame] | 163 | |
| 164 | // set the permission of the file to 640 |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 165 | std::filesystem::perms permission = |
| 166 | std::filesystem::perms::owner_read | |
| 167 | std::filesystem::perms::owner_write | |
| 168 | std::filesystem::perms::group_read; |
| 169 | std::filesystem::permissions(filename, permission); |
Ed Tanous | 5fb91ba | 2020-09-28 15:41:28 -0700 | [diff] [blame] | 170 | const auto& c = SessionStore::getInstance().getAuthMethodsConfig(); |
Ed Tanous | dc511aa | 2020-10-21 12:33:42 -0700 | [diff] [blame] | 171 | nlohmann::json data{ |
| 172 | {"auth_config", |
| 173 | {{"XToken", c.xtoken}, |
| 174 | {"Cookie", c.cookie}, |
| 175 | {"SessionToken", c.sessionToken}, |
| 176 | {"BasicAuth", c.basic}, |
| 177 | {"TLS", c.tls}} |
Ratan Gupta | 845cb7d | 2019-07-12 00:32:25 +0530 | [diff] [blame] | 178 | |
Ed Tanous | dc511aa | 2020-10-21 12:33:42 -0700 | [diff] [blame] | 179 | }, |
| 180 | {"system_uuid", systemUuid}, |
| 181 | {"revision", jsonRevision}, |
| 182 | {"timeout", SessionStore::getInstance().getTimeoutInSeconds()}}; |
Ed Tanous | 5fb91ba | 2020-09-28 15:41:28 -0700 | [diff] [blame] | 183 | |
| 184 | nlohmann::json& sessions = data["sessions"]; |
| 185 | sessions = nlohmann::json::array(); |
| 186 | for (const auto& p : SessionStore::getInstance().authTokens) |
| 187 | { |
| 188 | if (p.second->persistence != |
| 189 | persistent_data::PersistenceType::SINGLE_REQUEST) |
| 190 | { |
| 191 | sessions.push_back({ |
| 192 | {"unique_id", p.second->uniqueId}, |
| 193 | {"session_token", p.second->sessionToken}, |
| 194 | {"username", p.second->username}, |
| 195 | {"csrf_token", p.second->csrfToken}, |
Ed Tanous | 5fb91ba | 2020-09-28 15:41:28 -0700 | [diff] [blame] | 196 | #ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE |
| 197 | {"client_id", p.second->clientId}, |
| 198 | #endif |
| 199 | }); |
| 200 | } |
| 201 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 202 | persistentFile << data; |
| 203 | } |
| 204 | |
| 205 | std::string systemUuid{""}; |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 206 | }; |
| 207 | |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 208 | inline ConfigFile& getConfig() |
| 209 | { |
| 210 | static ConfigFile f; |
| 211 | return f; |
| 212 | } |
| 213 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 214 | } // namespace persistent_data |