2019-11-29 17:22:56 +00:00
|
|
|
#include <Interpreters/InterpreterCreateRowPolicyQuery.h>
|
|
|
|
#include <Parsers/ASTCreateRowPolicyQuery.h>
|
2020-03-07 17:37:38 +00:00
|
|
|
#include <Parsers/ASTExtendedRoleSet.h>
|
2019-11-29 17:22:56 +00:00
|
|
|
#include <Parsers/formatAST.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Access/AccessControlManager.h>
|
2020-01-26 09:49:53 +00:00
|
|
|
#include <Access/AccessFlags.h>
|
2019-11-29 17:22:56 +00:00
|
|
|
#include <boost/range/algorithm/sort.hpp>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2020-02-21 19:27:12 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
const String & checkCurrentDatabase(const String & current_database)
|
|
|
|
{
|
|
|
|
if (current_database.empty())
|
|
|
|
throw Exception("No current database", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
return current_database;
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateRowPolicyFromQueryImpl(
|
|
|
|
RowPolicy & policy,
|
|
|
|
const ASTCreateRowPolicyQuery & query,
|
2020-03-07 17:37:38 +00:00
|
|
|
const std::optional<ExtendedRoleSet> & roles_from_query = {},
|
2020-02-21 19:27:12 +00:00
|
|
|
const String & current_database = {})
|
|
|
|
{
|
|
|
|
if (query.alter)
|
|
|
|
{
|
|
|
|
if (!query.new_policy_name.empty())
|
|
|
|
policy.setName(query.new_policy_name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
policy.setDatabase(!query.name_parts.database.empty() ? query.name_parts.database : checkCurrentDatabase(current_database));
|
|
|
|
policy.setTableName(query.name_parts.table_name);
|
|
|
|
policy.setName(query.name_parts.policy_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (query.is_restrictive)
|
|
|
|
policy.setRestrictive(*query.is_restrictive);
|
|
|
|
|
|
|
|
for (const auto & [index, condition] : query.conditions)
|
|
|
|
policy.conditions[index] = condition ? serializeAST(*condition) : String{};
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
const ExtendedRoleSet * roles = nullptr;
|
|
|
|
std::optional<ExtendedRoleSet> temp_role_set;
|
2020-02-27 20:55:16 +00:00
|
|
|
if (roles_from_query)
|
2020-02-21 19:27:12 +00:00
|
|
|
roles = &*roles_from_query;
|
|
|
|
else if (query.roles)
|
|
|
|
roles = &temp_role_set.emplace(*query.roles);
|
|
|
|
|
|
|
|
if (roles)
|
2020-03-07 17:37:38 +00:00
|
|
|
policy.to_roles = *roles;
|
2020-02-21 19:27:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-29 17:22:56 +00:00
|
|
|
BlockIO InterpreterCreateRowPolicyQuery::execute()
|
|
|
|
{
|
|
|
|
const auto & query = query_ptr->as<const ASTCreateRowPolicyQuery &>();
|
|
|
|
auto & access_control = context.getAccessControlManager();
|
2020-01-24 16:20:36 +00:00
|
|
|
context.checkAccess(query.alter ? AccessType::ALTER_POLICY : AccessType::CREATE_POLICY);
|
2019-11-29 17:22:56 +00:00
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
std::optional<ExtendedRoleSet> roles_from_query;
|
2020-02-10 02:26:56 +00:00
|
|
|
if (query.roles)
|
2020-03-07 17:37:38 +00:00
|
|
|
roles_from_query = ExtendedRoleSet{*query.roles, access_control, context.getUserID()};
|
2020-02-10 02:26:56 +00:00
|
|
|
|
2020-02-21 19:27:12 +00:00
|
|
|
const String current_database = context.getCurrentDatabase();
|
|
|
|
|
2019-11-29 17:22:56 +00:00
|
|
|
if (query.alter)
|
|
|
|
{
|
|
|
|
auto update_func = [&](const AccessEntityPtr & entity) -> AccessEntityPtr
|
|
|
|
{
|
|
|
|
auto updated_policy = typeid_cast<std::shared_ptr<RowPolicy>>(entity->clone());
|
2020-02-21 19:27:12 +00:00
|
|
|
updateRowPolicyFromQueryImpl(*updated_policy, query, roles_from_query, current_database);
|
2019-11-29 17:22:56 +00:00
|
|
|
return updated_policy;
|
|
|
|
};
|
|
|
|
String full_name = query.name_parts.getFullName(context);
|
|
|
|
if (query.if_exists)
|
|
|
|
{
|
|
|
|
if (auto id = access_control.find<RowPolicy>(full_name))
|
|
|
|
access_control.tryUpdate(*id, update_func);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
access_control.update(access_control.getID<RowPolicy>(full_name), update_func);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto new_policy = std::make_shared<RowPolicy>();
|
2020-02-21 19:27:12 +00:00
|
|
|
updateRowPolicyFromQueryImpl(*new_policy, query, roles_from_query, current_database);
|
2019-11-29 17:22:56 +00:00
|
|
|
|
|
|
|
if (query.if_not_exists)
|
|
|
|
access_control.tryInsert(new_policy);
|
|
|
|
else if (query.or_replace)
|
|
|
|
access_control.insertOrReplace(new_policy);
|
|
|
|
else
|
|
|
|
access_control.insert(new_policy);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-21 19:27:12 +00:00
|
|
|
void InterpreterCreateRowPolicyQuery::updateRowPolicyFromQuery(RowPolicy & policy, const ASTCreateRowPolicyQuery & query)
|
2019-11-29 17:22:56 +00:00
|
|
|
{
|
2020-02-21 19:27:12 +00:00
|
|
|
updateRowPolicyFromQueryImpl(policy, query);
|
2019-11-29 17:22:56 +00:00
|
|
|
}
|
2020-02-21 19:27:12 +00:00
|
|
|
|
2019-11-29 17:22:56 +00:00
|
|
|
}
|