2020-04-20 22:07:00 +00:00
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
#include <Access/ExtendedRoleSet.h>
|
2020-02-10 02:26:56 +00:00
|
|
|
#include <Access/AccessControlManager.h>
|
|
|
|
#include <Access/User.h>
|
2020-02-21 00:17:07 +00:00
|
|
|
#include <Access/Role.h>
|
2020-03-07 17:37:38 +00:00
|
|
|
#include <Parsers/ASTExtendedRoleSet.h>
|
2020-02-10 02:26:56 +00:00
|
|
|
#include <Parsers/formatAST.h>
|
2020-02-21 19:27:12 +00:00
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2020-02-10 02:26:56 +00:00
|
|
|
#include <boost/range/algorithm/set_algorithm.hpp>
|
|
|
|
#include <boost/range/algorithm/sort.hpp>
|
2020-02-21 00:17:07 +00:00
|
|
|
#include <boost/range/algorithm_ext/push_back.hpp>
|
2020-02-10 02:26:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2020-02-25 18:10:48 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
2020-02-10 02:26:56 +00:00
|
|
|
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
ExtendedRoleSet::ExtendedRoleSet() = default;
|
|
|
|
ExtendedRoleSet::ExtendedRoleSet(const ExtendedRoleSet & src) = default;
|
|
|
|
ExtendedRoleSet & ExtendedRoleSet::operator =(const ExtendedRoleSet & src) = default;
|
|
|
|
ExtendedRoleSet::ExtendedRoleSet(ExtendedRoleSet && src) = default;
|
|
|
|
ExtendedRoleSet & ExtendedRoleSet::operator =(ExtendedRoleSet && src) = default;
|
|
|
|
|
|
|
|
|
|
|
|
ExtendedRoleSet::ExtendedRoleSet(AllTag)
|
2020-02-10 02:26:56 +00:00
|
|
|
{
|
|
|
|
all = true;
|
|
|
|
}
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
ExtendedRoleSet::ExtendedRoleSet(const UUID & id)
|
2020-02-10 02:26:56 +00:00
|
|
|
{
|
|
|
|
add(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
ExtendedRoleSet::ExtendedRoleSet(const std::vector<UUID> & ids_)
|
2020-02-10 02:26:56 +00:00
|
|
|
{
|
|
|
|
add(ids_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
ExtendedRoleSet::ExtendedRoleSet(const ASTExtendedRoleSet & ast)
|
2020-02-21 19:27:12 +00:00
|
|
|
{
|
2020-04-05 23:03:20 +00:00
|
|
|
init(ast, nullptr);
|
2020-02-21 19:27:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 23:03:20 +00:00
|
|
|
ExtendedRoleSet::ExtendedRoleSet(const ASTExtendedRoleSet & ast, const std::optional<UUID> & current_user_id)
|
2020-02-21 19:27:12 +00:00
|
|
|
{
|
2020-04-05 23:03:20 +00:00
|
|
|
init(ast, nullptr, current_user_id);
|
2020-02-21 19:27:12 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
ExtendedRoleSet::ExtendedRoleSet(const ASTExtendedRoleSet & ast, const AccessControlManager & manager)
|
2020-02-21 19:27:12 +00:00
|
|
|
{
|
2020-04-05 23:03:20 +00:00
|
|
|
init(ast, &manager);
|
2020-02-21 19:27:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 23:03:20 +00:00
|
|
|
ExtendedRoleSet::ExtendedRoleSet(const ASTExtendedRoleSet & ast, const AccessControlManager & manager, const std::optional<UUID> & current_user_id)
|
2020-02-21 19:27:12 +00:00
|
|
|
{
|
2020-04-05 23:03:20 +00:00
|
|
|
init(ast, &manager, current_user_id);
|
2020-02-21 19:27:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 23:03:20 +00:00
|
|
|
void ExtendedRoleSet::init(const ASTExtendedRoleSet & ast, const AccessControlManager * manager, const std::optional<UUID> & current_user_id)
|
2020-02-10 02:26:56 +00:00
|
|
|
{
|
|
|
|
all = ast.all;
|
|
|
|
|
2020-05-29 06:54:27 +00:00
|
|
|
auto name_to_id = [&ast, manager](const String & name) -> UUID
|
2020-02-21 19:27:12 +00:00
|
|
|
{
|
2020-05-29 06:54:27 +00:00
|
|
|
if (ast.id_mode)
|
2020-02-21 19:27:12 +00:00
|
|
|
return parse<UUID>(name);
|
|
|
|
assert(manager);
|
2020-05-29 06:54:27 +00:00
|
|
|
if (ast.can_contain_users && ast.can_contain_roles)
|
|
|
|
{
|
|
|
|
auto id = manager->find<User>(name);
|
|
|
|
if (id)
|
|
|
|
return *id;
|
|
|
|
return manager->getID<Role>(name);
|
|
|
|
}
|
|
|
|
else if (ast.can_contain_users)
|
|
|
|
{
|
|
|
|
return manager->getID<User>(name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(ast.can_contain_roles);
|
|
|
|
return manager->getID<Role>(name);
|
|
|
|
}
|
2020-02-21 19:27:12 +00:00
|
|
|
};
|
|
|
|
|
2020-02-10 02:26:56 +00:00
|
|
|
if (!ast.names.empty() && !all)
|
|
|
|
{
|
|
|
|
ids.reserve(ast.names.size());
|
|
|
|
for (const String & name : ast.names)
|
2020-02-21 19:27:12 +00:00
|
|
|
ids.insert(name_to_id(name));
|
2020-02-10 02:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ast.current_user && !all)
|
|
|
|
{
|
2020-02-21 19:27:12 +00:00
|
|
|
assert(current_user_id);
|
2020-02-10 02:26:56 +00:00
|
|
|
ids.insert(*current_user_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ast.except_names.empty())
|
|
|
|
{
|
|
|
|
except_ids.reserve(ast.except_names.size());
|
|
|
|
for (const String & except_name : ast.except_names)
|
2020-02-21 19:27:12 +00:00
|
|
|
except_ids.insert(name_to_id(except_name));
|
2020-02-10 02:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ast.except_current_user)
|
|
|
|
{
|
2020-02-21 19:27:12 +00:00
|
|
|
assert(current_user_id);
|
2020-02-10 02:26:56 +00:00
|
|
|
except_ids.insert(*current_user_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const UUID & except_id : except_ids)
|
|
|
|
ids.erase(except_id);
|
|
|
|
}
|
|
|
|
|
2020-02-21 19:27:12 +00:00
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
std::shared_ptr<ASTExtendedRoleSet> ExtendedRoleSet::toAST() const
|
2020-02-21 19:27:12 +00:00
|
|
|
{
|
2020-03-07 17:37:38 +00:00
|
|
|
auto ast = std::make_shared<ASTExtendedRoleSet>();
|
2020-02-21 19:27:12 +00:00
|
|
|
ast->id_mode = true;
|
|
|
|
ast->all = all;
|
|
|
|
|
|
|
|
if (!ids.empty())
|
|
|
|
{
|
|
|
|
ast->names.reserve(ids.size());
|
|
|
|
for (const UUID & id : ids)
|
|
|
|
ast->names.emplace_back(::DB::toString(id));
|
2020-04-20 22:07:00 +00:00
|
|
|
boost::range::sort(ast->names);
|
2020-02-21 19:27:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!except_ids.empty())
|
|
|
|
{
|
|
|
|
ast->except_names.reserve(except_ids.size());
|
|
|
|
for (const UUID & except_id : except_ids)
|
|
|
|
ast->except_names.emplace_back(::DB::toString(except_id));
|
2020-04-20 22:07:00 +00:00
|
|
|
boost::range::sort(ast->except_names);
|
2020-02-21 19:27:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ast;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
std::shared_ptr<ASTExtendedRoleSet> ExtendedRoleSet::toASTWithNames(const AccessControlManager & manager) const
|
2020-02-10 02:26:56 +00:00
|
|
|
{
|
2020-03-07 17:37:38 +00:00
|
|
|
auto ast = std::make_shared<ASTExtendedRoleSet>();
|
2020-02-10 02:26:56 +00:00
|
|
|
ast->all = all;
|
|
|
|
|
|
|
|
if (!ids.empty())
|
|
|
|
{
|
|
|
|
ast->names.reserve(ids.size());
|
|
|
|
for (const UUID & id : ids)
|
|
|
|
{
|
|
|
|
auto name = manager.tryReadName(id);
|
|
|
|
if (name)
|
|
|
|
ast->names.emplace_back(std::move(*name));
|
|
|
|
}
|
|
|
|
boost::range::sort(ast->names);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!except_ids.empty())
|
|
|
|
{
|
|
|
|
ast->except_names.reserve(except_ids.size());
|
|
|
|
for (const UUID & except_id : except_ids)
|
|
|
|
{
|
|
|
|
auto except_name = manager.tryReadName(except_id);
|
|
|
|
if (except_name)
|
|
|
|
ast->except_names.emplace_back(std::move(*except_name));
|
|
|
|
}
|
|
|
|
boost::range::sort(ast->except_names);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ast;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-29 19:35:56 +00:00
|
|
|
String ExtendedRoleSet::toString() const
|
|
|
|
{
|
|
|
|
auto ast = toAST();
|
|
|
|
return serializeAST(*ast);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
String ExtendedRoleSet::toStringWithNames(const AccessControlManager & manager) const
|
2020-02-10 02:26:56 +00:00
|
|
|
{
|
2020-02-21 19:27:12 +00:00
|
|
|
auto ast = toASTWithNames(manager);
|
2020-02-10 02:26:56 +00:00
|
|
|
return serializeAST(*ast);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
Strings ExtendedRoleSet::toStringsWithNames(const AccessControlManager & manager) const
|
2020-02-10 02:26:56 +00:00
|
|
|
{
|
2020-04-29 19:35:56 +00:00
|
|
|
if (!all && ids.empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
Strings res;
|
|
|
|
res.reserve(ids.size() + except_ids.size());
|
|
|
|
|
|
|
|
if (all)
|
|
|
|
res.emplace_back("ALL");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (const UUID & id : ids)
|
|
|
|
{
|
|
|
|
auto name = manager.tryReadName(id);
|
|
|
|
if (name)
|
|
|
|
res.emplace_back(std::move(*name));
|
|
|
|
}
|
|
|
|
std::sort(res.begin(), res.end());
|
|
|
|
}
|
2020-02-10 02:26:56 +00:00
|
|
|
|
2020-04-29 19:35:56 +00:00
|
|
|
if (!except_ids.empty())
|
2020-02-10 02:26:56 +00:00
|
|
|
{
|
2020-04-29 19:35:56 +00:00
|
|
|
res.emplace_back("EXCEPT");
|
|
|
|
size_t old_size = res.size();
|
|
|
|
for (const UUID & id : except_ids)
|
|
|
|
{
|
|
|
|
auto name = manager.tryReadName(id);
|
|
|
|
if (name)
|
|
|
|
res.emplace_back(std::move(*name));
|
|
|
|
}
|
|
|
|
std::sort(res.begin() + old_size, res.end());
|
2020-02-10 02:26:56 +00:00
|
|
|
}
|
2020-04-29 19:35:56 +00:00
|
|
|
|
|
|
|
return res;
|
2020-02-10 02:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
bool ExtendedRoleSet::empty() const
|
2020-02-10 02:26:56 +00:00
|
|
|
{
|
|
|
|
return ids.empty() && !all;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
void ExtendedRoleSet::clear()
|
2020-02-10 02:26:56 +00:00
|
|
|
{
|
|
|
|
ids.clear();
|
|
|
|
all = false;
|
|
|
|
except_ids.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
void ExtendedRoleSet::add(const UUID & id)
|
2020-02-10 02:26:56 +00:00
|
|
|
{
|
|
|
|
ids.insert(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
void ExtendedRoleSet::add(const std::vector<UUID> & ids_)
|
2020-02-10 02:26:56 +00:00
|
|
|
{
|
|
|
|
for (const auto & id : ids_)
|
|
|
|
add(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
bool ExtendedRoleSet::match(const UUID & id) const
|
2020-02-10 02:26:56 +00:00
|
|
|
{
|
2020-04-16 12:31:57 +00:00
|
|
|
return (all || ids.count(id)) && !except_ids.count(id);
|
2020-02-21 00:17:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
bool ExtendedRoleSet::match(const UUID & user_id, const boost::container::flat_set<UUID> & enabled_roles) const
|
2020-02-21 00:17:07 +00:00
|
|
|
{
|
2020-04-16 12:31:57 +00:00
|
|
|
if (!all && !ids.count(user_id))
|
2020-02-21 00:17:07 +00:00
|
|
|
{
|
|
|
|
bool found_enabled_role = std::any_of(
|
2020-04-16 12:31:57 +00:00
|
|
|
enabled_roles.begin(), enabled_roles.end(), [this](const UUID & enabled_role) { return ids.count(enabled_role); });
|
2020-02-21 00:17:07 +00:00
|
|
|
if (!found_enabled_role)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-16 12:31:57 +00:00
|
|
|
if (except_ids.count(user_id))
|
2020-02-21 00:17:07 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
bool in_except_list = std::any_of(
|
2020-04-16 12:31:57 +00:00
|
|
|
enabled_roles.begin(), enabled_roles.end(), [this](const UUID & enabled_role) { return except_ids.count(enabled_role); });
|
2020-03-08 21:53:03 +00:00
|
|
|
return !in_except_list;
|
2020-02-10 02:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
std::vector<UUID> ExtendedRoleSet::getMatchingIDs() const
|
2020-02-10 02:26:56 +00:00
|
|
|
{
|
|
|
|
if (all)
|
2020-03-07 17:37:38 +00:00
|
|
|
throw Exception("getAllMatchingIDs() can't get ALL ids without manager", ErrorCodes::LOGICAL_ERROR);
|
2020-02-10 02:26:56 +00:00
|
|
|
std::vector<UUID> res;
|
|
|
|
boost::range::set_difference(ids, except_ids, std::back_inserter(res));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
std::vector<UUID> ExtendedRoleSet::getMatchingIDs(const AccessControlManager & manager) const
|
2020-02-10 02:26:56 +00:00
|
|
|
{
|
|
|
|
if (!all)
|
|
|
|
return getMatchingIDs();
|
|
|
|
|
|
|
|
std::vector<UUID> res;
|
|
|
|
for (const UUID & id : manager.findAll<User>())
|
|
|
|
{
|
|
|
|
if (match(id))
|
|
|
|
res.push_back(id);
|
|
|
|
}
|
2020-02-21 00:17:07 +00:00
|
|
|
for (const UUID & id : manager.findAll<Role>())
|
|
|
|
{
|
|
|
|
if (match(id))
|
|
|
|
res.push_back(id);
|
|
|
|
}
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
return res;
|
2020-02-21 00:17:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
bool operator ==(const ExtendedRoleSet & lhs, const ExtendedRoleSet & rhs)
|
2020-02-10 02:26:56 +00:00
|
|
|
{
|
|
|
|
return (lhs.all == rhs.all) && (lhs.ids == rhs.ids) && (lhs.except_ids == rhs.except_ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|