LoadBalancingKMSClientProvider extends KeyProvider implements
    CryptoExtension,
    KeyProviderDelegationTokenExtension.DelegationTokenExtension { @Override public KeyVersion createKey(final String name, final byte[] material, final Options options) throws IOException { return doOp(new ProviderCallable<KeyVersion>() { @Override public KeyVersion call(KMSClientProvider provider) throws IOException { return provider.createKey(name, material, options); } }, nextIdx()); } LoadBalancingKMSClientProvider(KMSClientProvider[] providers,
      Configuration conf); @VisibleForTesting  LoadBalancingKMSClientProvider(KMSClientProvider[] providers, long seed,
      Configuration conf); @Override Token<?>[] addDelegationTokens(final String renewer, final Credentials credentials); @Override void warmUpEncryptedKeys(String... keyNames); @Override void drain(String keyName); @Override EncryptedKeyVersion generateEncryptedKey(final String encryptionKeyName); @Override KeyVersion decryptEncryptedKey(final EncryptedKeyVersion encryptedKeyVersion); @Override KeyVersion getKeyVersion(final String versionName); @Override List<String> getKeys(); @Override Metadata[] getKeysMetadata(final String... names); @Override List<KeyVersion> getKeyVersions(final String name); @Override KeyVersion getCurrentKey(final String name); @Override Metadata getMetadata(final String name); @Override KeyVersion createKey(final String name, final byte[] material,
      final Options options); @Override KeyVersion createKey(final String name, final Options options); @Override void deleteKey(final String name); @Override KeyVersion rollNewVersion(final String name, final byte[] material); @Override KeyVersion rollNewVersion(final String name); @Override void close(); @Override void flush(); static Logger LOG; }
@Test public void testLoadBalancing() throws Exception { Configuration conf = new Configuration(); KMSClientProvider p1 = mock(KMSClientProvider.class); when(p1.createKey(Mockito.anyString(), Mockito.any(Options.class))) .thenReturn( new KMSClientProvider.KMSKeyVersion("p1", "v1", new byte[0])); KMSClientProvider p2 = mock(KMSClientProvider.class); when(p2.createKey(Mockito.anyString(), Mockito.any(Options.class))) .thenReturn( new KMSClientProvider.KMSKeyVersion("p2", "v2", new byte[0])); KMSClientProvider p3 = mock(KMSClientProvider.class); when(p3.createKey(Mockito.anyString(), Mockito.any(Options.class))) .thenReturn( new KMSClientProvider.KMSKeyVersion("p3", "v3", new byte[0])); KeyProvider kp = new LoadBalancingKMSClientProvider( new KMSClientProvider[] { p1, p2, p3 }, 0, conf); assertEquals("p1", kp.createKey("test1", new Options(conf)).getName()); assertEquals("p2", kp.createKey("test2", new Options(conf)).getName()); assertEquals("p3", kp.createKey("test3", new Options(conf)).getName()); assertEquals("p1", kp.createKey("test4", new Options(conf)).getName()); }
@Test public void testLoadBalancingWithFailure() throws Exception { Configuration conf = new Configuration(); KMSClientProvider p1 = mock(KMSClientProvider.class); when(p1.createKey(Mockito.anyString(), Mockito.any(Options.class))) .thenReturn( new KMSClientProvider.KMSKeyVersion("p1", "v1", new byte[0])); when(p1.getKMSUrl()).thenReturn("p1"); KMSClientProvider p2 = mock(KMSClientProvider.class); when(p2.createKey(Mockito.anyString(), Mockito.any(Options.class))) .thenThrow(new NoSuchAlgorithmException("p2")); when(p2.getKMSUrl()).thenReturn("p2"); KMSClientProvider p3 = mock(KMSClientProvider.class); when(p3.createKey(Mockito.anyString(), Mockito.any(Options.class))) .thenReturn( new KMSClientProvider.KMSKeyVersion("p3", "v3", new byte[0])); when(p3.getKMSUrl()).thenReturn("p3"); KMSClientProvider p4 = mock(KMSClientProvider.class); when(p4.createKey(Mockito.anyString(), Mockito.any(Options.class))) .thenThrow(new IOException("p4")); when(p4.getKMSUrl()).thenReturn("p4"); KeyProvider kp = new LoadBalancingKMSClientProvider( new KMSClientProvider[] { p1, p2, p3, p4 }, 0, conf); assertEquals("p1", kp.createKey("test4", new Options(conf)).getName()); try { kp.createKey("test1", new Options(conf)).getName(); fail("Should fail since its not an IOException"); } catch (Exception e) { assertTrue(e instanceof NoSuchAlgorithmException); } assertEquals("p3", kp.createKey("test2", new Options(conf)).getName()); assertEquals("p1", kp.createKey("test3", new Options(conf)).getName()); }
@Test public void testLoadBalancingWithAllBadNodes() throws Exception { Configuration conf = new Configuration(); KMSClientProvider p1 = mock(KMSClientProvider.class); when(p1.createKey(Mockito.anyString(), Mockito.any(Options.class))) .thenThrow(new IOException("p1")); KMSClientProvider p2 = mock(KMSClientProvider.class); when(p2.createKey(Mockito.anyString(), Mockito.any(Options.class))) .thenThrow(new IOException("p2")); KMSClientProvider p3 = mock(KMSClientProvider.class); when(p3.createKey(Mockito.anyString(), Mockito.any(Options.class))) .thenThrow(new IOException("p3")); KMSClientProvider p4 = mock(KMSClientProvider.class); when(p4.createKey(Mockito.anyString(), Mockito.any(Options.class))) .thenThrow(new IOException("p4")); when(p1.getKMSUrl()).thenReturn("p1"); when(p2.getKMSUrl()).thenReturn("p2"); when(p3.getKMSUrl()).thenReturn("p3"); when(p4.getKMSUrl()).thenReturn("p4"); KeyProvider kp = new LoadBalancingKMSClientProvider( new KMSClientProvider[] { p1, p2, p3, p4 }, 0, conf); try { kp.createKey("test3", new Options(conf)).getName(); fail("Should fail since all providers threw an IOException"); } catch (Exception e) { assertTrue(e instanceof IOException); } }