|
32 | 32 | import org.apache.hadoop.conf.Configuration; |
33 | 33 | import org.apache.hadoop.fs.CommonConfigurationKeysPublic; |
34 | 34 | import org.apache.hadoop.io.DataInputByteBuffer; |
| 35 | +import org.apache.hadoop.security.AccessControlException; |
35 | 36 | import org.apache.hadoop.security.Credentials; |
36 | 37 | import org.apache.hadoop.security.SecurityUtil; |
37 | 38 | import org.apache.hadoop.security.UserGroupInformation; |
@@ -272,21 +273,62 @@ public ApplicationMasterProtocol run() { |
272 | 273 | client.registerApplicationMaster(request); |
273 | 274 | Assert.fail("Should fail with authorization error"); |
274 | 275 | } catch (Exception e) { |
275 | | - // Because there are no tokens, the request should be rejected as the |
276 | | - // server side will assume we are trying simple auth. |
277 | | - String expectedMessage = ""; |
278 | | - if (UserGroupInformation.isSecurityEnabled()) { |
279 | | - expectedMessage = "Client cannot authenticate via:[TOKEN]"; |
| 276 | + if (isCause(AccessControlException.class, e)) { |
| 277 | + // Because there are no tokens, the request should be rejected as the |
| 278 | + // server side will assume we are trying simple auth. |
| 279 | + String expectedMessage = ""; |
| 280 | + if (UserGroupInformation.isSecurityEnabled()) { |
| 281 | + expectedMessage = "Client cannot authenticate via:[TOKEN]"; |
| 282 | + } else { |
| 283 | + expectedMessage = |
| 284 | + "SIMPLE authentication is not enabled. Available:[TOKEN]"; |
| 285 | + } |
| 286 | + Assert.assertTrue(e.getCause().getMessage().contains(expectedMessage)); |
280 | 287 | } else { |
281 | | - expectedMessage = |
282 | | - "SIMPLE authentication is not enabled. Available:[TOKEN]"; |
| 288 | + throw e; |
283 | 289 | } |
284 | | - Assert.assertTrue(e.getCause().getMessage().contains(expectedMessage)); |
285 | 290 | } |
286 | 291 |
|
287 | 292 | // TODO: Add validation of invalid authorization when there's more data in |
288 | 293 | // the AMRMToken |
289 | 294 | } |
| 295 | + |
| 296 | + /** |
| 297 | + * Identify if an expected throwable included in an exception stack. We use |
| 298 | + * this because sometimes, an exception will be wrapped to another exception |
| 299 | + * before thrown. Like, |
| 300 | + * |
| 301 | + * <pre> |
| 302 | + * {@code |
| 303 | + * void methodA() throws IOException { |
| 304 | + * try { |
| 305 | + * // something |
| 306 | + * } catch (AccessControlException e) { |
| 307 | + * // do process |
| 308 | + * throw new IOException(e) |
| 309 | + * } |
| 310 | + * } |
| 311 | + * </pre> |
| 312 | + * |
| 313 | + * So we cannot simply catch AccessControlException by using |
| 314 | + * <pre> |
| 315 | + * {@code |
| 316 | + * try { |
| 317 | + * methodA() |
| 318 | + * } catch (AccessControlException e) { |
| 319 | + * // do something |
| 320 | + * } |
| 321 | + * </pre> |
| 322 | + * |
| 323 | + * This method is useful in such cases. |
| 324 | + */ |
| 325 | + private static boolean isCause( |
| 326 | + Class<? extends Throwable> expected, |
| 327 | + Throwable e |
| 328 | + ) { |
| 329 | + return (e != null) |
| 330 | + && (expected.isInstance(e) || isCause(expected, e.getCause())); |
| 331 | + } |
290 | 332 |
|
291 | 333 | private void waitForLaunchedState(RMAppAttempt attempt) |
292 | 334 | throws InterruptedException { |
|
0 commit comments