2021-03-05 14:57:16 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-08-01 14:12:34 +00:00
|
|
|
#include <Common/SettingsChanges.h>
|
2023-04-24 15:12:45 +00:00
|
|
|
#include <Access/AuthenticationData.h>
|
2021-03-05 14:57:16 +00:00
|
|
|
#include <Interpreters/ClientInfo.h>
|
2021-08-01 14:12:34 +00:00
|
|
|
#include <Interpreters/Context_fwd.h>
|
2023-07-30 22:09:03 +00:00
|
|
|
#include <Interpreters/SessionTracker.h>
|
2021-03-05 14:57:16 +00:00
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <memory>
|
2023-11-30 18:29:00 +00:00
|
|
|
#include <mutex>
|
2021-03-05 14:57:16 +00:00
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
namespace Poco::Net { class SocketAddress; }
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
class Credentials;
|
2021-11-01 14:03:20 +00:00
|
|
|
class AuthenticationData;
|
2021-03-05 14:57:16 +00:00
|
|
|
struct NamedSessionData;
|
|
|
|
class NamedSessionsStorage;
|
2021-08-01 14:12:34 +00:00
|
|
|
struct User;
|
|
|
|
using UserPtr = std::shared_ptr<const User>;
|
2021-03-05 14:57:16 +00:00
|
|
|
class SessionLog;
|
2021-03-05 14:57:16 +00:00
|
|
|
|
|
|
|
/** 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-03-05 14:57:16 +00:00
|
|
|
|
2022-09-18 07:11:52 +00:00
|
|
|
Session(const ContextPtr & global_context_, ClientInfo::Interface interface_, bool is_secure = false, const std::string & certificate = "");
|
2021-08-01 14:12:34 +00:00
|
|
|
~Session();
|
2021-03-05 14:57:16 +00:00
|
|
|
|
2021-10-20 09:45:32 +00:00
|
|
|
Session(const Session &&) = delete;
|
2022-01-16 05:12:55 +00:00
|
|
|
Session & operator=(const Session &&) = delete;
|
2021-03-05 14:57:16 +00:00
|
|
|
Session(const Session &) = delete;
|
2022-01-16 05:12:55 +00:00
|
|
|
Session & operator=(const Session &) = delete;
|
2021-03-05 14:57:16 +00:00
|
|
|
|
2021-08-01 14:12:34 +00:00
|
|
|
/// Provides information about the authentication type of a specified user.
|
2021-11-01 14:03:20 +00:00
|
|
|
AuthenticationType getAuthenticationType(const String & user_name) const;
|
2021-08-24 07:09:13 +00:00
|
|
|
|
2021-03-05 14:57:16 +00:00
|
|
|
/// Same as getAuthenticationType, but adds LoginFailure event in case of error.
|
2021-11-01 14:03:20 +00:00
|
|
|
AuthenticationType getAuthenticationTypeOrLogInFailure(const String & user_name) const;
|
2021-03-05 14:57:16 +00:00
|
|
|
|
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-03-05 14:57:16 +00:00
|
|
|
|
2022-05-17 16:22:52 +00:00
|
|
|
/// Writes a row about login failure into session log (if enabled)
|
2022-05-23 19:55:17 +00:00
|
|
|
void onAuthenticationFailure(const std::optional<String> & user_name, const Poco::Net::SocketAddress & address_, const Exception & e);
|
2022-05-17 16:22:52 +00:00
|
|
|
|
2023-07-07 10:49:50 +00:00
|
|
|
/// Returns a reference to the session's ClientInfo.
|
2021-08-01 14:12:34 +00:00
|
|
|
const ClientInfo & getClientInfo() const;
|
2021-03-05 14:57:16 +00:00
|
|
|
|
2023-07-07 10:49:50 +00:00
|
|
|
/// Modify the session's ClientInfo.
|
|
|
|
void setClientInfo(const ClientInfo & client_info);
|
|
|
|
void setClientName(const String & client_name);
|
|
|
|
void setClientInterface(ClientInfo::Interface interface);
|
|
|
|
void setClientVersion(UInt64 client_version_major, UInt64 client_version_minor, UInt64 client_version_patch, unsigned client_tcp_protocol_version);
|
|
|
|
void setClientConnectionId(uint32_t connection_id);
|
2023-10-29 16:50:39 +00:00
|
|
|
void setHttpClientInfo(ClientInfo::HTTPMethod http_method, const String & http_user_agent, const String & http_referer);
|
2023-07-07 10:49:50 +00:00
|
|
|
void setForwardedFor(const String & forwarded_for);
|
|
|
|
void setQuotaClientKey(const String & quota_key);
|
|
|
|
void setConnectionClientVersion(UInt64 client_version_major, UInt64 client_version_minor, UInt64 client_version_patch, unsigned client_tcp_protocol_version);
|
|
|
|
|
|
|
|
const OpenTelemetry::TracingContext & getClientTraceContext() const;
|
|
|
|
OpenTelemetry::TracingContext & getClientTraceContext();
|
|
|
|
|
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();
|
2021-03-05 14:57:16 +00:00
|
|
|
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-03-05 14:57:16 +00:00
|
|
|
|
2022-08-24 07:24:21 +00:00
|
|
|
ContextPtr sessionOrGlobalContext() const { return session_context ? session_context : global_context; }
|
2022-07-07 09:44:19 +00:00
|
|
|
|
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;
|
2021-03-05 14:57:16 +00:00
|
|
|
|
2021-10-10 14:32:01 +00:00
|
|
|
/// Releases the currently used session ID so it becomes available for reuse by another session.
|
|
|
|
void releaseSessionID();
|
|
|
|
|
2023-03-02 01:59:27 +00:00
|
|
|
/// Closes and removes session
|
|
|
|
void closeSession(const String & session_id);
|
|
|
|
|
2021-08-01 14:12:34 +00:00
|
|
|
private:
|
2021-03-05 14:57:16 +00:00
|
|
|
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;
|
2023-08-11 03:42:39 +00:00
|
|
|
void recordLoginSucess(ContextPtr login_context) const;
|
|
|
|
|
2021-03-05 14:57:16 +00:00
|
|
|
|
2021-03-05 14:57:16 +00:00
|
|
|
mutable bool notified_session_log_about_login = false;
|
2021-10-30 14:51:58 +00:00
|
|
|
const UUID auth_id;
|
2021-08-01 14:12:34 +00:00
|
|
|
const ContextPtr global_context;
|
2021-03-05 14:57:16 +00:00
|
|
|
|
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-03-05 14:57:16 +00:00
|
|
|
|
2021-08-01 14:12:34 +00:00
|
|
|
mutable UserPtr user;
|
|
|
|
std::optional<UUID> user_id;
|
2021-03-05 14:57:16 +00:00
|
|
|
|
|
|
|
ContextMutablePtr session_context;
|
2021-08-01 14:12:34 +00:00
|
|
|
mutable bool query_context_created = false;
|
|
|
|
|
2021-03-05 14:57:16 +00:00
|
|
|
std::shared_ptr<NamedSessionData> named_session;
|
2021-08-01 14:12:34 +00:00
|
|
|
bool named_session_created = false;
|
2021-10-19 12:54:28 +00:00
|
|
|
|
2023-07-30 22:09:03 +00:00
|
|
|
SessionTracker::SessionTrackerHandle session_tracker_handle;
|
|
|
|
|
2023-10-20 17:24:19 +00:00
|
|
|
/// Settings received from authentication server during authentication process
|
|
|
|
/// to set when creating a session context
|
|
|
|
SettingsChanges settings_from_auth_server;
|
|
|
|
|
2021-10-19 12:54:28 +00:00
|
|
|
Poco::Logger * log = nullptr;
|
2021-03-05 14:57:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|