Skip to content

Commit

Permalink
Adds tests for SecuritySettingsConfigurer class and modifies relevant…
Browse files Browse the repository at this point in the history
… classes and configuration

Signed-off-by: Darshit Chanpura <[email protected]>
  • Loading branch information
DarshitChanpura committed Nov 30, 2023
1 parent 8efedc7 commit 0c2759a
Show file tree
Hide file tree
Showing 6 changed files with 376 additions and 62 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ test {
jvmArgs += "-Xmx3072m"
if (JavaVersion.current() > JavaVersion.VERSION_1_8) {
jvmArgs += "--add-opens=java.base/java.io=ALL-UNNAMED"
jvmArgs += "--add-opens=java.base/java.util=ALL-UNNAMED"
}
retry {
failOnPassedAfterRetry = false
Expand Down Expand Up @@ -303,6 +304,7 @@ def setCommonTestConfig(Test task) {
task.jvmArgs += "-Xmx3072m"
if (JavaVersion.current() > JavaVersion.VERSION_1_8) {
task.jvmArgs += "--add-opens=java.base/java.io=ALL-UNNAMED"
task.jvmArgs += "--add-opens=java.base/java.util=ALL-UNNAMED"
}
task.retry {
failOnPassedAfterRetry = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public static void configureSecuritySettings() {
* Replaces the admin password in internal_users.yml with the custom or generated password
*/
static void updateAdminPassword() {

String initialAdminPassword = System.getenv("initialAdminPassword");
String ADMIN_PASSWORD_FILE_PATH = OPENSEARCH_CONF_DIR + "initialAdminPassword.txt";
String INTERNAL_USERS_FILE_PATH = OPENSEARCH_CONF_DIR + "opensearch-security" + File.separator + "internal_users.yml";
Expand Down Expand Up @@ -108,7 +107,7 @@ static void updateAdminPassword() {
writePasswordToInternalUsersFile(ADMIN_PASSWORD, INTERNAL_USERS_FILE_PATH);

} catch (IOException e) {
System.out.println("Exception: " + e.getMessage());
System.out.println("Exception updating the admin password : " + e.getMessage());
System.exit(-1);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.junit.Before;
import org.junit.Test;

import org.opensearch.security.test.SingleClusterTest;
import org.opensearch.security.tools.democonfig.util.NoExitSecurityManager;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
Expand Down Expand Up @@ -64,11 +64,14 @@
import static org.opensearch.security.tools.democonfig.Installer.setOpenSearchVariables;
import static org.opensearch.security.tools.democonfig.Installer.setSecurityVariables;
import static org.opensearch.security.tools.democonfig.Installer.skip_updates;
import static org.opensearch.security.tools.democonfig.util.DemoConfigHelperUtil.createDirectory;
import static org.opensearch.security.tools.democonfig.util.DemoConfigHelperUtil.createFile;
import static org.opensearch.security.tools.democonfig.util.DemoConfigHelperUtil.deleteDirectoryRecursive;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;

public class InstallerTests extends SingleClusterTest {
public class InstallerTests {
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final PrintStream originalOut = System.out;
private final InputStream originalIn = System.in;
Expand Down Expand Up @@ -489,50 +492,12 @@ public void setUpSecurityDirectories() {

public void tearDownSecurityDirectories() {
// Clean up testing directories or files
deleteFile(OPENSEARCH_PLUGINS_DIR + "opensearch-security" + File.separator + "opensearch-security-version.jar");
deleteFile(OPENSEARCH_LIB_PATH + "opensearch-osVersion.jar");
deleteDirectory(OPENSEARCH_PLUGINS_DIR + "opensearch-security");
deleteDirectory(OPENSEARCH_PLUGINS_DIR);
deleteDirectory(OPENSEARCH_LIB_PATH);
deleteFile(OPENSEARCH_CONF_DIR + File.separator + "securityadmin_demo.sh");
deleteDirectory(OPENSEARCH_CONF_DIR);
deleteDirectoryRecursive(OPENSEARCH_PLUGINS_DIR);
deleteDirectoryRecursive(OPENSEARCH_LIB_PATH);
deleteDirectoryRecursive(OPENSEARCH_CONF_DIR);
}

private void createDirectory(String path) {
File directory = new File(path);
if (!directory.exists() && !directory.mkdirs()) {
throw new RuntimeException("Failed to create directory: " + path);
}
}

private void createFile(String path) {
try {
File file = new File(path);
if (!file.exists() && !file.createNewFile()) {
throw new RuntimeException("Failed to create file: " + path);
}
} catch (Exception e) {
// without this the catch, we would need to throw exception,
// which would then require modifying caller method signature
throw new RuntimeException("Failed to create file: " + path, e);
}
}

private void deleteDirectory(String path) {
File directory = new File(path);
if (directory.exists() && !directory.delete()) {
throw new RuntimeException("Failed to delete directory: " + path);
}
}

private void deleteFile(String path) {
File file = new File(path);
if (file.exists() && !file.delete()) {
throw new RuntimeException("Failed to delete file: " + path);
}
}

private void setWritePermissions(String filePath) {
static void setWritePermissions(String filePath) {
if (!OS.toLowerCase().contains("win")) {
Path file = Paths.get(filePath);
Set<PosixFilePermission> perms = new HashSet<>();
Expand All @@ -544,19 +509,4 @@ private void setWritePermissions(String filePath) {
}
}
}

}

class NoExitSecurityManager extends SecurityManager {
@Override
public void checkPermission(java.security.Permission perm) {
// Allow everything except System.exit code 0 &b -1
if (perm instanceof java.lang.RuntimePermission && ("exitVM.0".equals(perm.getName()) || "exitVM.-1".equals(perm.getName()))) {
StringBuilder sb = new StringBuilder();
sb.append("System.exit(");
sb.append(perm.getName().contains("0") ? 0 : -1);
sb.append(") blocked to allow print statement testing.");
throw new SecurityException(sb.toString());
}
}
}
Loading

0 comments on commit 0c2759a

Please sign in to comment.