You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by co...@apache.org on 2020/05/22 16:00:21 UTC

[ws-wss4j] 01/03: Did a refactor of the caching factories to simplify passing parameters to EHCacheReplayCache

This is an automated email from the ASF dual-hosted git repository.

coheigea pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ws-wss4j.git

commit 16e0e309dbde924bf33f5e4116c2d20f5bed36e6
Author: Colm O hEigeartaigh <co...@apache.org>
AuthorDate: Thu May 21 17:49:14 2020 +0100

    Did a refactor of the caching factories to simplify passing parameters to EHCacheReplayCache
---
 .../wss4j/common/cache/EHCacheReplayCache.java     | 14 ++++-----
 .../common/cache/EHCacheReplayCacheFactory.java    | 36 ----------------------
 .../common/cache/MemoryReplayCacheFactory.java     | 34 --------------------
 ...ReplayCacheFactory.java => WSS4JCacheUtil.java} | 29 ++++++++---------
 .../apache/wss4j/common/cache/ReplayCacheTest.java | 23 +++++++++-----
 .../apache/wss4j/dom/common/SecurityTestUtil.java  |  8 +++--
 .../org/apache/wss4j/stax/test/ReplayTest.java     | 11 +++++--
 .../apache/wss4j/stax/test/UsernameTokenTest.java  |  9 ++++--
 8 files changed, 54 insertions(+), 110 deletions(-)

diff --git a/ws-security-common/src/main/java/org/apache/wss4j/common/cache/EHCacheReplayCache.java b/ws-security-common/src/main/java/org/apache/wss4j/common/cache/EHCacheReplayCache.java
index 222f2a8..159b589 100644
--- a/ws-security-common/src/main/java/org/apache/wss4j/common/cache/EHCacheReplayCache.java
+++ b/ws-security-common/src/main/java/org/apache/wss4j/common/cache/EHCacheReplayCache.java
@@ -19,11 +19,10 @@
 
 package org.apache.wss4j.common.cache;
 
-import java.io.File;
 import java.io.IOException;
 import java.net.URL;
+import java.nio.file.Path;
 import java.time.Instant;
-import java.util.Random;
 
 import org.apache.wss4j.common.ext.WSSecurityException;
 import org.apache.wss4j.common.util.Loader;
@@ -48,16 +47,17 @@ public class EHCacheReplayCache implements ReplayCache {
     private final CacheManager cacheManager;
     private final String key;
 
-    public EHCacheReplayCache(String key, URL configFileURL) throws WSSecurityException {
+    public EHCacheReplayCache(String key, URL configFileURL, Path diskstorePath) throws WSSecurityException {
         this.key = key;
         try {
             XmlConfiguration xmlConfig = new XmlConfiguration(getConfigFileURL(configFileURL));
             CacheConfigurationBuilder<String, EHCacheValue> configurationBuilder =
                     xmlConfig.newCacheConfigurationBuilderFromTemplate(CACHE_TEMPLATE_NAME, String.class, EHCacheValue.class);
-            // Note, we don't require strong random values here
-            String diskKey = key + "-" + Math.abs(new Random().nextInt());
-            cacheManager = CacheManagerBuilder.newCacheManagerBuilder().withCache(key, configurationBuilder)
-                     .with(CacheManagerBuilder.persistence(new File(System.getProperty("java.io.tmpdir"), diskKey))).build();
+            CacheManagerBuilder builder = CacheManagerBuilder.newCacheManagerBuilder().withCache(key, configurationBuilder);
+            if (diskstorePath != null) {
+                builder = builder.with(CacheManagerBuilder.persistence(diskstorePath.toFile()));
+            }
+            cacheManager = builder.build();
 
             cacheManager.init();
             cache = cacheManager.getCache(key, String.class, EHCacheValue.class);
diff --git a/ws-security-common/src/main/java/org/apache/wss4j/common/cache/EHCacheReplayCacheFactory.java b/ws-security-common/src/main/java/org/apache/wss4j/common/cache/EHCacheReplayCacheFactory.java
deleted file mode 100644
index 9ef0758..0000000
--- a/ws-security-common/src/main/java/org/apache/wss4j/common/cache/EHCacheReplayCacheFactory.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/**
- * 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.wss4j.common.cache;
-
-import java.net.URL;
-
-import org.apache.wss4j.common.ext.WSSecurityException;
-
-/**
- * A factory to return an EHCacheReplayCache instance.
- */
-public class EHCacheReplayCacheFactory extends ReplayCacheFactory {
-
-    public ReplayCache newReplayCache(String key, Object configuration) throws WSSecurityException {
-        URL configFileURL = getConfigFileURL(configuration);
-        return new EHCacheReplayCache(key, configFileURL);
-    }
-
-}
diff --git a/ws-security-common/src/main/java/org/apache/wss4j/common/cache/MemoryReplayCacheFactory.java b/ws-security-common/src/main/java/org/apache/wss4j/common/cache/MemoryReplayCacheFactory.java
deleted file mode 100644
index 685eccd..0000000
--- a/ws-security-common/src/main/java/org/apache/wss4j/common/cache/MemoryReplayCacheFactory.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/**
- * 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.wss4j.common.cache;
-
-
-import org.apache.wss4j.common.ext.WSSecurityException;
-
-/**
- * A factory to return a MemoryReplayCache instance.
- */
-public class MemoryReplayCacheFactory extends ReplayCacheFactory {
-
-    public ReplayCache newReplayCache(String key, Object configuration) throws WSSecurityException {
-        return new MemoryReplayCache();
-    }
-
-}
diff --git a/ws-security-common/src/main/java/org/apache/wss4j/common/cache/ReplayCacheFactory.java b/ws-security-common/src/main/java/org/apache/wss4j/common/cache/WSS4JCacheUtil.java
similarity index 68%
rename from ws-security-common/src/main/java/org/apache/wss4j/common/cache/ReplayCacheFactory.java
rename to ws-security-common/src/main/java/org/apache/wss4j/common/cache/WSS4JCacheUtil.java
index 213677a..38bd8ab 100644
--- a/ws-security-common/src/main/java/org/apache/wss4j/common/cache/ReplayCacheFactory.java
+++ b/ws-security-common/src/main/java/org/apache/wss4j/common/cache/WSS4JCacheUtil.java
@@ -22,20 +22,19 @@ package org.apache.wss4j.common.cache;
 import java.io.IOException;
 import java.net.URL;
 
-import org.apache.wss4j.common.ext.WSSecurityException;
 import org.apache.wss4j.common.util.Loader;
 
 /**
- * An abstract factory to return a ReplayCache instance. It returns an EHCacheReplayCacheFactory
- * if EH-Cache is available. Otherwise it returns a MemoryReplayCacheFactory.
+ * Some functionality to detect if EhCache is available or not.
  */
-public abstract class ReplayCacheFactory {
+public final class WSS4JCacheUtil {
 
     private static final org.slf4j.Logger LOG =
-        org.slf4j.LoggerFactory.getLogger(ReplayCacheFactory.class);
-    private static boolean ehCacheInstalled;
+        org.slf4j.LoggerFactory.getLogger(WSS4JCacheUtil.class);
+    private static final boolean EH_CACHE_INSTALLED;
 
     static {
+        boolean ehCacheInstalled = false;
         try {
             Class<?> cacheManagerClass = Class.forName("org.ehcache.CacheManager");
             if (cacheManagerClass != null) {
@@ -45,23 +44,18 @@ public abstract class ReplayCacheFactory {
             //ignore
             LOG.debug(e.getMessage());
         }
+        EH_CACHE_INSTALLED = ehCacheInstalled;
     }
 
-    public static synchronized boolean isEhCacheInstalled() {
-        return ehCacheInstalled;
+    private WSS4JCacheUtil() {
+        // complete
     }
 
-    public static ReplayCacheFactory newInstance() {
-        if (isEhCacheInstalled()) {
-            return new EHCacheReplayCacheFactory();
-        }
-
-        return new MemoryReplayCacheFactory();
+    public static boolean isEhCacheInstalled() {
+        return EH_CACHE_INSTALLED;
     }
 
-    public abstract ReplayCache newReplayCache(String key, Object configuration) throws WSSecurityException;
-
-    protected URL getConfigFileURL(Object o) {
+    public static URL getConfigFileURL(Object o) {
         if (o instanceof String) {
             try {
                 URL url = Loader.getResource((String)o);
@@ -79,4 +73,5 @@ public abstract class ReplayCacheFactory {
         return null;
     }
 
+
 }
diff --git a/ws-security-common/src/test/java/org/apache/wss4j/common/cache/ReplayCacheTest.java b/ws-security-common/src/test/java/org/apache/wss4j/common/cache/ReplayCacheTest.java
index 90778a7..60d264c 100644
--- a/ws-security-common/src/test/java/org/apache/wss4j/common/cache/ReplayCacheTest.java
+++ b/ws-security-common/src/test/java/org/apache/wss4j/common/cache/ReplayCacheTest.java
@@ -19,10 +19,13 @@
 
 package org.apache.wss4j.common.cache;
 
+import java.io.File;
 import java.io.IOException;
 import java.net.URL;
+import java.nio.file.Path;
 import java.time.Instant;
 import java.time.temporal.ChronoUnit;
+import java.util.Random;
 import java.util.UUID;
 
 import org.junit.jupiter.api.Test;
@@ -49,7 +52,7 @@ public class ReplayCacheTest {
 
     @Test
     public void testEhCacheReplayCache() throws Exception {
-        ReplayCache replayCache = new EHCacheReplayCache("xyz", (URL)null);
+        ReplayCache replayCache = new EHCacheReplayCache("xyz", (URL)null, getDiskstorePath("abc"));
 
         testReplayCacheInstance(replayCache);
 
@@ -58,8 +61,9 @@ public class ReplayCacheTest {
 
     @Test
     public void testEhCacheDifferentCaches() throws Exception {
-        ReplayCache replayCache = new EHCacheReplayCache("abc", (URL)null);
-        ReplayCache replayCache2 = new EHCacheReplayCache("cba", (URL)null);
+        ReplayCache replayCache = new EHCacheReplayCache("abc", (URL)null, getDiskstorePath("abc"));
+
+        ReplayCache replayCache2 = new EHCacheReplayCache("cba", (URL)null, getDiskstorePath("cba"));
 
         String id = UUID.randomUUID().toString();
         replayCache.add(id);
@@ -72,7 +76,7 @@ public class ReplayCacheTest {
 
     @Test
     public void testEhCacheCloseCacheTwice() throws Exception {
-        ReplayCache replayCache = new EHCacheReplayCache("abc", (URL) null);
+        ReplayCache replayCache = new EHCacheReplayCache("abc", (URL)null, getDiskstorePath("abc"));
         replayCache.close();
         replayCache.close();
     }
@@ -80,7 +84,7 @@ public class ReplayCacheTest {
     // No expiry specified so it falls back to the default
     @Test
     public void testEhCacheReplayCacheNoExpirySpecified() throws Exception {
-        ReplayCache replayCache = new EHCacheReplayCache("xyz", (URL)null);
+        ReplayCache replayCache = new EHCacheReplayCache("xyz", (URL)null, getDiskstorePath("xyz"));
 
         String id = UUID.randomUUID().toString();
         replayCache.add(id);
@@ -97,7 +101,7 @@ public class ReplayCacheTest {
     // The negative expiry is rejected and it falls back to the default
     @Test
     public void testEhCacheReplayCacheNegativeExpiry() throws Exception {
-        ReplayCache replayCache = new EHCacheReplayCache("xyz", (URL)null);
+        ReplayCache replayCache = new EHCacheReplayCache("xyz", (URL)null, getDiskstorePath("xyz"));
 
         String id = UUID.randomUUID().toString();
         replayCache.add(id, Instant.now().minusSeconds(100L));
@@ -114,7 +118,7 @@ public class ReplayCacheTest {
     // The huge expiry is rejected and it falls back to the default
     @Test
     public void testEhCacheReplayCacheHugeExpiry() throws Exception {
-        ReplayCache replayCache = new EHCacheReplayCache("xyz", (URL)null);
+        ReplayCache replayCache = new EHCacheReplayCache("xyz", (URL)null, getDiskstorePath("xyz"));
 
         String id = UUID.randomUUID().toString();
         replayCache.add(id, Instant.now().plus(14, ChronoUnit.HOURS));
@@ -145,6 +149,11 @@ public class ReplayCacheTest {
         replayCache.add(id, Instant.now().plusSeconds(1L));
         Thread.sleep(1250L);
         assertFalse(replayCache.contains(id));
+    }
 
+    private Path getDiskstorePath(String prefix) {
+        String diskKey = prefix + "-" + Math.abs(new Random().nextInt());
+        File diskstore = new File(System.getProperty("java.io.tmpdir"), diskKey);
+        return diskstore.toPath();
     }
 }
\ No newline at end of file
diff --git a/ws-security-dom/src/test/java/org/apache/wss4j/dom/common/SecurityTestUtil.java b/ws-security-dom/src/test/java/org/apache/wss4j/dom/common/SecurityTestUtil.java
index e8e349a..d6a8d3a 100644
--- a/ws-security-dom/src/test/java/org/apache/wss4j/dom/common/SecurityTestUtil.java
+++ b/ws-security-dom/src/test/java/org/apache/wss4j/dom/common/SecurityTestUtil.java
@@ -19,9 +19,10 @@
 package org.apache.wss4j.dom.common;
 
 import java.io.File;
+import java.util.Random;
 
+import org.apache.wss4j.common.cache.EHCacheReplayCache;
 import org.apache.wss4j.common.cache.ReplayCache;
-import org.apache.wss4j.common.cache.ReplayCacheFactory;
 import org.apache.wss4j.common.ext.WSSecurityException;
 import org.apache.wss4j.dom.util.WSSecurityUtil;
 import org.apache.xml.security.utils.XMLUtils;
@@ -50,8 +51,9 @@ public final class SecurityTestUtil {
     }
 
     public static ReplayCache createCache(String key) throws WSSecurityException {
-        ReplayCacheFactory replayCacheFactory = ReplayCacheFactory.newInstance();
         String cacheKey = key + XMLUtils.encodeToString(WSSecurityUtil.generateNonce(10));
-        return replayCacheFactory.newReplayCache(cacheKey, null);
+        String diskKey = key + "-" + Math.abs(new Random().nextInt());
+        File diskstore = new File(System.getProperty("java.io.tmpdir"), diskKey);
+        return new EHCacheReplayCache(cacheKey, null, diskstore.toPath());
     }
 }
diff --git a/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/ReplayTest.java b/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/ReplayTest.java
index ee3c182..00b0fed 100644
--- a/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/ReplayTest.java
+++ b/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/ReplayTest.java
@@ -20,16 +20,18 @@ package org.apache.wss4j.stax.test;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.File;
 import java.io.InputStream;
 import java.util.Properties;
+import java.util.Random;
 
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
 
+import org.apache.wss4j.common.cache.EHCacheReplayCache;
 import org.apache.wss4j.common.cache.ReplayCache;
-import org.apache.wss4j.common.cache.ReplayCacheFactory;
 import org.apache.wss4j.common.ext.WSSecurityException;
 import org.apache.wss4j.common.saml.bean.ConditionsBean;
 import org.apache.wss4j.common.saml.builder.SAML2Constants;
@@ -56,12 +58,15 @@ import static org.junit.jupiter.api.Assertions.fail;
 public class ReplayTest extends AbstractTestBase {
 
     private ReplayCache createCache(String key) throws WSSecurityException {
-        ReplayCacheFactory replayCacheFactory = ReplayCacheFactory.newInstance();
         byte[] nonceValue;
         try {
             nonceValue = WSSConstants.generateBytes(10);
             String cacheKey = key + XMLUtils.encodeToString(nonceValue);
-            return replayCacheFactory.newReplayCache(cacheKey, null);
+
+            String diskKey = key + "-" + Math.abs(new Random().nextInt());
+            File diskstore = new File(System.getProperty("java.io.tmpdir"), diskKey);
+
+            return new EHCacheReplayCache(cacheKey, null, diskstore.toPath());
         } catch (XMLSecurityException e) {
             throw new WSSecurityException(WSSecurityException.ErrorCode.FAILURE, e);
         }
diff --git a/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/UsernameTokenTest.java b/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/UsernameTokenTest.java
index c5ab607..048f368 100644
--- a/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/UsernameTokenTest.java
+++ b/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/UsernameTokenTest.java
@@ -20,6 +20,7 @@ package org.apache.wss4j.stax.test;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.File;
 import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
 import java.time.ZoneOffset;
@@ -29,6 +30,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
+import java.util.Random;
 
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
@@ -37,8 +39,8 @@ import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
 
 import org.apache.wss4j.common.ConfigurationConstants;
+import org.apache.wss4j.common.cache.EHCacheReplayCache;
 import org.apache.wss4j.common.cache.ReplayCache;
-import org.apache.wss4j.common.cache.ReplayCacheFactory;
 import org.apache.wss4j.common.ext.WSSecurityException;
 import org.apache.wss4j.common.util.DateUtil;
 import org.apache.wss4j.common.util.UsernameTokenUtil;
@@ -867,12 +869,13 @@ public class UsernameTokenTest extends AbstractTestBase {
     }
 
     private ReplayCache createCache(String key) throws WSSecurityException {
-        ReplayCacheFactory replayCacheFactory = ReplayCacheFactory.newInstance();
         byte[] nonceValue;
         try {
             nonceValue = WSSConstants.generateBytes(10);
             String cacheKey = key + XMLUtils.encodeToString(nonceValue);
-            return replayCacheFactory.newReplayCache(cacheKey, null);
+            String diskKey = key + "-" + Math.abs(new Random().nextInt());
+            File diskstore = new File(System.getProperty("java.io.tmpdir"), diskKey);
+            return new EHCacheReplayCache(cacheKey, null, diskstore.toPath());
         } catch (XMLSecurityException e) {
             throw new WSSecurityException(WSSecurityException.ErrorCode.FAILURE, e);
         }