|
28 | 28 | import org.opensearch.security.test.helper.file.FileHelper; |
29 | 29 | import org.opensearch.security.test.helper.rest.RestHelper.HttpResponse; |
30 | 30 |
|
| 31 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 32 | +import static org.hamcrest.Matchers.containsString; |
| 33 | +import static org.hamcrest.Matchers.equalTo; |
31 | 34 | import static org.opensearch.security.OpenSearchSecurityPlugin.PLUGINS_PREFIX; |
| 35 | +import static org.opensearch.security.dlic.rest.api.InternalUsersApiAction.RESTRICTED_FROM_USERNAME; |
| 36 | + |
32 | 37 |
|
33 | 38 | public class UserApiTest extends AbstractRestApiUnitTest { |
34 | 39 | private final String ENDPOINT; |
@@ -468,7 +473,7 @@ public void testPasswordRules() throws Exception { |
468 | 473 | addUserWithPassword("$1aAAAAAAAac", "$1aAAAAAAAAC", HttpStatus.SC_BAD_REQUEST); |
469 | 474 | addUserWithPassword(URLEncoder.encode("$1aAAAAAAAac%", "UTF-8"), "$1aAAAAAAAAC%", HttpStatus.SC_BAD_REQUEST); |
470 | 475 | addUserWithPassword(URLEncoder.encode("$1aAAAAAAAac%!=\"/\\;:test&~@^", "UTF-8").replace("+", "%2B"), "$1aAAAAAAAac%!=\\\"/\\\\;:test&~@^", HttpStatus.SC_BAD_REQUEST); |
471 | | - addUserWithPassword(URLEncoder.encode("$1aAAAAAAAac%!=\"/\\;: test&", "UTF-8"), "$1aAAAAAAAac%!=\\\"/\\\\;: test&123", HttpStatus.SC_CREATED); |
| 476 | + addUserWithPassword(URLEncoder.encode("$1aAAAAAAAac%!=\"/\\;: test&", "UTF-8"), "$1aAAAAAAAac%!=\\\"/\\\\;: test&123", HttpStatus.SC_BAD_REQUEST); |
472 | 477 |
|
473 | 478 | response = rh.executeGetRequest(PLUGINS_PREFIX + "/api/internalusers/nothinghthere?pretty", new Header[0]); |
474 | 479 | Assert.assertEquals(HttpStatus.SC_NOT_FOUND, response.getStatusCode()); |
@@ -624,7 +629,24 @@ public void testUserApiForNonSuperAdmin() throws Exception { |
624 | 629 | // Patch multiple hidden users |
625 | 630 | response = rh.executePatchRequest(ENDPOINT + "/internalusers", "[{ \"op\": \"add\", \"path\": \"/hide/description\", \"value\": \"foo\" }]", new Header[0]); |
626 | 631 | Assert.assertEquals(HttpStatus.SC_NOT_FOUND, response.getStatusCode()); |
| 632 | + } |
| 633 | + |
| 634 | + @Test |
| 635 | + public void restrictedUsernameContents() throws Exception { |
| 636 | + setup(); |
| 637 | + |
| 638 | + rh.keystore = "restapi/kirk-keystore.jks"; |
| 639 | + rh.sendAdminCertificate = true; |
| 640 | + |
| 641 | + RESTRICTED_FROM_USERNAME.stream().forEach(restrictedTerm -> { |
| 642 | + final String username = "nag" + restrictedTerm + "ilum"; |
| 643 | + final String url = ENDPOINT + "/internalusers/" + username; |
| 644 | + final String bodyWithDefaultPasswordHash = "{\"hash\": \"456\"}"; |
| 645 | + final HttpResponse response = rh.executePutRequest(url, bodyWithDefaultPasswordHash); |
627 | 646 |
|
| 647 | + assertThat("Expected " + username + " to be rejected", response.getStatusCode(), equalTo(HttpStatus.SC_BAD_REQUEST)); |
| 648 | + assertThat(response.getBody(), containsString(restrictedTerm)); |
| 649 | + }); |
628 | 650 | } |
629 | 651 |
|
630 | 652 | @Test |
|
0 commit comments