|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + * |
| 8 | + * Modifications Copyright OpenSearch Contributors. See |
| 9 | + * GitHub history for details. |
| 10 | + */ |
| 11 | + |
| 12 | +package org.opensearch.security.http; |
| 13 | + |
| 14 | +import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope; |
| 15 | +import org.apache.hc.core5.http.HttpStatus; |
| 16 | +import org.junit.ClassRule; |
| 17 | +import org.junit.Test; |
| 18 | +import org.junit.runner.RunWith; |
| 19 | +import org.opensearch.test.framework.TestIndex; |
| 20 | +import org.opensearch.test.framework.TestSecurityConfig; |
| 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 | + |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +import static org.junit.Assert.assertTrue; |
| 29 | +import static org.junit.Assert.assertNotNull; |
| 30 | +import static org.opensearch.security.support.ConfigConstants.SECURITY_SYSTEM_INDICES_ENABLED_KEY; |
| 31 | +import static org.opensearch.security.support.ConfigConstants.SECURITY_SYSTEM_INDICES_PERMISSIONS_ENABLED_KEY; |
| 32 | +import static org.opensearch.security.support.ConfigConstants.SECURITY_RESTAPI_ROLES_ENABLED; |
| 33 | +import static org.opensearch.security.support.ConfigConstants.SECURITY_SYSTEM_INDICES_KEY; |
| 34 | +import static org.opensearch.test.framework.TestSecurityConfig.AuthcDomain.AUTHC_HTTPBASIC_INTERNAL; |
| 35 | +import static org.opensearch.test.framework.TestSecurityConfig.Role.ALL_ACCESS; |
| 36 | + |
| 37 | +@RunWith(com.carrotsearch.randomizedtesting.RandomizedRunner.class) |
| 38 | +@ThreadLeakScope(ThreadLeakScope.Scope.NONE) |
| 39 | +public class ServiceAccountAuthenticationTest { |
| 40 | + |
| 41 | + public static final String SERVICE_ATTRIBUTE = "service"; |
| 42 | + |
| 43 | + static final TestSecurityConfig.User ADMIN_USER = new TestSecurityConfig.User("admin").roles(ALL_ACCESS); |
| 44 | + |
| 45 | + public static final String SERVICE_ACCOUNT_USER_NAME = "test-service-account"; |
| 46 | + |
| 47 | + static final TestSecurityConfig.User SERVICE_ACCOUNT_ADMIN_USER = new TestSecurityConfig.User(SERVICE_ACCOUNT_USER_NAME).attr( |
| 48 | + SERVICE_ATTRIBUTE, |
| 49 | + "true" |
| 50 | + ) |
| 51 | + .roles( |
| 52 | + new TestSecurityConfig.Role("test-service-account-role").clusterPermissions("*") |
| 53 | + .indexPermissions("*", "system:admin/system_index") |
| 54 | + .on("*") |
| 55 | + ); |
| 56 | + |
| 57 | + private static final TestIndex TEST_NON_SYS_INDEX = TestIndex.name("test-non-sys-index") |
| 58 | + .setting("index.number_of_shards", 1) |
| 59 | + .setting("index.number_of_replicas", 0) |
| 60 | + .build(); |
| 61 | + |
| 62 | + private static final TestIndex TEST_SYS_INDEX = TestIndex.name("test-sys-index") |
| 63 | + .setting("index.number_of_shards", 1) |
| 64 | + .setting("index.number_of_replicas", 0) |
| 65 | + .build(); |
| 66 | + |
| 67 | + @ClassRule |
| 68 | + public static final LocalCluster cluster = new LocalCluster.Builder().clusterManager(ClusterManager.SINGLENODE) |
| 69 | + .anonymousAuth(false) |
| 70 | + .users(ADMIN_USER, SERVICE_ACCOUNT_ADMIN_USER) |
| 71 | + .nodeSettings( |
| 72 | + Map.of( |
| 73 | + SECURITY_SYSTEM_INDICES_PERMISSIONS_ENABLED_KEY, |
| 74 | + true, |
| 75 | + SECURITY_SYSTEM_INDICES_ENABLED_KEY, |
| 76 | + true, |
| 77 | + SECURITY_RESTAPI_ROLES_ENABLED, |
| 78 | + List.of("user_admin__all_access"), |
| 79 | + SECURITY_SYSTEM_INDICES_KEY, |
| 80 | + List.of(TEST_SYS_INDEX.getName()) |
| 81 | + ) |
| 82 | + ) |
| 83 | + .authc(AUTHC_HTTPBASIC_INTERNAL) |
| 84 | + .indices(TEST_NON_SYS_INDEX, TEST_SYS_INDEX) |
| 85 | + .build(); |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testClusterHealthWithServiceAccountCred() { |
| 89 | + try (TestRestClient client = cluster.getRestClient(SERVICE_ACCOUNT_ADMIN_USER)) { |
| 90 | + client.confirmCorrectCredentials(SERVICE_ACCOUNT_USER_NAME); |
| 91 | + TestRestClient.HttpResponse response = client.get("_cluster/health"); |
| 92 | + response.assertStatusCode(HttpStatus.SC_FORBIDDEN); |
| 93 | + |
| 94 | + String responseBody = response.getBody(); |
| 95 | + |
| 96 | + assertNotNull("Response body should not be null", responseBody); |
| 97 | + assertTrue(responseBody.contains("\"type\":\"security_exception\"")); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testReadSysIndexWithServiceAccountCred() { |
| 103 | + try (TestRestClient client = cluster.getRestClient(SERVICE_ACCOUNT_ADMIN_USER)) { |
| 104 | + client.confirmCorrectCredentials(SERVICE_ACCOUNT_USER_NAME); |
| 105 | + TestRestClient.HttpResponse response = client.get(TEST_SYS_INDEX.getName()); |
| 106 | + response.assertStatusCode(HttpStatus.SC_OK); |
| 107 | + |
| 108 | + String responseBody = response.getBody(); |
| 109 | + |
| 110 | + assertNotNull("Response body should not be null", responseBody); |
| 111 | + assertTrue(responseBody.contains(TEST_SYS_INDEX.getName())); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testReadNonSysIndexWithServiceAccountCred() { |
| 117 | + try (TestRestClient client = cluster.getRestClient(SERVICE_ACCOUNT_ADMIN_USER)) { |
| 118 | + client.confirmCorrectCredentials(SERVICE_ACCOUNT_USER_NAME); |
| 119 | + TestRestClient.HttpResponse response = client.get(TEST_NON_SYS_INDEX.getName()); |
| 120 | + response.assertStatusCode(HttpStatus.SC_FORBIDDEN); |
| 121 | + |
| 122 | + String responseBody = response.getBody(); |
| 123 | + |
| 124 | + assertNotNull("Response body should not be null", responseBody); |
| 125 | + assertTrue(responseBody.contains("\"type\":\"security_exception\"")); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void testReadBothWithServiceAccountCred() { |
| 131 | + TestRestClient client = cluster.getRestClient(SERVICE_ACCOUNT_ADMIN_USER); |
| 132 | + client.confirmCorrectCredentials(SERVICE_ACCOUNT_USER_NAME); |
| 133 | + TestRestClient.HttpResponse response = client.get((TEST_SYS_INDEX.getName() + "," + TEST_NON_SYS_INDEX.getName())); |
| 134 | + response.assertStatusCode(HttpStatus.SC_FORBIDDEN); |
| 135 | + |
| 136 | + String responseBody = response.getBody(); |
| 137 | + |
| 138 | + assertNotNull("Response body should not be null", responseBody); |
| 139 | + assertTrue(responseBody.contains("\"type\":\"security_exception\"")); |
| 140 | + |
| 141 | + } |
| 142 | +} |
0 commit comments