From 5bbed6ef9a4f12b82856c60a9f783cb105cef236 Mon Sep 17 00:00:00 2001 From: andrejtonev <29177572+andrejtonev@users.noreply.github.com> Date: Tue, 1 Aug 2023 23:04:35 +0200 Subject: [PATCH] Implement user caching to speed up PullPlan (#1109) --- src/glue/auth_checker.cpp | 12 +++++++----- src/glue/auth_checker.hpp | 1 + src/query/interpreter.cpp | 2 ++ 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/glue/auth_checker.cpp b/src/glue/auth_checker.cpp index 3ea3f998b..1c6b9ab9a 100644 --- a/src/glue/auth_checker.cpp +++ b/src/glue/auth_checker.cpp @@ -95,12 +95,14 @@ std::unique_ptr AuthChecker::GetFineGra } try { auto locked_auth = auth_->Lock(); - auto user = locked_auth->GetUser(username); - if (!user) { - throw memgraph::query::QueryRuntimeException("User '{}' doesn't exist .", username); + if (username != user_.username()) { + auto maybe_user = locked_auth->GetUser(username); + if (!maybe_user) { + throw memgraph::query::QueryRuntimeException("User '{}' doesn't exist .", username); + } + user_ = std::move(*maybe_user); } - - return std::make_unique(std::move(*user), dba); + return std::make_unique(user_, dba); } catch (const memgraph::auth::AuthException &e) { throw memgraph::query::QueryRuntimeException(e.what()); diff --git a/src/glue/auth_checker.hpp b/src/glue/auth_checker.hpp index e0f917723..75c35eacf 100644 --- a/src/glue/auth_checker.hpp +++ b/src/glue/auth_checker.hpp @@ -39,6 +39,7 @@ class AuthChecker : public query::AuthChecker { private: memgraph::utils::Synchronized *auth_; + mutable auth::User user_; }; #ifdef MG_ENTERPRISE class FineGrainedAuthChecker : public query::FineGrainedAuthChecker { diff --git a/src/query/interpreter.cpp b/src/query/interpreter.cpp index 9a158f91e..e16dfe7c4 100644 --- a/src/query/interpreter.cpp +++ b/src/query/interpreter.cpp @@ -1221,6 +1221,8 @@ PullPlan::PullPlan(const std::shared_ptr plan, const Parameters &par ctx_.evaluation_context.labels = NamesToLabels(plan->ast_storage().labels_, dba); #ifdef MG_ENTERPRISE if (license::global_license_checker.IsEnterpriseValidFast() && username.has_value() && dba) { + // TODO How can we avoid creating this every time? If we must create it, it would be faster with an auth::User + // instead of the username auto auth_checker = interpreter_context->auth_checker->GetFineGrainedAuthChecker(*username, dba); // if the user has global privileges to read, edit and write anything, we don't need to perform authorization