@Override public void deleteKey(String name) throws IOException { getKeyProvider().deleteKey(name); getExtension().currentKeyCache.invalidate(name); getExtension().keyMetadataCache.invalidate(name); // invalidating all key versions as we don't know // which ones belonged to the deleted key getExtension().keyVersionCache.invalidateAll(); }
@Override public KeyVersion rollNewVersion(String name, byte[] material) throws IOException { KeyVersion key = getKeyProvider().rollNewVersion(name, material); invalidateCache(name); return key; }
@Override public Metadata getMetadata(String name) throws IOException { try { return getExtension().keyMetadataCache.get(name); } catch (ExecutionException ex) { Throwable cause = ex.getCause(); if (cause instanceof KeyNotFoundException) { return null; } else if (cause instanceof IOException) { throw (IOException) cause; } else { throw new IOException(cause); } } }
kmsConf.getLong(KMSConfiguration.CURR_KEY_CACHE_TIMEOUT_KEY, KMSConfiguration.CURR_KEY_CACHE_TIMEOUT_DEFAULT); keyProvider = new CachingKeyProvider(keyProvider, keyTimeOutMillis, currKeyTimeOutMillis);
@Override public KeyVersion getCurrentKey(String name) throws IOException { try { return getExtension().currentKeyCache.get(name); } catch (ExecutionException ex) { Throwable cause = ex.getCause(); if (cause instanceof KeyNotFoundException) { return null; } else if (cause instanceof IOException) { throw (IOException) cause; } else { throw new IOException(cause); } } }
@Test public void testRollNewVersion() throws Exception { KeyProvider.KeyVersion mockKey = Mockito.mock(KeyProvider.KeyVersion.class); KeyProvider mockProv = Mockito.mock(KeyProvider.class); Mockito.when(mockProv.getCurrentKey(Mockito.eq("k1"))).thenReturn(mockKey); Mockito.when(mockProv.getConf()).thenReturn(new Configuration()); KeyProvider cache = new CachingKeyProvider(mockProv, 100, 100); Assert.assertEquals(mockKey, cache.getCurrentKey("k1")); Mockito.verify(mockProv, Mockito.times(1)).getCurrentKey(Mockito.eq("k1")); cache.rollNewVersion("k1"); // asserting the cache is purged Assert.assertEquals(mockKey, cache.getCurrentKey("k1")); Mockito.verify(mockProv, Mockito.times(2)).getCurrentKey(Mockito.eq("k1")); cache.rollNewVersion("k1", new byte[0]); Assert.assertEquals(mockKey, cache.getCurrentKey("k1")); Mockito.verify(mockProv, Mockito.times(3)).getCurrentKey(Mockito.eq("k1")); }
@Override public void invalidateCache(String name) throws IOException { getKeyProvider().invalidateCache(name); getExtension().currentKeyCache.invalidate(name); getExtension().keyMetadataCache.invalidate(name); // invalidating all key versions as we don't know // which ones belonged to the deleted key getExtension().keyVersionCache.invalidateAll(); }
@Override public KeyVersion getKeyVersion(String versionName) throws IOException { try { return getExtension().keyVersionCache.get(versionName); } catch (ExecutionException ex) { Throwable cause = ex.getCause(); if (cause instanceof KeyNotFoundException) { return null; } else if (cause instanceof IOException) { throw (IOException) cause; } else { throw new IOException(cause); } } }
@Test public void testRollNewVersion() throws Exception { KeyProvider.KeyVersion mockKey = Mockito.mock(KeyProvider.KeyVersion.class); KeyProvider mockProv = Mockito.mock(KeyProvider.class); Mockito.when(mockProv.getCurrentKey(Mockito.eq("k1"))).thenReturn(mockKey); Mockito.when(mockProv.getConf()).thenReturn(new Configuration()); KeyProvider cache = new CachingKeyProvider(mockProv, 100, 100); Assert.assertEquals(mockKey, cache.getCurrentKey("k1")); Mockito.verify(mockProv, Mockito.times(1)).getCurrentKey(Mockito.eq("k1")); cache.rollNewVersion("k1"); // asserting the cache is purged Assert.assertEquals(mockKey, cache.getCurrentKey("k1")); Mockito.verify(mockProv, Mockito.times(2)).getCurrentKey(Mockito.eq("k1")); cache.rollNewVersion("k1", new byte[0]); Assert.assertEquals(mockKey, cache.getCurrentKey("k1")); Mockito.verify(mockProv, Mockito.times(3)).getCurrentKey(Mockito.eq("k1")); }
@Override public KeyVersion rollNewVersion(String name) throws NoSuchAlgorithmException, IOException { KeyVersion key = getKeyProvider().rollNewVersion(name); invalidateCache(name); return key; }
@Override public void deleteKey(String name) throws IOException { getKeyProvider().deleteKey(name); getExtension().currentKeyCache.invalidate(name); getExtension().keyMetadataCache.invalidate(name); // invalidating all key versions as we don't know // which ones belonged to the deleted key getExtension().keyVersionCache.invalidateAll(); }
@Override public KeyVersion getCurrentKey(String name) throws IOException { try { return getExtension().currentKeyCache.get(name); } catch (ExecutionException ex) { Throwable cause = ex.getCause(); if (cause instanceof KeyNotFoundException) { return null; } else if (cause instanceof IOException) { throw (IOException) cause; } else { throw new IOException(cause); } } }
@Test public void testMetadata() throws Exception { KeyProvider.Metadata mockMeta = Mockito.mock(KeyProvider.Metadata.class); KeyProvider mockProv = Mockito.mock(KeyProvider.class); Mockito.when(mockProv.getMetadata(Mockito.eq("k1"))).thenReturn(mockMeta); Mockito.when(mockProv.getMetadata(Mockito.eq("k2"))).thenReturn(null); Mockito.when(mockProv.getConf()).thenReturn(new Configuration()); KeyProvider cache = new CachingKeyProvider(mockProv, 100, 100); // asserting caching Assert.assertEquals(mockMeta, cache.getMetadata("k1")); Mockito.verify(mockProv, Mockito.times(1)).getMetadata(Mockito.eq("k1")); Assert.assertEquals(mockMeta, cache.getMetadata("k1")); Mockito.verify(mockProv, Mockito.times(1)).getMetadata(Mockito.eq("k1")); Thread.sleep(200); Assert.assertEquals(mockMeta, cache.getMetadata("k1")); Mockito.verify(mockProv, Mockito.times(2)).getMetadata(Mockito.eq("k1")); // asserting no caching when key is not known cache = new CachingKeyProvider(mockProv, 100, 100); Assert.assertEquals(null, cache.getMetadata("k2")); Mockito.verify(mockProv, Mockito.times(1)).getMetadata(Mockito.eq("k2")); Assert.assertEquals(null, cache.getMetadata("k2")); Mockito.verify(mockProv, Mockito.times(2)).getMetadata(Mockito.eq("k2")); }
@Override public void deleteKey(String name) throws IOException { getKeyProvider().deleteKey(name); getExtension().currentKeyCache.invalidate(name); getExtension().keyMetadataCache.invalidate(name); // invalidating all key versions as we don't know // which ones belonged to the deleted key getExtension().keyVersionCache.invalidateAll(); }
@Override public KeyVersion getCurrentKey(String name) throws IOException { try { return getExtension().currentKeyCache.get(name); } catch (ExecutionException ex) { Throwable cause = ex.getCause(); if (cause instanceof KeyNotFoundException) { return null; } else if (cause instanceof IOException) { throw (IOException) cause; } else { throw new IOException(cause); } } }
@Test public void testCurrentKey() throws Exception { KeyProvider.KeyVersion mockKey = Mockito.mock(KeyProvider.KeyVersion.class); KeyProvider mockProv = Mockito.mock(KeyProvider.class); Mockito.when(mockProv.getCurrentKey(Mockito.eq("k1"))).thenReturn(mockKey); Mockito.when(mockProv.getCurrentKey(Mockito.eq("k2"))).thenReturn(null); Mockito.when(mockProv.getConf()).thenReturn(new Configuration()); KeyProvider cache = new CachingKeyProvider(mockProv, 100, 100); // asserting caching Assert.assertEquals(mockKey, cache.getCurrentKey("k1")); Mockito.verify(mockProv, Mockito.times(1)).getCurrentKey(Mockito.eq("k1")); Assert.assertEquals(mockKey, cache.getCurrentKey("k1")); Mockito.verify(mockProv, Mockito.times(1)).getCurrentKey(Mockito.eq("k1")); Thread.sleep(1200); Assert.assertEquals(mockKey, cache.getCurrentKey("k1")); Mockito.verify(mockProv, Mockito.times(2)).getCurrentKey(Mockito.eq("k1")); // asserting no caching when key is not known cache = new CachingKeyProvider(mockProv, 100, 100); Assert.assertEquals(null, cache.getCurrentKey("k2")); Mockito.verify(mockProv, Mockito.times(1)).getCurrentKey(Mockito.eq("k2")); Assert.assertEquals(null, cache.getCurrentKey("k2")); Mockito.verify(mockProv, Mockito.times(2)).getCurrentKey(Mockito.eq("k2")); }
@Override public KeyVersion rollNewVersion(String name, byte[] material) throws IOException { KeyVersion key = getKeyProvider().rollNewVersion(name, material); getExtension().currentKeyCache.invalidate(name); getExtension().keyMetadataCache.invalidate(name); return key; }
@Override public KeyVersion getCurrentKey(String name) throws IOException { try { return getExtension().currentKeyCache.get(name); } catch (ExecutionException ex) { Throwable cause = ex.getCause(); if (cause instanceof KeyNotFoundException) { return null; } else if (cause instanceof IOException) { throw (IOException) cause; } else { throw new IOException(cause); } } }
@Test public void testCurrentKey() throws Exception { KeyProvider.KeyVersion mockKey = Mockito.mock(KeyProvider.KeyVersion.class); KeyProvider mockProv = Mockito.mock(KeyProvider.class); Mockito.when(mockProv.getCurrentKey(Mockito.eq("k1"))).thenReturn(mockKey); Mockito.when(mockProv.getCurrentKey(Mockito.eq("k2"))).thenReturn(null); Mockito.when(mockProv.getConf()).thenReturn(new Configuration()); KeyProvider cache = new CachingKeyProvider(mockProv, 100, 100); // asserting caching Assert.assertEquals(mockKey, cache.getCurrentKey("k1")); Mockito.verify(mockProv, Mockito.times(1)).getCurrentKey(Mockito.eq("k1")); Assert.assertEquals(mockKey, cache.getCurrentKey("k1")); Mockito.verify(mockProv, Mockito.times(1)).getCurrentKey(Mockito.eq("k1")); Thread.sleep(1200); Assert.assertEquals(mockKey, cache.getCurrentKey("k1")); Mockito.verify(mockProv, Mockito.times(2)).getCurrentKey(Mockito.eq("k1")); // asserting no caching when key is not known cache = new CachingKeyProvider(mockProv, 100, 100); Assert.assertEquals(null, cache.getCurrentKey("k2")); Mockito.verify(mockProv, Mockito.times(1)).getCurrentKey(Mockito.eq("k2")); Assert.assertEquals(null, cache.getCurrentKey("k2")); Mockito.verify(mockProv, Mockito.times(2)).getCurrentKey(Mockito.eq("k2")); }
@Override public KeyVersion rollNewVersion(String name, byte[] material) throws IOException { KeyVersion key = getKeyProvider().rollNewVersion(name, material); getExtension().currentKeyCache.invalidate(name); getExtension().keyMetadataCache.invalidate(name); return key; }