2021-03-05 14:57:16 +00:00
|
|
|
#include <Interpreters/Session.h>
|
|
|
|
|
2021-11-02 11:06:20 +00:00
|
|
|
#include <Access/AccessControl.h>
|
2021-03-05 14:57:16 +00:00
|
|
|
#include <Access/Credentials.h>
|
2021-08-01 14:12:34 +00:00
|
|
|
#include <Access/ContextAccess.h>
|
2021-03-05 14:57:16 +00:00
|
|
|
#include <Access/User.h>
|
2021-10-19 16:45:48 +00:00
|
|
|
#include <base/logger_useful.h>
|
2021-03-05 14:57:16 +00:00
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <Common/ThreadPool.h>
|
|
|
|
#include <Common/setThreadName.h>
|
|
|
|
#include <Interpreters/Context.h>
|
2021-03-05 14:57:16 +00:00
|
|
|
#include <Interpreters/SessionLog.h>
|
2021-03-05 14:57:16 +00:00
|
|
|
|
2021-10-19 16:45:48 +00:00
|
|
|
#include <magic_enum.hpp>
|
|
|
|
|
2021-03-05 14:57:16 +00:00
|
|
|
#include <atomic>
|
2021-08-01 14:12:34 +00:00
|
|
|
#include <condition_variable>
|
2021-03-05 14:57:16 +00:00
|
|
|
#include <deque>
|
2021-08-01 14:12:34 +00:00
|
|
|
#include <mutex>
|
|
|
|
#include <unordered_map>
|
2021-03-05 14:57:16 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2021-08-01 14:12:34 +00:00
|
|
|
|
2021-03-05 14:57:16 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
extern const int SESSION_NOT_FOUND;
|
|
|
|
extern const int SESSION_IS_LOCKED;
|
|
|
|
}
|
|
|
|
|
2021-08-01 14:12:34 +00:00
|
|
|
|
2021-03-05 14:57:16 +00:00
|
|
|
class NamedSessionsStorage;
|
|
|
|
|
2021-08-01 14:12:34 +00:00
|
|
|
/// User ID and session identifier. Named sessions are local to users.
|
|
|
|
using NamedSessionKey = std::pair<UUID, String>;
|
2021-03-05 14:57:16 +00:00
|
|
|
|
|
|
|
/// Named sessions. The user could specify session identifier to reuse settings and temporary tables in subsequent requests.
|
|
|
|
struct NamedSessionData
|
|
|
|
{
|
|
|
|
NamedSessionKey key;
|
|
|
|
UInt64 close_cycle = 0;
|
|
|
|
ContextMutablePtr context;
|
|
|
|
std::chrono::steady_clock::duration timeout;
|
|
|
|
NamedSessionsStorage & parent;
|
|
|
|
|
|
|
|
NamedSessionData(NamedSessionKey key_, ContextPtr context_, std::chrono::steady_clock::duration timeout_, NamedSessionsStorage & parent_)
|
|
|
|
: key(std::move(key_)), context(Context::createCopy(context_)), timeout(timeout_), parent(parent_)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void release();
|
|
|
|
};
|
|
|
|
|
|
|
|
class NamedSessionsStorage
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Key = NamedSessionKey;
|
|
|
|
|
2021-08-19 14:09:44 +00:00
|
|
|
static NamedSessionsStorage & instance()
|
|
|
|
{
|
|
|
|
static NamedSessionsStorage the_instance;
|
|
|
|
return the_instance;
|
|
|
|
}
|
|
|
|
|
2021-03-05 14:57:16 +00:00
|
|
|
~NamedSessionsStorage()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-08-19 14:09:44 +00:00
|
|
|
shutdown();
|
2021-03-05 14:57:16 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-19 14:09:44 +00:00
|
|
|
void shutdown()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::lock_guard lock{mutex};
|
|
|
|
sessions.clear();
|
|
|
|
if (!thread.joinable())
|
|
|
|
return;
|
|
|
|
quit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
cond.notify_one();
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
|
2021-03-05 14:57:16 +00:00
|
|
|
/// Find existing session or create a new.
|
2021-08-01 14:12:34 +00:00
|
|
|
std::pair<std::shared_ptr<NamedSessionData>, bool> acquireSession(
|
|
|
|
const ContextPtr & global_context,
|
|
|
|
const UUID & user_id,
|
2021-03-05 14:57:16 +00:00
|
|
|
const String & session_id,
|
|
|
|
std::chrono::steady_clock::duration timeout,
|
|
|
|
bool throw_if_not_found)
|
|
|
|
{
|
|
|
|
std::unique_lock lock(mutex);
|
|
|
|
|
2021-08-01 14:12:34 +00:00
|
|
|
Key key{user_id, session_id};
|
2021-03-05 14:57:16 +00:00
|
|
|
|
|
|
|
auto it = sessions.find(key);
|
|
|
|
if (it == sessions.end())
|
|
|
|
{
|
|
|
|
if (throw_if_not_found)
|
|
|
|
throw Exception("Session not found.", ErrorCodes::SESSION_NOT_FOUND);
|
|
|
|
|
|
|
|
/// Create a new session from current context.
|
2021-08-01 14:12:34 +00:00
|
|
|
auto context = Context::createCopy(global_context);
|
2021-03-05 14:57:16 +00:00
|
|
|
it = sessions.insert(std::make_pair(key, std::make_shared<NamedSessionData>(key, context, timeout, *this))).first;
|
2021-08-01 14:12:34 +00:00
|
|
|
const auto & session = it->second;
|
2021-08-19 14:09:44 +00:00
|
|
|
|
|
|
|
if (!thread.joinable())
|
|
|
|
thread = ThreadFromGlobalPool{&NamedSessionsStorage::cleanThread, this};
|
|
|
|
|
2021-08-01 14:12:34 +00:00
|
|
|
return {session, true};
|
2021-03-05 14:57:16 +00:00
|
|
|
}
|
2021-08-01 14:12:34 +00:00
|
|
|
else
|
2021-03-05 14:57:16 +00:00
|
|
|
{
|
2021-08-01 14:12:34 +00:00
|
|
|
/// Use existing session.
|
|
|
|
const auto & session = it->second;
|
2021-03-05 14:57:16 +00:00
|
|
|
|
2021-08-01 14:12:34 +00:00
|
|
|
if (!session.unique())
|
|
|
|
throw Exception("Session is locked by a concurrent client.", ErrorCodes::SESSION_IS_LOCKED);
|
|
|
|
return {session, false};
|
|
|
|
}
|
2021-03-05 14:57:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void releaseSession(NamedSessionData & session)
|
|
|
|
{
|
|
|
|
std::unique_lock lock(mutex);
|
|
|
|
scheduleCloseSession(session, lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
class SessionKeyHash
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
size_t operator()(const Key & key) const
|
|
|
|
{
|
|
|
|
SipHash hash;
|
|
|
|
hash.update(key.first);
|
|
|
|
hash.update(key.second);
|
|
|
|
return hash.get64();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// TODO it's very complicated. Make simple std::map with time_t or boost::multi_index.
|
|
|
|
using Container = std::unordered_map<Key, std::shared_ptr<NamedSessionData>, SessionKeyHash>;
|
|
|
|
using CloseTimes = std::deque<std::vector<Key>>;
|
|
|
|
Container sessions;
|
|
|
|
CloseTimes close_times;
|
|
|
|
std::chrono::steady_clock::duration close_interval = std::chrono::seconds(1);
|
|
|
|
std::chrono::steady_clock::time_point close_cycle_time = std::chrono::steady_clock::now();
|
|
|
|
UInt64 close_cycle = 0;
|
|
|
|
|
|
|
|
void scheduleCloseSession(NamedSessionData & session, std::unique_lock<std::mutex> &)
|
|
|
|
{
|
|
|
|
/// Push it on a queue of sessions to close, on a position corresponding to the timeout.
|
|
|
|
/// (timeout is measured from current moment of time)
|
|
|
|
|
|
|
|
const UInt64 close_index = session.timeout / close_interval + 1;
|
|
|
|
const auto new_close_cycle = close_cycle + close_index;
|
|
|
|
|
|
|
|
if (session.close_cycle != new_close_cycle)
|
|
|
|
{
|
|
|
|
session.close_cycle = new_close_cycle;
|
|
|
|
if (close_times.size() < close_index + 1)
|
|
|
|
close_times.resize(close_index + 1);
|
|
|
|
close_times[close_index].emplace_back(session.key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cleanThread()
|
|
|
|
{
|
|
|
|
setThreadName("SessionCleaner");
|
|
|
|
std::unique_lock lock{mutex};
|
2021-08-19 14:09:44 +00:00
|
|
|
while (!quit)
|
2021-03-05 14:57:16 +00:00
|
|
|
{
|
|
|
|
auto interval = closeSessions(lock);
|
|
|
|
if (cond.wait_for(lock, interval, [this]() -> bool { return quit; }))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Close sessions, that has been expired. Returns how long to wait for next session to be expired, if no new sessions will be added.
|
|
|
|
std::chrono::steady_clock::duration closeSessions(std::unique_lock<std::mutex> & lock)
|
|
|
|
{
|
|
|
|
const auto now = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
/// The time to close the next session did not come
|
|
|
|
if (now < close_cycle_time)
|
|
|
|
return close_cycle_time - now; /// Will sleep until it comes.
|
|
|
|
|
|
|
|
const auto current_cycle = close_cycle;
|
|
|
|
|
|
|
|
++close_cycle;
|
|
|
|
close_cycle_time = now + close_interval;
|
|
|
|
|
|
|
|
if (close_times.empty())
|
|
|
|
return close_interval;
|
|
|
|
|
|
|
|
auto & sessions_to_close = close_times.front();
|
|
|
|
|
|
|
|
for (const auto & key : sessions_to_close)
|
|
|
|
{
|
|
|
|
const auto session = sessions.find(key);
|
|
|
|
|
|
|
|
if (session != sessions.end() && session->second->close_cycle <= current_cycle)
|
|
|
|
{
|
|
|
|
if (!session->second.unique())
|
|
|
|
{
|
|
|
|
/// Skip but move it to close on the next cycle.
|
|
|
|
session->second->timeout = std::chrono::steady_clock::duration{0};
|
|
|
|
scheduleCloseSession(*session->second, lock);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
sessions.erase(session);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close_times.pop_front();
|
|
|
|
return close_interval;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::mutex mutex;
|
|
|
|
std::condition_variable cond;
|
2021-08-19 14:09:44 +00:00
|
|
|
ThreadFromGlobalPool thread;
|
|
|
|
bool quit = false;
|
2021-03-05 14:57:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void NamedSessionData::release()
|
|
|
|
{
|
|
|
|
parent.releaseSession(*this);
|
|
|
|
}
|
|
|
|
|
2021-08-19 14:09:44 +00:00
|
|
|
void Session::shutdownNamedSessions()
|
2021-03-05 14:57:16 +00:00
|
|
|
{
|
2021-08-19 14:09:44 +00:00
|
|
|
NamedSessionsStorage::instance().shutdown();
|
2021-03-05 14:57:16 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 13:25:47 +00:00
|
|
|
Session::Session(const ContextPtr & global_context_, ClientInfo::Interface interface_, bool is_secure)
|
2021-10-30 14:51:58 +00:00
|
|
|
: auth_id(UUIDHelpers::generateV4()),
|
2021-10-19 12:54:28 +00:00
|
|
|
global_context(global_context_),
|
2021-11-03 13:14:40 +00:00
|
|
|
interface(interface_),
|
2021-10-19 16:45:48 +00:00
|
|
|
log(&Poco::Logger::get(String{magic_enum::enum_name(interface_)} + "-Session"))
|
2021-03-05 14:57:16 +00:00
|
|
|
{
|
2021-08-01 14:12:34 +00:00
|
|
|
prepared_client_info.emplace();
|
|
|
|
prepared_client_info->interface = interface_;
|
2022-03-28 13:25:47 +00:00
|
|
|
prepared_client_info->is_secure = is_secure;
|
2021-03-05 14:57:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Session::~Session()
|
|
|
|
{
|
2021-10-19 12:54:28 +00:00
|
|
|
LOG_DEBUG(log, "{} Destroying {} of user {}",
|
2021-10-30 14:51:58 +00:00
|
|
|
toString(auth_id),
|
2021-10-19 12:54:28 +00:00
|
|
|
(named_session ? "named session '" + named_session->key.second + "'" : "unnamed session"),
|
|
|
|
(user_id ? toString(*user_id) : "<EMPTY>")
|
|
|
|
);
|
|
|
|
|
2021-08-01 14:12:34 +00:00
|
|
|
/// Early release a NamedSessionData.
|
|
|
|
if (named_session)
|
|
|
|
named_session->release();
|
2021-03-05 14:57:16 +00:00
|
|
|
|
|
|
|
if (notified_session_log_about_login)
|
|
|
|
{
|
2021-09-30 18:07:29 +00:00
|
|
|
if (auto session_log = getSessionLog(); session_log && user)
|
2021-10-30 14:51:58 +00:00
|
|
|
session_log->addLogOut(auth_id, user->getName(), getClientInfo());
|
2021-03-05 14:57:16 +00:00
|
|
|
}
|
2021-03-05 14:57:16 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 14:03:20 +00:00
|
|
|
AuthenticationType Session::getAuthenticationType(const String & user_name) const
|
2021-03-05 14:57:16 +00:00
|
|
|
{
|
2021-11-02 11:06:20 +00:00
|
|
|
return global_context->getAccessControl().read<User>(user_name)->auth_data.getType();
|
2021-03-05 14:57:16 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 14:03:20 +00:00
|
|
|
AuthenticationType Session::getAuthenticationTypeOrLogInFailure(const String & user_name) const
|
2021-03-05 14:57:16 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return getAuthenticationType(user_name);
|
|
|
|
}
|
|
|
|
catch (const Exception & e)
|
|
|
|
{
|
|
|
|
if (auto session_log = getSessionLog())
|
2021-10-30 14:51:58 +00:00
|
|
|
session_log->addLoginFailure(auth_id, getClientInfo(), user_name, e);
|
2021-03-05 14:57:16 +00:00
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-01 14:12:34 +00:00
|
|
|
void Session::authenticate(const String & user_name, const String & password, const Poco::Net::SocketAddress & address)
|
2021-03-05 14:57:16 +00:00
|
|
|
{
|
2021-08-01 14:12:34 +00:00
|
|
|
authenticate(BasicCredentials{user_name, password}, address);
|
2021-03-05 14:57:16 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 14:12:34 +00:00
|
|
|
void Session::authenticate(const Credentials & credentials_, const Poco::Net::SocketAddress & address_)
|
2021-03-05 14:57:16 +00:00
|
|
|
{
|
2021-08-01 14:12:34 +00:00
|
|
|
if (session_context)
|
|
|
|
throw Exception("If there is a session context it must be created after authentication", ErrorCodes::LOGICAL_ERROR);
|
2021-03-05 14:57:16 +00:00
|
|
|
|
2021-08-18 13:50:15 +00:00
|
|
|
auto address = address_;
|
|
|
|
if ((address == Poco::Net::SocketAddress{}) && (prepared_client_info->interface == ClientInfo::Interface::LOCAL))
|
|
|
|
address = Poco::Net::SocketAddress{"127.0.0.1", 0};
|
|
|
|
|
2021-10-19 12:54:28 +00:00
|
|
|
LOG_DEBUG(log, "{} Authenticating user '{}' from {}",
|
2021-10-30 14:51:58 +00:00
|
|
|
toString(auth_id), credentials_.getUserName(), address.toString());
|
2021-10-19 12:54:28 +00:00
|
|
|
|
2021-03-05 14:57:16 +00:00
|
|
|
try
|
|
|
|
{
|
2021-11-22 21:50:15 +00:00
|
|
|
user_id = global_context->getAccessControl().authenticate(credentials_, address.host());
|
2021-10-19 12:54:28 +00:00
|
|
|
LOG_DEBUG(log, "{} Authenticated with global context as user {}",
|
2021-10-30 14:51:58 +00:00
|
|
|
toString(auth_id), user_id ? toString(*user_id) : "<EMPTY>");
|
2021-03-05 14:57:16 +00:00
|
|
|
}
|
2021-08-30 17:37:07 +00:00
|
|
|
catch (const Exception & e)
|
2021-03-05 14:57:16 +00:00
|
|
|
{
|
2021-10-30 14:51:58 +00:00
|
|
|
LOG_DEBUG(log, "{} Authentication failed with error: {}", toString(auth_id), e.what());
|
2021-03-05 14:57:16 +00:00
|
|
|
if (auto session_log = getSessionLog())
|
2021-10-30 14:51:58 +00:00
|
|
|
session_log->addLoginFailure(auth_id, *prepared_client_info, credentials_.getUserName(), e);
|
2021-03-05 14:57:16 +00:00
|
|
|
throw;
|
|
|
|
}
|
2021-03-05 14:57:16 +00:00
|
|
|
|
2021-08-01 14:12:34 +00:00
|
|
|
prepared_client_info->current_user = credentials_.getUserName();
|
2021-08-18 13:50:15 +00:00
|
|
|
prepared_client_info->current_address = address;
|
2021-03-05 14:57:16 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 14:12:34 +00:00
|
|
|
ClientInfo & Session::getClientInfo()
|
2021-03-05 14:57:16 +00:00
|
|
|
{
|
2021-08-01 14:12:34 +00:00
|
|
|
return session_context ? session_context->getClientInfo() : *prepared_client_info;
|
2021-03-05 14:57:16 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 14:12:34 +00:00
|
|
|
const ClientInfo & Session::getClientInfo() const
|
2021-03-05 14:57:16 +00:00
|
|
|
{
|
2021-08-01 14:12:34 +00:00
|
|
|
return session_context ? session_context->getClientInfo() : *prepared_client_info;
|
2021-03-05 14:57:16 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 14:12:34 +00:00
|
|
|
ContextMutablePtr Session::makeSessionContext()
|
2021-03-05 14:57:16 +00:00
|
|
|
{
|
2021-08-01 14:12:34 +00:00
|
|
|
if (session_context)
|
|
|
|
throw Exception("Session context already exists", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
if (query_context_created)
|
|
|
|
throw Exception("Session context must be created before any query context", ErrorCodes::LOGICAL_ERROR);
|
2021-03-05 14:57:16 +00:00
|
|
|
|
2021-10-19 12:54:28 +00:00
|
|
|
LOG_DEBUG(log, "{} Creating session context with user_id: {}",
|
2021-10-30 14:51:58 +00:00
|
|
|
toString(auth_id), user_id ? toString(*user_id) : "<EMPTY>");
|
2021-08-01 14:12:34 +00:00
|
|
|
/// Make a new session context.
|
|
|
|
ContextMutablePtr new_session_context;
|
|
|
|
new_session_context = Context::createCopy(global_context);
|
|
|
|
new_session_context->makeSessionContext();
|
2021-03-05 14:57:16 +00:00
|
|
|
|
2021-08-01 14:12:34 +00:00
|
|
|
/// Copy prepared client info to the new session context.
|
|
|
|
auto & res_client_info = new_session_context->getClientInfo();
|
|
|
|
res_client_info = std::move(prepared_client_info).value();
|
|
|
|
prepared_client_info.reset();
|
2021-03-05 14:57:16 +00:00
|
|
|
|
2021-08-01 14:12:34 +00:00
|
|
|
/// Set user information for the new context: current profiles, roles, access rights.
|
|
|
|
if (user_id)
|
|
|
|
new_session_context->setUser(*user_id);
|
|
|
|
|
|
|
|
/// Session context is ready.
|
|
|
|
session_context = new_session_context;
|
|
|
|
user = session_context->getUser();
|
2021-03-05 14:57:16 +00:00
|
|
|
|
|
|
|
return session_context;
|
|
|
|
}
|
|
|
|
|
2021-03-05 14:57:16 +00:00
|
|
|
ContextMutablePtr Session::makeSessionContext(const String & session_name_, std::chrono::steady_clock::duration timeout_, bool session_check_)
|
2021-03-05 14:57:16 +00:00
|
|
|
{
|
2021-08-01 14:12:34 +00:00
|
|
|
if (session_context)
|
|
|
|
throw Exception("Session context already exists", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
if (query_context_created)
|
|
|
|
throw Exception("Session context must be created before any query context", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
2021-10-19 12:54:28 +00:00
|
|
|
LOG_DEBUG(log, "{} Creating named session context with name: {}, user_id: {}",
|
2021-10-30 14:51:58 +00:00
|
|
|
toString(auth_id), session_name_, user_id ? toString(*user_id) : "<EMPTY>");
|
2021-10-19 12:54:28 +00:00
|
|
|
|
2021-08-01 14:12:34 +00:00
|
|
|
/// Make a new session context OR
|
|
|
|
/// if the `session_id` and `user_id` were used before then just get a previously created session context.
|
|
|
|
std::shared_ptr<NamedSessionData> new_named_session;
|
|
|
|
bool new_named_session_created = false;
|
|
|
|
std::tie(new_named_session, new_named_session_created)
|
2021-03-05 14:57:16 +00:00
|
|
|
= NamedSessionsStorage::instance().acquireSession(global_context, user_id.value_or(UUID{}), session_name_, timeout_, session_check_);
|
2021-08-01 14:12:34 +00:00
|
|
|
|
|
|
|
auto new_session_context = new_named_session->context;
|
|
|
|
new_session_context->makeSessionContext();
|
|
|
|
|
|
|
|
/// Copy prepared client info to the session context, no matter it's been just created or not.
|
|
|
|
/// If we continue using a previously created session context found by session ID
|
|
|
|
/// it's necessary to replace the client info in it anyway, because it contains actual connection information (client address, etc.)
|
|
|
|
auto & res_client_info = new_session_context->getClientInfo();
|
|
|
|
res_client_info = std::move(prepared_client_info).value();
|
|
|
|
prepared_client_info.reset();
|
|
|
|
|
|
|
|
/// Set user information for the new context: current profiles, roles, access rights.
|
|
|
|
if (user_id && !new_session_context->getUser())
|
|
|
|
new_session_context->setUser(*user_id);
|
|
|
|
|
|
|
|
/// Session context is ready.
|
2021-03-05 14:57:16 +00:00
|
|
|
session_context = std::move(new_session_context);
|
2021-08-01 14:12:34 +00:00
|
|
|
named_session = new_named_session;
|
|
|
|
named_session_created = new_named_session_created;
|
|
|
|
user = session_context->getUser();
|
|
|
|
|
2021-03-05 14:57:16 +00:00
|
|
|
return session_context;
|
|
|
|
}
|
|
|
|
|
2021-08-01 14:12:34 +00:00
|
|
|
ContextMutablePtr Session::makeQueryContext(const ClientInfo & query_client_info) const
|
2021-03-05 14:57:16 +00:00
|
|
|
{
|
2021-08-01 14:12:34 +00:00
|
|
|
return makeQueryContextImpl(&query_client_info, nullptr);
|
2021-03-05 14:57:16 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 14:12:34 +00:00
|
|
|
ContextMutablePtr Session::makeQueryContext(ClientInfo && query_client_info) const
|
2021-03-05 14:57:16 +00:00
|
|
|
{
|
2021-08-01 14:12:34 +00:00
|
|
|
return makeQueryContextImpl(nullptr, &query_client_info);
|
2021-03-05 14:57:16 +00:00
|
|
|
}
|
|
|
|
|
2021-03-05 14:57:16 +00:00
|
|
|
std::shared_ptr<SessionLog> Session::getSessionLog() const
|
|
|
|
{
|
2021-11-03 13:14:40 +00:00
|
|
|
/// For the LOCAL interface we don't send events to the session log
|
|
|
|
/// because the LOCAL interface is internal, it does nothing with networking.
|
|
|
|
if (interface == ClientInfo::Interface::LOCAL)
|
|
|
|
return nullptr;
|
|
|
|
|
2021-03-05 14:57:16 +00:00
|
|
|
// take it from global context, since it outlives the Session and always available.
|
|
|
|
// please note that server may have session_log disabled, hence this may return nullptr.
|
|
|
|
return global_context->getSessionLog();
|
|
|
|
}
|
|
|
|
|
2021-08-01 14:12:34 +00:00
|
|
|
ContextMutablePtr Session::makeQueryContextImpl(const ClientInfo * client_info_to_copy, ClientInfo * client_info_to_move) const
|
2021-03-05 14:57:16 +00:00
|
|
|
{
|
2021-08-01 14:12:34 +00:00
|
|
|
/// We can create a query context either from a session context or from a global context.
|
|
|
|
bool from_session_context = static_cast<bool>(session_context);
|
|
|
|
|
|
|
|
/// Create a new query context.
|
|
|
|
ContextMutablePtr query_context = Context::createCopy(from_session_context ? session_context : global_context);
|
|
|
|
query_context->makeQueryContext();
|
|
|
|
|
2021-10-19 12:54:28 +00:00
|
|
|
LOG_DEBUG(log, "{} Creating query context from {} context, user_id: {}, parent context user: {}",
|
2021-10-30 14:51:58 +00:00
|
|
|
toString(auth_id),
|
2021-10-19 12:54:28 +00:00
|
|
|
from_session_context ? "session" : "global",
|
|
|
|
user_id ? toString(*user_id) : "<EMPTY>",
|
|
|
|
query_context->getUser() ? query_context->getUser()->getName() : "<NOT SET>");
|
|
|
|
|
2021-08-01 14:12:34 +00:00
|
|
|
/// Copy the specified client info to the new query context.
|
|
|
|
auto & res_client_info = query_context->getClientInfo();
|
|
|
|
if (client_info_to_move)
|
|
|
|
res_client_info = std::move(*client_info_to_move);
|
|
|
|
else if (client_info_to_copy && (client_info_to_copy != &getClientInfo()))
|
|
|
|
res_client_info = *client_info_to_copy;
|
|
|
|
|
|
|
|
/// Copy current user's name and address if it was authenticated after query_client_info was initialized.
|
|
|
|
if (prepared_client_info && !prepared_client_info->current_user.empty())
|
|
|
|
{
|
|
|
|
res_client_info.current_user = prepared_client_info->current_user;
|
|
|
|
res_client_info.current_address = prepared_client_info->current_address;
|
|
|
|
}
|
2021-03-05 14:57:16 +00:00
|
|
|
|
2021-08-01 14:12:34 +00:00
|
|
|
/// Set parameters of initial query.
|
|
|
|
if (res_client_info.query_kind == ClientInfo::QueryKind::NO_QUERY)
|
|
|
|
res_client_info.query_kind = ClientInfo::QueryKind::INITIAL_QUERY;
|
2021-03-05 14:57:16 +00:00
|
|
|
|
2021-08-01 14:12:34 +00:00
|
|
|
if (res_client_info.query_kind == ClientInfo::QueryKind::INITIAL_QUERY)
|
|
|
|
{
|
|
|
|
res_client_info.initial_user = res_client_info.current_user;
|
|
|
|
res_client_info.initial_address = res_client_info.current_address;
|
|
|
|
}
|
2021-03-05 14:57:16 +00:00
|
|
|
|
2021-11-18 13:04:42 +00:00
|
|
|
/// Sets that row policies of the initial user should be used too.
|
|
|
|
query_context->enableRowPoliciesOfInitialUser();
|
2021-08-01 14:12:34 +00:00
|
|
|
|
|
|
|
/// Set user information for the new context: current profiles, roles, access rights.
|
|
|
|
if (user_id && !query_context->getUser())
|
|
|
|
query_context->setUser(*user_id);
|
|
|
|
|
|
|
|
/// Query context is ready.
|
|
|
|
query_context_created = true;
|
|
|
|
user = query_context->getUser();
|
|
|
|
|
2021-03-05 14:57:16 +00:00
|
|
|
if (!notified_session_log_about_login)
|
|
|
|
{
|
2021-10-19 12:54:28 +00:00
|
|
|
if (auto session_log = getSessionLog(); user && user_id && session_log)
|
2021-03-05 14:57:16 +00:00
|
|
|
{
|
|
|
|
session_log->addLoginSuccess(
|
2021-10-30 14:51:58 +00:00
|
|
|
auth_id,
|
2021-03-05 14:57:16 +00:00
|
|
|
named_session ? std::optional<std::string>(named_session->key.second) : std::nullopt,
|
|
|
|
*query_context);
|
|
|
|
|
|
|
|
notified_session_log_about_login = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-01 14:12:34 +00:00
|
|
|
return query_context;
|
2021-03-05 14:57:16 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 14:32:01 +00:00
|
|
|
|
|
|
|
void Session::releaseSessionID()
|
|
|
|
{
|
|
|
|
if (!named_session)
|
|
|
|
return;
|
|
|
|
named_session->release();
|
|
|
|
named_session = nullptr;
|
|
|
|
}
|
|
|
|
|
2021-03-05 14:57:16 +00:00
|
|
|
}
|
2021-03-05 14:57:16 +00:00
|
|
|
|