2019-11-09 15:33:07 +00:00
|
|
|
#include <Access/IAccessStorage.h>
|
2020-09-17 09:58:34 +00:00
|
|
|
#include <Access/User.h>
|
2021-03-11 20:41:10 +00:00
|
|
|
#include <Access/Credentials.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>
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
using EntityType = IAccessStorage::EntityType;
|
|
|
|
using EntityTypeInfo = IAccessStorage::EntityTypeInfo;
|
|
|
|
|
2020-06-01 10:45:07 +00:00
|
|
|
|
|
|
|
String outputID(const UUID & id)
|
|
|
|
{
|
|
|
|
return "ID(" + toString(id) + ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
String outputTypeAndNameOrID(const IAccessStorage & storage, const UUID & id)
|
|
|
|
{
|
|
|
|
auto entity = storage.tryRead(id);
|
|
|
|
if (entity)
|
|
|
|
return entity->outputTypeAndName();
|
|
|
|
return outputID(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-21 19:31:27 +00:00
|
|
|
template <typename Func>
|
|
|
|
bool tryCall(const Func & function)
|
2020-05-03 03:12:03 +00:00
|
|
|
{
|
2020-06-01 10:45:07 +00:00
|
|
|
try
|
|
|
|
{
|
2020-07-21 19:31:27 +00:00
|
|
|
function();
|
|
|
|
return true;
|
2020-06-01 10:45:07 +00:00
|
|
|
}
|
2020-07-21 19:31:27 +00:00
|
|
|
catch (...)
|
2020-06-01 10:45:07 +00:00
|
|
|
{
|
2020-07-21 19:31:27 +00:00
|
|
|
return false;
|
2020-06-01 10:45:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-21 19:31:27 +00:00
|
|
|
class ErrorsTracker
|
2020-06-01 10:45:07 +00:00
|
|
|
{
|
2020-07-21 19:31:27 +00:00
|
|
|
public:
|
|
|
|
explicit ErrorsTracker(size_t count_) { succeed.reserve(count_); }
|
2020-06-01 10:45:07 +00:00
|
|
|
|
2020-07-21 19:31:27 +00:00
|
|
|
template <typename Func>
|
|
|
|
bool tryCall(const Func & func)
|
2020-06-01 10:45:07 +00:00
|
|
|
{
|
2020-07-21 19:31:27 +00:00
|
|
|
try
|
2020-06-01 10:45:07 +00:00
|
|
|
{
|
2020-07-21 19:31:27 +00:00
|
|
|
func();
|
2020-06-01 10:45:07 +00:00
|
|
|
}
|
2020-07-21 19:31:27 +00:00
|
|
|
catch (Exception & e)
|
2020-06-01 10:45:07 +00:00
|
|
|
{
|
2020-07-21 19:31:27 +00:00
|
|
|
if (!exception)
|
|
|
|
exception.emplace(e);
|
|
|
|
succeed.push_back(false);
|
|
|
|
return false;
|
2020-06-01 10:45:07 +00:00
|
|
|
}
|
2020-07-21 19:31:27 +00:00
|
|
|
catch (Poco::Exception & e)
|
2020-06-01 10:45:07 +00:00
|
|
|
{
|
2020-07-21 19:31:27 +00:00
|
|
|
if (!exception)
|
|
|
|
exception.emplace(Exception::CreateFromPocoTag{}, e);
|
|
|
|
succeed.push_back(false);
|
|
|
|
return false;
|
2020-06-01 10:45:07 +00:00
|
|
|
}
|
2020-07-21 19:31:27 +00:00
|
|
|
catch (std::exception & e)
|
|
|
|
{
|
|
|
|
if (!exception)
|
|
|
|
exception.emplace(Exception::CreateFromSTDTag{}, e);
|
|
|
|
succeed.push_back(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
succeed.push_back(true);
|
|
|
|
return true;
|
2020-06-01 10:45:07 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 19:31:27 +00:00
|
|
|
bool errors() const { return exception.has_value(); }
|
|
|
|
|
|
|
|
void showErrors(const char * format, const std::function<String(size_t)> & get_name_function)
|
2020-06-01 10:45:07 +00:00
|
|
|
{
|
2020-07-21 19:31:27 +00:00
|
|
|
if (!exception)
|
|
|
|
return;
|
|
|
|
|
2020-06-01 10:45:07 +00:00
|
|
|
Strings succeeded_names_list;
|
|
|
|
Strings failed_names_list;
|
2020-07-21 19:31:27 +00:00
|
|
|
for (size_t i = 0; i != succeed.size(); ++i)
|
2020-06-01 10:45:07 +00:00
|
|
|
{
|
2020-07-21 19:31:27 +00:00
|
|
|
String name = get_name_function(i);
|
|
|
|
if (succeed[i])
|
2020-06-01 10:45:07 +00:00
|
|
|
succeeded_names_list.emplace_back(name);
|
|
|
|
else
|
|
|
|
failed_names_list.emplace_back(name);
|
|
|
|
}
|
|
|
|
String succeeded_names = boost::algorithm::join(succeeded_names_list, ", ");
|
|
|
|
String failed_names = boost::algorithm::join(failed_names_list, ", ");
|
|
|
|
if (succeeded_names.empty())
|
|
|
|
succeeded_names = "none";
|
|
|
|
|
2020-07-21 19:31:27 +00:00
|
|
|
String error_message = format;
|
2020-06-01 10:45:07 +00:00
|
|
|
boost::replace_all(error_message, "{succeeded_names}", succeeded_names);
|
|
|
|
boost::replace_all(error_message, "{failed_names}", failed_names);
|
|
|
|
exception->addMessage(error_message);
|
|
|
|
exception->rethrow();
|
|
|
|
}
|
2020-07-21 19:31:27 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<bool> succeed;
|
|
|
|
std::optional<Exception> exception;
|
|
|
|
};
|
2020-05-03 03:12:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<UUID> IAccessStorage::findAll(EntityType type) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
|
|
|
return findAllImpl(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-03 03:12:03 +00:00
|
|
|
std::optional<UUID> IAccessStorage::find(EntityType type, const String & name) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
|
|
|
return findImpl(type, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-03 03:12:03 +00:00
|
|
|
std::vector<UUID> IAccessStorage::find(EntityType 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-03 03:12:03 +00:00
|
|
|
UUID IAccessStorage::getID(EntityType 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-03 03:12:03 +00:00
|
|
|
std::vector<UUID> IAccessStorage::getIDs(EntityType 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool IAccessStorage::exists(const UUID & id) const
|
|
|
|
{
|
|
|
|
return existsImpl(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AccessEntityPtr IAccessStorage::tryReadBase(const UUID & id) const
|
|
|
|
{
|
2020-07-21 19:31:27 +00:00
|
|
|
AccessEntityPtr entity;
|
|
|
|
auto func = [&] { entity = readImpl(id); };
|
|
|
|
if (!tryCall(func))
|
|
|
|
return nullptr;
|
|
|
|
return entity;
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
String IAccessStorage::readName(const UUID & id) const
|
|
|
|
{
|
|
|
|
return readNameImpl(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::optional<String> IAccessStorage::tryReadName(const UUID & id) const
|
|
|
|
{
|
2020-07-21 19:31:27 +00:00
|
|
|
String name;
|
|
|
|
auto func = [&] { name = readNameImpl(id); };
|
|
|
|
if (!tryCall(func))
|
|
|
|
return {};
|
|
|
|
return name;
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UUID IAccessStorage::insert(const AccessEntityPtr & entity)
|
|
|
|
{
|
|
|
|
return insertImpl(entity, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<UUID> IAccessStorage::insert(const std::vector<AccessEntityPtr> & multiple_entities)
|
|
|
|
{
|
2020-07-21 19:31:27 +00:00
|
|
|
ErrorsTracker tracker(multiple_entities.size());
|
|
|
|
|
|
|
|
std::vector<UUID> ids;
|
|
|
|
for (const auto & entity : multiple_entities)
|
|
|
|
{
|
|
|
|
UUID id;
|
|
|
|
auto func = [&] { id = insertImpl(entity, /* replace_if_exists = */ false); };
|
|
|
|
if (tracker.tryCall(func))
|
|
|
|
ids.push_back(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tracker.errors())
|
|
|
|
{
|
|
|
|
auto get_name_function = [&](size_t i) { return multiple_entities[i]->outputTypeAndName(); };
|
|
|
|
tracker.showErrors("Couldn't insert {failed_names}. Successfully inserted: {succeeded_names}", get_name_function);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ids;
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::optional<UUID> IAccessStorage::tryInsert(const AccessEntityPtr & entity)
|
|
|
|
{
|
2020-07-21 19:31:27 +00:00
|
|
|
UUID id;
|
|
|
|
auto func = [&] { id = insertImpl(entity, /* replace_if_exists = */ false); };
|
|
|
|
if (!tryCall(func))
|
|
|
|
return {};
|
|
|
|
return id;
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<UUID> IAccessStorage::tryInsert(const std::vector<AccessEntityPtr> & multiple_entities)
|
|
|
|
{
|
2020-07-21 19:31:27 +00:00
|
|
|
std::vector<UUID> ids;
|
|
|
|
for (const auto & entity : multiple_entities)
|
|
|
|
{
|
|
|
|
UUID id;
|
|
|
|
auto func = [&] { id = insertImpl(entity, /* replace_if_exists = */ false); };
|
|
|
|
if (tryCall(func))
|
|
|
|
ids.push_back(id);
|
|
|
|
}
|
|
|
|
return ids;
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UUID IAccessStorage::insertOrReplace(const AccessEntityPtr & entity)
|
|
|
|
{
|
2020-07-21 19:31:27 +00:00
|
|
|
return insertImpl(entity, /* replace_if_exists = */ true);
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<UUID> IAccessStorage::insertOrReplace(const std::vector<AccessEntityPtr> & multiple_entities)
|
|
|
|
{
|
2020-07-21 19:31:27 +00:00
|
|
|
ErrorsTracker tracker(multiple_entities.size());
|
|
|
|
|
|
|
|
std::vector<UUID> ids;
|
|
|
|
for (const auto & entity : multiple_entities)
|
|
|
|
{
|
|
|
|
UUID id;
|
|
|
|
auto func = [&] { id = insertImpl(entity, /* replace_if_exists = */ true); };
|
|
|
|
if (tracker.tryCall(func))
|
|
|
|
ids.push_back(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tracker.errors())
|
|
|
|
{
|
|
|
|
auto get_name_function = [&](size_t i) { return multiple_entities[i]->outputTypeAndName(); };
|
|
|
|
tracker.showErrors("Couldn't insert {failed_names}. Successfully inserted: {succeeded_names}", get_name_function);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ids;
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void IAccessStorage::remove(const UUID & id)
|
|
|
|
{
|
|
|
|
removeImpl(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void IAccessStorage::remove(const std::vector<UUID> & ids)
|
|
|
|
{
|
2020-07-21 19:31:27 +00:00
|
|
|
ErrorsTracker tracker(ids.size());
|
|
|
|
|
|
|
|
for (const auto & id : ids)
|
|
|
|
{
|
|
|
|
auto func = [&] { removeImpl(id); };
|
|
|
|
tracker.tryCall(func);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tracker.errors())
|
|
|
|
{
|
|
|
|
auto get_name_function = [&](size_t i) { return outputTypeAndNameOrID(*this, ids[i]); };
|
|
|
|
tracker.showErrors("Couldn't remove {failed_names}. Successfully removed: {succeeded_names}", get_name_function);
|
|
|
|
}
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool IAccessStorage::tryRemove(const UUID & id)
|
|
|
|
{
|
2020-07-21 19:31:27 +00:00
|
|
|
auto func = [&] { removeImpl(id); };
|
|
|
|
return tryCall(func);
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<UUID> IAccessStorage::tryRemove(const std::vector<UUID> & ids)
|
|
|
|
{
|
2020-07-21 19:31:27 +00:00
|
|
|
std::vector<UUID> removed_ids;
|
|
|
|
for (const auto & id : ids)
|
|
|
|
{
|
|
|
|
auto func = [&] { removeImpl(id); };
|
|
|
|
if (tryCall(func))
|
|
|
|
removed_ids.push_back(id);
|
|
|
|
}
|
|
|
|
return removed_ids;
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void IAccessStorage::update(const UUID & id, const UpdateFunc & update_func)
|
|
|
|
{
|
|
|
|
updateImpl(id, update_func);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void IAccessStorage::update(const std::vector<UUID> & ids, const UpdateFunc & update_func)
|
|
|
|
{
|
2020-07-21 19:31:27 +00:00
|
|
|
ErrorsTracker tracker(ids.size());
|
|
|
|
|
|
|
|
for (const auto & id : ids)
|
|
|
|
{
|
|
|
|
auto func = [&] { updateImpl(id, update_func); };
|
|
|
|
tracker.tryCall(func);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tracker.errors())
|
|
|
|
{
|
|
|
|
auto get_name_function = [&](size_t i) { return outputTypeAndNameOrID(*this, ids[i]); };
|
|
|
|
tracker.showErrors("Couldn't update {failed_names}. Successfully updated: {succeeded_names}", get_name_function);
|
|
|
|
}
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool IAccessStorage::tryUpdate(const UUID & id, const UpdateFunc & update_func)
|
|
|
|
{
|
2020-07-21 19:31:27 +00:00
|
|
|
auto func = [&] { updateImpl(id, update_func); };
|
|
|
|
return tryCall(func);
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<UUID> IAccessStorage::tryUpdate(const std::vector<UUID> & ids, const UpdateFunc & update_func)
|
|
|
|
{
|
2020-07-21 19:31:27 +00:00
|
|
|
std::vector<UUID> updated_ids;
|
|
|
|
for (const auto & id : ids)
|
|
|
|
{
|
|
|
|
auto func = [&] { updateImpl(id, update_func); };
|
|
|
|
if (tryCall(func))
|
|
|
|
updated_ids.push_back(id);
|
|
|
|
}
|
|
|
|
return updated_ids;
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-15 19:55:21 +00:00
|
|
|
scope_guard IAccessStorage::subscribeForChanges(EntityType 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
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-03 03:12:03 +00:00
|
|
|
bool IAccessStorage::hasSubscription(EntityType type) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
|
|
|
return hasSubscriptionImpl(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool IAccessStorage::hasSubscription(const UUID & id) const
|
|
|
|
{
|
|
|
|
return hasSubscriptionImpl(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void IAccessStorage::notify(const Notifications & notifications)
|
|
|
|
{
|
|
|
|
for (const auto & [fn, id, new_entity] : notifications)
|
|
|
|
fn(id, new_entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-17 09:58:34 +00:00
|
|
|
UUID IAccessStorage::login(
|
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-03-11 20:41:10 +00:00
|
|
|
return loginImpl(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-03-11 20:41:10 +00:00
|
|
|
tryLogCurrentException(getLogger(), credentials.getUserName() + ": Authentication failed");
|
|
|
|
throwCannotAuthenticate(credentials.getUserName());
|
2020-10-04 19:55:58 +00:00
|
|
|
}
|
2020-09-17 09:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UUID IAccessStorage::loginImpl(
|
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))
|
|
|
|
{
|
2020-10-04 19:55:58 +00:00
|
|
|
if (!isAddressAllowedImpl(*user, address))
|
|
|
|
throwAddressNotAllowed(address);
|
|
|
|
|
2021-03-11 20:41:10 +00:00
|
|
|
if (!areCredentialsValidImpl(*user, credentials, external_authenticators))
|
|
|
|
throwInvalidCredentials();
|
|
|
|
|
2020-10-04 19:55:58 +00:00
|
|
|
return *id;
|
2020-09-17 09:58:34 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-11 20:41:10 +00:00
|
|
|
throwNotFound(EntityType::USER, credentials.getUserName());
|
2020-09-17 09:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-11 20:41:10 +00:00
|
|
|
bool IAccessStorage::areCredentialsValidImpl(
|
|
|
|
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;
|
|
|
|
|
|
|
|
return user.authentication.areCredentialsValid(credentials, external_authenticators);
|
2020-09-17 09:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool IAccessStorage::isAddressAllowedImpl(const User & user, const Poco::Net::IPAddress & address) const
|
|
|
|
{
|
|
|
|
return user.allowed_client_hosts.contains(address);
|
|
|
|
}
|
|
|
|
|
2021-03-11 20:41:10 +00:00
|
|
|
|
2020-09-17 09:58:34 +00:00
|
|
|
UUID IAccessStorage::getIDOfLoggedUser(const String & user_name) const
|
|
|
|
{
|
|
|
|
return getIDOfLoggedUserImpl(user_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UUID IAccessStorage::getIDOfLoggedUserImpl(const String & user_name) const
|
|
|
|
{
|
|
|
|
return getID<User>(user_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-03 03:12:03 +00:00
|
|
|
void IAccessStorage::throwNotFound(EntityType type, const String & name) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
2020-05-03 03:12:03 +00:00
|
|
|
int error_code = EntityTypeInfo::get(type).not_found_error_code;
|
2020-08-12 20:48:53 +00:00
|
|
|
throw Exception("There is no " + outputEntityTypeAndName(type, name) + " in " + getStorageName(), error_code);
|
2019-11-09 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-03 03:12:03 +00:00
|
|
|
void IAccessStorage::throwBadCast(const UUID & id, EntityType type, const String & name, EntityType required_type)
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
|
|
|
throw Exception(
|
2020-06-01 10:45:07 +00:00
|
|
|
outputID(id) + ": " + outputEntityTypeAndName(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
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-03 03:12:03 +00:00
|
|
|
void IAccessStorage::throwIDCollisionCannotInsert(const UUID & id, EntityType type, const String & name, EntityType existing_type, const String & existing_name) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
|
|
|
throw Exception(
|
2020-06-01 10:45:07 +00:00
|
|
|
outputEntityTypeAndName(type, name) + ": cannot insert because the " + outputID(id) + " is already used by "
|
2020-08-12 20:48:53 +00:00
|
|
|
+ outputEntityTypeAndName(existing_type, existing_name) + " in " + getStorageName(),
|
2019-11-09 15:33:07 +00:00
|
|
|
ErrorCodes::ACCESS_ENTITY_ALREADY_EXISTS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-03 03:12:03 +00:00
|
|
|
void IAccessStorage::throwNameCollisionCannotInsert(EntityType type, const String & name) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
|
|
|
throw Exception(
|
2020-08-12 20:48:53 +00:00
|
|
|
outputEntityTypeAndName(type, name) + ": cannot insert because " + outputEntityTypeAndName(type, name) + " already exists in "
|
|
|
|
+ getStorageName(),
|
2019-11-09 15:33:07 +00:00
|
|
|
ErrorCodes::ACCESS_ENTITY_ALREADY_EXISTS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-03 03:12:03 +00:00
|
|
|
void IAccessStorage::throwNameCollisionCannotRename(EntityType type, const String & old_name, const String & new_name) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
|
|
|
throw Exception(
|
2020-05-03 03:12:03 +00:00
|
|
|
outputEntityTypeAndName(type, old_name) + ": cannot rename to " + backQuote(new_name) + " because "
|
2020-08-12 20:48:53 +00:00
|
|
|
+ outputEntityTypeAndName(type, new_name) + " already exists in " + getStorageName(),
|
2019-11-09 15:33:07 +00:00
|
|
|
ErrorCodes::ACCESS_ENTITY_ALREADY_EXISTS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-03 03:12:03 +00:00
|
|
|
void IAccessStorage::throwReadonlyCannotInsert(EntityType type, const String & name) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
|
|
|
throw Exception(
|
2020-08-12 20:48:53 +00:00
|
|
|
"Cannot insert " + outputEntityTypeAndName(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
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-03 03:12:03 +00:00
|
|
|
void IAccessStorage::throwReadonlyCannotUpdate(EntityType type, const String & name) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
|
|
|
throw Exception(
|
2020-08-12 20:48:53 +00:00
|
|
|
"Cannot update " + outputEntityTypeAndName(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
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-03 03:12:03 +00:00
|
|
|
void IAccessStorage::throwReadonlyCannotRemove(EntityType type, const String & name) const
|
2019-11-09 15:33:07 +00:00
|
|
|
{
|
|
|
|
throw Exception(
|
2020-08-12 20:48:53 +00:00
|
|
|
"Cannot remove " + outputEntityTypeAndName(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
|
|
|
}
|