From 4f98797ab2407a29fee47ba391d465609489b415 Mon Sep 17 00:00:00 2001 From: Harshit-kohli Date: Tue, 29 Oct 2024 11:35:58 -0700 Subject: [PATCH] keycloak changes --- .../utils/ExpiringConcurrentHashMap.java | 127 ------------------ .../ExpiringConcurrentHashMapListener.java | 7 - .../keycloak/mapper/SoamProtocolMapper.java | 28 ++-- .../tenant/mapper/TenantProtocolMapper.java | 17 ++- 4 files changed, 18 insertions(+), 161 deletions(-) delete mode 100644 docker/keycloak/extensions-24/services/src/main/java/com/github/bcgov/keycloak/common/utils/ExpiringConcurrentHashMap.java delete mode 100644 docker/keycloak/extensions-24/services/src/main/java/com/github/bcgov/keycloak/common/utils/ExpiringConcurrentHashMapListener.java diff --git a/docker/keycloak/extensions-24/services/src/main/java/com/github/bcgov/keycloak/common/utils/ExpiringConcurrentHashMap.java b/docker/keycloak/extensions-24/services/src/main/java/com/github/bcgov/keycloak/common/utils/ExpiringConcurrentHashMap.java deleted file mode 100644 index ca33c50..0000000 --- a/docker/keycloak/extensions-24/services/src/main/java/com/github/bcgov/keycloak/common/utils/ExpiringConcurrentHashMap.java +++ /dev/null @@ -1,127 +0,0 @@ -package com.github.bcgov.keycloak.common.utils; - -import com.github.bcgov.keycloak.mapper.SoamProtocolMapper; -import org.jboss.logging.Logger; - -import java.util.Date; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.TimeUnit; - -/** - * An expiring concurrent hash map solution which stores the keys and values only for a specific amount of time, and then expires after that - * time. - * - */ -public class ExpiringConcurrentHashMap extends ConcurrentHashMap { - - private static final long serialVersionUID = 1L; - - private static Logger logger = Logger.getLogger(SoamProtocolMapper.class); - private Map timeMap = new ConcurrentHashMap(); - private ExpiringConcurrentHashMapListener listener; - private long expiryInMillis; - private boolean mapAlive = true; - - public ExpiringConcurrentHashMap() { - this.expiryInMillis = 10000; - initialize(); - } - - public ExpiringConcurrentHashMap(ExpiringConcurrentHashMapListener listener) { - this.listener = listener; - this.expiryInMillis = 10000; - initialize(); - } - - public ExpiringConcurrentHashMap(long expiryInMillis) { - this.expiryInMillis = expiryInMillis; - initialize(); - } - - public ExpiringConcurrentHashMap(long expiryInMillis, ExpiringConcurrentHashMapListener listener) { - this.expiryInMillis = expiryInMillis; - this.listener = listener; - initialize(); - } - - void initialize() { - new CleanerThread().start(); - } - - public void registerRemovalListener(ExpiringConcurrentHashMapListener listener) { - this.listener = listener; - } - - @Override - public V put(K key, V value) { - if (!mapAlive) { - throw new IllegalStateException("ExpiringConcurrentHashMap is no longer alive.. Try creating a new one."); - } - Date date = new Date(); - timeMap.put(key, date.getTime()); - V returnVal = super.put(key, value); - if (listener != null) { - listener.notifyOnAdd(key, value); - } - return returnVal; - } - - @Override - public void putAll(Map m) { - if (!mapAlive) { - throw new IllegalStateException("ExpiringConcurrentHashMap Hashmap is no longer alive.. Try creating a new one."); - } - for (K key : m.keySet()) { - put(key, m.get(key)); - } - } - - @Override - public V putIfAbsent(K key, V value) { - if (!mapAlive) { - throw new IllegalStateException("ExpiringConcurrentHashMap Hashmap is no longer alive.. Try creating a new one."); - } - if (!containsKey(key)) { - return put(key, value); - } else { - return get(key); - } - } - - public void quitMap() { - mapAlive = false; - } - - public boolean isAlive() { - return mapAlive; - } - - class CleanerThread extends Thread { - - @Override - public void run() { - while (mapAlive) { - cleanMap(); - try { - TimeUnit.MILLISECONDS.sleep(expiryInMillis / 2); - } catch (InterruptedException e) { - logger.error("Error occurred running thread sleep: " + e.getMessage()); - } - } - } - - private void cleanMap() { - long currentTime = new Date().getTime(); - for (K key : timeMap.keySet()) { - if (currentTime > (timeMap.get(key) + expiryInMillis)) { - V value = remove(key); - timeMap.remove(key); - if (listener != null) { - listener.notifyOnRemoval(key, value); - } - } - } - } - } -} diff --git a/docker/keycloak/extensions-24/services/src/main/java/com/github/bcgov/keycloak/common/utils/ExpiringConcurrentHashMapListener.java b/docker/keycloak/extensions-24/services/src/main/java/com/github/bcgov/keycloak/common/utils/ExpiringConcurrentHashMapListener.java deleted file mode 100644 index 863ff0c..0000000 --- a/docker/keycloak/extensions-24/services/src/main/java/com/github/bcgov/keycloak/common/utils/ExpiringConcurrentHashMapListener.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.github.bcgov.keycloak.common.utils; - -public interface ExpiringConcurrentHashMapListener { - public void notifyOnAdd(K key, V value); - - public void notifyOnRemoval(K key, V value); -} diff --git a/docker/keycloak/extensions-24/services/src/main/java/com/github/bcgov/keycloak/mapper/SoamProtocolMapper.java b/docker/keycloak/extensions-24/services/src/main/java/com/github/bcgov/keycloak/mapper/SoamProtocolMapper.java index 2eb06a3..6a4b433 100644 --- a/docker/keycloak/extensions-24/services/src/main/java/com/github/bcgov/keycloak/mapper/SoamProtocolMapper.java +++ b/docker/keycloak/extensions-24/services/src/main/java/com/github/bcgov/keycloak/mapper/SoamProtocolMapper.java @@ -1,13 +1,13 @@ package com.github.bcgov.keycloak.mapper; -import com.github.bcgov.keycloak.common.utils.ExpiringConcurrentHashMap; -import com.github.bcgov.keycloak.common.utils.ExpiringConcurrentHashMapListener; import com.github.bcgov.keycloak.exception.SoamRuntimeException; import com.github.bcgov.keycloak.model.SoamLoginEntity; import com.github.bcgov.keycloak.model.SoamServicesCard; import com.github.bcgov.keycloak.model.SoamStudent; import com.github.bcgov.keycloak.rest.SoamRestUtils; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; import org.jboss.logging.Logger; import org.keycloak.models.ProtocolMapperModel; import org.keycloak.models.UserSessionModel; @@ -20,6 +20,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.TimeUnit; /** * SOAM Protocol Mapper Will be used to set Education specific claims for our @@ -38,22 +39,9 @@ public class SoamProtocolMapper extends AbstractOIDCProtocolMapper // OIDCAttributeMapperHelper.addTokenClaimNameConfig(configProperties); OIDCAttributeMapperHelper.addIncludeInTokensConfig(configProperties, SoamProtocolMapper.class); } - - //Create hashmap with 30 second expiry. - private ExpiringConcurrentHashMap loginDetailCache = new ExpiringConcurrentHashMap<>(30000, new ExpiringConcurrentHashMapListener() { - - @Override - public void notifyOnAdd(String key, SoamLoginEntity value) { - logger.debug("Adding SoamLoginEntity to SOAM cache, key: " + key); - } - - @Override - public void notifyOnRemoval(String key, SoamLoginEntity value) { - logger.debug("Removing SoamLoginEntity from SOAM cache, key: " + key); - logger.debug("Current cache size on this node: " + loginDetailCache.size()); - } - }); - + private Cache loginDetailCache = CacheBuilder.newBuilder() + .expireAfterWrite(30, TimeUnit.SECONDS) + .build(); public static final String PROVIDER_ID = "oidc-soam-mapper"; public List getConfigProperties() { @@ -77,8 +65,8 @@ public String getHelpText() { } private SoamLoginEntity fetchSoamLoginEntity(String type, String userGUID) { - if (loginDetailCache.containsKey(userGUID)) { - return loginDetailCache.get(userGUID); + if (null != loginDetailCache.getIfPresent(userGUID)) { + return loginDetailCache.getIfPresent(userGUID); } logger.debug("SOAM Fetching " + type + " Claims for UserGUID: " + userGUID); SoamLoginEntity soamLoginEntity = soamRestUtils.getSoamLoginEntity(type, userGUID); diff --git a/docker/keycloak/extensions-24/services/src/main/java/com/github/bcgov/keycloak/tenant/mapper/TenantProtocolMapper.java b/docker/keycloak/extensions-24/services/src/main/java/com/github/bcgov/keycloak/tenant/mapper/TenantProtocolMapper.java index 18b49ce..7879012 100644 --- a/docker/keycloak/extensions-24/services/src/main/java/com/github/bcgov/keycloak/tenant/mapper/TenantProtocolMapper.java +++ b/docker/keycloak/extensions-24/services/src/main/java/com/github/bcgov/keycloak/tenant/mapper/TenantProtocolMapper.java @@ -1,10 +1,10 @@ package com.github.bcgov.keycloak.tenant.mapper; -import com.github.bcgov.keycloak.common.utils.ExpiringConcurrentHashMap; -import com.github.bcgov.keycloak.common.utils.ExpiringConcurrentHashMapListener; import com.github.bcgov.keycloak.tenant.model.TenantAccess; import com.github.bcgov.keycloak.tenant.rest.TenantRestUtils; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; import org.jboss.logging.Logger; import org.keycloak.models.ClientSessionContext; import org.keycloak.models.KeycloakSession; @@ -19,6 +19,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.TimeUnit; /** * Tenant Protocol Mapper Will be used to set Tenant valid attribute @@ -38,7 +39,7 @@ public class TenantProtocolMapper extends AbstractOIDCProtocolMapper } //Create hashmap with 30 second expiry. - private ExpiringConcurrentHashMap loginDetailCache = new ExpiringConcurrentHashMap<>(30000, new ExpiringConcurrentHashMapListener() { +/* private ExpiringConcurrentHashMap loginDetailCache = new ExpiringConcurrentHashMap<>(30000, new ExpiringConcurrentHashMapListener() { @Override public void notifyOnAdd(String key, TenantAccess value) { @@ -50,8 +51,10 @@ public void notifyOnRemoval(String key, TenantAccess value) { logger.debug("Removing TenantAccessEntity from Tenant cache, key: " + key); logger.debug("Current cache size on this node: " + loginDetailCache.size()); } - }); - + });*/ + private Cache loginDetailCache = CacheBuilder.newBuilder() + .expireAfterWrite(30, TimeUnit.SECONDS) + .build(); public static final String PROVIDER_ID = "oidc-tenant-mapper"; public List getConfigProperties() { @@ -75,8 +78,8 @@ public String getHelpText() { } private TenantAccess fetchTenantAccessEntity(String clientID, String tenantID) { - if (loginDetailCache.containsKey(tenantID)) { - return loginDetailCache.get(tenantID); + if (null != loginDetailCache.getIfPresent(tenantID)) { + return loginDetailCache.getIfPresent(tenantID); } logger.debug("Tenant Access Fetching by Tenant ID: " + tenantID + " and Client ID: " + clientID); TenantAccess tenantAccess = tenantRestUtils.checkForValidTenant(clientID, tenantID);