|
34 | 34 | import org.apache.cloudstack.api.command.admin.config.ListCfgsByCmd; |
35 | 35 | import org.apache.cloudstack.framework.config.ConfigKey; |
36 | 36 | import org.apache.cloudstack.framework.config.impl.ConfigDepotImpl; |
| 37 | +import org.apache.logging.log4j.Logger; |
37 | 38 | import org.junit.After; |
38 | 39 | import org.junit.Assert; |
39 | 40 | import org.junit.Before; |
40 | 41 | import org.junit.Test; |
41 | 42 | import org.junit.runner.RunWith; |
| 43 | +import org.mockito.ArgumentCaptor; |
42 | 44 | import org.mockito.Mock; |
43 | 45 | import org.mockito.Mockito; |
44 | 46 | import org.mockito.junit.MockitoJUnitRunner; |
@@ -463,26 +465,81 @@ public void testVerify2FAWhenExpectedCommandIsNotCalled() throws UnknownHostExce |
463 | 465 | } |
464 | 466 |
|
465 | 467 | @Test |
466 | | - public void shouldNotLogPostRequestParametersForAddObjectStoragePool() { |
467 | | - boolean result = servlet.shouldLogPostRequestParameters("addObjectStoragePool", new HashMap<>()); |
| 468 | + public void shouldNotLogRequestParametersForAddObjectStoragePool() { |
| 469 | + boolean result = servlet.shouldLogRequestParameters("addObjectStoragePool", new HashMap<>()); |
468 | 470 |
|
469 | 471 | Assert.assertFalse(result); |
470 | 472 | } |
471 | 473 |
|
472 | 474 | @Test |
473 | | - public void shouldLogPostRequestParametersForCommandWithoutSensitiveParameters() { |
474 | | - boolean result = servlet.shouldLogPostRequestParameters("listZones", new HashMap<>()); |
| 475 | + public void shouldLogRequestParametersForCommandWithoutSensitiveParameters() { |
| 476 | + boolean result = servlet.shouldLogRequestParameters("listZones", new HashMap<>()); |
475 | 477 |
|
476 | 478 | Assert.assertTrue(result); |
477 | 479 | } |
478 | 480 |
|
479 | 481 | @Test |
480 | | - public void shouldNotLogPostRequestParametersContainingUserData() { |
| 482 | + public void shouldNotLogRequestParametersContainingUserData() { |
481 | 483 | Map<String, String[]> params = new HashMap<>(); |
482 | 484 | params.put(ApiConstants.USER_DATA, new String[] {"sensitive-user-data"}); |
483 | 485 |
|
484 | | - boolean result = servlet.shouldLogPostRequestParameters("deployVirtualMachine", params); |
| 486 | + boolean result = servlet.shouldLogRequestParameters("deployVirtualMachine", params); |
485 | 487 |
|
486 | 488 | Assert.assertFalse(result); |
487 | 489 | } |
| 490 | + |
| 491 | + @Test |
| 492 | + public void shouldReplaceQueryStringContainingUserDataWithCommandName() { |
| 493 | + Map<String, String[]> params = new HashMap<>(); |
| 494 | + params.put(ApiConstants.USER_DATA, new String[] {"SYNTHETIC_USER_DATA"}); |
| 495 | + String queryString = "command=deployVirtualMachine&userdata=SYNTHETIC_USER_DATA"; |
| 496 | + |
| 497 | + String result = servlet.getCleanQueryString("deployVirtualMachine", queryString, params); |
| 498 | + |
| 499 | + Assert.assertEquals("command=deployVirtualMachine", result); |
| 500 | + Assert.assertFalse(result.contains("SYNTHETIC_USER_DATA")); |
| 501 | + } |
| 502 | + |
| 503 | + @Test |
| 504 | + public void shouldReplaceSensitiveQueryStringWithCommandName() { |
| 505 | + Map<String, String[]> params = new HashMap<>(); |
| 506 | + String queryString = "command=addObjectStoragePool&details%5B1%5D.value=SYNTHETIC_SECRET_KEY"; |
| 507 | + |
| 508 | + String result = servlet.getCleanQueryString("addObjectStoragePool", queryString, params); |
| 509 | + |
| 510 | + Assert.assertEquals("command=addObjectStoragePool", result); |
| 511 | + Assert.assertFalse(result.contains("SYNTHETIC_SECRET_KEY")); |
| 512 | + } |
| 513 | + |
| 514 | + @Test |
| 515 | + public void shouldKeepOrdinaryQueryString() { |
| 516 | + Map<String, String[]> params = new HashMap<>(); |
| 517 | + String queryString = "command=listZones&response=json"; |
| 518 | + |
| 519 | + String result = servlet.getCleanQueryString("listZones", queryString, params); |
| 520 | + |
| 521 | + Assert.assertEquals(queryString, result); |
| 522 | + } |
| 523 | + |
| 524 | + @Test |
| 525 | + public void shouldLogDuplicateParameterNameAndCountWithoutValues() { |
| 526 | + Logger originalLogger = ApiServlet.LOGGER; |
| 527 | + Logger logger = Mockito.mock(Logger.class); |
| 528 | + ApiServlet.LOGGER = logger; |
| 529 | + Map<String, String[]> params = new HashMap<>(); |
| 530 | + params.put("details[1].value", new String[] {"SYNTHETIC_SECRET_ONE", "SYNTHETIC_SECRET_TWO"}); |
| 531 | + |
| 532 | + try { |
| 533 | + servlet.checkSingleQueryParameterValue(params); |
| 534 | + |
| 535 | + ArgumentCaptor<String> message = ArgumentCaptor.forClass(String.class); |
| 536 | + Mockito.verify(logger).warn(message.capture()); |
| 537 | + Assert.assertTrue(message.getValue().contains("details[1].value")); |
| 538 | + Assert.assertTrue(message.getValue().contains("2 values")); |
| 539 | + Assert.assertFalse(message.getValue().contains("SYNTHETIC_SECRET_ONE")); |
| 540 | + Assert.assertFalse(message.getValue().contains("SYNTHETIC_SECRET_TWO")); |
| 541 | + } finally { |
| 542 | + ApiServlet.LOGGER = originalLogger; |
| 543 | + } |
| 544 | + } |
488 | 545 | } |
0 commit comments