|
34 | 34 | import org.apache.hadoop.crypto.key.KeyProvider.Options;
|
35 | 35 | import org.apache.hadoop.crypto.key.KeyProviderCryptoExtension;
|
36 | 36 | import org.apache.hadoop.security.authentication.client.AuthenticationException;
|
| 37 | +import org.apache.hadoop.security.authorize.AuthorizationException; |
37 | 38 | import org.junit.Test;
|
38 | 39 | import org.mockito.Mockito;
|
39 | 40 |
|
@@ -257,4 +258,66 @@ public void testClassCastException() throws Exception {
|
257 | 258 | "AuthenticationException"));
|
258 | 259 | }
|
259 | 260 | }
|
| 261 | + |
| 262 | + /** |
| 263 | + * tests {@link LoadBalancingKMSClientProvider#warmUpEncryptedKeys(String...)} |
| 264 | + * error handling in case when all the providers throws {@link IOException}. |
| 265 | + * @throws Exception |
| 266 | + */ |
| 267 | + @Test |
| 268 | + public void testWarmUpEncryptedKeysWhenAllProvidersFail() throws Exception { |
| 269 | + Configuration conf = new Configuration(); |
| 270 | + KMSClientProvider p1 = mock(KMSClientProvider.class); |
| 271 | + String keyName = "key1"; |
| 272 | + Mockito.doThrow(new IOException(new AuthorizationException("p1"))).when(p1) |
| 273 | + .warmUpEncryptedKeys(Mockito.anyString()); |
| 274 | + KMSClientProvider p2 = mock(KMSClientProvider.class); |
| 275 | + Mockito.doThrow(new IOException(new AuthorizationException("p2"))).when(p2) |
| 276 | + .warmUpEncryptedKeys(Mockito.anyString()); |
| 277 | + |
| 278 | + when(p1.getKMSUrl()).thenReturn("p1"); |
| 279 | + when(p2.getKMSUrl()).thenReturn("p2"); |
| 280 | + |
| 281 | + LoadBalancingKMSClientProvider kp = new LoadBalancingKMSClientProvider( |
| 282 | + new KMSClientProvider[] {p1, p2}, 0, conf); |
| 283 | + try { |
| 284 | + kp.warmUpEncryptedKeys(keyName); |
| 285 | + fail("Should fail since both providers threw IOException"); |
| 286 | + } catch (Exception e) { |
| 287 | + assertTrue(e.getCause() instanceof IOException); |
| 288 | + } |
| 289 | + Mockito.verify(p1, Mockito.times(1)).warmUpEncryptedKeys(keyName); |
| 290 | + Mockito.verify(p2, Mockito.times(1)).warmUpEncryptedKeys(keyName); |
| 291 | + } |
| 292 | + |
| 293 | + /** |
| 294 | + * tests {@link LoadBalancingKMSClientProvider#warmUpEncryptedKeys(String...)} |
| 295 | + * error handling in case atleast one provider succeeds. |
| 296 | + * @throws Exception |
| 297 | + */ |
| 298 | + @Test |
| 299 | + public void testWarmUpEncryptedKeysWhenOneProviderSucceeds() |
| 300 | + throws Exception { |
| 301 | + Configuration conf = new Configuration(); |
| 302 | + KMSClientProvider p1 = mock(KMSClientProvider.class); |
| 303 | + String keyName = "key1"; |
| 304 | + Mockito.doThrow(new IOException(new AuthorizationException("p1"))).when(p1) |
| 305 | + .warmUpEncryptedKeys(Mockito.anyString()); |
| 306 | + KMSClientProvider p2 = mock(KMSClientProvider.class); |
| 307 | + Mockito.doNothing().when(p2) |
| 308 | + .warmUpEncryptedKeys(Mockito.anyString()); |
| 309 | + |
| 310 | + when(p1.getKMSUrl()).thenReturn("p1"); |
| 311 | + when(p2.getKMSUrl()).thenReturn("p2"); |
| 312 | + |
| 313 | + LoadBalancingKMSClientProvider kp = new LoadBalancingKMSClientProvider( |
| 314 | + new KMSClientProvider[] {p1, p2}, 0, conf); |
| 315 | + try { |
| 316 | + kp.warmUpEncryptedKeys(keyName); |
| 317 | + } catch (Exception e) { |
| 318 | + fail("Should not throw Exception since p2 doesn't throw Exception"); |
| 319 | + } |
| 320 | + Mockito.verify(p1, Mockito.times(1)).warmUpEncryptedKeys(keyName); |
| 321 | + Mockito.verify(p2, Mockito.times(1)).warmUpEncryptedKeys(keyName); |
| 322 | + } |
260 | 323 | }
|
0 commit comments