Details
Description
Solution: See updated documentation here: https://knox.apache.org/books/knox-1-2-0/user-guide.html#LDAP+Authentication+Caching
Ensure that the following is set:
<param> <name>main.cacheManager</name> <value>org.apache.knox.gateway.shirorealm.KnoxCacheManager</value> </param>
Problem
Issue is described on the note "Error:Shiro environment initialization failed" when starting Knox;
ERROR env.EnvironmentLoader (EnvironmentLoader.java:initEnvironment(146)) - Shiro environment initialization failed org.apache.shiro.cache.CacheException: net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following
After implementing the solution of creating a new ehcache.xml and reference as <ehcache name="knox-mytopology1"> we still see the issue:
Caused by: net.sf.ehcache.CacheException: Another CacheManager with same name 'knox-tp' already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:
Steps to reproduce
When enabling the following parameters:
<param>
<name>main.cacheManager</name>
<value>org.apache.shiro.cache.ehcache.EhCacheManager</value>
</param>
<param>
<name>main.securityManager.cacheManager</name>
<value>$cacheManager</value>
</param>
<param>
<name>main.ldapRealm.authenticationCachingEnabled</name>
<value>true</value>
</param>
More Info
This KNOX-804 was open to solve this same issue but is not documented anywhere, but we can see the coding changes in GitHub.
- The issue seems to be a racing condition on the Shiro lib class, in which depending on the processing power of the server there are two threads within the same "if" statement;
- The Default constructor only builds the Object, and does not do much:
/**Default no argument constructor*/ public EhCacheManager() { }
- Accordingly with shiro documentation:
/** Initializes this instance. <p/> If a {@link #setCacheManager CacheManager} has been explicitly set (e.g. via Dependency Injection or programmatically) prior to calling this method, this method does nothing. <p/> However, if no {@code CacheManager} has been set, the default Ehcache singleton will be initialized, where Ehcache will look for an {@code ehcache.xml} file at the root of the classpath. If one is not found, Ehcache will use its own failsafe configuration file. <p/> Because Shiro cannot use the failsafe defaults (fail-safe expunges cached objects after 2 minutes, something not desirable for Shiro sessions), this class manages an internal default configuration for this case. @throws org.apache.shiro.cache.CacheException if there are any CacheExceptions thrown by EhCache. @see net.sf.ehcache.CacheManager#create */ public final void init() throws CacheException { ensureCacheManager(); } private net.sf.ehcache.CacheManager ensureCacheManager() { ***It fails in here. }
- This is the head of the exception: The source of the existing CacheManager is:
InputStreamConfigurationSource [stream=java.io.BufferedInputStream@675ffd1d] at org.apache.shiro.cache.ehcache.EhCacheManager.ensureCacheManager(EhCacheManager.java:224) at org.apache.shiro.cache.ehcache.EhCacheManager.init(EhCacheManager.java:199) at org.apache.shiro.util.LifecycleUtils.init(LifecycleUtils.java:45) at org.apache.shiro.util.LifecycleUtils.init(LifecycleUtils.java:40)
- Starting from
KNOX-804, the KnoxCacheManager was introduced. Remember that Init() will do nothing if anything in case the setCacheManager is explicitly set.
Attachments
Issue Links
- relates to
-
KNOX-804 Knox ehcache usage has intermittent CacheManger exception
- Closed