1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
#include <Interpreters/Access/InterpreterShowGrantsQuery.h>
#include <Parsers/Access/ASTGrantQuery.h>
#include <Parsers/Access/ASTRolesOrUsersSet.h>
#include <Parsers/Access/ASTShowGrantsQuery.h>
#include <Parsers/formatAST.h>
#include <Access/AccessControl.h>
#include <Access/CachedAccessChecking.h>
#include <Access/ContextAccess.h>
#include <Access/EnabledRolesInfo.h>
#include <Access/Role.h>
#include <Access/RolesOrUsersSet.h>
#include <Access/User.h>
#include <Columns/ColumnString.h>
#include <DataTypes/DataTypeString.h>
#include <Interpreters/Context.h>
#include <Processors/Sources/SourceFromSingleChunk.h>
#include <boost/range/algorithm_ext/push_back.hpp>
#include <base/sort.h>
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
namespace
{
template <typename T>
ASTs getGrantQueriesImpl(
const T & grantee,
const AccessControl * access_control /* not used if attach_mode == true */,
bool attach_mode = false)
{
ASTs res;
std::shared_ptr<ASTRolesOrUsersSet> grantees = std::make_shared<ASTRolesOrUsersSet>();
grantees->names.push_back(grantee.getName());
std::shared_ptr<ASTGrantQuery> current_query = nullptr;
for (const auto & element : grantee.access.getElements())
{
if (element.empty())
continue;
if (current_query)
{
const auto & prev_element = current_query->access_rights_elements.back();
bool continue_with_current_query = element.sameDatabaseAndTableAndParameter(prev_element) && element.sameOptions(prev_element);
if (!continue_with_current_query)
current_query = nullptr;
}
if (!current_query)
{
current_query = std::make_shared<ASTGrantQuery>();
current_query->grantees = grantees;
current_query->attach_mode = attach_mode;
if (element.is_partial_revoke)
current_query->is_revoke = true;
res.push_back(current_query);
}
current_query->access_rights_elements.emplace_back(std::move(element));
}
for (const auto & element : grantee.granted_roles.getElements())
{
if (element.empty())
continue;
auto grant_query = std::make_shared<ASTGrantQuery>();
grant_query->grantees = grantees;
grant_query->admin_option = element.admin_option;
grant_query->attach_mode = attach_mode;
if (attach_mode)
grant_query->roles = RolesOrUsersSet{element.ids}.toAST();
else
grant_query->roles = RolesOrUsersSet{element.ids}.toASTWithNames(*access_control);
res.push_back(std::move(grant_query));
}
return res;
}
ASTs getGrantQueriesImpl(
const IAccessEntity & entity,
const AccessControl * access_control /* not used if attach_mode == true */,
bool attach_mode = false)
{
if (const User * user = typeid_cast<const User *>(&entity))
return getGrantQueriesImpl(*user, access_control, attach_mode);
if (const Role * role = typeid_cast<const Role *>(&entity))
return getGrantQueriesImpl(*role, access_control, attach_mode);
throw Exception(ErrorCodes::LOGICAL_ERROR, "{} is expected to be user or role", entity.formatTypeWithName());
}
}
BlockIO InterpreterShowGrantsQuery::execute()
{
BlockIO res;
res.pipeline = executeImpl();
return res;
}
QueryPipeline InterpreterShowGrantsQuery::executeImpl()
{
/// Build a create query.
ASTs grant_queries = getGrantQueries();
/// Build the result column.
MutableColumnPtr column = ColumnString::create();
WriteBufferFromOwnString grant_buf;
for (const auto & grant_query : grant_queries)
{
grant_buf.restart();
formatAST(*grant_query, grant_buf, false, true);
column->insert(grant_buf.str());
}
/// Prepare description of the result column.
WriteBufferFromOwnString desc_buf;
const auto & show_query = query_ptr->as<const ASTShowGrantsQuery &>();
formatAST(show_query, desc_buf, false, true);
String desc = desc_buf.str();
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 QueryPipeline(std::make_shared<SourceFromSingleChunk>(Block{{std::move(column), std::make_shared<DataTypeString>(), desc}}));
}
std::vector<AccessEntityPtr> InterpreterShowGrantsQuery::getEntities() const
{
const auto & access = getContext()->getAccess();
const auto & access_control = getContext()->getAccessControl();
const auto & show_query = query_ptr->as<ASTShowGrantsQuery &>();
auto ids = RolesOrUsersSet{*show_query.for_roles, access_control, getContext()->getUserID()}.getMatchingIDs(access_control);
CachedAccessChecking show_users(access, AccessType::SHOW_USERS);
CachedAccessChecking show_roles(access, AccessType::SHOW_ROLES);
bool throw_if_access_denied = !show_query.for_roles->all;
auto current_user = access->getUser();
auto roles_info = access->getRolesInfo();
std::vector<AccessEntityPtr> entities;
for (const auto & id : ids)
{
auto entity = access_control.tryRead(id);
if (!entity)
continue;
bool is_current_user = (id == access->getUserID());
bool is_enabled_or_granted_role = entity->isTypeOf<Role>()
&& (current_user->granted_roles.isGranted(id) || roles_info->enabled_roles.contains(id));
if ((is_current_user /* Any user can see his own grants */)
|| (is_enabled_or_granted_role /* and grants from the granted roles */)
|| (entity->isTypeOf<User>() && show_users.checkAccess(throw_if_access_denied))
|| (entity->isTypeOf<Role>() && show_roles.checkAccess(throw_if_access_denied)))
entities.push_back(entity);
}
::sort(entities.begin(), entities.end(), IAccessEntity::LessByTypeAndName{});
return entities;
}
ASTs InterpreterShowGrantsQuery::getGrantQueries() const
{
auto entities = getEntities();
const auto & access_control = getContext()->getAccessControl();
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 AccessControl & access_control)
{
return getGrantQueriesImpl(user_or_role, &access_control, false);
}
ASTs InterpreterShowGrantsQuery::getAttachGrantQueries(const IAccessEntity & user_or_role)
{
return getGrantQueriesImpl(user_or_role, nullptr, true);
}
}
|