Skip to content

Commit ba495d1

Browse files
authored
Remote Infinispan should return count per client only for the current realm (keycloak#44948)
Closes keycloak#44577 Signed-off-by: Pedro Ruivo <[email protected]> Co-authored-by: Pedro Ruivo <[email protected]>
1 parent ab546c9 commit ba495d1

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/query/ClientSessionQueries.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private ClientSessionQueries() {
3535
public static final String CLIENT_SESSION = Marshalling.protoEntity(RemoteAuthenticatedClientSessionEntity.class);
3636

3737
private static final String FETCH_USER_SESSION_ID = "SELECT e.userSessionId FROM %s as e WHERE e.realmId = :realmId && e.clientId = :clientId ORDER BY e.userSessionId".formatted(CLIENT_SESSION);
38-
private static final String PER_CLIENT_COUNT = "SELECT e.clientId, count(e.clientId) FROM %s as e GROUP BY e.clientId ORDER BY e.clientId".formatted(CLIENT_SESSION);
38+
private static final String PER_CLIENT_COUNT = "SELECT e.clientId, count(e.clientId) FROM %s as e WHERE e.realmId = :realmId GROUP BY e.clientId ORDER BY e.clientId".formatted(CLIENT_SESSION);
3939
private static final String CLIENT_SESSION_COUNT = "SELECT count(e) FROM %s as e WHERE e.realmId = :realmId && e.clientId = :clientId".formatted(CLIENT_SESSION);
4040
private static final String FROM_USER_SESSION = "FROM %s as e WHERE e.userSessionId = :userSessionId ORDER BY e.clientId".formatted(CLIENT_SESSION);
4141
private static final String IDS_FROM_USER_SESSION = "SELECT e.clientId FROM %s as e WHERE e.userSessionId = :userSessionId ORDER BY e.clientId".formatted(CLIENT_SESSION);
@@ -52,8 +52,9 @@ public static Query<Object[]> fetchUserSessionIdForClientId(RemoteCache<ClientSe
5252
/**
5353
* Returns a projection with the client ID and its number of active client sessions.
5454
*/
55-
public static Query<Object[]> activeClientCount(RemoteCache<ClientSessionKey, RemoteAuthenticatedClientSessionEntity> cache) {
56-
return cache.query(PER_CLIENT_COUNT);
55+
public static Query<Object[]> activeClientCount(RemoteCache<ClientSessionKey, RemoteAuthenticatedClientSessionEntity> cache, String realmId) {
56+
return cache.<Object[]>query(PER_CLIENT_COUNT)
57+
.setParameter("realmId", realmId);
5758
}
5859

5960
/**

model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/remote/RemoteUserSessionProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public long getActiveUserSessions(RealmModel realm, ClientModel client) {
168168

169169
@Override
170170
public Map<String, Long> getActiveClientSessionStats(RealmModel realm, boolean offline) {
171-
var query = ClientSessionQueries.activeClientCount(getClientSessionTransaction(offline).getCache());
171+
var query = ClientSessionQueries.activeClientCount(getClientSessionTransaction(offline).getCache(), realm.getId());
172172
return QueryHelper.streamAll(query, batchSize, QueryHelper.PROJECTION_TO_STRING_LONG_ENTRY)
173173
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
174174
}

testsuite/model/src/test/java/org/keycloak/testsuite/model/infinispan/InfinispanIckleQueryTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,9 +459,9 @@ public void testClientSessionQueries() {
459459
expectedResults = CLIENTS.stream().map(s -> new ClientSessionKey(userSession, s)).map(Objects::toString).collect(Collectors.toSet());
460460
assertQuery(query2, objects -> objects.createCacheKey().toString(), expectedResults);
461461

462-
// each client has user-session * realms active client sessions
463-
query = ClientSessionQueries.activeClientCount(cache);
464-
expectedResults = CLIENTS.stream().map(s -> String.format("%s-%s", s, USER_SESSIONS.size() * REALMS.size())).collect(Collectors.toSet());
462+
// each client has user-session active client sessions
463+
query = ClientSessionQueries.activeClientCount(cache, realm);
464+
expectedResults = CLIENTS.stream().map(s -> String.format("%s-%s", s, USER_SESSIONS.size())).collect(Collectors.toSet());
465465
assertQuery(query, objects -> String.format("%s-%s", objects[0], objects[1]), expectedResults);
466466
}
467467

0 commit comments

Comments
 (0)