|
| 1 | +/* |
| 2 | +* Copyright OpenSearch Contributors |
| 3 | +* SPDX-License-Identifier: Apache-2.0 |
| 4 | +* |
| 5 | +* The OpenSearch Contributors require contributions made to |
| 6 | +* this file be licensed under the Apache-2.0 license or a |
| 7 | +* compatible open source license. |
| 8 | +* |
| 9 | +*/ |
| 10 | +package org.opensearch.security.http; |
| 11 | + |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope; |
| 15 | +import org.junit.ClassRule; |
| 16 | +import org.junit.Test; |
| 17 | +import org.junit.runner.RunWith; |
| 18 | + |
| 19 | +import org.opensearch.test.framework.TestSecurityConfig.AuthcDomain; |
| 20 | +import org.opensearch.test.framework.TestSecurityConfig.User; |
| 21 | +import org.opensearch.test.framework.cluster.ClusterManager; |
| 22 | +import org.opensearch.test.framework.cluster.LocalCluster; |
| 23 | +import org.opensearch.test.framework.cluster.TestRestClient; |
| 24 | +import org.opensearch.test.framework.cluster.TestRestClient.HttpResponse; |
| 25 | + |
| 26 | +import static org.apache.http.HttpStatus.SC_OK; |
| 27 | +import static org.apache.http.HttpStatus.SC_UNAUTHORIZED; |
| 28 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 29 | +import static org.hamcrest.Matchers.is; |
| 30 | +import static org.hamcrest.Matchers.notNullValue; |
| 31 | + |
| 32 | +@RunWith(com.carrotsearch.randomizedtesting.RandomizedRunner.class) |
| 33 | +@ThreadLeakScope(ThreadLeakScope.Scope.NONE) |
| 34 | +public class BasicWithAnonymousAuthTests { |
| 35 | + static final User TEST_USER = new User("test_user").password("s3cret"); |
| 36 | + |
| 37 | + public static final String CUSTOM_ATTRIBUTE_NAME = "superhero"; |
| 38 | + static final User SUPER_USER = new User("super-user").password("super-password").attr(CUSTOM_ATTRIBUTE_NAME, "true"); |
| 39 | + public static final String NOT_EXISTING_USER = "not-existing-user"; |
| 40 | + public static final String INVALID_PASSWORD = "secret-password"; |
| 41 | + |
| 42 | + public static final AuthcDomain AUTHC_DOMAIN = new AuthcDomain("basic", 0).httpAuthenticatorWithChallenge("basic").backend("internal"); |
| 43 | + |
| 44 | + @ClassRule |
| 45 | + public static final LocalCluster cluster = new LocalCluster.Builder().clusterManager(ClusterManager.SINGLENODE) |
| 46 | + .anonymousAuth(true) |
| 47 | + .authc(AUTHC_DOMAIN) |
| 48 | + .users(TEST_USER, SUPER_USER) |
| 49 | + .build(); |
| 50 | + |
| 51 | + /** No automatic login post anonymous auth request **/ |
| 52 | + @Test |
| 53 | + public void testShouldRespondWith401WhenUserDoesNotExist() { |
| 54 | + try (TestRestClient client = cluster.getRestClient(NOT_EXISTING_USER, INVALID_PASSWORD)) { |
| 55 | + HttpResponse response = client.getAuthInfo(); |
| 56 | + |
| 57 | + assertThat(response, is(notNullValue())); |
| 58 | + response.assertStatusCode(SC_UNAUTHORIZED); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testShouldRespondWith401WhenUserNameIsIncorrect() { |
| 64 | + try (TestRestClient client = cluster.getRestClient(NOT_EXISTING_USER, TEST_USER.getPassword())) { |
| 65 | + HttpResponse response = client.getAuthInfo(); |
| 66 | + |
| 67 | + assertThat(response, is(notNullValue())); |
| 68 | + response.assertStatusCode(SC_UNAUTHORIZED); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testShouldRespondWith401WhenPasswordIsIncorrect() { |
| 74 | + try (TestRestClient client = cluster.getRestClient(TEST_USER.getName(), INVALID_PASSWORD)) { |
| 75 | + HttpResponse response = client.getAuthInfo(); |
| 76 | + |
| 77 | + assertThat(response, is(notNullValue())); |
| 78 | + response.assertStatusCode(SC_UNAUTHORIZED); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** Test `?auth_type=""` param to authinfo request **/ |
| 83 | + @Test |
| 84 | + public void testShouldAutomaticallyLoginAsAnonymousIfNoCredentialsArePassed() { |
| 85 | + try (TestRestClient client = cluster.getRestClient()) { |
| 86 | + |
| 87 | + HttpResponse response = client.getAuthInfo(); |
| 88 | + |
| 89 | + assertThat(response, is(notNullValue())); |
| 90 | + response.assertStatusCode(SC_OK); |
| 91 | + |
| 92 | + HttpResponse response2 = client.getAuthInfo(Map.of("auth_type", "anonymous")); |
| 93 | + |
| 94 | + assertThat(response2, is(notNullValue())); |
| 95 | + response2.assertStatusCode(SC_OK); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testShouldNotAutomaticallyLoginAsAnonymousIfRequestIsNonAnonymousLogin() { |
| 101 | + try (TestRestClient client = cluster.getRestClient()) { |
| 102 | + |
| 103 | + HttpResponse response = client.getAuthInfo(Map.of("auth_type", "saml")); |
| 104 | + |
| 105 | + assertThat(response, is(notNullValue())); |
| 106 | + response.assertStatusCode(SC_UNAUTHORIZED); |
| 107 | + |
| 108 | + // should contain a redirect link |
| 109 | + assertThat(response.containHeader("WWW-Authenticate"), is(true)); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments