|
| 1 | +#include <Interpreters/Access/InterpreterExecuteAsQuery.h> |
| 2 | + |
| 3 | +#include <Access/AccessControl.h> |
| 4 | +#include <Access/User.h> |
| 5 | +#include <Core/Settings.h> |
| 6 | +#include <Core/ServerSettings.h> |
| 7 | +#include <Parsers/Access/ASTExecuteAsQuery.h> |
| 8 | +#include <Parsers/Access/ASTUserNameWithHost.h> |
| 9 | +#include <Interpreters/Context.h> |
| 10 | +#include <Interpreters/InterpreterFactory.h> |
| 11 | +#include <Interpreters/QueryFlags.h> |
| 12 | +#include <Interpreters/executeQuery.h> |
| 13 | + |
| 14 | + |
| 15 | +namespace DB |
| 16 | +{ |
| 17 | + |
| 18 | +namespace ErrorCodes |
| 19 | +{ |
| 20 | + extern const int SUPPORT_IS_DISABLED; |
| 21 | +} |
| 22 | + |
| 23 | +namespace ServerSetting |
| 24 | +{ |
| 25 | + extern const ServerSettingsBool allow_impersonate_user; |
| 26 | +} |
| 27 | + |
| 28 | +namespace |
| 29 | +{ |
| 30 | + /// Creates another query context to execute a query as another user. |
| 31 | + ContextMutablePtr impersonateQueryContext(ContextPtr context, const String & target_user_name) |
| 32 | + { |
| 33 | + auto new_context = Context::createCopy(context->getGlobalContext()); |
| 34 | + new_context->setClientInfo(context->getClientInfo()); |
| 35 | + new_context->makeQueryContext(); |
| 36 | + |
| 37 | + const auto & database = context->getCurrentDatabase(); |
| 38 | + if (!database.empty() && database != new_context->getCurrentDatabase()) |
| 39 | + new_context->setCurrentDatabase(database); |
| 40 | + |
| 41 | + new_context->setInsertionTable(context->getInsertionTable(), context->getInsertionTableColumnNames()); |
| 42 | + new_context->setProgressCallback(context->getProgressCallback()); |
| 43 | + new_context->setProcessListElement(context->getProcessListElement()); |
| 44 | + |
| 45 | + if (context->getCurrentTransaction()) |
| 46 | + new_context->setCurrentTransaction(context->getCurrentTransaction()); |
| 47 | + |
| 48 | + if (context->getZooKeeperMetadataTransaction()) |
| 49 | + new_context->initZooKeeperMetadataTransaction(context->getZooKeeperMetadataTransaction()); |
| 50 | + |
| 51 | + new_context->setUser(context->getAccessControl().getID<User>(target_user_name)); |
| 52 | + |
| 53 | + /// We need to update the client info to make currentUser() return `target_user_name`. |
| 54 | + new_context->setCurrentUserName(target_user_name); |
| 55 | + new_context->setInitialUserName(target_user_name); |
| 56 | + |
| 57 | + auto changed_settings = context->getSettingsRef().changes(); |
| 58 | + new_context->clampToSettingsConstraints(changed_settings, SettingSource::QUERY); |
| 59 | + new_context->applySettingsChanges(changed_settings); |
| 60 | + |
| 61 | + return new_context; |
| 62 | + } |
| 63 | + |
| 64 | + /// Changes the session context to execute all following queries in this session as another user. |
| 65 | + void impersonateSessionContext(ContextMutablePtr context, const String & target_user_name) |
| 66 | + { |
| 67 | + auto database = context->getCurrentDatabase(); |
| 68 | + auto changed_settings = context->getSettingsRef().changes(); |
| 69 | + |
| 70 | + context->setUser(context->getAccessControl().getID<User>(target_user_name)); |
| 71 | + |
| 72 | + /// We need to update the client info to make currentUser() return `target_user_name`. |
| 73 | + context->setCurrentUserName(target_user_name); |
| 74 | + context->setInitialUserName(target_user_name); |
| 75 | + |
| 76 | + context->clampToSettingsConstraints(changed_settings, SettingSource::QUERY); |
| 77 | + context->applySettingsChanges(changed_settings); |
| 78 | + |
| 79 | + if (!database.empty() && database != context->getCurrentDatabase()) |
| 80 | + context->setCurrentDatabase(database); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | +BlockIO InterpreterExecuteAsQuery::execute() |
| 86 | +{ |
| 87 | + if (!getContext()->getGlobalContext()->getServerSettings()[ServerSetting::allow_impersonate_user]) |
| 88 | + throw Exception(ErrorCodes::SUPPORT_IS_DISABLED, "IMPERSONATE feature is disabled, set allow_impersonate_user to 1 to enable"); |
| 89 | + |
| 90 | + const auto & query = query_ptr->as<const ASTExecuteAsQuery &>(); |
| 91 | + String target_user_name = query.target_user->toString(); |
| 92 | + getContext()->checkAccess(AccessType::IMPERSONATE, target_user_name); |
| 93 | + |
| 94 | + if (query.subquery) |
| 95 | + { |
| 96 | + /// EXECUTE AS <user> <subquery> |
| 97 | + auto subquery_context = impersonateQueryContext(getContext(), target_user_name); |
| 98 | + return executeQuery(query.subquery->formatWithSecretsOneLine(), subquery_context, QueryFlags{ .internal = true }).second; |
| 99 | + } |
| 100 | + else |
| 101 | + { |
| 102 | + /// EXECUTE AS <user> |
| 103 | + impersonateSessionContext(getContext()->getSessionContext(), target_user_name); |
| 104 | + return {}; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | + |
| 109 | +void registerInterpreterExecuteAsQuery(InterpreterFactory & factory) |
| 110 | +{ |
| 111 | + auto create_fn = [] (const InterpreterFactory::Arguments & args) |
| 112 | + { |
| 113 | + return std::make_unique<InterpreterExecuteAsQuery>(args.query, args.context); |
| 114 | + }; |
| 115 | + factory.registerInterpreter("InterpreterExecuteAsQuery", create_fn); |
| 116 | +} |
| 117 | + |
| 118 | +} |
0 commit comments