Skip to content

Commit 5b3093d

Browse files
committed
do not use hash to determine if roles changed (similar to ClickHouse#88412)
1 parent 229eb98 commit 5b3093d

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

src/Access/TokenAccessStorage.cpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ TokenAccessStorage::TokenAccessStorage(const String & storage_name_, AccessContr
4343
}
4444
common_role_names.swap(common_roles_cfg);
4545

46-
external_role_hashes.clear();
46+
user_external_roles.clear();
4747
users_per_roles.clear();
4848
roles_per_users.clear();
4949
granted_role_names.clear();
@@ -223,7 +223,7 @@ std::optional<std::pair<String, AccessEntityType>> TokenAccessStorage::readNameW
223223
return memory_storage.readNameWithType(id, throw_if_not_exists);
224224
}
225225

226-
void TokenAccessStorage::assignRolesNoLock(User & user, const std::set<String> & external_roles, std::size_t external_roles_hash) const
226+
void TokenAccessStorage::assignRolesNoLock(User & user, const std::set<String> & external_roles) const
227227
{
228228
const auto & user_name = user.getName();
229229
auto & granted_roles = user.granted_roles;
@@ -251,7 +251,7 @@ void TokenAccessStorage::assignRolesNoLock(User & user, const std::set<String> &
251251
}
252252
};
253253

254-
external_role_hashes.erase(user_name);
254+
user_external_roles.erase(user_name);
255255
granted_roles = {};
256256
const auto old_role_names = std::move(roles_per_users[user_name]);
257257

@@ -299,31 +299,28 @@ void TokenAccessStorage::assignRolesNoLock(User & user, const std::set<String> &
299299
granted_role_ids.erase(iit);
300300
}
301301

302-
// Actualize roles_per_users mapping and external_role_hashes cache.
302+
// Actualize roles_per_users mapping and user_external_roles cache.
303303
if (external_roles.empty())
304304
roles_per_users.erase(user_name);
305305
else
306306
roles_per_users[user_name] = external_roles;
307307

308-
external_role_hashes[user_name] = external_roles_hash;
308+
user_external_roles[user_name] = external_roles;
309309
}
310310

311311
void TokenAccessStorage::updateAssignedRolesNoLock(const UUID & id, const String & user_name, const std::set<String> & external_roles) const
312312
{
313-
// No need to include common_role_names in this hash each time, since they don't change.
314-
const auto external_roles_hash = boost::hash<std::set<String>>{}(external_roles);
315-
316313
// Map and grant the roles from scratch only if the list of external role has changed.
317-
const auto it = external_role_hashes.find(user_name);
318-
if (it != external_role_hashes.end() && it->second == external_roles_hash)
314+
const auto it = user_external_roles.find(user_name);
315+
if (it != user_external_roles.end() && it->second == external_roles)
319316
return;
320317

321-
auto update_func = [this, &external_roles, external_roles_hash] (const AccessEntityPtr & entity_, const UUID &) -> AccessEntityPtr
318+
auto update_func = [this, &external_roles] (const AccessEntityPtr & entity_, const UUID &) -> AccessEntityPtr
322319
{
323320
if (auto user = typeid_cast<std::shared_ptr<const User>>(entity_))
324321
{
325322
auto changed_user = typeid_cast<std::shared_ptr<User>>(user->clone());
326-
assignRolesNoLock(*changed_user, external_roles, external_roles_hash);
323+
assignRolesNoLock(*changed_user, external_roles);
327324
return changed_user;
328325
}
329326
return entity_;
@@ -390,7 +387,7 @@ std::optional<AuthResult> TokenAccessStorage::authenticateImpl(
390387

391388
if (new_user)
392389
{
393-
assignRolesNoLock(*new_user, external_roles, boost::hash<std::set<String>>{}(external_roles));
390+
assignRolesNoLock(*new_user, external_roles);
394391
id = memory_storage.insert(new_user);
395392
}
396393
else

src/Access/TokenAccessStorage.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class TokenAccessStorage : public IAccessStorage
5151
std::optional<re2::RE2> roles_filter = std::nullopt;
5252

5353
std::set<String> common_role_names; // role name that should be granted to all users at all times
54-
mutable std::map<String, std::size_t> external_role_hashes;
54+
mutable std::map<String, std::set<String>> user_external_roles;
5555
mutable std::map<String, std::set<String>> users_per_roles; // role name -> user names (...it should be granted to; may but don't have to exist for common roles)
5656
mutable std::map<String, std::set<String>> roles_per_users; // user name -> role names (...that should be granted to it; may but don't have to include common roles)
5757
mutable std::map<UUID, String> granted_role_names; // (currently granted) role id -> its name
@@ -65,7 +65,7 @@ class TokenAccessStorage : public IAccessStorage
6565
bool areTokenCredentialsValidNoLock(const User & user, const Credentials & credentials, const ExternalAuthenticators & external_authenticators) const;
6666

6767
void applyRoleChangeNoLock(bool grant, const UUID & role_id, const String & role_name);
68-
void assignRolesNoLock(User & user, const std::set<String> & external_roles, std::size_t external_roles_hash) const;
68+
void assignRolesNoLock(User & user, const std::set<String> & external_roles) const;
6969
void updateAssignedRolesNoLock(const UUID & id, const String & user_name, const std::set<String> & external_roles) const;
7070

7171
protected:

0 commit comments

Comments
 (0)