Skip to content

Commit

Permalink
Makes all static methods package-private for tests
Browse files Browse the repository at this point in the history
Signed-off-by: Darshit Chanpura <[email protected]>
  • Loading branch information
DarshitChanpura committed Nov 15, 2023
1 parent 680306b commit 3d8449f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void createDemoCertificates() {
* @param filePath the file which needs to be written
* @param content the content which needs to be written to this file
*/
private static void writeCertificateToFile(String filePath, String content) {
static void writeCertificateToFile(String filePath, String content) {
try {
FileWriter fileWriter = new FileWriter(filePath, StandardCharsets.UTF_8);
fileWriter.write(content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static void main(String[] options) {
/**
* Prints deprecation warning and other headers for the script
*/
private static void printScriptHeaders() {
static void printScriptHeaders() {
System.out.println("**************************************************************************");
System.out.println("** This tool will be deprecated in the next major release of OpenSearch **");
System.out.println("** https://github.com/opensearch-project/security/issues/1755 **");
Expand All @@ -89,7 +89,7 @@ private static void printScriptHeaders() {
* Reads the options passed to the script
* @param options an array of strings containing options passed to the script
*/
private static void readOptions(String[] options) {
static void readOptions(String[] options) {
// set script execution dir
SCRIPT_DIR = options[0];

Expand Down Expand Up @@ -123,7 +123,7 @@ private static void readOptions(String[] options) {
/**
* Prints the help menu when -h option is passed
*/
private static void showHelp() {
static void showHelp() {
System.out.println("install_demo_configuration.sh [-y] [-i] [-c]");
System.out.println(" -h show help");
System.out.println(" -y confirm all installation dialogues automatically");
Expand All @@ -140,7 +140,7 @@ private static void showHelp() {
* Prompt the user and collect user inputs
* Input collection will be skipped if -y option was passed
*/
private static void gatherUserInputs() {
static void gatherUserInputs() {
if (!assumeyes) {
try (Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8)) {

Expand Down Expand Up @@ -170,7 +170,7 @@ private static void gatherUserInputs() {
* @param message prompt question
* @return true or false based on user input
*/
private static boolean confirmAction(Scanner scanner, String message) {
static boolean confirmAction(Scanner scanner, String message) {
System.out.print(message + " [y/N] ");
String response = scanner.nextLine();
return response.equalsIgnoreCase("yes") || response.equalsIgnoreCase("y");
Expand All @@ -179,7 +179,7 @@ private static boolean confirmAction(Scanner scanner, String message) {
/**
* Initialize all class level variables required
*/
private static void initializeVariables() {
static void initializeVariables() {
setBaseDir();
setOpenSearchVariables();
setSecurityVariables();
Expand All @@ -188,7 +188,7 @@ private static void initializeVariables() {
/**
* Sets the base directory to be used by the script
*/
private static void setBaseDir() {
static void setBaseDir() {
File baseDirFile = new File(SCRIPT_DIR).getParentFile().getParentFile().getParentFile();
BASE_DIR = baseDirFile != null ? baseDirFile.getAbsolutePath() : null;

Expand All @@ -203,7 +203,7 @@ private static void setBaseDir() {
/**
* Sets the variables for items at OpenSearch level
*/
private static void setOpenSearchVariables() {
static void setOpenSearchVariables() {
OPENSEARCH_CONF_FILE = BASE_DIR + "config" + File.separator + "opensearch.yml";
OPENSEARCH_BIN_DIR = BASE_DIR + "bin" + File.separator;
OPENSEARCH_PLUGINS_DIR = BASE_DIR + "plugins" + File.separator;
Expand Down Expand Up @@ -244,7 +244,7 @@ private static void setOpenSearchVariables() {
* Returns the installation type based on the underlying operating system
* @return will be one of `.zip`, `.tar.gz` or `rpm/deb`
*/
private static String determineInstallType() {
static String determineInstallType() {
// windows (.bat execution)
if (OS.toLowerCase().contains("win")) {
return ".zip";
Expand All @@ -264,7 +264,7 @@ private static String determineInstallType() {
/**
* Sets the path variables for items at OpenSearch security plugin level
*/
private static void setSecurityVariables() {
static void setSecurityVariables() {
if (!(new File(OPENSEARCH_PLUGINS_DIR + "opensearch-security").exists())) {
System.out.println("OpenSearch Security plugin not installed. Quit.");
System.exit(-1);
Expand All @@ -291,7 +291,7 @@ private static void setSecurityVariables() {
/**
* Prints the initialized variables
*/
private static void printVariables() {
static void printVariables() {
System.out.println("OpenSearch install type: " + OPENSEARCH_INSTALL_TYPE + " on " + OS);
System.out.println("OpenSearch config dir: " + OPENSEARCH_CONF_DIR);
System.out.println("OpenSearch config file: " + OPENSEARCH_CONF_FILE);
Expand All @@ -305,7 +305,7 @@ private static void printVariables() {
/**
* Prints end of script execution message and creates security admin demo file.
*/
private static void finishScriptExecution() {
static void finishScriptExecution() {
System.out.println("### Success");
System.out.println("### Execute this script now on all your nodes and then start all nodes");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void configureSecuritySettings() {
/**
* Replaces the admin password in internal_users.yml with the custom or generated password
*/
private static void updateAdminPassword() {
static void updateAdminPassword() {
String ADMIN_PASSWORD = "";
String initialAdminPassword = System.getenv("initialAdminPassword");
String ADMIN_PASSWORD_FILE_PATH = OPENSEARCH_CONF_DIR + "initialAdminPassword.txt";
Expand Down Expand Up @@ -114,7 +114,7 @@ private static void updateAdminPassword() {
* @param internalUsersFile the file path string to internal_users.yml file
* @throws IOException while reading, writing to files
*/
private static void writePasswordToInternalUsersFile(String adminPassword, String internalUsersFile) throws IOException {
static void writePasswordToInternalUsersFile(String adminPassword, String internalUsersFile) throws IOException {
String hashedAdminPassword = Hasher.hash(adminPassword.toCharArray());

if (hashedAdminPassword.isEmpty()) {
Expand Down Expand Up @@ -148,7 +148,7 @@ private static void writePasswordToInternalUsersFile(String adminPassword, Strin
/**
* Checks if security plugin is already configured. If so, the script execution will not continue.
*/
private static void checkIfSecurityPluginIsAlreadyConfigured() {
static void checkIfSecurityPluginIsAlreadyConfigured() {
// Check if the configuration file contains the 'plugins.security' string
if (OPENSEARCH_CONF_FILE != null && new File(OPENSEARCH_CONF_FILE).exists()) {
try (BufferedReader br = new BufferedReader(new FileReader(OPENSEARCH_CONF_FILE, StandardCharsets.UTF_8))) {
Expand All @@ -172,7 +172,7 @@ private static void checkIfSecurityPluginIsAlreadyConfigured() {
/**
* Update opensearch.yml with security configuration information
*/
private static void writeSecurityConfigToOpenSearchYML() {
static void writeSecurityConfigToOpenSearchYML() {
String securityConfig = buildSecurityConfigString();

try (FileWriter writer = new FileWriter(OPENSEARCH_CONF_FILE, StandardCharsets.UTF_8, true)) {
Expand All @@ -187,7 +187,7 @@ private static void writeSecurityConfigToOpenSearchYML() {
* Helper method to build security configuration to append to opensearch.yml
* @return the configuration string to be written to opensearch.yml
*/
private static String buildSecurityConfigString() {
static String buildSecurityConfigString() {
StringBuilder securityConfigLines = new StringBuilder();

securityConfigLines.append("\n")
Expand Down Expand Up @@ -235,7 +235,7 @@ private static String buildSecurityConfigString() {
* @param filePath path to opensearch.yml
* @return true is present, false otherwise
*/
private static boolean isNetworkHostAlreadyPresent(String filePath) {
static boolean isNetworkHostAlreadyPresent(String filePath) {
try {
String searchString = "^network.host";
return isStringAlreadyPresentInFile(filePath, searchString);
Expand All @@ -249,7 +249,7 @@ private static boolean isNetworkHostAlreadyPresent(String filePath) {
* @param filePath path to opensearch.yml
* @return true if present, false otherwise
*/
private static boolean isNodeMaxLocalStorageNodesAlreadyPresent(String filePath) {
static boolean isNodeMaxLocalStorageNodesAlreadyPresent(String filePath) {
try {
String searchString = "^node.max_local_storage_nodes";
return isStringAlreadyPresentInFile(filePath, searchString);
Expand All @@ -265,7 +265,7 @@ private static boolean isNodeMaxLocalStorageNodesAlreadyPresent(String filePath)
* @return true if string is present, false otherwise
* @throws IOException if there was exception reading the file
*/
private static boolean isStringAlreadyPresentInFile(String filePath, String searchString) throws IOException {
static boolean isStringAlreadyPresentInFile(String filePath, String searchString) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(filePath, StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
Expand Down

0 comments on commit 3d8449f

Please sign in to comment.