2020-07-23 17:55:24 +00:00
|
|
|
#include <Access/LDAPAccessStorage.h>
|
2021-11-02 11:06:20 +00:00
|
|
|
#include <Access/AccessControl.h>
|
2021-01-06 03:40:47 +00:00
|
|
|
#include <Access/ExternalAuthenticators.h>
|
2020-07-23 17:55:24 +00:00
|
|
|
#include <Access/User.h>
|
2020-08-20 07:39:27 +00:00
|
|
|
#include <Access/Role.h>
|
2021-03-11 20:41:10 +00:00
|
|
|
#include <Access/Credentials.h>
|
2020-11-19 22:02:18 +00:00
|
|
|
#include <Access/LDAPClient.h>
|
2020-08-15 12:17:07 +00:00
|
|
|
#include <Common/Exception.h>
|
2022-04-27 15:05:45 +00:00
|
|
|
#include <Common/logger_useful.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/scope_guard.h>
|
2020-07-23 17:55:24 +00:00
|
|
|
#include <Poco/Util/AbstractConfiguration.h>
|
2020-09-20 22:51:38 +00:00
|
|
|
#include <Poco/JSON/JSON.h>
|
|
|
|
#include <Poco/JSON/Object.h>
|
|
|
|
#include <Poco/JSON/Stringifier.h>
|
2020-11-19 22:02:18 +00:00
|
|
|
#include <boost/container_hash/hash.hpp>
|
2020-10-08 20:57:23 +00:00
|
|
|
#include <boost/range/algorithm/copy.hpp>
|
|
|
|
#include <iterator>
|
2020-09-20 22:51:38 +00:00
|
|
|
#include <sstream>
|
2020-11-21 20:44:54 +00:00
|
|
|
#include <unordered_map>
|
2020-07-23 17:55:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int BAD_ARGUMENTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-16 18:43:55 +00:00
|
|
|
LDAPAccessStorage::LDAPAccessStorage(const String & storage_name_, AccessControl & access_control_, const Poco::Util::AbstractConfiguration & config, const String & prefix)
|
2022-06-19 11:16:36 +00:00
|
|
|
: IAccessStorage(storage_name_), access_control(access_control_), memory_storage(storage_name_, access_control.getChangesNotifier(), false)
|
2020-07-23 17:55:24 +00:00
|
|
|
{
|
2022-05-16 18:43:55 +00:00
|
|
|
setConfiguration(config, prefix);
|
2020-07-23 17:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-22 16:47:52 +00:00
|
|
|
String LDAPAccessStorage::getLDAPServerName() const
|
|
|
|
{
|
2023-10-25 09:26:16 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2021-03-11 20:41:10 +00:00
|
|
|
return ldap_server_name;
|
2020-10-22 16:47:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-16 18:43:55 +00:00
|
|
|
void LDAPAccessStorage::setConfiguration(const Poco::Util::AbstractConfiguration & config, const String & prefix)
|
2020-07-23 17:55:24 +00:00
|
|
|
{
|
2023-10-25 09:26:16 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2020-10-03 13:31:02 +00:00
|
|
|
|
2020-08-20 07:39:27 +00:00
|
|
|
// TODO: switch to passing config as a ConfigurationView and remove this extra prefix once a version of Poco with proper implementation is available.
|
2020-08-18 10:54:02 +00:00
|
|
|
const String prefix_str = (prefix.empty() ? "" : prefix + ".");
|
|
|
|
|
|
|
|
const bool has_server = config.has(prefix_str + "server");
|
2020-08-20 07:39:27 +00:00
|
|
|
const bool has_roles = config.has(prefix_str + "roles");
|
2020-11-19 22:02:18 +00:00
|
|
|
const bool has_role_mapping = config.has(prefix_str + "role_mapping");
|
2020-07-23 17:55:24 +00:00
|
|
|
|
|
|
|
if (!has_server)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Missing 'server' field for LDAP user directory");
|
2020-07-23 17:55:24 +00:00
|
|
|
|
2021-03-11 20:41:10 +00:00
|
|
|
const auto ldap_server_name_cfg = config.getString(prefix_str + "server");
|
|
|
|
if (ldap_server_name_cfg.empty())
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Empty 'server' field for LDAP user directory");
|
2020-07-23 17:55:24 +00:00
|
|
|
|
2020-11-19 22:02:18 +00:00
|
|
|
std::set<String> common_roles_cfg;
|
2020-08-20 07:39:27 +00:00
|
|
|
if (has_roles)
|
|
|
|
{
|
|
|
|
Poco::Util::AbstractConfiguration::Keys role_names;
|
|
|
|
config.keys(prefix_str + "roles", role_names);
|
2020-08-15 12:17:07 +00:00
|
|
|
|
2020-08-20 07:39:27 +00:00
|
|
|
// Currently, we only extract names of roles from the section names and assign them directly and unconditionally.
|
2020-11-19 22:02:18 +00:00
|
|
|
common_roles_cfg.insert(role_names.begin(), role_names.end());
|
|
|
|
}
|
|
|
|
|
2021-03-28 22:23:20 +00:00
|
|
|
LDAPClient::RoleSearchParamsList role_search_params_cfg;
|
2020-11-19 22:02:18 +00:00
|
|
|
if (has_role_mapping)
|
|
|
|
{
|
|
|
|
Poco::Util::AbstractConfiguration::Keys all_keys;
|
|
|
|
config.keys(prefix, all_keys);
|
|
|
|
for (const auto & key : all_keys)
|
|
|
|
{
|
2024-05-09 02:20:54 +00:00
|
|
|
if (key == "role_mapping" || key.starts_with("role_mapping["))
|
2021-03-28 22:23:20 +00:00
|
|
|
parseLDAPRoleSearchParams(role_search_params_cfg.emplace_back(), config, prefix_str + key);
|
2020-11-19 22:02:18 +00:00
|
|
|
}
|
2020-08-20 07:39:27 +00:00
|
|
|
}
|
2020-07-23 17:55:24 +00:00
|
|
|
|
2021-03-11 20:41:10 +00:00
|
|
|
ldap_server_name = ldap_server_name_cfg;
|
2020-11-19 22:02:18 +00:00
|
|
|
role_search_params.swap(role_search_params_cfg);
|
|
|
|
common_role_names.swap(common_roles_cfg);
|
|
|
|
|
2020-12-27 20:54:24 +00:00
|
|
|
external_role_hashes.clear();
|
2020-11-19 22:02:18 +00:00
|
|
|
users_per_roles.clear();
|
2020-12-27 20:54:24 +00:00
|
|
|
roles_per_users.clear();
|
2020-11-19 22:02:18 +00:00
|
|
|
granted_role_names.clear();
|
|
|
|
granted_role_ids.clear();
|
|
|
|
|
2022-05-16 18:43:55 +00:00
|
|
|
role_change_subscription = access_control.subscribeForChanges<Role>(
|
2020-08-26 20:34:33 +00:00
|
|
|
[this] (const UUID & id, const AccessEntityPtr & entity)
|
|
|
|
{
|
2024-05-09 02:20:54 +00:00
|
|
|
this->processRoleChange(id, entity);
|
2020-08-26 20:34:33 +00:00
|
|
|
}
|
|
|
|
);
|
2020-07-23 17:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-26 20:34:33 +00:00
|
|
|
void LDAPAccessStorage::processRoleChange(const UUID & id, const AccessEntityPtr & entity)
|
|
|
|
{
|
2023-10-25 09:26:16 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2020-12-27 20:54:24 +00:00
|
|
|
const auto role = typeid_cast<std::shared_ptr<const Role>>(entity);
|
2020-11-19 22:02:18 +00:00
|
|
|
const auto it = granted_role_names.find(id);
|
2020-10-08 20:57:23 +00:00
|
|
|
|
2020-12-27 20:54:24 +00:00
|
|
|
if (role) // Added or renamed a role.
|
2020-08-26 20:34:33 +00:00
|
|
|
{
|
2020-11-19 22:02:18 +00:00
|
|
|
const auto & new_role_name = role->getName();
|
2020-12-27 20:54:24 +00:00
|
|
|
if (it != granted_role_names.end()) // Renamed a granted role.
|
2020-11-19 22:02:18 +00:00
|
|
|
{
|
|
|
|
const auto & old_role_name = it->second;
|
|
|
|
if (new_role_name != old_role_name)
|
|
|
|
{
|
2020-12-27 20:54:24 +00:00
|
|
|
// Revoke the old role first, then grant the new role.
|
2020-11-19 22:02:18 +00:00
|
|
|
applyRoleChangeNoLock(false /* revoke */, id, old_role_name);
|
2020-12-27 20:54:24 +00:00
|
|
|
applyRoleChangeNoLock(true /* grant */, id, new_role_name);
|
2020-11-19 22:02:18 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-27 20:54:24 +00:00
|
|
|
else // Added a role.
|
|
|
|
{
|
|
|
|
applyRoleChangeNoLock(true /* grant */, id, new_role_name);
|
|
|
|
}
|
2020-08-26 20:34:33 +00:00
|
|
|
}
|
2020-12-27 20:54:24 +00:00
|
|
|
else // Removed a role.
|
2020-11-19 22:02:18 +00:00
|
|
|
{
|
2020-12-27 20:54:24 +00:00
|
|
|
if (it != granted_role_names.end()) // Removed a granted role.
|
2020-11-19 22:02:18 +00:00
|
|
|
{
|
|
|
|
const auto & old_role_name = it->second;
|
|
|
|
applyRoleChangeNoLock(false /* revoke */, id, old_role_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LDAPAccessStorage::applyRoleChangeNoLock(bool grant, const UUID & role_id, const String & role_name)
|
|
|
|
{
|
|
|
|
std::vector<UUID> user_ids;
|
|
|
|
|
2020-12-27 20:54:24 +00:00
|
|
|
// Build a list of ids of the relevant users.
|
2022-04-18 10:18:43 +00:00
|
|
|
if (common_role_names.contains(role_name))
|
2020-08-26 20:34:33 +00:00
|
|
|
{
|
2020-11-19 22:02:18 +00:00
|
|
|
user_ids = memory_storage.findAll<User>();
|
|
|
|
}
|
2020-11-19 22:26:52 +00:00
|
|
|
else
|
|
|
|
{
|
2020-11-19 22:02:18 +00:00
|
|
|
const auto it = users_per_roles.find(role_name);
|
|
|
|
if (it != users_per_roles.end())
|
|
|
|
{
|
|
|
|
const auto & user_names = it->second;
|
|
|
|
user_ids.reserve(user_names.size());
|
2020-12-27 20:54:24 +00:00
|
|
|
|
2020-11-19 22:02:18 +00:00
|
|
|
for (const auto & user_name : user_names)
|
|
|
|
{
|
|
|
|
if (const auto user_id = memory_storage.find<User>(user_name))
|
|
|
|
user_ids.emplace_back(*user_id);
|
|
|
|
}
|
|
|
|
}
|
2020-10-08 20:57:23 +00:00
|
|
|
}
|
|
|
|
|
2020-12-27 20:54:24 +00:00
|
|
|
// Update the granted roles of the relevant users.
|
2020-11-19 22:02:18 +00:00
|
|
|
if (!user_ids.empty())
|
2020-10-08 20:57:23 +00:00
|
|
|
{
|
2020-11-19 22:02:18 +00:00
|
|
|
auto update_func = [&role_id, &grant] (const AccessEntityPtr & entity_) -> AccessEntityPtr
|
2020-08-26 20:34:33 +00:00
|
|
|
{
|
2020-10-08 20:57:23 +00:00
|
|
|
if (auto user = typeid_cast<std::shared_ptr<const User>>(entity_))
|
2020-08-26 20:34:33 +00:00
|
|
|
{
|
2020-10-08 20:57:23 +00:00
|
|
|
auto changed_user = typeid_cast<std::shared_ptr<User>>(user->clone());
|
2020-11-19 22:02:18 +00:00
|
|
|
if (grant)
|
2021-02-26 22:37:00 +00:00
|
|
|
changed_user->granted_roles.grant(role_id);
|
2020-11-19 22:02:18 +00:00
|
|
|
else
|
2021-02-26 22:37:00 +00:00
|
|
|
changed_user->granted_roles.revoke(role_id);
|
2020-10-08 20:57:23 +00:00
|
|
|
return changed_user;
|
|
|
|
}
|
|
|
|
return entity_;
|
|
|
|
};
|
2020-11-19 22:02:18 +00:00
|
|
|
|
|
|
|
memory_storage.update(user_ids, update_func);
|
2020-12-27 20:54:24 +00:00
|
|
|
}
|
2020-11-19 22:02:18 +00:00
|
|
|
|
2020-12-27 20:54:24 +00:00
|
|
|
// Actualize granted_role_* mappings.
|
|
|
|
if (grant)
|
|
|
|
{
|
|
|
|
if (!user_ids.empty())
|
2020-11-19 22:02:18 +00:00
|
|
|
{
|
|
|
|
granted_role_names.insert_or_assign(role_id, role_name);
|
|
|
|
granted_role_ids.insert_or_assign(role_name, role_id);
|
|
|
|
}
|
2020-12-27 20:54:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
granted_role_names.erase(role_id);
|
|
|
|
granted_role_ids.erase(role_name);
|
2020-10-08 20:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-11 20:41:10 +00:00
|
|
|
void LDAPAccessStorage::assignRolesNoLock(User & user, const LDAPClient::SearchResultsList & external_roles) const
|
2020-10-08 20:57:23 +00:00
|
|
|
{
|
2021-03-11 20:41:10 +00:00
|
|
|
const auto external_roles_hash = boost::hash<LDAPClient::SearchResultsList>{}(external_roles);
|
2024-05-09 02:20:54 +00:00
|
|
|
assignRolesNoLock(user, external_roles, external_roles_hash);
|
2020-12-27 20:54:24 +00:00
|
|
|
}
|
2020-11-19 22:02:18 +00:00
|
|
|
|
|
|
|
|
2024-03-14 12:16:33 +00:00
|
|
|
void LDAPAccessStorage::assignRolesNoLock(User & user, const LDAPClient::SearchResultsList & external_roles, std::size_t external_roles_hash) const
|
2020-12-27 20:54:24 +00:00
|
|
|
{
|
|
|
|
const auto & user_name = user.getName();
|
2021-02-26 22:37:00 +00:00
|
|
|
auto & granted_roles = user.granted_roles;
|
2022-02-25 19:04:48 +00:00
|
|
|
auto local_role_names = mapExternalRolesNoLock(external_roles);
|
2020-11-19 22:02:18 +00:00
|
|
|
|
2020-12-27 20:54:24 +00:00
|
|
|
auto grant_role = [this, &user_name, &granted_roles] (const String & role_name, const bool common)
|
2020-10-08 20:57:23 +00:00
|
|
|
{
|
2020-12-27 20:54:24 +00:00
|
|
|
auto it = granted_role_ids.find(role_name);
|
2020-11-19 22:02:18 +00:00
|
|
|
if (it == granted_role_ids.end())
|
|
|
|
{
|
2022-05-16 18:43:55 +00:00
|
|
|
if (const auto role_id = access_control.find<Role>(role_name))
|
2020-12-27 20:54:24 +00:00
|
|
|
{
|
|
|
|
granted_role_names.insert_or_assign(*role_id, role_name);
|
|
|
|
it = granted_role_ids.insert_or_assign(role_name, *role_id).first;
|
|
|
|
}
|
2020-11-19 22:02:18 +00:00
|
|
|
}
|
2020-12-27 20:54:24 +00:00
|
|
|
|
|
|
|
if (it != granted_role_ids.end())
|
2020-11-19 22:02:18 +00:00
|
|
|
{
|
|
|
|
const auto & role_id = it->second;
|
2021-02-26 22:37:00 +00:00
|
|
|
granted_roles.grant(role_id);
|
2020-11-19 22:02:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-12-27 20:54:24 +00:00
|
|
|
LOG_WARNING(getLogger(), "Unable to grant {} role '{}' to user '{}': role not found", (common ? "common" : "mapped"), role_name, user_name);
|
2020-11-19 22:02:18 +00:00
|
|
|
}
|
2020-12-27 20:54:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
external_role_hashes.erase(user_name);
|
2021-02-26 22:37:00 +00:00
|
|
|
granted_roles = {};
|
2020-12-27 20:54:24 +00:00
|
|
|
const auto old_role_names = std::move(roles_per_users[user_name]);
|
|
|
|
|
|
|
|
// Grant the common roles first.
|
|
|
|
for (const auto & role_name : common_role_names)
|
|
|
|
{
|
|
|
|
grant_role(role_name, true /* common */);
|
2020-11-19 22:02:18 +00:00
|
|
|
}
|
|
|
|
|
2020-12-27 20:54:24 +00:00
|
|
|
// Grant the mapped external roles and actualize users_per_roles mapping.
|
|
|
|
// local_role_names allowed to overlap with common_role_names.
|
|
|
|
for (const auto & role_name : local_role_names)
|
2020-11-19 22:02:18 +00:00
|
|
|
{
|
2020-12-27 20:54:24 +00:00
|
|
|
grant_role(role_name, false /* mapped */);
|
2020-11-19 22:02:18 +00:00
|
|
|
users_per_roles[role_name].insert(user_name);
|
|
|
|
}
|
|
|
|
|
2020-12-27 20:54:24 +00:00
|
|
|
// Cleanup users_per_roles and granted_role_* mappings.
|
|
|
|
for (const auto & old_role_name : old_role_names)
|
|
|
|
{
|
2022-04-18 10:18:43 +00:00
|
|
|
if (local_role_names.contains(old_role_name))
|
2020-12-27 20:54:24 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
const auto rit = users_per_roles.find(old_role_name);
|
|
|
|
if (rit == users_per_roles.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto & user_names = rit->second;
|
|
|
|
user_names.erase(user_name);
|
|
|
|
|
|
|
|
if (!user_names.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
users_per_roles.erase(rit);
|
|
|
|
|
2022-04-18 10:18:43 +00:00
|
|
|
if (common_role_names.contains(old_role_name))
|
2020-12-27 20:54:24 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
const auto iit = granted_role_ids.find(old_role_name);
|
|
|
|
if (iit == granted_role_ids.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const auto old_role_id = iit->second;
|
|
|
|
granted_role_names.erase(old_role_id);
|
|
|
|
granted_role_ids.erase(iit);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actualize roles_per_users mapping and external_role_hashes cache.
|
|
|
|
if (local_role_names.empty())
|
|
|
|
roles_per_users.erase(user_name);
|
|
|
|
else
|
|
|
|
roles_per_users[user_name] = std::move(local_role_names);
|
|
|
|
|
|
|
|
external_role_hashes[user_name] = external_roles_hash;
|
2020-11-19 22:02:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-11 20:41:10 +00:00
|
|
|
void LDAPAccessStorage::updateAssignedRolesNoLock(const UUID & id, const String & user_name, const LDAPClient::SearchResultsList & external_roles) const
|
2020-11-19 22:02:18 +00:00
|
|
|
{
|
2020-12-27 20:54:24 +00:00
|
|
|
// No need to include common_role_names in this hash each time, since they don't change.
|
2021-03-11 20:41:10 +00:00
|
|
|
const auto external_roles_hash = boost::hash<LDAPClient::SearchResultsList>{}(external_roles);
|
2020-11-19 22:02:18 +00:00
|
|
|
|
2020-12-27 20:54:24 +00:00
|
|
|
// Map and grant the roles from scratch only if the list of external role has changed.
|
2020-11-19 22:02:18 +00:00
|
|
|
const auto it = external_role_hashes.find(user_name);
|
2020-12-27 20:54:24 +00:00
|
|
|
if (it != external_role_hashes.end() && it->second == external_roles_hash)
|
2020-11-19 22:02:18 +00:00
|
|
|
return;
|
|
|
|
|
2020-12-27 20:54:24 +00:00
|
|
|
auto update_func = [this, &external_roles, external_roles_hash] (const AccessEntityPtr & entity_) -> AccessEntityPtr
|
2020-11-19 22:02:18 +00:00
|
|
|
{
|
|
|
|
if (auto user = typeid_cast<std::shared_ptr<const User>>(entity_))
|
|
|
|
{
|
|
|
|
auto changed_user = typeid_cast<std::shared_ptr<User>>(user->clone());
|
2020-12-27 20:54:24 +00:00
|
|
|
assignRolesNoLock(*changed_user, external_roles, external_roles_hash);
|
2020-11-19 22:02:18 +00:00
|
|
|
return changed_user;
|
|
|
|
}
|
|
|
|
return entity_;
|
|
|
|
};
|
|
|
|
|
|
|
|
memory_storage.update(id, update_func);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-11 20:41:10 +00:00
|
|
|
std::set<String> LDAPAccessStorage::mapExternalRolesNoLock(const LDAPClient::SearchResultsList & external_roles) const
|
2020-11-19 22:02:18 +00:00
|
|
|
{
|
|
|
|
std::set<String> role_names;
|
|
|
|
|
|
|
|
if (external_roles.size() != role_search_params.size())
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Unable to map external roles");
|
2020-11-19 22:02:18 +00:00
|
|
|
|
|
|
|
for (std::size_t i = 0; i < external_roles.size(); ++i)
|
|
|
|
{
|
|
|
|
const auto & external_role_set = external_roles[i];
|
2020-12-17 14:27:30 +00:00
|
|
|
const auto & prefix = role_search_params[i].prefix;
|
|
|
|
|
2020-11-19 22:02:18 +00:00
|
|
|
for (const auto & external_role : external_role_set)
|
|
|
|
{
|
2020-12-17 14:27:30 +00:00
|
|
|
if (
|
|
|
|
prefix.size() < external_role.size() &&
|
|
|
|
external_role.compare(0, prefix.size(), prefix) == 0
|
|
|
|
)
|
2020-11-19 22:02:18 +00:00
|
|
|
{
|
2020-12-17 14:48:12 +00:00
|
|
|
role_names.emplace(external_role, prefix.size());
|
2020-11-19 22:02:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return role_names;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-11 20:41:10 +00:00
|
|
|
bool LDAPAccessStorage::areLDAPCredentialsValidNoLock(const User & user, const Credentials & credentials,
|
2021-03-28 22:23:20 +00:00
|
|
|
const ExternalAuthenticators & external_authenticators, LDAPClient::SearchResultsList & role_search_results) const
|
2020-11-19 22:02:18 +00:00
|
|
|
{
|
2021-03-11 20:41:10 +00:00
|
|
|
if (!credentials.isReady())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (credentials.getUserName() != user.getName())
|
|
|
|
return false;
|
|
|
|
|
2022-01-13 11:11:23 +00:00
|
|
|
if (typeid_cast<const AlwaysAllowCredentials *>(&credentials))
|
|
|
|
return true;
|
|
|
|
|
2021-03-11 20:41:10 +00:00
|
|
|
if (const auto * basic_credentials = dynamic_cast<const BasicCredentials *>(&credentials))
|
2021-03-28 22:23:20 +00:00
|
|
|
return external_authenticators.checkLDAPCredentials(ldap_server_name, *basic_credentials, &role_search_params, &role_search_results);
|
2021-03-11 20:41:10 +00:00
|
|
|
|
|
|
|
return false;
|
2020-08-26 20:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-18 10:54:02 +00:00
|
|
|
const char * LDAPAccessStorage::getStorageType() const
|
|
|
|
{
|
|
|
|
return STORAGE_TYPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-20 22:51:38 +00:00
|
|
|
String LDAPAccessStorage::getStorageParamsJSON() const
|
2020-08-18 10:54:02 +00:00
|
|
|
{
|
2023-10-25 09:26:16 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2020-09-20 22:51:38 +00:00
|
|
|
Poco::JSON::Object params_json;
|
2020-08-18 10:54:02 +00:00
|
|
|
|
2021-03-11 20:41:10 +00:00
|
|
|
params_json.set("server", ldap_server_name);
|
2020-11-21 15:08:02 +00:00
|
|
|
|
|
|
|
Poco::JSON::Array common_role_names_json;
|
|
|
|
for (const auto & role : common_role_names)
|
|
|
|
{
|
|
|
|
common_role_names_json.add(role);
|
|
|
|
}
|
|
|
|
params_json.set("roles", common_role_names_json);
|
|
|
|
|
|
|
|
Poco::JSON::Array role_mappings_json;
|
|
|
|
for (const auto & role_mapping : role_search_params)
|
|
|
|
{
|
|
|
|
Poco::JSON::Object role_mapping_json;
|
|
|
|
|
|
|
|
role_mapping_json.set("base_dn", role_mapping.base_dn);
|
|
|
|
role_mapping_json.set("search_filter", role_mapping.search_filter);
|
|
|
|
role_mapping_json.set("attribute", role_mapping.attribute);
|
2020-12-17 14:27:30 +00:00
|
|
|
role_mapping_json.set("prefix", role_mapping.prefix);
|
2020-11-21 15:08:02 +00:00
|
|
|
|
|
|
|
String scope;
|
|
|
|
switch (role_mapping.scope)
|
|
|
|
{
|
2021-03-11 20:41:10 +00:00
|
|
|
case LDAPClient::SearchParams::Scope::BASE: scope = "base"; break;
|
|
|
|
case LDAPClient::SearchParams::Scope::ONE_LEVEL: scope = "one_level"; break;
|
|
|
|
case LDAPClient::SearchParams::Scope::SUBTREE: scope = "subtree"; break;
|
|
|
|
case LDAPClient::SearchParams::Scope::CHILDREN: scope = "children"; break;
|
2020-11-21 15:08:02 +00:00
|
|
|
}
|
|
|
|
role_mapping_json.set("scope", scope);
|
|
|
|
|
|
|
|
role_mappings_json.add(role_mapping_json);
|
|
|
|
}
|
|
|
|
params_json.set("role_mappings", role_mappings_json);
|
2020-08-18 10:54:02 +00:00
|
|
|
|
2020-11-09 19:07:38 +00:00
|
|
|
std::ostringstream oss; // STYLE_CHECK_ALLOW_STD_STRING_STREAM
|
2020-11-07 00:14:53 +00:00
|
|
|
oss.exceptions(std::ios::failbit);
|
2020-09-20 22:51:38 +00:00
|
|
|
Poco::JSON::Stringifier::stringify(params_json, oss);
|
|
|
|
|
|
|
|
return oss.str();
|
2020-08-26 18:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-18 20:54:18 +00:00
|
|
|
std::optional<UUID> LDAPAccessStorage::findImpl(AccessEntityType type, const String & name) const
|
2020-07-23 17:55:24 +00:00
|
|
|
{
|
2023-10-25 09:26:16 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2020-07-23 17:55:24 +00:00
|
|
|
return memory_storage.find(type, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-18 20:54:18 +00:00
|
|
|
std::vector<UUID> LDAPAccessStorage::findAllImpl(AccessEntityType type) const
|
2020-07-23 17:55:24 +00:00
|
|
|
{
|
2023-10-25 09:26:16 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2020-07-23 17:55:24 +00:00
|
|
|
return memory_storage.findAll(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-22 21:50:15 +00:00
|
|
|
bool LDAPAccessStorage::exists(const UUID & id) const
|
2020-07-23 17:55:24 +00:00
|
|
|
{
|
2023-10-25 09:26:16 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2020-07-23 17:55:24 +00:00
|
|
|
return memory_storage.exists(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-11 16:29:38 +00:00
|
|
|
AccessEntityPtr LDAPAccessStorage::readImpl(const UUID & id, bool throw_if_not_exists) const
|
2020-07-23 17:55:24 +00:00
|
|
|
{
|
2023-10-25 09:26:16 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2021-12-11 16:29:38 +00:00
|
|
|
return memory_storage.read(id, throw_if_not_exists);
|
2020-07-23 17:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-06-15 18:25:13 +00:00
|
|
|
std::optional<std::pair<String, AccessEntityType>> LDAPAccessStorage::readNameWithTypeImpl(const UUID & id, bool throw_if_not_exists) const
|
2020-07-23 17:55:24 +00:00
|
|
|
{
|
2023-10-25 09:26:16 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2022-06-15 18:25:13 +00:00
|
|
|
return memory_storage.readNameWithType(id, throw_if_not_exists);
|
2020-07-23 17:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-20 17:24:19 +00:00
|
|
|
std::optional<AuthResult> LDAPAccessStorage::authenticateImpl(
|
2021-12-13 08:27:23 +00:00
|
|
|
const Credentials & credentials,
|
|
|
|
const Poco::Net::IPAddress & address,
|
|
|
|
const ExternalAuthenticators & external_authenticators,
|
2022-03-14 14:32:58 +00:00
|
|
|
bool throw_if_user_not_exists,
|
|
|
|
bool /* allow_no_password */,
|
|
|
|
bool /* allow_plaintext_password */) const
|
2020-09-20 22:51:38 +00:00
|
|
|
{
|
2023-10-25 09:26:16 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2021-03-11 20:41:10 +00:00
|
|
|
auto id = memory_storage.find<User>(credentials.getUserName());
|
2022-01-13 11:11:23 +00:00
|
|
|
UserPtr user = id ? memory_storage.read<User>(*id) : nullptr;
|
2020-10-04 19:55:58 +00:00
|
|
|
|
2022-01-13 11:11:23 +00:00
|
|
|
std::shared_ptr<User> new_user;
|
|
|
|
if (!user)
|
|
|
|
{
|
|
|
|
// User does not exist, so we create one, and will add it if authentication is successful.
|
|
|
|
new_user = std::make_shared<User>();
|
|
|
|
new_user->setName(credentials.getUserName());
|
|
|
|
new_user->auth_data = AuthenticationData(AuthenticationType::LDAP);
|
|
|
|
new_user->auth_data.setLDAPServerName(ldap_server_name);
|
|
|
|
user = new_user;
|
|
|
|
}
|
2021-11-22 21:15:37 +00:00
|
|
|
|
2022-01-13 11:11:23 +00:00
|
|
|
if (!isAddressAllowed(*user, address))
|
|
|
|
throwAddressNotAllowed(address);
|
2021-03-11 20:41:10 +00:00
|
|
|
|
2022-01-13 11:11:23 +00:00
|
|
|
LDAPClient::SearchResultsList external_roles;
|
|
|
|
if (!areLDAPCredentialsValidNoLock(*user, credentials, external_authenticators, external_roles))
|
|
|
|
{
|
|
|
|
// We don't know why the authentication has just failed:
|
|
|
|
// either there is no such user in LDAP or the password is not correct.
|
|
|
|
// We treat this situation as if there is no such user because we don't want to block
|
|
|
|
// other storages following this LDAPAccessStorage from trying to authenticate on their own.
|
|
|
|
if (throw_if_user_not_exists)
|
|
|
|
throwNotFound(AccessEntityType::USER, credentials.getUserName());
|
|
|
|
else
|
|
|
|
return {};
|
|
|
|
}
|
2020-11-19 22:02:18 +00:00
|
|
|
|
2022-01-13 11:11:23 +00:00
|
|
|
if (new_user)
|
|
|
|
{
|
2022-01-14 12:16:10 +00:00
|
|
|
// TODO: if these were AlwaysAllowCredentials, then mapped external roles are not available here,
|
|
|
|
// since without a password we can't authenticate and retrieve roles from the LDAP server.
|
|
|
|
|
2022-01-13 11:11:23 +00:00
|
|
|
assignRolesNoLock(*new_user, external_roles);
|
|
|
|
id = memory_storage.insert(new_user);
|
2020-09-20 22:51:38 +00:00
|
|
|
}
|
2020-10-04 19:55:58 +00:00
|
|
|
else
|
2020-09-20 22:51:38 +00:00
|
|
|
{
|
2022-01-13 11:11:23 +00:00
|
|
|
// Just in case external_roles are changed. This will be no-op if they are not.
|
|
|
|
updateAssignedRolesNoLock(*id, user->getName(), external_roles);
|
2020-09-20 22:51:38 +00:00
|
|
|
}
|
2022-01-13 11:11:23 +00:00
|
|
|
|
2023-10-20 17:24:19 +00:00
|
|
|
if (id)
|
|
|
|
return AuthResult{ .user_id = *id };
|
|
|
|
return std::nullopt;
|
2020-10-04 19:55:58 +00:00
|
|
|
}
|
2022-06-15 18:25:13 +00:00
|
|
|
|
2020-07-23 17:55:24 +00:00
|
|
|
}
|