2019-11-09 15:33:07 +00:00
|
|
|
#include <Access/IAccessStorage.h>
|
2021-11-01 14:03:20 +00:00
|
|
|
#include <Access/Authentication.h>
|
2021-03-11 20:41:10 +00:00
|
|
|
#include <Access/Credentials.h>
|
2021-11-01 14:03:20 +00:00
|
|
|
#include <Access/User.h>
|
2019-11-09 15:33:07 +00:00
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <Common/quoteString.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <Poco/UUIDGenerator.h>
|
|
|
|
#include <Poco/Logger.h>
|
2021-10-02 19:47:35 +00:00
|
|
|
#include <base/FnTraits.h>
|
2021-11-18 20:54:18 +00:00
|
|
|
#include <boost/algorithm/string/join.hpp>
|
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
2019-11-09 15:33:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ACCESS_ENTITY_ALREADY_EXISTS;
|
2020-05-03 03:12:03 +00:00
|
|
|
extern const int ACCESS_ENTITY_NOT_FOUND;
|
2020-02-26 10:50:13 +00:00
|
|
|
extern const int ACCESS_STORAGE_READONLY;
|
2020-10-04 19:55:58 +00:00
|
|
|
extern const int WRONG_PASSWORD;
|
|
|
|
extern const int IP_ADDRESS_NOT_ALLOWED;
|
2020-09-17 09:58:34 +00:00
|
|
|
extern const int AUTHENTICATION_FAILED;
|
2020-06-03 14:17:41 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-03 03:12:03 +00:00
|
|
|
namespace
|
|
|
|
{
|
2020-06-01 10:45:07 +00:00
|
|
|
String outputID(const UUID & id)
|
|
|
|
{
|
|
|
|
return "ID(" + toString(id) + ")";
|
|
|
|
}
|
2020-05-03 03:12:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-18 20:54:18 +00:00
|
|
|
std::vector<UUID> IAccessStorage::findAll(AccessEntityType type) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
|
|
|
return findAllImpl(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-18 20:54:18 +00:00
|
|
|
std::optional<UUID> IAccessStorage::find(AccessEntityType type, const String & name) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
|
|
|
return findImpl(type, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-18 20:54:18 +00:00
|
|
|
std::vector<UUID> IAccessStorage::find(AccessEntityType type, const Strings & names) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
|
|
|
std::vector<UUID> ids;
|
|
|
|
ids.reserve(names.size());
|
|
|
|
for (const String & name : names)
|
|
|
|
{
|
|
|
|
auto id = findImpl(type, name);
|
|
|
|
if (id)
|
|
|
|
ids.push_back(*id);
|
|
|
|
}
|
|
|
|
return ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-18 20:54:18 +00:00
|
|
|
UUID IAccessStorage::getID(AccessEntityType type, const String & name) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
|
|
|
auto id = findImpl(type, name);
|
|
|
|
if (id)
|
|
|
|
return *id;
|
|
|
|
throwNotFound(type, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-18 20:54:18 +00:00
|
|
|
std::vector<UUID> IAccessStorage::getIDs(AccessEntityType type, const Strings & names) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
|
|
|
std::vector<UUID> ids;
|
|
|
|
ids.reserve(names.size());
|
|
|
|
for (const String & name : names)
|
|
|
|
ids.push_back(getID(type, name));
|
|
|
|
return ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-11 16:29:38 +00:00
|
|
|
String IAccessStorage::readName(const UUID & id) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
2021-12-11 16:29:38 +00:00
|
|
|
return *readNameImpl(id, /* throw_if_not_exists = */ true);
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-11 16:29:38 +00:00
|
|
|
std::optional<String> IAccessStorage::readName(const UUID & id, bool throw_if_not_exists) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
2021-12-11 16:29:38 +00:00
|
|
|
return readNameImpl(id, throw_if_not_exists);
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-11 16:29:38 +00:00
|
|
|
Strings IAccessStorage::readNames(const std::vector<UUID> & ids, bool throw_if_not_exists) const
|
2021-07-22 16:07:03 +00:00
|
|
|
{
|
|
|
|
Strings res;
|
|
|
|
res.reserve(ids.size());
|
|
|
|
for (const auto & id : ids)
|
2021-12-11 16:29:38 +00:00
|
|
|
{
|
|
|
|
if (auto name = readNameImpl(id, throw_if_not_exists))
|
|
|
|
res.emplace_back(std::move(name).value());
|
|
|
|
}
|
2021-07-22 16:07:03 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-09 15:33:07 +00:00
|
|
|
std::optional<String> IAccessStorage::tryReadName(const UUID & id) const
|
|
|
|
{
|
2021-12-11 16:29:38 +00:00
|
|
|
return readName(id, /* throw_if_not_exists = */ false);
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-22 16:07:03 +00:00
|
|
|
Strings IAccessStorage::tryReadNames(const std::vector<UUID> & ids) const
|
|
|
|
{
|
2021-12-11 16:29:38 +00:00
|
|
|
return readNames(ids, /* throw_if_not_exists = */ false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::optional<String> IAccessStorage::readNameImpl(const UUID & id, bool throw_if_not_exists) const
|
|
|
|
{
|
|
|
|
if (auto entity = read(id, throw_if_not_exists))
|
|
|
|
return entity->getName();
|
|
|
|
return std::nullopt;
|
2021-07-22 16:07:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-09 15:33:07 +00:00
|
|
|
UUID IAccessStorage::insert(const AccessEntityPtr & entity)
|
|
|
|
{
|
2021-12-11 22:18:10 +00:00
|
|
|
return *insertImpl(entity, /* replace_if_exists = */ false, /* throw_if_exists = */ true);
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-11 22:18:10 +00:00
|
|
|
std::optional<UUID> IAccessStorage::insert(const AccessEntityPtr & entity, bool replace_if_exists, bool throw_if_exists)
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
2021-12-11 22:18:10 +00:00
|
|
|
return insertImpl(entity, replace_if_exists, throw_if_exists);
|
|
|
|
}
|
2020-07-21 19:31:27 +00:00
|
|
|
|
2021-12-11 22:18:10 +00:00
|
|
|
|
|
|
|
std::vector<UUID> IAccessStorage::insert(const std::vector<AccessEntityPtr> & multiple_entities, bool replace_if_exists, bool throw_if_exists)
|
|
|
|
{
|
|
|
|
if (multiple_entities.empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
if (multiple_entities.size() == 1)
|
2020-07-21 19:31:27 +00:00
|
|
|
{
|
2021-12-11 22:18:10 +00:00
|
|
|
if (auto id = insert(multiple_entities[0], replace_if_exists, throw_if_exists))
|
|
|
|
return {*id};
|
|
|
|
return {};
|
2020-07-21 19:31:27 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 22:18:10 +00:00
|
|
|
std::vector<AccessEntityPtr> successfully_inserted;
|
|
|
|
try
|
2020-07-21 19:31:27 +00:00
|
|
|
{
|
2021-12-11 22:18:10 +00:00
|
|
|
std::vector<UUID> ids;
|
|
|
|
for (const auto & entity : multiple_entities)
|
|
|
|
{
|
|
|
|
if (auto id = insertImpl(entity, replace_if_exists, throw_if_exists))
|
|
|
|
{
|
|
|
|
successfully_inserted.push_back(entity);
|
|
|
|
ids.push_back(*id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ids;
|
|
|
|
}
|
|
|
|
catch (Exception & e)
|
|
|
|
{
|
|
|
|
if (!successfully_inserted.empty())
|
|
|
|
{
|
|
|
|
String successfully_inserted_str;
|
|
|
|
for (auto entity : successfully_inserted)
|
|
|
|
{
|
|
|
|
if (!successfully_inserted_str.empty())
|
|
|
|
successfully_inserted_str += ", ";
|
|
|
|
successfully_inserted_str += entity->formatTypeWithName();
|
|
|
|
}
|
|
|
|
e.addMessage("After successfully inserting {}/{}: {}", successfully_inserted.size(), multiple_entities.size(), successfully_inserted_str);
|
|
|
|
}
|
|
|
|
e.rethrow();
|
|
|
|
__builtin_unreachable();
|
2020-07-21 19:31:27 +00:00
|
|
|
}
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::optional<UUID> IAccessStorage::tryInsert(const AccessEntityPtr & entity)
|
|
|
|
{
|
2021-12-11 22:18:10 +00:00
|
|
|
return insert(entity, /* replace_if_exists = */ false, /* throw_if_exists = */ false);
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<UUID> IAccessStorage::tryInsert(const std::vector<AccessEntityPtr> & multiple_entities)
|
|
|
|
{
|
2021-12-11 22:18:10 +00:00
|
|
|
return insert(multiple_entities, /* replace_if_exists = */ false, /* throw_if_exists = */ false);
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UUID IAccessStorage::insertOrReplace(const AccessEntityPtr & entity)
|
|
|
|
{
|
2021-12-11 22:18:10 +00:00
|
|
|
return *insertImpl(entity, /* replace_if_exists = */ true, /* throw_if_exists = */ false);
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<UUID> IAccessStorage::insertOrReplace(const std::vector<AccessEntityPtr> & multiple_entities)
|
|
|
|
{
|
2021-12-11 22:18:10 +00:00
|
|
|
return insert(multiple_entities, /* replace_if_exists = */ true, /* throw_if_exists = */ false);
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-12 13:19:07 +00:00
|
|
|
bool IAccessStorage::remove(const UUID & id, bool throw_if_not_exists)
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
2021-12-12 13:19:07 +00:00
|
|
|
return removeImpl(id, throw_if_not_exists);
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-12 13:19:07 +00:00
|
|
|
std::vector<UUID> IAccessStorage::remove(const std::vector<UUID> & ids, bool throw_if_not_exists)
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
2021-12-12 13:19:07 +00:00
|
|
|
if (ids.empty())
|
|
|
|
return {};
|
|
|
|
if (ids.size() == 1)
|
|
|
|
return remove(ids[0], throw_if_not_exists) ? ids : std::vector<UUID>{};
|
2020-07-21 19:31:27 +00:00
|
|
|
|
2021-12-12 13:19:07 +00:00
|
|
|
Strings removed_names;
|
|
|
|
try
|
2020-07-21 19:31:27 +00:00
|
|
|
{
|
2021-12-12 13:19:07 +00:00
|
|
|
std::vector<UUID> removed_ids;
|
|
|
|
for (const auto & id : ids)
|
|
|
|
{
|
|
|
|
auto name = tryReadName(id);
|
|
|
|
if (remove(id, throw_if_not_exists))
|
|
|
|
{
|
|
|
|
removed_ids.push_back(id);
|
|
|
|
if (name)
|
|
|
|
removed_names.push_back(std::move(name).value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return removed_ids;
|
2020-07-21 19:31:27 +00:00
|
|
|
}
|
2021-12-12 13:19:07 +00:00
|
|
|
catch (Exception & e)
|
2020-07-21 19:31:27 +00:00
|
|
|
{
|
2021-12-12 13:19:07 +00:00
|
|
|
if (!removed_names.empty())
|
|
|
|
{
|
|
|
|
String removed_names_str;
|
|
|
|
for (const auto & name : removed_names)
|
|
|
|
{
|
|
|
|
if (!removed_names_str.empty())
|
|
|
|
removed_names_str += ", ";
|
|
|
|
removed_names_str += backQuote(name);
|
|
|
|
}
|
|
|
|
e.addMessage("After successfully removing {}/{}: {}", removed_names.size(), ids.size(), removed_names_str);
|
|
|
|
}
|
|
|
|
e.rethrow();
|
|
|
|
__builtin_unreachable();
|
2020-07-21 19:31:27 +00:00
|
|
|
}
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool IAccessStorage::tryRemove(const UUID & id)
|
|
|
|
{
|
2021-12-12 13:19:07 +00:00
|
|
|
return remove(id, /* throw_if_not_exists = */ false);
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<UUID> IAccessStorage::tryRemove(const std::vector<UUID> & ids)
|
|
|
|
{
|
2021-12-12 13:19:07 +00:00
|
|
|
return remove(ids, /* throw_if_not_exists = */ false);
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-12 09:42:15 +00:00
|
|
|
bool IAccessStorage::update(const UUID & id, const UpdateFunc & update_func, bool throw_if_not_exists)
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
2021-12-12 09:42:15 +00:00
|
|
|
return updateImpl(id, update_func, throw_if_not_exists);
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-12 09:42:15 +00:00
|
|
|
std::vector<UUID> IAccessStorage::update(const std::vector<UUID> & ids, const UpdateFunc & update_func, bool throw_if_not_exists)
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
2021-12-12 09:42:15 +00:00
|
|
|
if (ids.empty())
|
|
|
|
return {};
|
|
|
|
if (ids.size() == 1)
|
|
|
|
return update(ids[0], update_func, throw_if_not_exists) ? ids : std::vector<UUID>{};
|
2020-07-21 19:31:27 +00:00
|
|
|
|
2021-12-12 09:42:15 +00:00
|
|
|
Strings names_of_updated;
|
|
|
|
try
|
2020-07-21 19:31:27 +00:00
|
|
|
{
|
2021-12-12 09:42:15 +00:00
|
|
|
std::vector<UUID> ids_of_updated;
|
|
|
|
for (const auto & id : ids)
|
|
|
|
{
|
|
|
|
auto name = tryReadName(id);
|
|
|
|
if (update(id, update_func, throw_if_not_exists))
|
|
|
|
{
|
|
|
|
ids_of_updated.push_back(id);
|
|
|
|
if (name)
|
|
|
|
names_of_updated.push_back(std::move(name).value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ids_of_updated;
|
2020-07-21 19:31:27 +00:00
|
|
|
}
|
2021-12-12 09:42:15 +00:00
|
|
|
catch (Exception & e)
|
2020-07-21 19:31:27 +00:00
|
|
|
{
|
2021-12-12 09:42:15 +00:00
|
|
|
if (!names_of_updated.empty())
|
|
|
|
{
|
|
|
|
String names_of_updated_str;
|
|
|
|
for (const auto & name : names_of_updated)
|
|
|
|
{
|
|
|
|
if (!names_of_updated_str.empty())
|
|
|
|
names_of_updated_str += ", ";
|
|
|
|
names_of_updated_str += backQuote(name);
|
|
|
|
}
|
|
|
|
e.addMessage("After successfully updating {}/{}: {}", names_of_updated.size(), ids.size(), names_of_updated_str);
|
|
|
|
}
|
|
|
|
e.rethrow();
|
|
|
|
__builtin_unreachable();
|
2020-07-21 19:31:27 +00:00
|
|
|
}
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool IAccessStorage::tryUpdate(const UUID & id, const UpdateFunc & update_func)
|
|
|
|
{
|
2021-12-12 09:42:15 +00:00
|
|
|
return update(id, update_func, /* throw_if_not_exists = */ false);
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<UUID> IAccessStorage::tryUpdate(const std::vector<UUID> & ids, const UpdateFunc & update_func)
|
|
|
|
{
|
2021-12-12 09:42:15 +00:00
|
|
|
return update(ids, update_func, /* throw_if_not_exists = */ false);
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-18 20:54:18 +00:00
|
|
|
scope_guard IAccessStorage::subscribeForChanges(AccessEntityType type, const OnChangedHandler & handler) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
|
|
|
return subscribeForChangesImpl(type, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-15 19:55:21 +00:00
|
|
|
scope_guard IAccessStorage::subscribeForChanges(const UUID & id, const OnChangedHandler & handler) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
|
|
|
return subscribeForChangesImpl(id, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-15 19:55:21 +00:00
|
|
|
scope_guard IAccessStorage::subscribeForChanges(const std::vector<UUID> & ids, const OnChangedHandler & handler) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
2021-06-15 19:55:21 +00:00
|
|
|
scope_guard subscriptions;
|
2019-11-09 15:33:07 +00:00
|
|
|
for (const auto & id : ids)
|
2020-01-29 15:51:12 +00:00
|
|
|
subscriptions.join(subscribeForChangesImpl(id, handler));
|
|
|
|
return subscriptions;
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void IAccessStorage::notify(const Notifications & notifications)
|
|
|
|
{
|
|
|
|
for (const auto & [fn, id, new_entity] : notifications)
|
|
|
|
fn(id, new_entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-22 21:50:15 +00:00
|
|
|
UUID IAccessStorage::authenticate(
|
2021-03-11 20:41:10 +00:00
|
|
|
const Credentials & credentials,
|
2020-09-17 09:58:34 +00:00
|
|
|
const Poco::Net::IPAddress & address,
|
2020-10-04 20:24:09 +00:00
|
|
|
const ExternalAuthenticators & external_authenticators,
|
2020-10-06 15:23:08 +00:00
|
|
|
bool replace_exception_with_cannot_authenticate) const
|
2020-09-17 09:58:34 +00:00
|
|
|
{
|
2020-10-06 15:23:08 +00:00
|
|
|
try
|
|
|
|
{
|
2021-11-22 21:50:15 +00:00
|
|
|
return authenticateImpl(credentials, address, external_authenticators);
|
2020-10-04 19:55:58 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2020-10-06 15:23:08 +00:00
|
|
|
if (!replace_exception_with_cannot_authenticate)
|
2020-10-04 20:24:09 +00:00
|
|
|
throw;
|
|
|
|
|
2021-08-10 05:36:28 +00:00
|
|
|
tryLogCurrentException(getLogger(), "from: " + address.toString() + ", user: " + credentials.getUserName() + ": Authentication failed");
|
2021-03-11 20:41:10 +00:00
|
|
|
throwCannotAuthenticate(credentials.getUserName());
|
2020-10-04 19:55:58 +00:00
|
|
|
}
|
2020-09-17 09:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-22 21:50:15 +00:00
|
|
|
UUID IAccessStorage::authenticateImpl(
|
2021-03-11 20:41:10 +00:00
|
|
|
const Credentials & credentials,
|
2020-09-17 09:58:34 +00:00
|
|
|
const Poco::Net::IPAddress & address,
|
|
|
|
const ExternalAuthenticators & external_authenticators) const
|
|
|
|
{
|
2021-03-11 20:41:10 +00:00
|
|
|
if (auto id = find<User>(credentials.getUserName()))
|
2020-09-17 09:58:34 +00:00
|
|
|
{
|
|
|
|
if (auto user = tryRead<User>(*id))
|
|
|
|
{
|
2021-11-22 21:50:15 +00:00
|
|
|
if (!isAddressAllowed(*user, address))
|
2020-10-04 19:55:58 +00:00
|
|
|
throwAddressNotAllowed(address);
|
|
|
|
|
2021-11-22 21:50:15 +00:00
|
|
|
if (!areCredentialsValid(*user, credentials, external_authenticators))
|
2021-03-11 20:41:10 +00:00
|
|
|
throwInvalidCredentials();
|
|
|
|
|
2020-10-04 19:55:58 +00:00
|
|
|
return *id;
|
2020-09-17 09:58:34 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-18 20:54:18 +00:00
|
|
|
throwNotFound(AccessEntityType::USER, credentials.getUserName());
|
2020-09-17 09:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-22 21:50:15 +00:00
|
|
|
bool IAccessStorage::areCredentialsValid(
|
2021-03-11 20:41:10 +00:00
|
|
|
const User & user,
|
|
|
|
const Credentials & credentials,
|
|
|
|
const ExternalAuthenticators & external_authenticators) const
|
2020-09-17 09:58:34 +00:00
|
|
|
{
|
2021-03-11 20:41:10 +00:00
|
|
|
if (!credentials.isReady())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (credentials.getUserName() != user.getName())
|
|
|
|
return false;
|
|
|
|
|
2021-11-01 14:03:20 +00:00
|
|
|
return Authentication::areCredentialsValid(credentials, user.auth_data, external_authenticators);
|
2020-09-17 09:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-22 21:50:15 +00:00
|
|
|
bool IAccessStorage::isAddressAllowed(const User & user, const Poco::Net::IPAddress & address) const
|
2020-09-17 09:58:34 +00:00
|
|
|
{
|
|
|
|
return user.allowed_client_hosts.contains(address);
|
|
|
|
}
|
|
|
|
|
2021-03-11 20:41:10 +00:00
|
|
|
|
2019-11-09 15:33:07 +00:00
|
|
|
UUID IAccessStorage::generateRandomID()
|
|
|
|
{
|
|
|
|
static Poco::UUIDGenerator generator;
|
|
|
|
UUID id;
|
|
|
|
generator.createRandom().copyTo(reinterpret_cast<char *>(&id));
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Poco::Logger * IAccessStorage::getLogger() const
|
|
|
|
{
|
|
|
|
Poco::Logger * ptr = log.load();
|
|
|
|
if (!ptr)
|
|
|
|
log.store(ptr = &Poco::Logger::get("Access(" + storage_name + ")"), std::memory_order_relaxed);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void IAccessStorage::throwNotFound(const UUID & id) const
|
|
|
|
{
|
2020-08-12 20:48:53 +00:00
|
|
|
throw Exception(outputID(id) + " not found in " + getStorageName(), ErrorCodes::ACCESS_ENTITY_NOT_FOUND);
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-18 20:54:18 +00:00
|
|
|
void IAccessStorage::throwNotFound(AccessEntityType type, const String & name) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
2021-11-18 20:54:18 +00:00
|
|
|
int error_code = AccessEntityTypeInfo::get(type).not_found_error_code;
|
|
|
|
throw Exception("There is no " + formatEntityTypeWithName(type, name) + " in " + getStorageName(), error_code);
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-18 20:54:18 +00:00
|
|
|
void IAccessStorage::throwBadCast(const UUID & id, AccessEntityType type, const String & name, AccessEntityType required_type)
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
|
|
|
throw Exception(
|
2021-11-18 20:54:18 +00:00
|
|
|
outputID(id) + ": " + formatEntityTypeWithName(type, name) + " expected to be of type " + toString(required_type),
|
2020-06-03 12:59:11 +00:00
|
|
|
ErrorCodes::LOGICAL_ERROR);
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-18 20:54:18 +00:00
|
|
|
void IAccessStorage::throwIDCollisionCannotInsert(const UUID & id, AccessEntityType type, const String & name, AccessEntityType existing_type, const String & existing_name) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
|
|
|
throw Exception(
|
2021-11-18 20:54:18 +00:00
|
|
|
formatEntityTypeWithName(type, name) + ": cannot insert because the " + outputID(id) + " is already used by "
|
|
|
|
+ formatEntityTypeWithName(existing_type, existing_name) + " in " + getStorageName(),
|
2019-11-09 15:33:07 +00:00
|
|
|
ErrorCodes::ACCESS_ENTITY_ALREADY_EXISTS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-18 20:54:18 +00:00
|
|
|
void IAccessStorage::throwNameCollisionCannotInsert(AccessEntityType type, const String & name) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
|
|
|
throw Exception(
|
2021-11-18 20:54:18 +00:00
|
|
|
formatEntityTypeWithName(type, name) + ": cannot insert because " + formatEntityTypeWithName(type, name) + " already exists in "
|
2020-08-12 20:48:53 +00:00
|
|
|
+ getStorageName(),
|
2019-11-09 15:33:07 +00:00
|
|
|
ErrorCodes::ACCESS_ENTITY_ALREADY_EXISTS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-18 20:54:18 +00:00
|
|
|
void IAccessStorage::throwNameCollisionCannotRename(AccessEntityType type, const String & old_name, const String & new_name) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
|
|
|
throw Exception(
|
2021-11-18 20:54:18 +00:00
|
|
|
formatEntityTypeWithName(type, old_name) + ": cannot rename to " + backQuote(new_name) + " because "
|
|
|
|
+ formatEntityTypeWithName(type, new_name) + " already exists in " + getStorageName(),
|
2019-11-09 15:33:07 +00:00
|
|
|
ErrorCodes::ACCESS_ENTITY_ALREADY_EXISTS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-18 20:54:18 +00:00
|
|
|
void IAccessStorage::throwReadonlyCannotInsert(AccessEntityType type, const String & name) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
|
|
|
throw Exception(
|
2021-11-18 20:54:18 +00:00
|
|
|
"Cannot insert " + formatEntityTypeWithName(type, name) + " to " + getStorageName() + " because this storage is readonly",
|
2020-02-26 10:50:13 +00:00
|
|
|
ErrorCodes::ACCESS_STORAGE_READONLY);
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-18 20:54:18 +00:00
|
|
|
void IAccessStorage::throwReadonlyCannotUpdate(AccessEntityType type, const String & name) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
|
|
|
throw Exception(
|
2021-11-18 20:54:18 +00:00
|
|
|
"Cannot update " + formatEntityTypeWithName(type, name) + " in " + getStorageName() + " because this storage is readonly",
|
2020-02-26 10:50:13 +00:00
|
|
|
ErrorCodes::ACCESS_STORAGE_READONLY);
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-18 20:54:18 +00:00
|
|
|
void IAccessStorage::throwReadonlyCannotRemove(AccessEntityType type, const String & name) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
|
|
|
throw Exception(
|
2021-11-18 20:54:18 +00:00
|
|
|
"Cannot remove " + formatEntityTypeWithName(type, name) + " from " + getStorageName() + " because this storage is readonly",
|
2020-02-26 10:50:13 +00:00
|
|
|
ErrorCodes::ACCESS_STORAGE_READONLY);
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
2020-09-17 09:58:34 +00:00
|
|
|
|
2020-10-04 19:55:58 +00:00
|
|
|
void IAccessStorage::throwAddressNotAllowed(const Poco::Net::IPAddress & address)
|
|
|
|
{
|
|
|
|
throw Exception("Connections from " + address.toString() + " are not allowed", ErrorCodes::IP_ADDRESS_NOT_ALLOWED);
|
|
|
|
}
|
|
|
|
|
2021-03-11 20:41:10 +00:00
|
|
|
void IAccessStorage::throwInvalidCredentials()
|
2020-10-04 19:55:58 +00:00
|
|
|
{
|
2021-03-11 20:41:10 +00:00
|
|
|
throw Exception("Invalid credentials", ErrorCodes::WRONG_PASSWORD);
|
2020-10-04 19:55:58 +00:00
|
|
|
}
|
2020-09-17 09:58:34 +00:00
|
|
|
|
|
|
|
void IAccessStorage::throwCannotAuthenticate(const String & user_name)
|
|
|
|
{
|
2020-10-27 11:04:03 +00:00
|
|
|
/// We use the same message for all authentication failures because we don't want to give away any unnecessary information for security reasons,
|
2020-09-17 09:58:34 +00:00
|
|
|
/// only the log will show the exact reason.
|
|
|
|
throw Exception(user_name + ": Authentication failed: password is incorrect or there is no user with such name", ErrorCodes::AUTHENTICATION_FAILED);
|
|
|
|
}
|
|
|
|
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|