From fb9768d6e5b21411974558a4d13781ba747cfb1f Mon Sep 17 00:00:00 2001 From: Darshit Chanpura Date: Thu, 30 Nov 2023 14:40:21 -0500 Subject: [PATCH] Upddates line end characters to use System.lineSeparator() Signed-off-by: Darshit Chanpura --- .../tools/democonfig/InstallerTests.java | 70 +++++++++---------- .../SecuritySettingsConfigurerTests.java | 54 +++++++------- 2 files changed, 62 insertions(+), 62 deletions(-) diff --git a/src/test/java/org/opensearch/security/tools/democonfig/InstallerTests.java b/src/test/java/org/opensearch/security/tools/democonfig/InstallerTests.java index 427d17d988..54de766fe6 100644 --- a/src/test/java/org/opensearch/security/tools/democonfig/InstallerTests.java +++ b/src/test/java/org/opensearch/security/tools/democonfig/InstallerTests.java @@ -92,8 +92,8 @@ public void restoreStreams() { public void testPrintScriptHeaders() { printScriptHeaders(); - String expectedOutput = "### OpenSearch Security Demo Installer\n" - + "### ** Warning: Do not use on production or public reachable systems **\n"; + String expectedOutput = "### OpenSearch Security Demo Installer" + System.lineSeparator() + + "### ** Warning: Do not use on production or public reachable systems **" + System.lineSeparator(); assertThat(expectedOutput, equalTo(outContent.toString())); } @@ -144,13 +144,13 @@ public void testGatherUserInputs_withoutAssumeYes() { assertThat(cluster_mode, is(false)); // set initsecurity and cluster_mode to no - readInputStream("y\nn\nn\n"); // pass all 3 inputs as y + readInputStream("y\nn\nn" + System.lineSeparator()); // pass all 3 inputs as y gatherUserInputs(); assertThat(outContent.toString(), containsString("Install demo certificates?")); assertThat(outContent.toString(), containsString("Initialize Security Modules?")); assertThat(outContent.toString(), containsString("Cluster mode requires additional setup of:")); - assertThat(outContent.toString(), containsString(" - Virtual memory (vm.max_map_count)\n")); + assertThat(outContent.toString(), containsString(" - Virtual memory (vm.max_map_count)" + System.lineSeparator())); assertThat(outContent.toString(), containsString("Enable cluster mode?")); assertThat(initsecurity, is(false)); @@ -159,13 +159,13 @@ public void testGatherUserInputs_withoutAssumeYes() { outContent.reset(); // set initsecurity and cluster_mode to no - readInputStream("y\ny\ny\n"); // pass all 3 inputs as y + readInputStream("y\ny\ny" + System.lineSeparator()); // pass all 3 inputs as y gatherUserInputs(); assertThat(outContent.toString(), containsString("Install demo certificates?")); assertThat(outContent.toString(), containsString("Initialize Security Modules?")); assertThat(outContent.toString(), containsString("Cluster mode requires additional setup of:")); - assertThat(outContent.toString(), containsString(" - Virtual memory (vm.max_map_count)\n")); + assertThat(outContent.toString(), containsString(" - Virtual memory (vm.max_map_count)" + System.lineSeparator())); assertThat(outContent.toString(), containsString("Enable cluster mode?")); assertThat(initsecurity, is(true)); @@ -177,13 +177,13 @@ public void testGatherUserInputs_withoutAssumeYes() { try { System.setSecurityManager(new NoExitSecurityManager()); - readInputStream("n\nn\nn\n"); + readInputStream("n\nn\nn" + System.lineSeparator()); gatherUserInputs(); assertThat(outContent.toString(), containsString("Install demo certificates?")); assertThat(outContent.toString(), not(containsString("Initialize Security Modules?"))); assertThat(outContent.toString(), not(containsString("Cluster mode requires additional setup of:"))); - assertThat(outContent.toString(), not(containsString(" - Virtual memory (vm.max_map_count)\n"))); + assertThat(outContent.toString(), not(containsString(" - Virtual memory (vm.max_map_count)" + System.lineSeparator()))); assertThat(outContent.toString(), not(containsString("Enable cluster mode?"))); } catch (SecurityException e) { assertThat(e.getMessage(), equalTo("System.exit(0) blocked to allow print statement testing.")); @@ -200,7 +200,7 @@ public void testGatherUserInputs_withoutAssumeYes() { assertThat(initsecurity, is(true)); assertThat(cluster_mode, is(true)); - readInputStream("y\ny\ny\n"); // pass all 3 inputs as y + readInputStream("y\ny\ny" + System.lineSeparator()); // pass all 3 inputs as y gatherUserInputs(); assertThat(outContent.toString(), containsString("Install demo certificates?")); @@ -369,14 +369,14 @@ public void testPrintVariables() { printVariables(); - String expectedOutput = "OpenSearch install type: installType on OS\n" - + "OpenSearch config dir: confDir\n" - + "OpenSearch config file: confFile\n" - + "OpenSearch bin dir: /bin\n" - + "OpenSearch plugins dir: /plugins\n" - + "OpenSearch lib dir: /lib\n" - + "Detected OpenSearch Version: osVersion\n" - + "Detected OpenSearch Security Version: version\n"; + String expectedOutput = "OpenSearch install type: installType on OS" + System.lineSeparator() + + "OpenSearch config dir: confDir" + System.lineSeparator() + + "OpenSearch config file: confFile" + System.lineSeparator() + + "OpenSearch bin dir: /bin" + System.lineSeparator() + + "OpenSearch plugins dir: /plugins" + System.lineSeparator() + + "OpenSearch lib dir: /lib" + System.lineSeparator() + + "Detected OpenSearch Version: osVersion" + System.lineSeparator() + + "Detected OpenSearch Security Version: version" + System.lineSeparator(); assertEquals(expectedOutput, outContent.toString()); } @@ -400,19 +400,19 @@ public void testFinishScriptExecution() { String lastLine = SecuritySettingsConfigurer.getSecurityAdminCommands(securityAdminScriptPath)[1]; // Verify the expected output - String expectedOutput = "### Success\n" - + "### Execute this script now on all your nodes and then start all nodes\n" - + "### After the whole cluster is up execute: \n" + String expectedOutput = "### Success" + System.lineSeparator() + + "### Execute this script now on all your nodes and then start all nodes" + System.lineSeparator() + + "### After the whole cluster is up execute: " + System.lineSeparator() + lastLine - + "\n" + + "" + System.lineSeparator() + "### or run ." + File.separator - + "securityadmin_demo.sh\n" - + "### After that you can also use the Security Plugin ConfigurationGUI\n" + + "securityadmin_demo.sh" + System.lineSeparator() + + "### After that you can also use the Security Plugin ConfigurationGUI" + System.lineSeparator() + "### To access your secured cluster open https://: and log in with admin/" + SecuritySettingsConfigurer.ADMIN_PASSWORD - + ".\n" - + "### (Ignore the SSL certificate warning because we installed self-signed demo certificates)\n"; + + "." + System.lineSeparator() + + "### (Ignore the SSL certificate warning because we installed self-signed demo certificates)" + System.lineSeparator(); assertEquals(expectedOutput, outContent.toString()); @@ -438,10 +438,10 @@ public void testFinishScriptExecution_withInitSecurityEnabled() { setWritePermissions(securityAdminDemoScriptPath); String lastLine = SecuritySettingsConfigurer.getSecurityAdminCommands(securityAdminScriptPath)[1]; - String expectedOutput = "### Success\n" - + "### Execute this script now on all your nodes and then start all nodes\n" - + "### OpenSearch Security will be automatically initialized.\n" - + "### If you like to change the runtime configuration \n" + String expectedOutput = "### Success" + System.lineSeparator() + + "### Execute this script now on all your nodes and then start all nodes" + System.lineSeparator() + + "### OpenSearch Security will be automatically initialized." + System.lineSeparator() + + "### If you like to change the runtime configuration " + System.lineSeparator() + "### change the files in .." + File.separator + ".." @@ -450,17 +450,17 @@ public void testFinishScriptExecution_withInitSecurityEnabled() { + File.separator + "config" + File.separator - + "opensearch-security and execute: \n" + + "opensearch-security and execute: " + System.lineSeparator() + lastLine - + "\n" + + "" + System.lineSeparator() + "### or run ." + File.separator - + "securityadmin_demo.sh\n" - + "### To use the Security Plugin ConfigurationGUI\n" + + "securityadmin_demo.sh" + System.lineSeparator() + + "### To use the Security Plugin ConfigurationGUI" + System.lineSeparator() + "### To access your secured cluster open https://: and log in with admin/" + SecuritySettingsConfigurer.ADMIN_PASSWORD - + ".\n" - + "### (Ignore the SSL certificate warning because we installed self-signed demo certificates)\n"; + + "." + System.lineSeparator() + + "### (Ignore the SSL certificate warning because we installed self-signed demo certificates)" + System.lineSeparator(); assertEquals(expectedOutput, outContent.toString()); diff --git a/src/test/java/org/opensearch/security/tools/democonfig/SecuritySettingsConfigurerTests.java b/src/test/java/org/opensearch/security/tools/democonfig/SecuritySettingsConfigurerTests.java index 89cbc096fe..707e63b276 100644 --- a/src/test/java/org/opensearch/security/tools/democonfig/SecuritySettingsConfigurerTests.java +++ b/src/test/java/org/opensearch/security/tools/democonfig/SecuritySettingsConfigurerTests.java @@ -188,40 +188,40 @@ public void testConfigFileDoesNotExist() { public void testBuildSecurityConfigString() { String actual = SecuritySettingsConfigurer.buildSecurityConfigString(); - String expected = "\n" - + "######## Start OpenSearch Security Demo Configuration ########\n" - + "# WARNING: revise all the lines below before you go into production\n" - + "plugins.security.ssl.transport.pemcert_filepath: esnode.pem\n" - + "plugins.security.ssl.transport.pemkey_filepath: esnode-key.pem\n" - + "plugins.security.ssl.transport.pemtrustedcas_filepath: root-ca.pem\n" - + "plugins.security.ssl.transport.enforce_hostname_verification: false\n" - + "plugins.security.ssl.http.enabled: true\n" - + "plugins.security.ssl.http.pemcert_filepath: esnode.pem\n" - + "plugins.security.ssl.http.pemkey_filepath: esnode-key.pem\n" - + "plugins.security.ssl.http.pemtrustedcas_filepath: root-ca.pem\n" - + "plugins.security.allow_unsafe_democertificates: true\n" - + "plugins.security.authcz.admin_dn:\n" - + " - CN=kirk,OU=client,O=client,L=test, C=de\n" - + "\n" - + "plugins.security.audit.type: internal_opensearch\n" - + "plugins.security.enable_snapshot_restore_privilege: true\n" - + "plugins.security.check_snapshot_restore_write_privileges: true\n" - + "plugins.security.restapi.roles_enabled: [\"all_access\", \"security_rest_api_access\"]\n" - + "plugins.security.system_indices.enabled: true\n" - + "plugins.security.system_indices.indices: [.plugins-ml-config, .plugins-ml-connector, .plugins-ml-model-group, .plugins-ml-model, .plugins-ml-task, .plugins-ml-conversation-meta, .plugins-ml-conversation-interactions, .opendistro-alerting-config, .opendistro-alerting-alert*, .opendistro-anomaly-results*, .opendistro-anomaly-detector*, .opendistro-anomaly-checkpoints, .opendistro-anomaly-detection-state, .opendistro-reports-*, .opensearch-notifications-*, .opensearch-notebooks, .opensearch-observability, .ql-datasources, .opendistro-asynchronous-search-response*, .replication-metadata-store, .opensearch-knn-models, .geospatial-ip2geo-data*]\n" - + "node.max_local_storage_nodes: 3\n" - + "######## End OpenSearch Security Demo Configuration ########\n"; + String expected = "" + System.lineSeparator() + + "######## Start OpenSearch Security Demo Configuration ########" + System.lineSeparator() + + "# WARNING: revise all the lines below before you go into productionSystem.lineSeparator()" + + "plugins.security.ssl.transport.pemcert_filepath: esnode.pem" + System.lineSeparator() + + "plugins.security.ssl.transport.pemkey_filepath: esnode-key.pem" + System.lineSeparator() + + "plugins.security.ssl.transport.pemtrustedcas_filepath: root-ca.pem" + System.lineSeparator() + + "plugins.security.ssl.transport.enforce_hostname_verification: false" + System.lineSeparator() + + "plugins.security.ssl.http.enabled: true" + System.lineSeparator() + + "plugins.security.ssl.http.pemcert_filepath: esnode.pem" + System.lineSeparator() + + "plugins.security.ssl.http.pemkey_filepath: esnode-key.pem" + System.lineSeparator() + + "plugins.security.ssl.http.pemtrustedcas_filepath: root-ca.pem" + System.lineSeparator() + + "plugins.security.allow_unsafe_democertificates: true" + System.lineSeparator() + + "plugins.security.authcz.admin_dn:" + System.lineSeparator() + + " - CN=kirk,OU=client,O=client,L=test, C=de" + System.lineSeparator() + + "" + System.lineSeparator() + + "plugins.security.audit.type: internal_opensearch" + System.lineSeparator() + + "plugins.security.enable_snapshot_restore_privilege: true" + System.lineSeparator() + + "plugins.security.check_snapshot_restore_write_privileges: true" + System.lineSeparator() + + "plugins.security.restapi.roles_enabled: [\"all_access\", \"security_rest_api_access\"]" + System.lineSeparator() + + "plugins.security.system_indices.enabled: true" + System.lineSeparator() + + "plugins.security.system_indices.indices: [.plugins-ml-config, .plugins-ml-connector, .plugins-ml-model-group, .plugins-ml-model, .plugins-ml-task, .plugins-ml-conversation-meta, .plugins-ml-conversation-interactions, .opendistro-alerting-config, .opendistro-alerting-alert*, .opendistro-anomaly-results*, .opendistro-anomaly-detector*, .opendistro-anomaly-checkpoints, .opendistro-anomaly-detection-state, .opendistro-reports-*, .opensearch-notifications-*, .opensearch-notebooks, .opensearch-observability, .ql-datasources, .opendistro-asynchronous-search-response*, .replication-metadata-store, .opensearch-knn-models, .geospatial-ip2geo-data*]" + System.lineSeparator() + + "node.max_local_storage_nodes: 3" + System.lineSeparator() + + "######## End OpenSearch Security Demo Configuration ########" + System.lineSeparator(); assertThat(actual, is(equalTo(expected))); Installer.initsecurity = true; actual = SecuritySettingsConfigurer.buildSecurityConfigString(); - assertThat(actual, containsString("plugins.security.allow_default_init_securityindex: true\n")); + assertThat(actual, containsString("plugins.security.allow_default_init_securityindex: true" + System.lineSeparator())); Installer.cluster_mode = true; actual = SecuritySettingsConfigurer.buildSecurityConfigString(); - assertThat(actual, containsString("network.host: 0.0.0.0\n")); - assertThat(actual, containsString("node.name: smoketestnode\n")); - assertThat(actual, containsString("cluster.initial_cluster_manager_nodes: smoketestnode\n")); + assertThat(actual, containsString("network.host: 0.0.0.0" + System.lineSeparator())); + assertThat(actual, containsString("node.name: smoketestnode" + System.lineSeparator())); + assertThat(actual, containsString("cluster.initial_cluster_manager_nodes: smoketestnode" + System.lineSeparator())); } @Test