From 4e30e996a933cfcc987962ffee7824031d6432a6 Mon Sep 17 00:00:00 2001 From: thanicz Date: Fri, 4 Oct 2024 12:48:21 +0200 Subject: [PATCH 1/2] KNOX-3067: Upgrade ehcache to 3.3.1 --- gateway-provider-security-shiro/pom.xml | 8 +- .../apache/knox/gateway/ShiroMessages.java | 18 ++- .../gateway/shirorealm/KnoxCacheManager.java | 133 +++++++++++++++++- .../shirorealm/KnoxCacheManagerTest.java | 48 +++++++ pom.xml | 28 ++-- 5 files changed, 218 insertions(+), 17 deletions(-) create mode 100644 gateway-provider-security-shiro/src/test/java/org/apache/knox/gateway/shirorealm/KnoxCacheManagerTest.java diff --git a/gateway-provider-security-shiro/pom.xml b/gateway-provider-security-shiro/pom.xml index fb380c2e61..0bf0daf80f 100644 --- a/gateway-provider-security-shiro/pom.xml +++ b/gateway-provider-security-shiro/pom.xml @@ -73,12 +73,12 @@ - org.apache.shiro - shiro-ehcache + org.ehcache + ehcache - net.sf.ehcache - ehcache-core + org.ehcache.integrations.shiro + shiro-ehcache3 diff --git a/gateway-provider-security-shiro/src/main/java/org/apache/knox/gateway/ShiroMessages.java b/gateway-provider-security-shiro/src/main/java/org/apache/knox/gateway/ShiroMessages.java index 5a51664bf3..6b1a1e4116 100644 --- a/gateway-provider-security-shiro/src/main/java/org/apache/knox/gateway/ShiroMessages.java +++ b/gateway-provider-security-shiro/src/main/java/org/apache/knox/gateway/ShiroMessages.java @@ -20,12 +20,28 @@ import org.apache.knox.gateway.i18n.messages.Message; import org.apache.knox.gateway.i18n.messages.MessageLevel; import org.apache.knox.gateway.i18n.messages.Messages; +import org.apache.knox.gateway.i18n.messages.StackTrace; @Messages(logger="org.apache.knox.gateway") public interface ShiroMessages { - @Message(level = MessageLevel.INFO, text = "Request {0} matches unauthenticated path configured in topology, letting it through" ) + @Message( level = MessageLevel.INFO, text = "Request {0} matches unauthenticated path configured in topology, letting it through" ) void unauthenticatedPathBypass(String uri); @Message( level = MessageLevel.WARN, text = "Invalid URL pattern for rule: {0}" ) void invalidURLPattern(String rule); + + @Message( level = MessageLevel.TRACE, text = "Acquiring EhcacheShiro instance named {0}" ) + void acquireEhcacheShiro(String name); + + @Message( level = MessageLevel.INFO, text = "Cache with name {0} does not yet exist. Creating now." ) + void noCacheFound(String name); + + @Message( level = MessageLevel.INFO, text = "Added EhcacheShiro named {0}" ) + void ehcacheShiroAdded(String name); + + @Message( level = MessageLevel.INFO, text = "Using existing EhcacheShiro named {0}" ) + void usingExistingEhcacheShiro(String name); + + @Message( level = MessageLevel.WARN, text = "The Shiro managed CacheManager threw an Exception while closing: {0}" ) + void errorClosingManagedCacheManager(@StackTrace(level=MessageLevel.WARN) Exception e); } diff --git a/gateway-provider-security-shiro/src/main/java/org/apache/knox/gateway/shirorealm/KnoxCacheManager.java b/gateway-provider-security-shiro/src/main/java/org/apache/knox/gateway/shirorealm/KnoxCacheManager.java index 7b1ad97725..b5af9fa9d9 100644 --- a/gateway-provider-security-shiro/src/main/java/org/apache/knox/gateway/shirorealm/KnoxCacheManager.java +++ b/gateway-provider-security-shiro/src/main/java/org/apache/knox/gateway/shirorealm/KnoxCacheManager.java @@ -17,11 +17,136 @@ */ package org.apache.knox.gateway.shirorealm; -import org.apache.shiro.cache.ehcache.EhCacheManager; +import org.apache.knox.gateway.ShiroMessages; +import org.apache.knox.gateway.i18n.messages.MessagesFactory; +import org.apache.shiro.ShiroException; +import org.apache.shiro.cache.Cache; +import org.apache.shiro.cache.CacheException; +import org.apache.shiro.io.ResourceUtils; +import org.apache.shiro.util.Destroyable; +import org.apache.shiro.util.Initializable; +import org.ehcache.CacheManager; +import org.ehcache.config.CacheConfiguration; +import org.ehcache.config.builders.CacheConfigurationBuilder; +import org.ehcache.config.builders.CacheManagerBuilder; +import org.ehcache.integrations.shiro.EhcacheShiro; +import org.ehcache.xml.XmlConfiguration; -public class KnoxCacheManager extends EhCacheManager { +import java.net.MalformedURLException; +import java.net.URL; - public KnoxCacheManager() { - setCacheManager(net.sf.ehcache.CacheManager.create()); +public class KnoxCacheManager implements org.apache.shiro.cache.CacheManager, Initializable, Destroyable { + private static final ShiroMessages LOG = MessagesFactory.get(ShiroMessages.class); + + private org.ehcache.CacheManager manager; + private String cacheManagerConfigFile = "classpath:org/ehcache/integrations/shiro/ehcache.xml"; + private boolean cacheManagerImplicitlyCreated; + private XmlConfiguration cacheConfiguration; + + public CacheManager getCacheManager() { + return manager; + } + + public void setCacheManager(CacheManager cacheManager) { + try { + destroy(); + } catch (Exception e) { + LOG.errorClosingManagedCacheManager(e); + } + manager = cacheManager; + cacheManagerImplicitlyCreated = false; + } + + public String getCacheManagerConfigFile() { + return cacheManagerConfigFile; + } + + public void setCacheManagerConfigFile(String cacheManagerConfigFile) { + this.cacheManagerConfigFile = cacheManagerConfigFile; + } + + @Override + public Cache getCache(String name) throws CacheException { + LOG.acquireEhcacheShiro(name); + try { + org.ehcache.Cache cache = ensureCacheManager().getCache(name, Object.class, Object.class); + + if (cache == null) { + LOG.noCacheFound(name); + cache = createCache(name); + LOG.ehcacheShiroAdded(name); + } else { + LOG.usingExistingEhcacheShiro(name); + } + return new EhcacheShiro<>(cache); + } catch (MalformedURLException | ClassNotFoundException | InstantiationException | IllegalAccessException e) { + throw new CacheException(e); + } + } + + private synchronized org.ehcache.Cache createCache(String name) + throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException { + org.ehcache.Cache cache = ensureCacheManager().getCache(name, Object.class, Object.class); + if (cache == null) { + XmlConfiguration xmlConfiguration = getConfiguration(); + CacheConfigurationBuilder configurationBuilder = xmlConfiguration.newCacheConfigurationBuilderFromTemplate( + "defaultCacheConfiguration", Object.class, Object.class); + CacheConfiguration cacheConfiguration = configurationBuilder.build(); + cache = ensureCacheManager().createCache(name, cacheConfiguration); + } + return cache; + } + + private org.ehcache.CacheManager ensureCacheManager() throws MalformedURLException { + if (manager == null) { + manager = CacheManagerBuilder.newCacheManager(getConfiguration()); + manager.init(); + + cacheManagerImplicitlyCreated = true; + } + + return manager; + } + + private URL getResource() { + String URL = ResourceUtils.hasResourcePrefix(this.cacheManagerConfigFile) ? + stripPrefix(this.cacheManagerConfigFile) : this.cacheManagerConfigFile; + + ClassLoader loader = Thread.currentThread().getContextClassLoader(); + if (loader == null) { + loader = this.getClass().getClassLoader(); + } + + return loader.getResource(URL); + } + + + private static String stripPrefix(String resourcePath) { + return resourcePath.substring(resourcePath.indexOf(':') + 1); + } + + private XmlConfiguration getConfiguration() throws MalformedURLException { + if (cacheConfiguration == null) { + cacheConfiguration = new XmlConfiguration(getResource()); + } + + return cacheConfiguration; + } + + @Override + public void destroy() { + if (cacheManagerImplicitlyCreated && manager != null) { + manager.close(); + manager = null; + } + } + + @Override + public void init() throws ShiroException { + try { + ensureCacheManager(); + } catch (MalformedURLException e) { + throw new ShiroException(e); + } } } diff --git a/gateway-provider-security-shiro/src/test/java/org/apache/knox/gateway/shirorealm/KnoxCacheManagerTest.java b/gateway-provider-security-shiro/src/test/java/org/apache/knox/gateway/shirorealm/KnoxCacheManagerTest.java new file mode 100644 index 0000000000..39d0932350 --- /dev/null +++ b/gateway-provider-security-shiro/src/test/java/org/apache/knox/gateway/shirorealm/KnoxCacheManagerTest.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.knox.gateway.shirorealm; + +import org.apache.shiro.cache.Cache; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class KnoxCacheManagerTest { + + @Test + public void testConfigFileManipulation() { + KnoxCacheManager cacheManager = new KnoxCacheManager(); + cacheManager.setCacheManagerConfigFile("config.xml"); + + assertEquals("config.xml", cacheManager.getCacheManagerConfigFile()); + } + + @Test + public void testGetCache() throws Exception { + KnoxCacheManager cacheManager = new KnoxCacheManager(); + Cache cache = cacheManager.getCache("cache"); + + cache.put("testK", "testV"); + assertEquals("testV", cache.get("testK")); + + cacheManager.destroy(); + assertNull(cacheManager.getCacheManager()); + } + +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index d1d1075f63..2ed83d43cf 100644 --- a/pom.xml +++ b/pom.xml @@ -198,7 +198,7 @@ 2.1.3 4.3 2.7.8 - 2.6.11 + 3.3.1 3.0.0 1.2.18 1.11.0 @@ -267,6 +267,7 @@ 0.11.4 4.3.3 1.10.0 + 1.0.0 1.2.6 2.0.0 1.7.30 @@ -2040,11 +2041,6 @@ shiro-core ${shiro.version} - - org.apache.shiro - shiro-ehcache - ${shiro.version} - org.apache.shiro shiro-web @@ -2052,11 +2048,27 @@ - net.sf.ehcache - ehcache-core + org.ehcache + ehcache ${ehcache.version} + + org.ehcache.integrations.shiro + shiro-ehcache3 + ${shiro-ehcache3.version} + + + org.ehcache + ehcache + + + org.apache.shiro + shiro-core + + + + org.glassfish.main.libpam4j libpam4j From e219aeea78439fd8b9f1d5bfc9b9f8a123a64ee8 Mon Sep 17 00:00:00 2001 From: thanicz Date: Fri, 4 Oct 2024 16:55:52 +0200 Subject: [PATCH 2/2] KNOX-3067: Adjust log messages --- .../src/main/java/org/apache/knox/gateway/ShiroMessages.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gateway-provider-security-shiro/src/main/java/org/apache/knox/gateway/ShiroMessages.java b/gateway-provider-security-shiro/src/main/java/org/apache/knox/gateway/ShiroMessages.java index 6b1a1e4116..28879e724d 100644 --- a/gateway-provider-security-shiro/src/main/java/org/apache/knox/gateway/ShiroMessages.java +++ b/gateway-provider-security-shiro/src/main/java/org/apache/knox/gateway/ShiroMessages.java @@ -33,7 +33,7 @@ public interface ShiroMessages { @Message( level = MessageLevel.TRACE, text = "Acquiring EhcacheShiro instance named {0}" ) void acquireEhcacheShiro(String name); - @Message( level = MessageLevel.INFO, text = "Cache with name {0} does not yet exist. Creating now." ) + @Message( level = MessageLevel.INFO, text = "Cache with name {0} does not exist yet. Creating now." ) void noCacheFound(String name); @Message( level = MessageLevel.INFO, text = "Added EhcacheShiro named {0}" ) @@ -42,6 +42,6 @@ public interface ShiroMessages { @Message( level = MessageLevel.INFO, text = "Using existing EhcacheShiro named {0}" ) void usingExistingEhcacheShiro(String name); - @Message( level = MessageLevel.WARN, text = "The Shiro managed CacheManager threw an Exception while closing: {0}" ) + @Message( level = MessageLevel.WARN, text = "There was an error closing the CacheManager, reason: {0}" ) void errorClosingManagedCacheManager(@StackTrace(level=MessageLevel.WARN) Exception e); }