4242import java .io .InputStream ;
4343import java .io .InputStreamReader ;
4444import java .io .BufferedReader ;
45- import java .net .HttpURLConnection ;
46- import java .net .URL ;
4745import static org .junit .Assert .assertTrue ;
4846import static org .junit .Assert .assertEquals ;
4947import static org .junit .Assert .assertFalse ;
5048
5149
5250public class SystemdIT {
53- private static final String OPENSEARCH_URL = "http://localhost:9200" ; // OpenSearch URL (port 9200)
54- private static String containerId ;
51+
5552 private static String opensearchPid ;
56- private static final String CONTAINER_NAME = "opensearch-systemd-test-container" ;
5753
5854 @ BeforeClass
5955 public static void setup () throws IOException , InterruptedException {
60- containerId = getContainerId ();
61-
62- String status = executeCommand ("docker exec " + containerId + " systemctl status opensearch" , "Failed to check OpenSearch status" );
63-
6456 opensearchPid = getOpenSearchPid ();
6557
6658 if (opensearchPid .isEmpty ()) {
6759 throw new RuntimeException ("Failed to find OpenSearch process ID" );
6860 }
6961 }
7062
71- private static String getContainerId () throws IOException , InterruptedException {
72- return executeCommand ("docker ps -qf name=" + CONTAINER_NAME , "OpenSearch container '" + CONTAINER_NAME + "' is not running" );
73- }
74-
7563 private static String getOpenSearchPid () throws IOException , InterruptedException {
76- String command = "docker exec " + containerId + " systemctl show --property=MainPID opensearch" ;
64+ String command = "systemctl show --property=MainPID opensearch" ;
7765 String output = executeCommand (command , "Failed to get OpenSearch PID" );
7866 return output .replace ("MainPID=" , "" ).trim ();
7967 }
8068
8169 private boolean checkPathExists (String path ) throws IOException , InterruptedException {
82- String command = String .format ("docker exec %s test -e %s && echo true || echo false" , containerId , path );
70+ String command = String .format ("test -e %s && echo true || echo false" , path );
8371 return Boolean .parseBoolean (executeCommand (command , "Failed to check path existence" ));
8472 }
8573
8674 private boolean checkPathReadable (String path ) throws IOException , InterruptedException {
87- String command = String .format ("docker exec %s su opensearch -s /bin/sh -c 'test -r %s && echo true || echo false'" , containerId , path );
75+ String command = String .format ("su opensearch -s /bin/sh -c 'test -r %s && echo true || echo false'" , path );
8876 return Boolean .parseBoolean (executeCommand (command , "Failed to check read permission" ));
8977 }
9078
9179 private boolean checkPathWritable (String path ) throws IOException , InterruptedException {
92- String command = String .format ("docker exec %s su opensearch -s /bin/sh -c 'test -w %s && echo true || echo false'" , containerId , path );
80+ String command = String .format ("su opensearch -s /bin/sh -c 'test -w %s && echo true || echo false'" , path );
9381 return Boolean .parseBoolean (executeCommand (command , "Failed to check write permission" ));
9482 }
9583
9684 private String getPathOwnership (String path ) throws IOException , InterruptedException {
97- String command = String .format ("docker exec %s stat -c '%%U:%%G' %s" , containerId , path );
85+ String command = String .format ("stat -c '%%U:%%G' %s" , path );
9886 return executeCommand (command , "Failed to get path ownership" );
9987 }
10088
@@ -113,44 +101,6 @@ private static String executeCommand(String command, String errorMessage) throws
113101 }
114102 }
115103
116- @ Test
117- public void testClusterHealth () throws IOException {
118- HttpURLConnection healthCheck = (HttpURLConnection ) new URL (OPENSEARCH_URL + "/_cluster/health" ).openConnection ();
119- healthCheck .setRequestMethod ("GET" );
120- int healthResponseCode = healthCheck .getResponseCode ();
121- assertTrue (healthResponseCode == HttpURLConnection .HTTP_OK );
122- }
123-
124- @ Test
125- public void testMaxProcesses () throws IOException , InterruptedException {
126- String limits = executeCommand ("docker exec " + containerId + " cat /proc/" + opensearchPid + "/limits" , "Failed to read process limits" );
127- assertTrue ("Max processes limit should be 4096 or unlimited" ,
128- limits .contains ("Max processes 4096 4096" ) ||
129- limits .contains ("Max processes unlimited unlimited" ));
130- }
131-
132- @ Test
133- public void testFileDescriptorLimit () throws IOException , InterruptedException {
134- String limits = executeCommand ("docker exec " + containerId + " cat /proc/" + opensearchPid + "/limits" , "Failed to read process limits" );
135- assertTrue ("File descriptor limit should be at least 65535" ,
136- limits .contains ("Max open files 65535 65535" ) ||
137- limits .contains ("Max open files unlimited unlimited" ));
138- }
139-
140- @ Test
141- public void testSystemCallFilter () throws IOException , InterruptedException {
142- // Check if Seccomp is enabled
143- String seccomp = executeCommand ("docker exec " + containerId + " grep Seccomp /proc/" + opensearchPid + "/status" , "Failed to read Seccomp status" );
144- assertFalse ("Seccomp should be enabled" , seccomp .contains ("0" ));
145-
146- // Test specific system calls that should be blocked
147- String rebootResult = executeCommand ("docker exec " + containerId + " su opensearch -c 'kill -s SIGHUP 1' 2>&1 || echo 'Operation not permitted'" , "Failed to test reboot system call" );
148- assertTrue ("Reboot system call should be blocked" , rebootResult .contains ("Operation not permitted" ));
149-
150- String swapResult = executeCommand ("docker exec " + containerId + " su opensearch -c 'swapon -a' 2>&1 || echo 'Operation not permitted'" , "Failed to test swap system call" );
151- assertTrue ("Swap system call should be blocked" , swapResult .contains ("Operation not permitted" ));
152- }
153-
154104 @ Test
155105 public void testReadOnlyPaths () throws IOException , InterruptedException {
156106 String [] readOnlyPaths = {
@@ -180,56 +130,4 @@ public void testReadWritePaths() throws IOException, InterruptedException {
180130 }
181131 }
182132
183- @ Test
184- public void testOpensearchProcessCannotExit () throws IOException , InterruptedException {
185-
186- String scriptContent = "#!/bin/sh\n " +
187- "if [ $# -ne 1 ]; then\n " +
188- " echo \" Usage: $0 <PID>\" \n " +
189- " exit 1\n " +
190- "fi\n " +
191- "if kill -15 $1 2>/dev/null; then\n " +
192- " echo \" SIGTERM signal sent to process $1\" \n " +
193- "else\n " +
194- " echo \" Failed to send SIGTERM to process $1\" \n " +
195- "fi\n " +
196- "sleep 2\n " +
197- "if kill -0 $1 2>/dev/null; then\n " +
198- " echo \" Process $1 is still running\" \n " +
199- "else\n " +
200- " echo \" Process $1 has terminated\" \n " +
201- "fi" ;
202-
203- String [] command = {
204- "docker" ,
205- "exec" ,
206- "-u" , "testuser" ,
207- containerId ,
208- "sh" ,
209- "-c" ,
210- "echo '" + scriptContent .replace ("'" , "'\" '\" '" ) + "' > /tmp/terminate.sh && chmod +x /tmp/terminate.sh && /tmp/terminate.sh " + opensearchPid
211- };
212-
213- ProcessBuilder processBuilder = new ProcessBuilder (command );
214- Process process = processBuilder .start ();
215-
216- // Wait a moment for any potential termination to take effect
217- Thread .sleep (2000 );
218-
219- // Check if the OpenSearch process is still running
220- String processCheck = executeCommand (
221- "docker exec " + containerId + " kill -0 " + opensearchPid + " 2>/dev/null && echo 'Running' || echo 'Not running'" ,
222- "Failed to check process status"
223- );
224-
225- // Verify the OpenSearch service status
226- String serviceStatus = executeCommand (
227- "docker exec " + containerId + " systemctl is-active opensearch" ,
228- "Failed to check OpenSearch service status"
229- );
230-
231- assertTrue ("OpenSearch process should still be running" , processCheck .contains ("Running" ));
232- assertEquals ("OpenSearch service should be active" , "active" , serviceStatus .trim ());
233- }
234-
235133}
0 commit comments