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