|
@@ -34,6 +34,7 @@ import org.apache.hadoop.crypto.key.KeyProvider;
|
|
|
import org.apache.hadoop.crypto.key.KeyProvider.Options;
|
|
|
import org.apache.hadoop.crypto.key.KeyProviderCryptoExtension;
|
|
|
import org.apache.hadoop.security.authentication.client.AuthenticationException;
|
|
|
+import org.apache.hadoop.security.authorize.AuthorizationException;
|
|
|
import org.junit.Test;
|
|
|
import org.mockito.Mockito;
|
|
|
|
|
@@ -257,4 +258,66 @@ public class TestLoadBalancingKMSClientProvider {
|
|
|
"AuthenticationException"));
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * tests {@link LoadBalancingKMSClientProvider#warmUpEncryptedKeys(String...)}
|
|
|
+ * error handling in case when all the providers throws {@link IOException}.
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void testWarmUpEncryptedKeysWhenAllProvidersFail() throws Exception {
|
|
|
+ Configuration conf = new Configuration();
|
|
|
+ KMSClientProvider p1 = mock(KMSClientProvider.class);
|
|
|
+ String keyName = "key1";
|
|
|
+ Mockito.doThrow(new IOException(new AuthorizationException("p1"))).when(p1)
|
|
|
+ .warmUpEncryptedKeys(Mockito.anyString());
|
|
|
+ KMSClientProvider p2 = mock(KMSClientProvider.class);
|
|
|
+ Mockito.doThrow(new IOException(new AuthorizationException("p2"))).when(p2)
|
|
|
+ .warmUpEncryptedKeys(Mockito.anyString());
|
|
|
+
|
|
|
+ when(p1.getKMSUrl()).thenReturn("p1");
|
|
|
+ when(p2.getKMSUrl()).thenReturn("p2");
|
|
|
+
|
|
|
+ LoadBalancingKMSClientProvider kp = new LoadBalancingKMSClientProvider(
|
|
|
+ new KMSClientProvider[] {p1, p2}, 0, conf);
|
|
|
+ try {
|
|
|
+ kp.warmUpEncryptedKeys(keyName);
|
|
|
+ fail("Should fail since both providers threw IOException");
|
|
|
+ } catch (Exception e) {
|
|
|
+ assertTrue(e.getCause() instanceof IOException);
|
|
|
+ }
|
|
|
+ Mockito.verify(p1, Mockito.times(1)).warmUpEncryptedKeys(keyName);
|
|
|
+ Mockito.verify(p2, Mockito.times(1)).warmUpEncryptedKeys(keyName);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * tests {@link LoadBalancingKMSClientProvider#warmUpEncryptedKeys(String...)}
|
|
|
+ * error handling in case atleast one provider succeeds.
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void testWarmUpEncryptedKeysWhenOneProviderSucceeds()
|
|
|
+ throws Exception {
|
|
|
+ Configuration conf = new Configuration();
|
|
|
+ KMSClientProvider p1 = mock(KMSClientProvider.class);
|
|
|
+ String keyName = "key1";
|
|
|
+ Mockito.doThrow(new IOException(new AuthorizationException("p1"))).when(p1)
|
|
|
+ .warmUpEncryptedKeys(Mockito.anyString());
|
|
|
+ KMSClientProvider p2 = mock(KMSClientProvider.class);
|
|
|
+ Mockito.doNothing().when(p2)
|
|
|
+ .warmUpEncryptedKeys(Mockito.anyString());
|
|
|
+
|
|
|
+ when(p1.getKMSUrl()).thenReturn("p1");
|
|
|
+ when(p2.getKMSUrl()).thenReturn("p2");
|
|
|
+
|
|
|
+ LoadBalancingKMSClientProvider kp = new LoadBalancingKMSClientProvider(
|
|
|
+ new KMSClientProvider[] {p1, p2}, 0, conf);
|
|
|
+ try {
|
|
|
+ kp.warmUpEncryptedKeys(keyName);
|
|
|
+ } catch (Exception e) {
|
|
|
+ fail("Should not throw Exception since p2 doesn't throw Exception");
|
|
|
+ }
|
|
|
+ Mockito.verify(p1, Mockito.times(1)).warmUpEncryptedKeys(keyName);
|
|
|
+ Mockito.verify(p2, Mockito.times(1)).warmUpEncryptedKeys(keyName);
|
|
|
+ }
|
|
|
}
|