ClickHouse/src/Interpreters/Session.h

97 lines
3.7 KiB
C++
Raw Normal View History

#pragma once
2021-08-01 14:12:34 +00:00
#include <Common/SettingsChanges.h>
#include <Access/Authentication.h>
#include <Interpreters/ClientInfo.h>
2021-08-01 14:12:34 +00:00
#include <Interpreters/Context_fwd.h>
#include <chrono>
#include <memory>
#include <optional>
namespace Poco::Net { class SocketAddress; }
namespace DB
{
class Credentials;
class Authentication;
struct NamedSessionData;
class NamedSessionsStorage;
2021-08-01 14:12:34 +00:00
struct User;
using UserPtr = std::shared_ptr<const User>;
class SessionLog;
/** Represents user-session from the server perspective,
* basically it is just a smaller subset of Context API, simplifies Context management.
*
* Holds session context, facilitates acquisition of NamedSession and proper creation of query contexts.
*/
class Session
{
public:
2021-08-19 14:09:44 +00:00
/// Stops using named sessions. The method must be called at the server shutdown.
static void shutdownNamedSessions();
2021-08-01 14:12:34 +00:00
Session(const ContextPtr & global_context_, ClientInfo::Interface interface_);
Session(Session &&);
~Session();
Session(const Session &) = delete;
Session& operator=(const Session &) = delete;
2021-08-01 14:12:34 +00:00
/// Provides information about the authentication type of a specified user.
Authentication::Type getAuthenticationType(const String & user_name) const;
2021-08-24 07:09:13 +00:00
/// Same as getAuthenticationType, but adds LoginFailure event in case of error.
Authentication::Type getAuthenticationTypeOrLogInFailure(const String & user_name) const;
2021-08-01 14:12:34 +00:00
/// Sets the current user, checks the credentials and that the specified address is allowed to connect from.
/// The function throws an exception if there is no such user or password is wrong.
void authenticate(const String & user_name, const String & password, const Poco::Net::SocketAddress & address);
void authenticate(const Credentials & credentials_, const Poco::Net::SocketAddress & address_);
2021-08-01 14:12:34 +00:00
/// Returns a reference to session ClientInfo.
ClientInfo & getClientInfo();
const ClientInfo & getClientInfo() const;
2021-08-01 14:12:34 +00:00
/// Makes a session context, can be used one or zero times.
/// The function also assigns an user to this context.
ContextMutablePtr makeSessionContext();
ContextMutablePtr makeSessionContext(const String & session_name_, std::chrono::steady_clock::duration timeout_, bool session_check_);
2021-08-01 14:12:34 +00:00
ContextMutablePtr sessionContext() { return session_context; }
ContextPtr sessionContext() const { return session_context; }
2021-08-01 14:12:34 +00:00
/// Makes a query context, can be used multiple times, with or without makeSession() called earlier.
/// The query context will be created from a copy of a session context if it exists, or from a copy of
/// a global context otherwise. In the latter case the function also assigns an user to this context.
ContextMutablePtr makeQueryContext() const { return makeQueryContext(getClientInfo()); }
ContextMutablePtr makeQueryContext(const ClientInfo & query_client_info) const;
ContextMutablePtr makeQueryContext(ClientInfo && query_client_info) const;
/// Releases the currently used session ID so it becomes available for reuse by another session.
void releaseSessionID();
2021-08-01 14:12:34 +00:00
private:
std::shared_ptr<SessionLog> getSessionLog() const;
2021-08-01 14:12:34 +00:00
ContextMutablePtr makeQueryContextImpl(const ClientInfo * client_info_to_copy, ClientInfo * client_info_to_move) const;
mutable bool notified_session_log_about_login = false;
const UUID session_id;
2021-08-01 14:12:34 +00:00
const ContextPtr global_context;
2021-08-01 14:12:34 +00:00
/// ClientInfo that will be copied to a session context when it's created.
std::optional<ClientInfo> prepared_client_info;
2021-08-01 14:12:34 +00:00
mutable UserPtr user;
std::optional<UUID> user_id;
ContextMutablePtr session_context;
2021-08-01 14:12:34 +00:00
mutable bool query_context_created = false;
std::shared_ptr<NamedSessionData> named_session;
2021-08-01 14:12:34 +00:00
bool named_session_created = false;
};
}