2020-02-04 22:37:04 +00:00
|
|
|
#include <Interpreters/InterpreterShowGrantsQuery.h>
|
|
|
|
#include <Parsers/ASTShowGrantsQuery.h>
|
|
|
|
#include <Parsers/ASTGrantQuery.h>
|
2020-05-30 20:10:45 +00:00
|
|
|
#include <Parsers/ASTRolesOrUsersSet.h>
|
2020-02-04 22:37:04 +00:00
|
|
|
#include <Parsers/formatAST.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <DataStreams/OneBlockInputStream.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <Access/AccessControlManager.h>
|
|
|
|
#include <Access/User.h>
|
2020-02-17 02:59:56 +00:00
|
|
|
#include <Access/Role.h>
|
2020-06-06 07:21:02 +00:00
|
|
|
#include <Access/RolesOrUsersSet.h>
|
|
|
|
#include <boost/range/algorithm/sort.hpp>
|
|
|
|
#include <boost/range/algorithm_ext/push_back.hpp>
|
2020-02-04 22:37:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2020-02-21 19:27:12 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2020-02-04 22:37:04 +00:00
|
|
|
namespace
|
|
|
|
{
|
2020-02-21 19:27:12 +00:00
|
|
|
template <typename T>
|
|
|
|
ASTs getGrantQueriesImpl(
|
|
|
|
const T & grantee,
|
|
|
|
const AccessControlManager * manager /* not used if attach_mode == true */,
|
|
|
|
bool attach_mode = false)
|
|
|
|
{
|
|
|
|
ASTs res;
|
|
|
|
|
2020-05-30 20:10:45 +00:00
|
|
|
std::shared_ptr<ASTRolesOrUsersSet> to_roles = std::make_shared<ASTRolesOrUsersSet>();
|
2020-02-21 19:27:12 +00:00
|
|
|
to_roles->names.push_back(grantee.getName());
|
|
|
|
|
2020-06-20 22:44:52 +00:00
|
|
|
std::shared_ptr<ASTGrantQuery> current_query = nullptr;
|
2020-02-21 19:27:12 +00:00
|
|
|
|
2020-06-20 22:44:52 +00:00
|
|
|
auto elements = grantee.access.getElements();
|
|
|
|
for (const auto & element : elements)
|
2020-04-20 22:07:00 +00:00
|
|
|
{
|
2020-06-20 22:44:52 +00:00
|
|
|
if (current_query)
|
2020-02-21 19:27:12 +00:00
|
|
|
{
|
2020-06-20 22:44:52 +00:00
|
|
|
const auto & prev_element = current_query->access_rights_elements.back();
|
|
|
|
bool continue_using_current_query = (element.database == prev_element.database)
|
|
|
|
&& (element.any_database == prev_element.any_database) && (element.table == prev_element.table)
|
|
|
|
&& (element.any_table == prev_element.any_table) && (element.grant_option == current_query->grant_option)
|
|
|
|
&& (element.kind == current_query->kind);
|
|
|
|
if (!continue_using_current_query)
|
|
|
|
current_query = nullptr;
|
|
|
|
}
|
2020-04-20 22:07:00 +00:00
|
|
|
|
2020-06-20 22:44:52 +00:00
|
|
|
if (!current_query)
|
|
|
|
{
|
|
|
|
current_query = std::make_shared<ASTGrantQuery>();
|
|
|
|
current_query->kind = element.kind;
|
|
|
|
current_query->attach = attach_mode;
|
|
|
|
current_query->grant_option = element.grant_option;
|
|
|
|
current_query->to_roles = to_roles;
|
|
|
|
res.push_back(current_query);
|
2020-02-21 19:27:12 +00:00
|
|
|
}
|
2020-06-20 22:44:52 +00:00
|
|
|
|
|
|
|
current_query->access_rights_elements.emplace_back(std::move(element));
|
2020-02-21 19:27:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 22:07:00 +00:00
|
|
|
auto grants_roles = grantee.granted_roles.getGrants();
|
2020-02-21 19:27:12 +00:00
|
|
|
|
2020-04-20 22:07:00 +00:00
|
|
|
for (bool admin_option : {false, true})
|
|
|
|
{
|
|
|
|
const auto & roles = admin_option ? grants_roles.grants_with_admin_option : grants_roles.grants;
|
2020-02-21 19:27:12 +00:00
|
|
|
if (roles.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto grant_query = std::make_shared<ASTGrantQuery>();
|
|
|
|
using Kind = ASTGrantQuery::Kind;
|
|
|
|
grant_query->kind = Kind::GRANT;
|
|
|
|
grant_query->attach = attach_mode;
|
|
|
|
grant_query->admin_option = admin_option;
|
|
|
|
grant_query->to_roles = to_roles;
|
|
|
|
if (attach_mode)
|
2020-05-30 20:10:45 +00:00
|
|
|
grant_query->roles = RolesOrUsersSet{roles}.toAST();
|
2020-02-21 19:27:12 +00:00
|
|
|
else
|
2020-05-30 20:10:45 +00:00
|
|
|
grant_query->roles = RolesOrUsersSet{roles}.toASTWithNames(*manager);
|
2020-02-21 19:27:12 +00:00
|
|
|
res.push_back(std::move(grant_query));
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTs getGrantQueriesImpl(
|
|
|
|
const IAccessEntity & entity,
|
|
|
|
const AccessControlManager * manager /* not used if attach_mode == true */,
|
|
|
|
bool attach_mode = false)
|
|
|
|
{
|
|
|
|
if (const User * user = typeid_cast<const User *>(&entity))
|
|
|
|
return getGrantQueriesImpl(*user, manager, attach_mode);
|
|
|
|
if (const Role * role = typeid_cast<const Role *>(&entity))
|
|
|
|
return getGrantQueriesImpl(*role, manager, attach_mode);
|
2020-05-03 03:12:03 +00:00
|
|
|
throw Exception(entity.outputTypeAndName() + " is expected to be user or role", ErrorCodes::LOGICAL_ERROR);
|
2020-02-21 19:27:12 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 22:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BlockIO InterpreterShowGrantsQuery::execute()
|
|
|
|
{
|
|
|
|
BlockIO res;
|
|
|
|
res.in = executeImpl();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BlockInputStreamPtr InterpreterShowGrantsQuery::executeImpl()
|
|
|
|
{
|
|
|
|
/// Build a create query.
|
2020-06-06 07:21:02 +00:00
|
|
|
ASTs grant_queries = getGrantQueries();
|
2020-02-04 22:37:04 +00:00
|
|
|
|
|
|
|
/// Build the result column.
|
|
|
|
MutableColumnPtr column = ColumnString::create();
|
2020-11-09 16:05:40 +00:00
|
|
|
WriteBufferFromOwnString grant_buf;
|
2020-02-04 22:37:04 +00:00
|
|
|
for (const auto & grant_query : grant_queries)
|
|
|
|
{
|
2020-11-09 16:05:40 +00:00
|
|
|
grant_buf.restart();
|
|
|
|
formatAST(*grant_query, grant_buf, false, true);
|
|
|
|
column->insert(grant_buf.str());
|
2020-02-04 22:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Prepare description of the result column.
|
2020-11-09 16:05:40 +00:00
|
|
|
WriteBufferFromOwnString desc_buf;
|
2020-06-06 07:21:02 +00:00
|
|
|
const auto & show_query = query_ptr->as<const ASTShowGrantsQuery &>();
|
2020-11-09 16:05:40 +00:00
|
|
|
formatAST(show_query, desc_buf, false, true);
|
|
|
|
String desc = desc_buf.str();
|
2020-02-04 22:37:04 +00:00
|
|
|
String prefix = "SHOW ";
|
|
|
|
if (desc.starts_with(prefix))
|
|
|
|
desc = desc.substr(prefix.length()); /// `desc` always starts with "SHOW ", so we can trim this prefix.
|
|
|
|
|
|
|
|
return std::make_shared<OneBlockInputStream>(Block{{std::move(column), std::make_shared<DataTypeString>(), desc}});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-06 07:21:02 +00:00
|
|
|
std::vector<AccessEntityPtr> InterpreterShowGrantsQuery::getEntities() const
|
2020-02-04 22:37:04 +00:00
|
|
|
{
|
2020-06-06 07:21:02 +00:00
|
|
|
const auto & show_query = query_ptr->as<ASTShowGrantsQuery &>();
|
2020-02-17 02:59:56 +00:00
|
|
|
const auto & access_control = context.getAccessControlManager();
|
2020-06-06 07:21:02 +00:00
|
|
|
auto ids = RolesOrUsersSet{*show_query.for_roles, access_control, context.getUserID()}.getMatchingIDs(access_control);
|
2020-02-17 02:59:56 +00:00
|
|
|
|
2020-06-06 07:21:02 +00:00
|
|
|
std::vector<AccessEntityPtr> entities;
|
|
|
|
for (const auto & id : ids)
|
2020-02-17 02:59:56 +00:00
|
|
|
{
|
2020-06-06 07:21:02 +00:00
|
|
|
auto entity = access_control.tryRead(id);
|
|
|
|
if (entity)
|
|
|
|
entities.push_back(entity);
|
2020-02-17 02:59:56 +00:00
|
|
|
}
|
2020-02-04 22:37:04 +00:00
|
|
|
|
2020-06-06 07:21:02 +00:00
|
|
|
boost::range::sort(entities, IAccessEntity::LessByTypeAndName{});
|
|
|
|
return entities;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ASTs InterpreterShowGrantsQuery::getGrantQueries() const
|
|
|
|
{
|
|
|
|
auto entities = getEntities();
|
|
|
|
const auto & access_control = context.getAccessControlManager();
|
|
|
|
|
|
|
|
ASTs grant_queries;
|
|
|
|
for (const auto & entity : entities)
|
|
|
|
boost::range::push_back(grant_queries, getGrantQueries(*entity, access_control));
|
|
|
|
|
|
|
|
return grant_queries;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ASTs InterpreterShowGrantsQuery::getGrantQueries(const IAccessEntity & user_or_role, const AccessControlManager & access_control)
|
|
|
|
{
|
|
|
|
return getGrantQueriesImpl(user_or_role, &access_control, false);
|
2020-02-21 19:27:12 +00:00
|
|
|
}
|
2020-02-04 22:37:04 +00:00
|
|
|
|
2020-02-17 02:59:56 +00:00
|
|
|
|
2020-02-21 19:27:12 +00:00
|
|
|
ASTs InterpreterShowGrantsQuery::getAttachGrantQueries(const IAccessEntity & user_or_role)
|
|
|
|
{
|
|
|
|
return getGrantQueriesImpl(user_or_role, nullptr, true);
|
2020-02-04 22:37:04 +00:00
|
|
|
}
|
2020-02-21 19:27:12 +00:00
|
|
|
|
2020-02-04 22:37:04 +00:00
|
|
|
}
|