Skip to content

Commit

Permalink
Fixes the tool to address issue when running on Windows
Browse files Browse the repository at this point in the history
Signed-off-by: Darshit Chanpura <[email protected]>
  • Loading branch information
DarshitChanpura committed Nov 8, 2023
1 parent 69d8ec0 commit 729a65d
Showing 1 changed file with 35 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@

package org.opensearch.security.tools;

import org.opensearch.common.settings.Settings;
import org.opensearch.security.dlic.rest.validation.PasswordValidator;
import org.opensearch.security.dlic.rest.validation.RequestContentValidator;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
Expand All @@ -28,6 +25,10 @@
import java.util.Scanner;
import java.util.Set;

import org.opensearch.common.settings.Settings;
import org.opensearch.security.dlic.rest.validation.PasswordValidator;
import org.opensearch.security.dlic.rest.validation.RequestContentValidator;

import static org.opensearch.security.support.ConfigConstants.SECURITY_RESTAPI_PASSWORD_MIN_LENGTH;
import static org.opensearch.security.support.ConfigConstants.SECURITY_RESTAPI_PASSWORD_VALIDATION_REGEX;
import static org.opensearch.security.user.UserService.generatePassword;
Expand Down Expand Up @@ -340,29 +341,33 @@ private static void setAdminPassword() {
System.exit(-1);
}

File tempFile = new File(INTERNAL_USERS_FILE_PATH + ".tmp");
BufferedReader reader = new BufferedReader(new FileReader(INTERNAL_USERS_FILE_PATH));
FileWriter writer = new FileWriter(tempFile);
Path tempFilePath = Paths.get(INTERNAL_USERS_FILE_PATH + ".tmp");
Path internalUsersPath = Paths.get(INTERNAL_USERS_FILE_PATH);

String line;
while ((line = reader.readLine()) != null) {
if (line.matches(" *hash: *\"\\$2a\\$12\\$VcCDgh2NDk07JGN0rjGbM.Ad41qVR/YFJcgHp0UGns5JDymv..TOG\"")) {
line = line.replace(
"\"$2a$12$VcCDgh2NDk07JGN0rjGbM.Ad41qVR/YFJcgHp0UGns5JDymv..TOG\"",
"\"" + hashedAdminPassword + "\""
);
try (
BufferedReader reader = new BufferedReader(new FileReader(INTERNAL_USERS_FILE_PATH));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFilePath.toFile()))
) {
String line;
while ((line = reader.readLine()) != null) {
if (line.matches(" *hash: *\"\\$2a\\$12\\$VcCDgh2NDk07JGN0rjGbM.Ad41qVR/YFJcgHp0UGns5JDymv..TOG\"")) {
line = line.replace(
"\"$2a$12$VcCDgh2NDk07JGN0rjGbM.Ad41qVR/YFJcgHp0UGns5JDymv..TOG\"",
"\"" + hashedAdminPassword + "\""
);
}
writer.write(line + System.lineSeparator());
}
writer.write(line + System.lineSeparator());
}

reader.close();
writer.close();

if (!tempFile.renameTo(new File(INTERNAL_USERS_FILE_PATH))) {
try {
Files.move(tempFilePath, internalUsersPath, java.nio.file.StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new IOException("Unable to update the internal users file with the hashed password.");
}

} catch (IOException e) {
System.out.println("Exception: " + e.getMessage());
System.exit(-1);
}
}
Expand Down Expand Up @@ -491,14 +496,17 @@ private static void runSecurityAdminCommands() {
createSecurityAdminDemoScript(securityAdminScriptPath, securityAdminDemoScriptPath);

// Make securityadmin_demo script executable
Path file = Paths.get(securityAdminDemoScriptPath);
Set<PosixFilePermission> perms = new HashSet<>();
// Add the execute permission for owner, group, and others
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.OWNER_EXECUTE);
perms.add(PosixFilePermission.GROUP_EXECUTE);
perms.add(PosixFilePermission.OTHERS_EXECUTE);
Files.setPosixFilePermissions(file, perms);
// not needed for windows
if (!System.getProperty("os.name").toLowerCase().contains("win")) {
Path file = Paths.get(securityAdminDemoScriptPath);
Set<PosixFilePermission> perms = new HashSet<>();
// Add the execute permission for owner, group, and others
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.OWNER_EXECUTE);
perms.add(PosixFilePermission.GROUP_EXECUTE);
perms.add(PosixFilePermission.OTHERS_EXECUTE);
Files.setPosixFilePermissions(file, perms);
}

// Read the last line of the security-admin script
String lastLine = "";
Expand Down

0 comments on commit 729a65d

Please sign in to comment.