[java-support] branch master updated: JSPT-87 - Scriptable DataSealerKeyStrategy

Scott Cantor cantor.2 at osu.edu
Mon Jun 10 10:59:19 EDT 2019


This is an automated email from the git hooks/post-receive script.

scantor pushed a commit to branch master
in repository java-support.

View the commit online:
http://git.shibboleth.net/view/?p=java-support.git;a=commit;h=9a4231236d59146fd6e14868f289c052f65fab87

The following commit(s) were added to refs/heads/master by this push:
       new  9a42312   JSPT-87 - Scriptable DataSealerKeyStrategy
9a42312 is described below

commit 9a4231236d59146fd6e14868f289c052f65fab87
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Mon Jun 10 10:59:17 2019 -0400

    JSPT-87 - Scriptable DataSealerKeyStrategy
    
    https://issues.shibboleth.net/jira/browse/JSPT-87
    
    Add control of key cache size.
---
 .../support/security/impl/ScriptedKeyStrategy.java | 27 +++++++++++++++++++---
 .../security/impl/ScriptedKeyStrategyTest.java     | 20 +++++++++++++---
 2 files changed, 41 insertions(+), 6 deletions(-)

diff --git a/src/main/java/net/shibboleth/utilities/java/support/security/impl/ScriptedKeyStrategy.java b/src/main/java/net/shibboleth/utilities/java/support/security/impl/ScriptedKeyStrategy.java
index 099d956..c3301ff 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/security/impl/ScriptedKeyStrategy.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/security/impl/ScriptedKeyStrategy.java
@@ -31,6 +31,7 @@ import javax.script.ScriptContext;
 import javax.script.ScriptException;
 import javax.script.SimpleScriptContext;
 
+import net.shibboleth.utilities.java.support.annotation.constraint.NonNegative;
 import net.shibboleth.utilities.java.support.annotation.constraint.NonnullAfterInit;
 import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
 import net.shibboleth.utilities.java.support.collection.Pair;
@@ -83,9 +84,13 @@ public class ScriptedKeyStrategy extends AbstractInitializableComponent implemen
     /** Task that checks for updated key version. */
     @Nullable private TimerTask updateTask;
     
+    /** Size of key cache to maintain. */
+    @NonNegative private long cacheSize;
+    
     /** Constructor. */
     public ScriptedKeyStrategy() {
-        keyCache = new LinkedHashMap<>(10);
+        cacheSize = 30;
+        keyCache = new LinkedHashMap<>((int) cacheSize);
         updateInterval = Duration.ofMinutes(15);
     }
 
@@ -124,6 +129,7 @@ public class ScriptedKeyStrategy extends AbstractInitializableComponent implemen
      */
     public void setUpdateInterval(@Nonnull final Duration interval) {
         ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
+        ComponentSupport.ifDestroyedThrowDestroyedComponentException(this);
         
         Constraint.isNotNull(interval, "Interval cannot be null");
         Constraint.isFalse(interval.isNegative(), "Interval cannot be negative");
@@ -140,10 +146,25 @@ public class ScriptedKeyStrategy extends AbstractInitializableComponent implemen
      */
     public void setUpdateTaskTimer(@Nullable final Timer timer) {
         ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
+        ComponentSupport.ifDestroyedThrowDestroyedComponentException(this);
 
         updateTaskTimer = timer;
     }
     
+    /**
+     * Set the number of keys to cache.
+     * 
+     * <p>Defaults to 30.</p>
+     * 
+     * @param size size of cache
+     */
+    public void setCacheSize(@NonNegative final long size) {
+        ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
+        ComponentSupport.ifDestroyedThrowDestroyedComponentException(this);
+
+        cacheSize = Constraint.isGreaterThanOrEqual(0, size, "Key cache size cannot be negative");
+    }
+    
     /** {@inheritDoc} */
     @Override
     public void doInitialize() throws ComponentInitializationException {
@@ -256,9 +277,9 @@ public class ScriptedKeyStrategy extends AbstractInitializableComponent implemen
         
         synchronized(this) {
             int size = keyCache.size();
-            if (size > 30) {
+            if (size > cacheSize) {
                 final Iterator<String> iter = keyCache.keySet().iterator();
-                while (size > 30) {
+                while (size > cacheSize) {
                     iter.next();
                     iter.remove();
                     size--;
diff --git a/src/test/java/net/shibboleth/utilities/java/support/security/impl/ScriptedKeyStrategyTest.java b/src/test/java/net/shibboleth/utilities/java/support/security/impl/ScriptedKeyStrategyTest.java
index 06040d0..575ab13 100644
--- a/src/test/java/net/shibboleth/utilities/java/support/security/impl/ScriptedKeyStrategyTest.java
+++ b/src/test/java/net/shibboleth/utilities/java/support/security/impl/ScriptedKeyStrategyTest.java
@@ -61,6 +61,7 @@ public class ScriptedKeyStrategyTest {
         strategy.setUpdateInterval(Duration.ofSeconds(1));
         strategy.setKeyScript(new EvaluableScript("javascript", new File(scriptPath)));
         strategy.setCustomObject(customMap);
+        strategy.setCacheSize(1);
         strategy.initialize();
     }
     
@@ -78,21 +79,34 @@ public class ScriptedKeyStrategyTest {
     }
     
     @Test public void testScriptedKeystoreKeyStrategy() throws Exception {
-
+    
         
         Assert.assertEquals(strategy.getDefaultKey().getFirst(), "secret1");
         try {
             strategy.getKey("secret2");
             Assert.fail("secret2 should not exist");
         } catch (final KeyException e) {
-
+    
         }
-
+    
         customMap.put("secret2", keyGenerator.generateKey());
         customMap.put("default", "secret2");
         Thread.sleep(5000);
         Assert.assertEquals(strategy.getDefaultKey().getFirst(), "secret2");
         Assert.assertNotNull(strategy.getKey("secret1"));
+    
+        customMap.put("secret3", keyGenerator.generateKey());
+        customMap.put("default", "secret3");
+        customMap.remove("secret1");
+        Thread.sleep(5000);
+        Assert.assertEquals(strategy.getDefaultKey().getFirst(), "secret3");
+        Assert.assertNotNull(strategy.getKey("secret2"));
+        try {
+            strategy.getKey("secret1");
+            Assert.fail("secret1 should not exist");
+        } catch (final KeyException e) {
+    
+        }
     }
     
 }
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list