From d88c701c8bd9e24de495f8dbe63a6e02ef67ffd2 Mon Sep 17 00:00:00 2001 From: Sangwan Kwon Date: Fri, 24 Apr 2020 16:52:20 +0900 Subject: [PATCH] Remove database Signed-off-by: Sangwan Kwon --- src/osquery/core/CMakeLists.txt | 2 - src/osquery/core/database/database.cpp | 59 ------ src/osquery/core/database/database.h | 99 ---------- .../core/database/in_memory_database.cpp | 173 ------------------ .../core/database/in_memory_database.h | 87 --------- 5 files changed, 420 deletions(-) delete mode 100644 src/osquery/core/database/database.cpp delete mode 100644 src/osquery/core/database/database.h delete mode 100644 src/osquery/core/database/in_memory_database.cpp delete mode 100644 src/osquery/core/database/in_memory_database.h diff --git a/src/osquery/core/CMakeLists.txt b/src/osquery/core/CMakeLists.txt index fce4e27..34d0bcd 100644 --- a/src/osquery/core/CMakeLists.txt +++ b/src/osquery/core/CMakeLists.txt @@ -14,8 +14,6 @@ ADD_OSQUERY_LIBRARY(osquery_core tables.cpp query.cpp - database/database.cpp - database/in_memory_database.cpp plugins/logger.cpp plugins/plugin.cpp plugins/sql.cpp diff --git a/src/osquery/core/database/database.cpp b/src/osquery/core/database/database.cpp deleted file mode 100644 index 80df93b..0000000 --- a/src/osquery/core/database/database.cpp +++ /dev/null @@ -1,59 +0,0 @@ -/** - * Copyright (c) 2018-present, Facebook, Inc. - * All rights reserved. - * - * This source code is licensed in accordance with the terms specified in - * the LICENSE file found in the root directory of this source tree. - */ - -#include -#include - -namespace osquery { - -Expected Database::getInt32(const std::string& domain, - const std::string& key) { - Expected string_value = getString(domain, key); - if (string_value) { - auto value = tryTo(*string_value); - if (value) { - return *value; - } else { - return createError(DatabaseError::FailToReadData, value.takeError()) - << "Failed to convert string to int"; - } - } else { - return string_value.takeError(); - } -} - -ExpectedSuccess Database::putInt32(const std::string& domain, - const std::string& key, - const int32_t value) { - std::string buffer = std::to_string(value); - return putString(domain, key, buffer); -} - -Expected Database::getInt32Or( - const std::string& domain, - const std::string& key, - const int32_t default_value) { - auto result = getInt32(domain, key); - if (!result && result.getError() == DatabaseError::KeyNotFound) { - return default_value; - } - return result; -} - -Expected Database::getStringOr( - const std::string& domain, - const std::string& key, - const std::string& default_value) { - auto result = getString(domain, key); - if (!result && result.getError() == DatabaseError::KeyNotFound) { - return default_value; - } - return result; -} - -} // namespace osquery diff --git a/src/osquery/core/database/database.h b/src/osquery/core/database/database.h deleted file mode 100644 index a683da8..0000000 --- a/src/osquery/core/database/database.h +++ /dev/null @@ -1,99 +0,0 @@ -/** - * Copyright (c) 2018-present, Facebook, Inc. - * All rights reserved. - * - * This source code is licensed in accordance with the terms specified in - * the LICENSE file found in the root directory of this source tree. - */ - -#pragma once - -#include - -#include -#include -#include -#include - -namespace osquery { - -enum class DatabaseError { - // Unknown error, currently unused - Unknown = 1, - DbIsNotOpen = 2, - InvalidPath = 3, - FailToDestroyDB = 4, - FailToOpenDatabase = 5, - FailToReadData = 6, - FailToWriteData = 7, - KeyNotFound = 8, - DomainNotFound = 9, - // Corruption or other unrecoverable error after DB can't be longer used - // Database should be closed, destroyed and opened again - // If this error was received during data access, then application - // is likely to die soon - // See message and/or underlying error for details - Panic = 10, -}; - -class Database { - public: - explicit Database(std::string name) : name_(std::move(name)) {} - virtual ~Database() = default; - - const std::string& getName() const { - return name_; - } - - virtual ExpectedSuccess open() = 0; - virtual void close() = 0; - - // This funcion should completely destroy db, so after next open - // db should be fresh new - // Implementation can expect that db is closed before - // calling destroyDB and should crash/fail in case when db is still open - virtual ExpectedSuccess destroyDB() = 0; - - // Return default value in case of NotFound error - Expected getInt32Or(const std::string& domain, - const std::string& key, - const int32_t default_value = 0); - Expected getStringOr( - const std::string& domain, - const std::string& key, - const std::string& default_value = ""); - - virtual Expected getInt32(const std::string& domain, - const std::string& key); - virtual Expected getString( - const std::string& domain, const std::string& key) = 0; - - virtual ExpectedSuccess putInt32(const std::string& domain, - const std::string& key, - const int32_t value); - virtual ExpectedSuccess putString( - const std::string& domain, - const std::string& key, - const std::string& value) = 0; - - virtual Expected, DatabaseError> getKeys( - const std::string& domain, const std::string& prefix = "") = 0; - - // This function designed to write batch of data as one operation and get - // as much performance as possbile. Because of this, db may not guarantee - // data consistency or atomic nature of operation - // Please see actual function implementation for details and limitations - virtual ExpectedSuccess putStringsUnsafe( - const std::string& domain, - const std::vector>& data) = 0; - - void panic(const Error& error) { - LOG(ERROR) << "Database did panic: " << error.getMessage(); - debug_only::fail("Database did panic"); - } - - private: - const std::string name_; -}; - -} // namespace osquery diff --git a/src/osquery/core/database/in_memory_database.cpp b/src/osquery/core/database/in_memory_database.cpp deleted file mode 100644 index 63002da..0000000 --- a/src/osquery/core/database/in_memory_database.cpp +++ /dev/null @@ -1,173 +0,0 @@ -/** - * Copyright (c) 2018-present, Facebook, Inc. - * All rights reserved. - * - * This source code is licensed in accordance with the terms specified in - * the LICENSE file found in the root directory of this source tree. - */ - -#include -#include - -#include -#include - -namespace osquery { - -template -std::vector InMemoryStorage::getKeys( - const std::string& prefix) const { - std::vector result; - for (const auto& iter : storage_) { - if (boost::starts_with(iter.first, prefix)) { - result.push_back(iter.first); - } - } - return result; -} - -template -void InMemoryStorage::put(const std::string& key, - const StorageType value) { - storage_[key] = value; -} - -template -Expected InMemoryStorage::get( - const std::string& key) const { - auto iter = storage_.find(key); - if (iter != storage_.end()) { - return iter->second; - } - return createError(DatabaseError::KeyNotFound) - << "Can't find value for key " << key; -} - -void InMemoryDatabase::close() { - VLOG(1) << "Closing db... "; - debug_only::verifyTrue(is_open_, "database is not open"); - is_open_ = false; - auto status = destroyDB(); - debug_only::verifyTrue(status.isValue(), - "InMemoryDatabase::destroyDB couldn't fail"); -} - -ExpectedSuccess InMemoryDatabase::destroyDB() { - VLOG(1) << "Destroying in memory db"; - storage_.clear(); - return Success(); -} - -ExpectedSuccess InMemoryDatabase::open() { - debug_only::verifyTrue(!is_open_, "database is already open"); - is_open_ = true; - return Success(); -} - -Error InMemoryDatabase::domainNotFoundError( - const std::string& domain) const { - return createError(DatabaseError::DomainNotFound) - << "Can't find domain: " << domain; -} - -template -Expected InMemoryDatabase::getValue(const std::string& domain, - const std::string& key) { - debug_only::verifyTrue(is_open_, "database is not open"); - if (!is_open_) { - return createError(DatabaseError::DbIsNotOpen) << "Database is closed"; - } - auto storage_iter = storage_.find(domain); - if (storage_iter == storage_.end()) { - return domainNotFoundError(domain); - } - std::lock_guard lock(storage_iter->second->getMutex()); - auto result = storage_iter->second->get(key); - if (result) { - DataType value = result.take(); - if (value.type() == typeid(T)) { - return boost::get(value); - } else { - auto error = createError(DatabaseError::KeyNotFound) - << "Requested wrong type for: " << domain << ":" << key - << " stored type: " << value.type().name() - << " requested type " - << boost::core::demangle(typeid(T).name()); - LOG(ERROR) << error.getMessage(); - debug_only::fail(error.getMessage().c_str()); - return std::move(error); - } - } - return result.takeError(); -} - -template -ExpectedSuccess InMemoryDatabase::putValue( - const std::string& domain, const std::string& key, const T& value) { - debug_only::verifyTrue(is_open_, "database is not open"); - if (!is_open_) { - return createError(DatabaseError::DbIsNotOpen) << "Database is closed"; - } - auto storage_iter = storage_.find(domain); - if (storage_iter == storage_.end()) { - return domainNotFoundError(domain); - } - std::lock_guard lock(storage_iter->second->getMutex()); - debug_only::verify( - [&storage_iter, &key]() { - auto result = storage_iter->second->get(key); - return result ? result.get().type() == typeid(T) : true; - }, - "changing type is not allowed"); - storage_iter->second->put(key, value); - return Success(); -} - -Expected InMemoryDatabase::getString( - const std::string& domain, const std::string& key) { - return getValue(domain, key); -} - -ExpectedSuccess InMemoryDatabase::putString( - const std::string& domain, - const std::string& key, - const std::string& value) { - return putValue(domain, key, value); -} - -Expected InMemoryDatabase::getInt32( - const std::string& domain, const std::string& key) { - return getValue(domain, key); -} - -ExpectedSuccess InMemoryDatabase::putInt32( - const std::string& domain, const std::string& key, const int32_t value) { - return putValue(domain, key, value); -} - -Expected, DatabaseError> InMemoryDatabase::getKeys( - const std::string& domain, const std::string& prefix) { - debug_only::verifyTrue(is_open_, "database is not open"); - auto storage_iter = storage_.find(domain); - if (storage_iter == storage_.end()) { - return domainNotFoundError(domain); - } - return storage_iter->second->getKeys(prefix); -} - -ExpectedSuccess InMemoryDatabase::putStringsUnsafe( - const std::string& domain, - const std::vector>& data) { - debug_only::verifyTrue(is_open_, "database is not open"); - auto storage_iter = storage_.find(domain); - if (storage_iter == storage_.end()) { - return domainNotFoundError(domain); - } - std::lock_guard lock(storage_iter->second->getMutex()); - for (const auto& pair : data) { - storage_iter->second->put(pair.first, pair.second); - } - return Success(); -} - -} // namespace osquery diff --git a/src/osquery/core/database/in_memory_database.h b/src/osquery/core/database/in_memory_database.h deleted file mode 100644 index d70a1af..0000000 --- a/src/osquery/core/database/in_memory_database.h +++ /dev/null @@ -1,87 +0,0 @@ -/** - * Copyright (c) 2018-present, Facebook, Inc. - * All rights reserved. - * - * This source code is licensed in accordance with the terms specified in - * the LICENSE file found in the root directory of this source tree. - */ - -#pragma once - -#include -#include -#include - -#include - -namespace osquery { - -template -class InMemoryStorage final { - public: - void put(const std::string& key, const StorageType value); - Expected get(const std::string& key) const; - std::vector getKeys(const std::string& prefix = "") const; - - std::mutex& getMutex() { - return mutex_; - } - - private: - std::unordered_map storage_; - std::mutex mutex_; -}; - -class InMemoryDatabase final : public Database { - public: - explicit InMemoryDatabase(std::string name) : Database(std::move(name)){}; - ~InMemoryDatabase() override {} - - ExpectedSuccess destroyDB() override; - ExpectedSuccess open() override; - - void close() override; - - Expected getInt32(const std::string& domain, - const std::string& key) override; - Expected getString( - const std::string& domain, const std::string& key) override; - - ExpectedSuccess putInt32(const std::string& domain, - const std::string& key, - const int32_t value) override; - ExpectedSuccess putString(const std::string& domain, - const std::string& key, - const std::string& value) override; - - Expected, DatabaseError> getKeys( - const std::string& domain, const std::string& prefix = "") override; - - // This method bypass type validation and will silently update value - // even if type was changed (e.g int->string) - ExpectedSuccess putStringsUnsafe( - const std::string& domain, - const std::vector>& data) override; - - private: - template - Expected getValue(const std::string& domain, - const std::string& key); - template - ExpectedSuccess putValue(const std::string& domain, - const std::string& key, - const T& value); - - Error domainNotFoundError(const std::string& domain) const; - - private: - bool is_open_ = false; - - using DataType = boost::variant; - using InMemoryStorageRef = std::unique_ptr>; - - // storage map is built on open, so no need to protect it with locks - std::unordered_map storage_; -}; - -} // namespace osquery -- 2.34.1