Skip to content

Commit 7789f9a

Browse files
committed
Show JSM in action
Signed-off-by: Craig Perkins <cwperx@amazon.com>
1 parent 81b9d9b commit 7789f9a

File tree

4 files changed

+148
-17
lines changed

4 files changed

+148
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
}

src/integrationTest/java/org/opensearch/security/systemindex/SystemIndexTests.java

-15
Original file line numberDiff line numberDiff line change
@@ -314,19 +314,4 @@ public void regularUserShouldGetNoResultsWhenSearchingSystemIndex() {
314314
assertThat(response1.getBody(), response1.getBody().contains("\"hits\":{\"total\":{\"value\":0,\"relation\":\"eq\"}"));
315315
}
316316
}
317-
318-
@Test
319-
public void testRunCode() {
320-
try (TestRestClient client = cluster.getRestClient(USER_ADMIN)) {
321-
// TODO write a test that calls POST /run-code with a simple System.out.println("Hello, world!")
322-
String javaCode = "System.out.println(\\\"Hello, world!\\\");";
323-
String requestBody = "{\"code\": \"" + javaCode + "\"}";
324-
325-
HttpResponse response = client.postJson("run-code", requestBody);
326-
327-
// Verify response
328-
assertThat(response.getStatusCode(), equalTo(RestStatus.OK.getStatus()));
329-
assertThat(response.getBody(), response.getBody().contains("\"acknowledged\":true"));
330-
}
331-
}
332317
}

src/integrationTest/java/org/opensearch/test/framework/cluster/LocalOpenSearchCluster.java

+19-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import java.net.InetAddress;
3434
import java.net.InetSocketAddress;
3535
import java.nio.file.Files;
36+
import java.security.AccessController;
37+
import java.security.PrivilegedAction;
3638
import java.util.ArrayList;
3739
import java.util.Collection;
3840
import java.util.Collections;
@@ -486,8 +488,23 @@ public CompletableFuture<Boolean> stop(long timeout, TimeUnit timeUnit) {
486488
running = false;
487489

488490
if (node != null) {
489-
node.close();
490-
boolean stopped = node.awaitClose(timeout, timeUnit);
491+
boolean stopped = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
492+
@Override
493+
public Boolean run() {
494+
try {
495+
node.close();
496+
} catch (IOException e) {
497+
System.out.println("caught e: " + e.getMessage());
498+
throw new RuntimeException(e);
499+
}
500+
try {
501+
return node.awaitClose(timeout, timeUnit);
502+
} catch (InterruptedException e) {
503+
System.out.println("caught InterruptedException e: " + e.getMessage());
504+
throw new RuntimeException(e);
505+
}
506+
}
507+
});
491508
node = null;
492509
return stopped;
493510
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
grant {
13+
permission org.opensearch.SpecialPermission;
14+
permission java.lang.RuntimePermission "modifyThread";
15+
permission javax.management.MBeanServerPermission "createMBeanServer";
16+
permission javax.management.MBeanPermission "*","*";
17+
//permission java.io.FilePermission "/Users/cwperx/Projects/opensearch/OpenSearch/build/distribution/local/opensearch-3.0.0-SNAPSHOT/config/opensearch.yml","read,write,delete,execute";
18+
permission java.io.FilePermission "/Users/cwperx/.sdkman/-","read,write,delete,execute";
19+
permission java.io.FilePermission "/Users/cwperx/.gradle/-","read,write,delete,execute";
20+
permission java.io.FilePermission "/Users/cwperx/.m2/repository/-","read,write,delete,execute";
21+
permission java.io.FilePermission "/Users/cwperx/Projects/opensearch/security/build/-","read,write,delete,execute";
22+
23+
permission javax.security.auth.AuthPermission "*";
24+
25+
permission java.util.PropertyPermission "*","read,write";
26+
27+
permission java.security.SecurityPermission "setProperty.ocsp.enable";
28+
29+
permission java.net.NetPermission "*";
30+
permission java.net.SocketPermission "*", "connect,accept,resolve";
31+
permission java.security.SecurityPermission "*";
32+
33+
permission java.lang.RuntimePermission "*";
34+
35+
// since the gradle test worker jar is on the test classpath, our tests should be able to read it
36+
permission java.io.FilePermission "${gradle.worker.jar}", "read";
37+
permission java.lang.reflect.ReflectPermission "*";
38+
permission org.opensearch.secure_sm.ThreadContextPermission "*";
39+
};

0 commit comments

Comments
 (0)