Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,14 @@ public CertificateGenerator(Installer installer) {
public void createDemoCertificates() {
for (Certificates cert : Certificates.values()) {
String filePath = this.installer.OPENSEARCH_CONF_DIR + File.separator + cert.getFileName();
writeCertificateToFile(filePath, cert.getContent());
}
}

/**
* Helper method to write the certificates to their own file
* @param filePath the file which needs to be written
* @param content the content which needs to be written to this file
*/
static void writeCertificateToFile(String filePath, String content) {
try {
FileWriter fileWriter = new FileWriter(filePath, StandardCharsets.UTF_8);
fileWriter.write(content);
fileWriter.close();
} catch (IOException e) {
System.err.println("Error writing certificate file: " + filePath);
System.exit(-1);
try {
FileWriter fileWriter = new FileWriter(filePath, StandardCharsets.UTF_8);
fileWriter.write(cert.getContent());
fileWriter.close();
} catch (IOException e) {
System.err.println("Error writing certificate file: " + filePath);
installer.getExitHandler().exit(-1);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.security.tools.democonfig;

/**
* Default ExitHandler implementation that calls System.exit.
*/
public final class DefaultExitHandler implements ExitHandler {
@Override
public void exit(int status) {
System.exit(status);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.security.tools.democonfig;

/**
* An interface to handle exit behavior.
*/
public interface ExitHandler {
void exit(int status);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

package org.opensearch.security.tools.democonfig;

// CS-SUPPRESS-SINGLE: RegexpSingleline Extension is used to refer to file extensions, keeping this rule disable for the whole file
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
Expand Down Expand Up @@ -40,7 +41,6 @@ public class Installer {
private static Installer instance;

private static SecuritySettingsConfigurer securitySettingsConfigurer;

private static CertificateGenerator certificateGenerator;

boolean assumeyes = false;
Expand Down Expand Up @@ -71,19 +71,37 @@ public class Installer {
// To print help information for this script
private final HelpFormatter formatter = new HelpFormatter();

private ExitHandler exitHandler;

/**
* We do not want this class to be instantiated more than once,
* as we are following Singleton Factory pattern
* as we are following the Singleton pattern.
*/
private Installer() {
this.OS = System.getProperty("os.name") + " " + System.getProperty("os.version") + " " + System.getProperty("os.arch");
FILE_EXTENSION = OS.toLowerCase().contains("win") ? ".bat" : ".sh";
options = new Options();
// Use the default exit handler (simply calls System.exit)
this.exitHandler = new DefaultExitHandler();
}

/**
* Allows dependency injection of an ExitHandler.
*/
public void setExitHandler(ExitHandler exitHandler) {
this.exitHandler = exitHandler;
}

/**
* Returns a singleton instance of this class
* @return an existing instance OR a new instance if there was no existing instance
* Returns current exit handler
*/
public ExitHandler getExitHandler() {
return this.exitHandler;
}

/**
* Returns a singleton instance of this class.
* @return an existing instance OR a new instance if there was no existing instance.
*/
public static Installer getInstance() {
if (instance == null) {
Expand All @@ -95,8 +113,8 @@ public static Installer getInstance() {
}

/**
* Installs the demo security configuration
* @param options the options passed to the script
* Installs the demo security configuration.
* @param options the options passed to the script.
*/
public void installDemoConfiguration(String[] options) throws IOException {
readOptions(options);
Expand All @@ -116,7 +134,7 @@ public static void main(String[] options) throws IOException {
}

/**
* Builds options supported by this tool
* Builds options supported by this tool.
*/
void buildOptions() {
options.addOption("h", "show-help", false, "Shows help for this tool.");
Expand Down Expand Up @@ -148,16 +166,16 @@ void buildOptions() {
}

/**
* Prints headers that indicate the start of script execution
* Prints headers that indicate the start of script execution.
*/
static void printScriptHeaders() {
System.out.println("### OpenSearch Security Demo Installer");
System.out.println("### ** Warning: Do not use on production or public reachable systems **");
}

/**
* Reads the options passed to the script
* @param args an array of strings containing options passed to the script
* Reads the options passed to the script.
* @param args an array of strings containing options passed to the script.
*/
void readOptions(String[] args) {
// set script execution dir
Expand All @@ -179,28 +197,28 @@ void readOptions(String[] args) {

} catch (ParseException exp) {
System.out.println("ERR: Parsing failed. Reason: " + exp.getMessage());
System.exit(-1);
exitHandler.exit(-1);
}
}

/**
* Prints the help menu when -h option is passed
* Prints the help menu when -h option is passed.
*/
void showHelp() {
formatter.printHelp("install_demo_configuration" + FILE_EXTENSION, options, true);
System.exit(0);
exitHandler.exit(0);
}

/**
* Prompt the user and collect user inputs
* Input collection will be skipped if -y option was passed
* Prompt the user and collect user inputs.
* Input collection will be skipped if -y option was passed.
*/
void gatherUserInputs() {
if (!assumeyes) {
try (Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8)) {

if (!confirmAction(scanner, "Install demo certificates?")) {
System.exit(0);
exitHandler.exit(0);
}

if (!initsecurity) {
Expand All @@ -218,9 +236,9 @@ void gatherUserInputs() {

/**
* Helper method to scan user inputs.
* @param scanner object to be used for scanning user input
* @param message prompt question
* @return true or false based on user input
* @param scanner object to be used for scanning user input.
* @param message prompt question.
* @return true or false based on user input.
*/
boolean confirmAction(Scanner scanner, String message) {
System.out.print(message + " [y/N] ");
Expand All @@ -229,7 +247,7 @@ boolean confirmAction(Scanner scanner, String message) {
}

/**
* Initialize all class level variables required
* Initialize all class level variables required.
*/
void initializeVariables() {
setBaseDir();
Expand All @@ -238,22 +256,22 @@ void initializeVariables() {
}

/**
* Sets the base directory to be used by the script
* Sets the base directory to be used by the script.
*/
void setBaseDir() {
File baseDirFile = new File(SCRIPT_DIR).getParentFile().getParentFile().getParentFile();
BASE_DIR = baseDirFile != null ? baseDirFile.getAbsolutePath() : null;

if (BASE_DIR == null || !new File(BASE_DIR).isDirectory()) {
System.out.println("DEBUG: basedir does not exist");
System.exit(-1);
exitHandler.exit(-1);
}

BASE_DIR += File.separator;
}

/**
* Sets the variables for items at OpenSearch level
* Sets the variables for items at OpenSearch level.
*/
void setOpenSearchVariables() {
OPENSEARCH_CONF_FILE = BASE_DIR + "config" + File.separator + "opensearch.yml";
Expand All @@ -266,17 +284,17 @@ void setOpenSearchVariables() {

if (!errorMessages.isEmpty()) {
errorMessages.forEach(System.out::println);
System.exit(-1);
exitHandler.exit(-1);
}

OPENSEARCH_CONF_DIR = new File(OPENSEARCH_CONF_FILE).getParent();
OPENSEARCH_CONF_DIR = new File(OPENSEARCH_CONF_DIR).getAbsolutePath() + File.separator;
}

/**
* Helper method
* Returns a set of error messages for the paths that didn't contain files/directories
* @return a set containing error messages if any, empty otherwise
* Helper method.
* Returns a set of error messages for the paths that didn't contain files/directories.
* @return a set containing error messages if any, empty otherwise.
*/
private Set<String> validatePaths() {
Set<String> errorMessages = new HashSet<>();
Expand All @@ -299,8 +317,8 @@ private Set<String> validatePaths() {
}

/**
* Returns the installation type based on the underlying operating system
* @return will be one of `.zip`, `.tar.gz` or `rpm/deb`
* Returns the installation type based on the underlying operating system.
* @return will be one of `.zip`, `.tar.gz` or `rpm/deb`.
*/
String determineInstallType() {
// windows (.bat execution)
Expand All @@ -320,12 +338,12 @@ String determineInstallType() {
}

/**
* Sets the path variables for items at OpenSearch security plugin level
* Sets the path variables for items at OpenSearch security plugin level.
*/
void setSecurityVariables() {
if (!(new File(OPENSEARCH_PLUGINS_DIR + "opensearch-security").exists())) {
System.out.println("OpenSearch Security plugin not installed. Quit.");
System.exit(-1);
exitHandler.exit(-1);
}

// Extract OpenSearch version and Security version
Expand All @@ -349,7 +367,7 @@ void setSecurityVariables() {
}

/**
* Prints the initialized variables
* Prints the initialized variables.
*/
void printVariables() {
System.out.println("OpenSearch install type: " + OPENSEARCH_INSTALL_TYPE + " on " + OS);
Expand Down Expand Up @@ -439,9 +457,11 @@ void finishScriptExecution() {

/**
* FOR TESTS ONLY
* resets the installer state to allow testing with fresh instance for the next test.
* Resets the installer state to allow testing with a fresh instance for the next test.
*/
static void resetInstance() {
instance = null;
}

}
// CS-ENFORCE-SINGLE
Loading
Loading