|
| 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.systemindex; |
| 11 | + |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope; |
| 16 | +import org.junit.ClassRule; |
| 17 | +import org.junit.Test; |
| 18 | +import org.junit.runner.RunWith; |
| 19 | + |
| 20 | +import org.opensearch.core.rest.RestStatus; |
| 21 | +import org.opensearch.security.systemindex.sampleplugin.SystemIndexPlugin1; |
| 22 | +import org.opensearch.security.systemindex.sampleplugin.SystemIndexPlugin2; |
| 23 | +import org.opensearch.test.framework.TestSecurityConfig.AuthcDomain; |
| 24 | +import org.opensearch.test.framework.cluster.ClusterManager; |
| 25 | +import org.opensearch.test.framework.cluster.LocalCluster; |
| 26 | +import org.opensearch.test.framework.cluster.TestRestClient; |
| 27 | +import org.opensearch.test.framework.cluster.TestRestClient.HttpResponse; |
| 28 | + |
| 29 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 30 | +import static org.hamcrest.Matchers.equalTo; |
| 31 | +import static org.opensearch.security.support.ConfigConstants.SECURITY_RESTAPI_ROLES_ENABLED; |
| 32 | +import static org.opensearch.security.support.ConfigConstants.SECURITY_SYSTEM_INDICES_ENABLED_KEY; |
| 33 | +import static org.opensearch.test.framework.TestSecurityConfig.Role.ALL_ACCESS; |
| 34 | +import static org.opensearch.test.framework.TestSecurityConfig.User.USER_ADMIN; |
| 35 | + |
| 36 | +@RunWith(com.carrotsearch.randomizedtesting.RandomizedRunner.class) |
| 37 | +@ThreadLeakScope(ThreadLeakScope.Scope.NONE) |
| 38 | +public class RunCodeTests { |
| 39 | + |
| 40 | + public static final AuthcDomain AUTHC_DOMAIN = new AuthcDomain("basic", 0).httpAuthenticatorWithChallenge("basic").backend("internal"); |
| 41 | + |
| 42 | + @ClassRule |
| 43 | + public static final LocalCluster cluster = new LocalCluster.Builder().clusterManager(ClusterManager.SINGLENODE) |
| 44 | + .anonymousAuth(false) |
| 45 | + .authc(AUTHC_DOMAIN) |
| 46 | + .users(USER_ADMIN) |
| 47 | + .plugin(SystemIndexPlugin1.class, SystemIndexPlugin2.class) |
| 48 | + .nodeSettings( |
| 49 | + Map.of( |
| 50 | + SECURITY_RESTAPI_ROLES_ENABLED, |
| 51 | + List.of("user_" + USER_ADMIN.getName() + "__" + ALL_ACCESS.getName()), |
| 52 | + SECURITY_SYSTEM_INDICES_ENABLED_KEY, |
| 53 | + true |
| 54 | + ) |
| 55 | + ) |
| 56 | + .build(); |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testRunCode() { |
| 60 | + // Define the policy file location |
| 61 | + String policyFile = "integration-test.policy"; |
| 62 | + |
| 63 | + // Set the system property for security policy |
| 64 | + System.setProperty("java.security.policy", RunCodeTests.class.getClassLoader().getResource(policyFile).getPath()); |
| 65 | + |
| 66 | + // Enable the Security Manager (Deprecated in Java 17+) |
| 67 | + System.setSecurityManager(new SecurityManager()); |
| 68 | + System.out.println("Security manager: " + System.getSecurityManager()); |
| 69 | + try (TestRestClient client = cluster.getRestClient(USER_ADMIN)) { |
| 70 | + // TODO write a test that calls POST /run-code with a simple System.out.println("Hello, world!") |
| 71 | + // String javaCode = "System.setProperty(\\\"test.key\\\",\\\"Hello, world!\\\");"; |
| 72 | + // String javaCode = "System.out.println(\\\"Hello, world!\\\");"; |
| 73 | + String javaCode = |
| 74 | + "java.nio.file.Path filePath = java.nio.file.Paths.get(\\\"/Users/cwperx/Projects/opensearch/OpenSearch/build/distribution/local/opensearch-3.0.0-SNAPSHOT/config/opensearch.yml\\\");" |
| 75 | + + "String content = new String(java.nio.file.Files.readAllBytes(filePath), java.nio.charset.StandardCharsets.UTF_8);" |
| 76 | + + "System.out.println(\\\"content: \\\" + content);"; |
| 77 | + String requestBody = "{\"code\": \"" + javaCode + "\"}"; |
| 78 | + |
| 79 | + System.out.println("Calling run-code"); |
| 80 | + |
| 81 | + HttpResponse response = client.postJson("run-code", requestBody); |
| 82 | + |
| 83 | + System.out.println("Finished run-code"); |
| 84 | + |
| 85 | + // Verify response |
| 86 | + assertThat(response.getStatusCode(), equalTo(RestStatus.OK.getStatus())); |
| 87 | + assertThat(response.getBody(), response.getBody().contains("\"acknowledged\":true")); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments