Skip to content

Commit

Permalink
Add indices test case
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Liang <[email protected]>
  • Loading branch information
RyanL1997 committed Oct 28, 2023
1 parent 6d0a820 commit 8ecb683
Showing 1 changed file with 55 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.opensearch.test.framework.TestIndex;
import org.opensearch.test.framework.TestSecurityConfig;
import org.opensearch.test.framework.cluster.ClusterManager;
import org.opensearch.test.framework.cluster.LocalCluster;
Expand All @@ -38,6 +39,8 @@ public class ServiceAccountAuthenticationTest {

public static final String DEFAULT_PASSWORD = "secret";

public static final String SERVICE_ATTRIBUTE = "service";

static final TestSecurityConfig.User ADMIN_USER = new TestSecurityConfig.User("admin").roles(ALL_ACCESS);

public static final String SERVICE_ACCOUNT_USER_NAME = "admin-extension";
Expand All @@ -47,9 +50,20 @@ public class ServiceAccountAuthenticationTest {
.indexPermissions("*", "system:admin/system_index")
.on("*");

static final TestSecurityConfig.User SERVICE_ACCOUNT_ADMIN_USER = new TestSecurityConfig.User(SERVICE_ACCOUNT_USER_NAME).roles(
SERVICE_ACCOUNT_ADMIN_ROLE
).attr("service", true);
static final TestSecurityConfig.User SERVICE_ACCOUNT_ADMIN_USER = new TestSecurityConfig.User(SERVICE_ACCOUNT_USER_NAME).attr(
SERVICE_ATTRIBUTE,
"true"
).roles(SERVICE_ACCOUNT_ADMIN_ROLE);

private static final TestIndex TEST_NON_SYS_INDEX = TestIndex.name("test-non-sys-index")
.setting("index.number_of_shards", 1)
.setting("index.number_of_replicas", 0)
.build();

private static final TestIndex TEST_SYS_INDEX = TestIndex.name("test-sys-index")
.setting("index.number_of_shards", 1)
.setting("index.number_of_replicas", 0)
.build();

@ClassRule
public static final LocalCluster cluster = new LocalCluster.Builder().clusterManager(ClusterManager.SINGLENODE)
Expand All @@ -59,26 +73,18 @@ public class ServiceAccountAuthenticationTest {
Map.of(
SECURITY_SYSTEM_INDICES_PERMISSIONS_ENABLED_KEY,
true,
SECURITY_ALLOW_DEFAULT_INIT_SECURITYINDEX,
SECURITY_SYSTEM_INDICES_ENABLED_KEY,
true,
SECURITY_RESTAPI_ROLES_ENABLED,
List.of("user_admin__all_access")
List.of("user_admin__all_access"),
SECURITY_SYSTEM_INDICES_KEY,
List.of("test-sys-index")
)
)
.authc(AUTHC_HTTPBASIC_INTERNAL)
.indices(TEST_NON_SYS_INDEX, TEST_SYS_INDEX)
.build();

// TODO: REMOVE THIS DEBUGGING TEST CASE
@Test
public void testClusterHealthWithAdminCred() {
try (TestRestClient client = cluster.getRestClient("admin", DEFAULT_PASSWORD)) {
client.confirmCorrectCredentials("admin");
TestRestClient.HttpResponse response = client.get("_cluster/health");
response.assertStatusCode(HttpStatus.SC_OK);
System.out.println(response);
}
}

@Test
public void testClusterHealthWithServiceAccountCred() throws JsonProcessingException {
try (TestRestClient client = cluster.getRestClient("admin-extension", DEFAULT_PASSWORD)) {
Expand All @@ -95,4 +101,37 @@ public void testClusterHealthWithServiceAccountCred() throws JsonProcessingExcep
assertEquals("security_exception", typeField);
}
}

@Test
public void testReadSysIndexWithServiceAccountCred() {
try (TestRestClient client = cluster.getRestClient("admin-extension", DEFAULT_PASSWORD)) {
client.confirmCorrectCredentials("admin-extension");
TestRestClient.HttpResponse response = client.get("test-sys-index");
response.assertStatusCode(HttpStatus.SC_OK);
// TODO: REMOVE THIS AND PARSING/CHECKING THE RESPONSE
System.out.println(response);
}
}

@Test
public void testReadNonSysIndexWithServiceAccountCred() {
try (TestRestClient client = cluster.getRestClient("admin-extension", DEFAULT_PASSWORD)) {
client.confirmCorrectCredentials("admin-extension");
TestRestClient.HttpResponse response = client.get("test-non-sys-index");
response.assertStatusCode(HttpStatus.SC_FORBIDDEN);
// TODO: REMOVE THIS AND PARSING/CHECKING THE RESPONSE
System.out.println(response);
}
}

// TODO: REMOVE THIS DEBUGGING TEST CASE
@Test
public void testReadNonSysIndexWithAdminCred() {
try (TestRestClient client = cluster.getRestClient("admin", DEFAULT_PASSWORD)) {
client.confirmCorrectCredentials("admin");
TestRestClient.HttpResponse response = client.get("test-non-sys-index");
response.assertStatusCode(HttpStatus.SC_OK);
System.out.println(response);
}
}
}

0 comments on commit 8ecb683

Please sign in to comment.